代码功能:检测并画出图像中的轮廓
主要函数:cvFindContours、cvDrawContours
1 #include <cv.h>
2 #include <highgui.h>
3 #include <cxcore.h>
4 #include <iostream.h>
5
6 void main()
7 {
8 int i=0;
9 int mode=CV_RETR_CCOMP; //the mode of contours
10 int num_contours=0;
11 CvScalar ext_color,inn_color;
12
13 CvMemStorage*storage=cvCreateMemStorage(0);
14 CvSeq*contour=0;
15
16 IplImage*pimg=NULL;
17 IplImage*CTImg=NULL;
18 IplImage*src=cvLoadImage("test.bmp",-1);
19
20 pimg=cvCreateImage(cvGetSize(src),src->depth,1);
21 CTImg=cvCreateImage(cvGetSize(pimg),IPL_DEPTH_8U,3);
22 cvCvtColor(src,pimg,CV_BGR2GRAY);
23
24 cvNamedWindow("source",CV_WINDOW_AUTOSIZE);
25 cvNamedWindow("gray",CV_WINDOW_AUTOSIZE);
26 cvNamedWindow("contours",CV_WINDOW_AUTOSIZE);
27
28 cvShowImage("source",src);
29 //cvThreshold(pimg,pimg,200,250,CV_THRESH_BINARY);
30 cvErode(pimg,pimg,0,1);
31 cvDilate(pimg,pimg,0,1);
32 cvCanny(pimg,pimg,2,20,3);
33 //cvErode(pimg,pimg,0,2);
34 //cvDilate(pimg,pimg,0,1);
35 //Find the Contours
36 mode=CV_RETR_LIST;
37 num_contours=cvFindContours(pimg,storage,&contour,sizeof(CvContour),mode,CV_CHAIN_APPROX_NONE);
38 cout<<"The number of the contours is :"<<num_contours<<endl;
39 //Draw the Contours
40 for (;contour!=0;contour=contour->h_next)
41 {
42 inn_color=CV_RGB(rand()&255,rand()&255,rand()&255);
43 ext_color=CV_RGB(rand()&255,rand()&255,rand()&255);
44 cvDrawContours(CTImg,contour,ext_color,inn_color,1,2,8,cvPoint(0,0));
45 }
46
47 cvShowImage("gray",pimg);
48 cvShowImage("contours",CTImg);
49 cvWaitKey(0);
50 cvSaveImage("Contours.bmp",CTImg);
51 cvSaveImage("Canny.bmp",pimg);
52
53 cvReleaseImage(&pimg);
54 cvReleaseImage(&CTImg);
55 cvReleaseImage(&src);
56 cvReleaseMemStorage(&storage);
57
58 cvDestroyAllWindows();
59
60 }
2 #include <highgui.h>
3 #include <cxcore.h>
4 #include <iostream.h>
5
6 void main()
7 {
8 int i=0;
9 int mode=CV_RETR_CCOMP; //the mode of contours
10 int num_contours=0;
11 CvScalar ext_color,inn_color;
12
13 CvMemStorage*storage=cvCreateMemStorage(0);
14 CvSeq*contour=0;
15
16 IplImage*pimg=NULL;
17 IplImage*CTImg=NULL;
18 IplImage*src=cvLoadImage("test.bmp",-1);
19
20 pimg=cvCreateImage(cvGetSize(src),src->depth,1);
21 CTImg=cvCreateImage(cvGetSize(pimg),IPL_DEPTH_8U,3);
22 cvCvtColor(src,pimg,CV_BGR2GRAY);
23
24 cvNamedWindow("source",CV_WINDOW_AUTOSIZE);
25 cvNamedWindow("gray",CV_WINDOW_AUTOSIZE);
26 cvNamedWindow("contours",CV_WINDOW_AUTOSIZE);
27
28 cvShowImage("source",src);
29 //cvThreshold(pimg,pimg,200,250,CV_THRESH_BINARY);
30 cvErode(pimg,pimg,0,1);
31 cvDilate(pimg,pimg,0,1);
32 cvCanny(pimg,pimg,2,20,3);
33 //cvErode(pimg,pimg,0,2);
34 //cvDilate(pimg,pimg,0,1);
35 //Find the Contours
36 mode=CV_RETR_LIST;
37 num_contours=cvFindContours(pimg,storage,&contour,sizeof(CvContour),mode,CV_CHAIN_APPROX_NONE);
38 cout<<"The number of the contours is :"<<num_contours<<endl;
39 //Draw the Contours
40 for (;contour!=0;contour=contour->h_next)
41 {
42 inn_color=CV_RGB(rand()&255,rand()&255,rand()&255);
43 ext_color=CV_RGB(rand()&255,rand()&255,rand()&255);
44 cvDrawContours(CTImg,contour,ext_color,inn_color,1,2,8,cvPoint(0,0));
45 }
46
47 cvShowImage("gray",pimg);
48 cvShowImage("contours",CTImg);
49 cvWaitKey(0);
50 cvSaveImage("Contours.bmp",CTImg);
51 cvSaveImage("Canny.bmp",pimg);
52
53 cvReleaseImage(&pimg);
54 cvReleaseImage(&CTImg);
55 cvReleaseImage(&src);
56 cvReleaseMemStorage(&storage);
57
58 cvDestroyAllWindows();
59
60 }
效果图: