1 /*
2 * =====================================================================================
3 *
4 * Filename: select2save.cpp
5 * Description: 获取感兴趣的选区,自动创建目录和resize大小
6 * 环境:opencv2.4.4和vs2010
7 *
8 *
9 * Version: 2
10 * Created: 2013/10/14 19:52:09
11 * Author: yuliyang
12 * Weibo: @礼杨_HDU
13 * Mail: wzyuliyang911@gmail.com
14 * Blog: http://www.cnblogs.com/yuliyang
15 *
16 * =====================================================================================
17 */
18 #include "opencvcv.h"
19 /*-----------------------------------------------------------------------------
20 * 很奇怪的的是cvresize()在这个头文件里,加了它才有定义,否则编译会说函数未定义
21 *-----------------------------------------------------------------------------*/
22 #include "opencvhighgui.h"
23 #include "opencv2/core/core.hpp"
24 #include "opencv2/highgui/highgui.hpp"
25 #include "opencv2/imgproc/imgproc.hpp"
26 #include <stdio.h>
27 #include <math.h>
28 using namespace cv;
29
30 IplImage* org = 0;
31 IplImage* img = 0;
32 IplImage* tmp = 0;
33 IplImage* dst = 0;
34 IplImage* dst_resize = 0;
35 static int n=0;
36 static char savename[50];
37 static char savename_resize[50];
38 void on_mouse( int event, int x, int y, int flags, void* ustc)
39 {
40 static CvPoint pre_pt = {-1,-1};
41 static CvPoint cur_pt = {-1,-1};
42 CvFont font;
43 cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);
44 char temp[16];
45 char lenth_and_height[30];
46
47 if( event == CV_EVENT_LBUTTONDOWN )
48 {
49 cvCopy(org,img);
50 sprintf(temp,"(%d,%d)",x,y);
51 pre_pt = cvPoint(x,y);
52 cvPutText(img,temp, pre_pt, &font, cvScalar(0,0, 0, 255));
53 cvCircle( img, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
54 cvShowImage( "img", img );
55 cvCopy(img,tmp);
56 }
57 else if( event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
58 {
59 cvCopy(tmp,img);
60 sprintf(temp,"(%d,%d)",x,y);
61 cur_pt = cvPoint(x,y);
62 cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
63 cvShowImage( "img", img );
64 }
65 else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
66 {
67 cvCopy(tmp,img);
68 sprintf(temp,"(%d,%d)",x,y);
69 cur_pt = cvPoint(x,y);
70 cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
71 cvRectangle(img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
72 sprintf(lenth_and_height,"(Width:%d,Height:%d)",abs(cur_pt.x-pre_pt.x),abs(cur_pt.y-pre_pt.y));
73 /*-----------------------------------------------------------------------------
74 *
75 *
76 * 为了方便随时查看自己选区的宽度和高度,特意加了一个点,该点计算为取矩形窗左上点和右下点的中点
77 *
78 *
79 *-----------------------------------------------------------------------------*/
80 CvPoint origin;
81 origin.x=(pre_pt.x+cur_pt.x)/2;
82 origin.y=(pre_pt.y+cur_pt.y)/2;
83 cvPutText(img,lenth_and_height, origin, &font, cvScalar(0,0, 0, 255));
84 cvShowImage( "img", img );
85 }
86 else if( event == CV_EVENT_LBUTTONUP )
87 {
88 cvCopy(tmp,img);
89 sprintf(temp,"(%d,%d)",x,y);
90 cur_pt = cvPoint(x,y);
91 cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
92 cvCircle( img, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
93 cvRectangle( img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
94 cvShowImage( "img", img );
95 cvCopy(img,tmp);
96 int width=abs(pre_pt.x-cur_pt.x);
97 int height=abs(pre_pt.y-cur_pt.y);
98 if(width==0 || height==0)
99 {
100 cvDestroyWindow("dst");
101 return;
102 }
103 dst=cvCreateImage(cvSize(width,height),org->depth,org->nChannels);
104 CvRect rect;
105 if(pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y)
106 {
107 rect=cvRect(pre_pt.x,pre_pt.y,width,height);
108 }
109 else if(pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y)
110 {
111 rect=cvRect(cur_pt.x,pre_pt.y,width,height);
112 }
113 else if(pre_pt.x>cur_pt.x && pre_pt.y>cur_pt.y)
114 {
115 rect=cvRect(cur_pt.x,cur_pt.y,width,height);
116 }
117 else if(pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y)
118 {
119 rect=cvRect(pre_pt.x,cur_pt.y,width,height);
120 }
121 cvSetImageROI(org,rect);
122 cvCopy(org,dst);
123
124 /*-----------------------------------------------------------------------------
125 * 定义保存图像的大小
126 *-----------------------------------------------------------------------------*/
127 CvSize dst_cvsize;
128 dst_cvsize.width=64;
129 dst_cvsize.height=128;
130 dst_resize = cvCreateImage( dst_cvsize, org->depth, org->nChannels);
131 cvResize(org, dst_resize, CV_INTER_LINEAR);
132
133 cvResetImageROI(org);
134 cvDestroyWindow("dst");
135 //cvNamedWindow("dst",1);
136 //cvShowImage("dst",dst);
137
138 /*-----------------------------------------------------------------------------
139 * 保存未resize的图像
140 *-----------------------------------------------------------------------------*/
141 sprintf(savename,".\picture\save%03d.bmp",n);
142 /*-----------------------------------------------------------------------------
143 * 保存在resize文件夹下
144 *-----------------------------------------------------------------------------*/
145 sprintf(savename_resize,".\picture_resize\save%03dr.bmp",n);
146
147 cvSaveImage(savename,dst);
148 cvSaveImage(savename_resize,dst_resize);
149 n++;
150 }
151 }
152 int main(int argc, char *argv[])
153 {
154
155 /*-----------------------------------------------------------------------------
156 * 用DOS命令创建文件夹,用于分类resize和未resize的原图像
157 *-----------------------------------------------------------------------------*/
158 system("md .\picture_resize");
159 system("md .\picture");
160 org=cvLoadImage(argv[1],1);
161 img=cvCloneImage(org);
162 tmp=cvCloneImage(org);
163 cvNamedWindow("img",1);
164 cvSetMouseCallback( "img", on_mouse, 0 );
165
166 cvShowImage("img",img);
167 cvWaitKey(0);
168 cvDestroyAllWindows();
169 cvReleaseImage(&org);
170 cvReleaseImage(&img);
171 cvReleaseImage(&tmp);
172 cvReleaseImage(&dst);
173 return 0;
174 }