Code
1 #include "cv.h" //canny
2 #include "highgui.h"
3 #include "stdio.h"
4
5 CvFont font;
6 CvScalar cs=cvScalar(0);
7 CvCapture* file;
8 CvPoint p;
9 CvPoint point1;
10 void (*func)();
11 bool down=false;
12 bool draw=false;
13 bool finish=false;
14 int R[8];
15 int G[8];
16 int B[8];
17 int g_framecnt=0;
18 int g_pause=0;
19
20
21 void mystatic(IplImage* img,CvRect* rect)
22 {
23 int i,j;
24 char* p=img->imageData+rect->y*img->widthStep+rect->x*img->nChannels;
25 char* p1=p;
26
27 memset(R,0,8*sizeof(int));
28 memset(G,0,8*sizeof(int));
29 memset(B,0,8*sizeof(int));
30
31 for(i=0;i<rect->height;i++)
32 {
33 p1=p+i*img->widthStep;
34 for(j=0;j<rect->width;j++)
35 {
36 int temp;
37 temp=*(p1++);
38 R[temp/32]++;
39 temp=*(p1++);
40 G[temp/32]++;
41 temp=*(p1++);
42 B[temp/32]++;
43 }
44 }
45 }
46
47
48 void drawbin(IplImage* img)
49 {
50 int i=0;
51 int temp=img->width*img->height;
52
53 cvZero(img);
54 cvLine(img,cvPoint(10,10),cvPoint(10,(img->height-30)/3),cvScalar(0,0,255));
55 cvLine(img,cvPoint((img->width-30),(img->height-30)/3),cvPoint(10,(img->height-30)/3),cvScalar(0,0,255));
56
57 for (i=0;i<8;i++)
58 {
59 cvRectangle(img,cvPoint(10+5+(img->width-30)*i/8,(img->height-30)/3),cvPoint(10+(img->width-30)*(i+1)/8,(img->height-30)*(temp-R[i])/(3*temp)),cvScalar(0,0,255),-1);
60 }
61
62 cvLine(img,cvPoint(10,10+(img->height-30)/3),cvPoint(10,(img->height-30)*2/3),cvScalar(0,255,0));
63 cvLine(img,cvPoint((img->width-30),(img->height-30)*2/3),cvPoint(10,(img->height-30)*2/3),cvScalar(0,255,0));
64
65 for (i=0;i<8;i++)
66 {
67 cvRectangle(img,cvPoint(10+5+(img->width-30)*i/8,(img->height-30)*2/3),cvPoint(10+(img->width-30)*(i+1)/8,(img->height-30)*2*(temp-G[i])/(3*temp)),cvScalar(0,255,0),-1);
68 }
69
70 cvLine(img,cvPoint(10,10+(img->height-30)*2/3),cvPoint(10,(img->height-30)),cvScalar(255,0,0));
71 cvLine(img,cvPoint((img->width-30),(img->height-30)),cvPoint(10,(img->height-30)),cvScalar(255,0,0));
72 for (i=0;i<8;i++)
73 {
74 cvRectangle(img,cvPoint(10+5+(img->width-30)*i/8,(img->height-30)),cvPoint(10+(img->width-30)*(i+1)/8,(img->height-30)*(temp-B[i])/temp),cvScalar(255,0,0),-1);
75 }
76
77 }
78
79 void onchose(int pos)
80 {
81 static int cnt=(int)cvGetCaptureProperty(file,CV_CAP_PROP_FRAME_COUNT);
82
83 if (g_framecnt)
84 {
85 cvSetCaptureProperty(
86 file,
87 CV_CAP_PROP_POS_FRAMES,
88 (pos-1)*cnt/9
89 );
90
91 }
92
93 }
94
95
96 void myCallback(int event,int x,int y,int flags,void* param)
97 {
98 switch(event)
99 {
100 case CV_EVENT_MOUSEMOVE:
101 p=cvPoint(x,y);
102
103 break;
104
105 case CV_EVENT_LBUTTONDOWN:
106 cs=cvScalar(255,0,0);
107 p=cvPoint(x,y);
108 break;
109
110 case CV_EVENT_RBUTTONDOWN:
111 cs=cvScalar(0,0,255);
112 p=cvPoint(x,y);
113 break;
114
115 default:
116 break;
117 }
118 }
119
120 void myCallback2(int event,int x,int y,int flags,void* param)
121 {
122 IplImage* img=(IplImage*)param;
123 switch(event)
124 {
125 case CV_EVENT_MOUSEMOVE:
126 if (down)
127 {
128 point1=cvPoint(x,y);
129 }
130 break;
131
132 case CV_EVENT_LBUTTONDOWN:
133 down=true;
134 cs=cvScalar(255,0,0);
135 p=cvPoint(x,y);
136 point1=cvPoint(x+1,y+1);
137 break;
138
139 case CV_EVENT_LBUTTONUP:
140 down=false;
141
142 break;
143
144 default:
145 break;
146 }
147 }
148
149 void DrawCallback(int event,int x,int y,int flags,void* param)
150 {
151 switch(event)
152 {
153 case CV_EVENT_LBUTTONDOWN:
154 draw=true;
155 p=cvPoint(x,y);
156 break;
157 case CV_EVENT_LBUTTONUP:
158 finish=true;
159 break;
160 case CV_EVENT_MOUSEMOVE:
161 if (draw)
162 {
163 point1=cvPoint(x,y);
164 }
165 break;
166 }
167 }
168
169
170 int main(int argc,char **argv)
171 {
172 /*ex_4_1+ex_4_2
173 IplImage* raw;
174
175 IplImage* gray;
176 IplImage* gray1;
177
178 IplImage* canny;
179 IplImage* canny1;
180
181 IplImage* merge;
182 bool first=true;
183 char temp[30];
184 int i;
185 int j;
186 char* q;
187 char* p1;
188
189 if (argc<2)
190 {
191 return -1;
192 }
193
194 CvCapture* file=cvCreateFileCapture(argv[1]);
195 if (file==NULL)
196 {
197 return -1;
198 }
199 cvNamedWindow( "RAW", CV_WINDOW_AUTOSIZE );
200 cvNamedWindow( "GRAY", CV_WINDOW_AUTOSIZE );
201 cvNamedWindow( "CANNY", CV_WINDOW_AUTOSIZE );
202 cvNamedWindow( "MERGE", CV_WINDOW_AUTOSIZE );
203
204 cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX,0.3,0.3,0,1,8);
205
206
207 while(raw=cvQueryFrame(file))
208 {
209
210 cvShowImage("RAW",raw);
211 if (first)
212 {
213 first=false;
214 gray=cvCreateImage(cvSize(raw->width,raw->height),8,1);
215 canny=cvCreateImage(cvSize(raw->width,raw->height),8,1);
216 merge=cvCreateImage(cvSize(raw->width*3,raw->height),raw->depth,raw->nChannels);
217
218 gray1=cvCreateImage(cvSize(raw->width,raw->height),raw->depth,raw->nChannels);
219 canny1=cvCreateImage(cvSize(raw->width,raw->height),raw->depth,raw->nChannels);
220
221 cvSetMouseCallback("MERGE",myCallback,(void*) merge);
222 }
223 cvConvertImage(raw,gray,0);
224 cvShowImage("GRAY",gray);
225 cvCanny( gray, canny, 10, 100, 3 );
226 cvShowImage("CANNY",canny);
227
228 cvConvertImage(gray,gray1,0);
229
230 cvConvertImage(canny,canny1,0);
231
232 p1=merge->imageData;
233 q=raw->imageData;
234 for(i=0;i<raw->height;i++)
235 {
236 for(j=0;j<raw->widthStep;j++)
237 *(p1++)=*(q++);
238 p1+=2*raw->widthStep;
239 }
240
241 q=gray1->imageData;
242 p1=merge->imageData+raw->widthStep;
243 for(i=0;i<raw->height;i++)
244 {
245 for(j=0;j<raw->widthStep;j++)
246 *(p1++)=*(q++);
247 p1+=2*raw->widthStep;
248 }
249
250 q=canny1->imageData;
251 p1=merge->imageData+2*raw->widthStep;
252 for(i=0;i<raw->height;i++)
253 {
254 for(j=0;j<raw->widthStep;j++)
255 *(p1++)=*(q++);
256 p1+=2*raw->widthStep;
257 }
258
259
260 cvPutText(merge,"This is raw pict",cvPoint(20,20),&font,cvScalar(0,0,255));
261 cvPutText(merge,"This is gray pict",cvPoint(raw->width+20,20),&font,cvScalar(0,0,255));
262 cvPutText(merge,"This is canny pict",cvPoint(2*raw->width+20,20),&font,cvScalar(0,0,255));
263
264 sprintf(temp,"coordinate x:%d,y:%d",p.x,p.y);
265 cvPutText(merge,temp,cvPoint(20,40),&font,cs);
266
267 cvShowImage("MERGE",merge);
268 char c = (char)cvWaitKey(10);
269 if( c == 27 ) break;
270 }
271 cvReleaseImage(&raw);
272 cvReleaseImage(&gray);
273 cvReleaseImage(&canny);
274 cvReleaseImage(&gray1);
275 cvReleaseImage(&canny1);
276 cvReleaseImage(&merge);
277 cvReleaseCapture(&file);
278
279 cvDestroyWindow("RAW");
280 cvDestroyWindow("GRAY");
281 cvDestroyWindow("CANNY");
282 cvDestroyWindow("MERGE");
283
284 return 0;
285 */
286 /*ex_4_3
287 IplImage* img;
288 IplImage* imgBack;
289 IplImage* hist;
290 CvRect rc;
291
292 if (argc<2)
293 {
294 return -1;
295 }
296
297 img=cvLoadImage(argv[1],1);
298 if (img==NULL)
299 {
300 return -1;
301 }
302
303 imgBack=cvCreateImage(cvSize(img->width,img->height),img->depth,img->nChannels);
304 hist=cvCreateImage(cvSize(img->width,img->height),img->depth,img->nChannels);
305
306 cvCopy(img,imgBack,NULL);
307
308 cvNamedWindow("My Rect",CV_WINDOW_AUTOSIZE);
309 cvNamedWindow("HIST",CV_WINDOW_AUTOSIZE);
310
311 cvSetMouseCallback("My Rect",myCallback2,(void*) img);
312
313
314 while(1)
315 {
316 if(down)
317 {
318 cvCopy(imgBack,img,NULL);
319
320 rc.width=(point1.x-p.x)>0?(rc.x=p.x,point1.x-p.x):(rc.x=point1.x,p.x-point1.x);
321 rc.height=(point1.y-p.y)>0?(rc.y=p.y,point1.y-p.y):(rc.y=point1.y,p.y-point1.y);
322
323 if(rc.height==0)
324 {
325 rc.height=1;
326 }
327 if (rc.width==0)
328 {
329 rc.width=1;
330 }
331
332 if(rc.x+rc.width>img->width)
333 {
334 rc.width=img->width-rc.x;
335 }
336
337 if(rc.y+rc.height>img->height)
338 {
339 rc.height=img->height-rc.y;
340 }
341
342 cvSetImageROI(img,rc);
343 cvAddS(img,cs,img,NULL);
344 cvResetImageROI(img);
345
346 mystatic(img,&rc);
347 drawbin(hist);
348
349 //cvRectangle(img,p,point1,cs);
350 }
351
352 cvShowImage("My Rect",img);
353 cvShowImage("HIST",hist);
354 if(cvWaitKey(100)==27)
355 break;
356 }
357 cvReleaseImage(&img);
358 cvReleaseImage(&imgBack);
359 cvReleaseImage(&hist);
360 cvDestroyWindow("My Rect");
361 cvDestroyWindow("HIST");
362 return 0;
363 */
364 /*ex_4_4
365
366 IplImage* img;
367
368 if (argc<2)
369 {
370 return -1;
371 }
372
373 file=cvCreateFileCapture(argv[1]);
374 if (file==NULL)
375 {
376 return -1;
377 }
378 cvNamedWindow("CTRL",CV_WINDOW_AUTOSIZE);
379
380 cvCreateTrackbar("chose","CTRL",&g_framecnt,10,onchose);
381
382 cvCreateTrackbar("pause","CTRL",&g_pause,1,NULL);
383
384 while(1)
385 {
386
387 if (!g_pause)
388 {
389 img=cvQueryFrame(file);
390 if (img==NULL)
391 {
392 break;
393 }
394 }
395 cvShowImage("CTRL",img);
396 if(cvWaitKey(15)==27)
397 break;
398 }
399
400 cvReleaseCapture(&file);
401 cvDestroyWindow("CTRL");
402 return 0;
403 */
404 /*ex_4_5*/
405 IplImage* img;
406 IplImage* imgback;
407 CvBox2D box;
408 img=cvCreateImage(cvSize(500,500),8,1);
409 imgback=cvCreateImage(cvSize(500,500),8,1);
410 cvZero(img);
411 cvCopy(img,imgback,NULL);
412 cvNamedWindow("Draw",CV_WINDOW_AUTOSIZE);
413 cvCreateTrackbar("chose","Draw",&g_framecnt,10,NULL);
414 cvSetMouseCallback("Draw",DrawCallback,(void*) img);
415 while(1)
416 {
417 if(draw)
418 {
419 cvCopy(imgback,img,NULL);
420 cvLine(img,p,point1,cvScalar(255,255,255));
421 //cvCircle(img,p,(int)sqrt((double)((p.x-point1.x)*(p.x-point1.x)+(p.y-point1.y)*(p.y-point1.y))),cvScalar(255,255,255));
422 //box.center=cvPoint2D32f(p.x,p.y);
423 //box.size=cvSize2D32f(abs(point1.x-p.x),abs(point1.y-p.y));
424 //box.angle=0.0f;
425 //cvEllipseBox(img,box,cvScalar(255,255,255));
426 if (finish)
427 {
428 finish=false;
429 draw=false;
430 cvCopy(img,imgback,NULL);
431 }
432
433 }
434 cvShowImage("Draw",img);
435 if(cvWaitKey(100)==27)
436 break;
437 }
438 cvReleaseImage(&img);
439 cvDestroyWindow("Draw");
440 return 0;
441
442 }
443 GUI
窗口函数:
cvNamedWindow(const char* name,
int flags = CV_WINDOW_AUTOSIZE); flags=0 可拉伸
cvDestroyWindow();
void* cvGetWindowHandle( const char* name );
const char* cvGetWindowName( void* window_handle );
void cvResizeWindow(
const char* name,
int width,
int height
);
void cvMoveWindow( const char* name, int x, int y );
void cvDestroyAllWindows( void );
int cvStartWindowThread( void );
等待键盘:
while( 1 ) {
if( cvWaitKey(100)==27 ) break; cvWaitKey(0) 一直等待
}
鼠标:
void CvMouseCallback(
int event,
int x,
int y,
int flags,
void* param
);
void cvSetMouseCallback(
const char* window_name,
CvMouseCallback on_mouse,
void* param = NULL
);
滑动条:
int cvCreateTrackbar(
const char* trackbar_name,
const char* window_name,
int* value,
int count,
CvTrackbarCallback on_change
);
void (*callback)( int position )
int cvGetTrackbarPos(
const char* trackbar_name,
const char* window_name
);
void cvSetTrackbarPos(
const char* trackbar_name,
const char* window_name,
int pos
);
HARDWARE
CvCapture* cvCreateFileCapture( const char* filename );
CvCapture* cvCreateCameraCapture( int index ); -1 可打开列表供选择
int cvGrabFrame( CvCapture* capture ); 1 }
} <=>IplImage* cvQueryFrame( CvCapture* capture );
IplImage* cvRetrieveFrame( CvCapture* capture );2}
void cvReleaseCapture( CvCapture** capture );
double cvGetCaptureProperty(
CvCapture* capture,
int property_id
);
int cvSetCaptureProperty(
CvCapture* capture,
int property_id,
double value
);
CvVideoWriter* cvCreateVideoWriter(
const char* filename,
int fourcc,
double fps,
CvSize frame_size,
int is_color = 1
);
int cvWriteFrame(
CvVideoWriter* writer,
const IplImage* image
);
void cvReleaseVideoWriter(
CvVideoWriter** writer
);
FILE SYSTEM
IplImage* cvLoadImage(
const char* filename,
int iscolor = CV_LOAD_IMAGE_COLOR (自动转换为3channelsx8bits)
//CV_LOAD_IMAGE_GRAYSCALE
//CV_LOAD_IMAGE_ANYCOLOR,
//CV_LOAD_IMAGE_UNCHANGED, ///+CV_LOAD_IMAGE_ANYDEPTH
);
CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH.(设置色深)
int cvSaveImage(
const char* filename,
const CvArr* image
);
图像转换:
void cvConvertImage(
const CvArr* src,
CvArr* dst,
int flags = 0 flip the image vertically
);