1 /********************************************************************************
\r
4 * This program is demonstration for functions: cvConvexHull() and
\r
5 * cvConvexityDefects. Program finds contours, convex hull
\r
6 * and defects of convexity of this contours.
\r
8 * Trackbar specify threshold parametr.
\r
10 * White lines is contours. Blue lines is convex hull.
\r
11 * Red triangles marks defects of convexity.
\r
14 * Autor: Denis Burenkov.
\r
18 ********************************************************************************/
\r
20 char file_name[] = "image009.bmp";
\r
22 char wndname01[] = "Source image";
\r
23 char wndname02[] = "Output image";
\r
24 char barname01[] = "Threshold factor";
\r
25 int slider_pos[1] = {70};
\r
28 // Load the source image. HighGUI use.
\r
29 IPLIMAGE image01 = load_iplimage( file_name );
\r
33 printf("File '%s' not found.\n", file_name);
\r
37 // Create the destination images. HighGUI use.
\r
38 IPLIMAGE image02 = cvCreateImage(cvSize(image01->width, image01->height),
\r
42 IPLIMAGE image03 = cvCreateImage(cvSize(image01->width, image01->height),
\r
46 IPLIMAGE image04 = cvCreateImage(cvSize(image01->width, image01->height),
\r
50 // Make onechannel image. IPL use.
\r
51 iplColorToGray(image01,image03);
\r
54 // Create windows. HighGUI use.
\r
55 named_window(wndname01, 0);
\r
56 named_window(wndname02, 0);
\r
58 // Show the image. HighGUI use.
\r
59 show_iplimage(wndname01, image03);
\r
64 // Define trackbar callback functon. This function find contours, convex hull
\r
65 // and defects of convexity. Then draw it all.
\r
69 CvMemStorage* stor02;
\r
70 CvMemStorage* stor03;
\r
76 CvPoint* PointArray;
\r
77 CvConvexityDefect* defectArray;
\r
80 // Create memory storage for sequence.
\r
81 stor = cvCreateMemStorage(0);
\r
82 stor02 = cvCreateMemStorage(0);
\r
83 stor03 = cvCreateMemStorage(0);
\r
85 // Threshold the source image. This needful for cvFindContours().
\r
86 cvThreshold( image03, image02, slider_pos[0], 255, CV_THRESH_BINARY );
\r
88 // Find all contours.
\r
89 cvFindContours( image02, stor, &cont, sizeof(CvContour),
\r
90 CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
\r
95 // This cycle draw all contours and find convex hull
\r
96 // and defects of convexity.
\r
97 for(;cont;cont = cont->h_next)
\r
99 int i; // Indicator of cycles.
\r
100 int count = cont->total; // This is number point in contour
\r
105 // Alloc memory for contour point set.
\r
106 PointArray = malloc( count*sizeof(CvPoint) );
\r
108 // Alloc memory for indices of convex hull vertices.
\r
109 hull = malloc(sizeof(int)*count);
\r
111 // Get contour point set.
\r
112 cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);
\r
115 // Find convex hull for curent contour.
\r
116 cvConvexHull( PointArray,
\r
119 CV_COUNTER_CLOCKWISE,
\r
123 // Find convex hull for current contour.
\r
124 // This required for cvConvexityDefects().
\r
125 seqhull = cvContourConvexHull( cont,
\r
126 CV_COUNTER_CLOCKWISE,
\r
129 // This required for cvConvexityDefects().
\r
130 // Otherwise cvConvexityDefects() falled.
\r
134 // Find defects of convexity of current contours.
\r
135 defects = cvConvexityDefects( cont,
\r
139 // This cycle marks all defects of convexity of current contours.
\r
140 for(;defects;defects = defects->h_next)
\r
142 int nomdef = defects->total; // defect amount
\r
147 // Alloc memory for defect set.
\r
148 defectArray = malloc(sizeof(CvConvexityDefect)*nomdef);
\r
151 cvCvtSeqToArray(defects,defectArray, CV_WHOLE_SEQ);
\r
153 // Draw marks for all defects.
\r
154 for(i=0; i<nomdef; i++)
\r
156 cvLine(image04, *(defectArray[i].start),
\r
157 *(defectArray[i].depth_point),RGB(0,0,255), 0, 8);
\r
158 cvLine(image04, *(defectArray[i].depth_point),
\r
159 *(defectArray[i].end),RGB(0,0,255), 0, 8);
\r
166 // Draw current contour.
\r
167 cvDrawContours(image04,cont,RGB(255,255,255),RGB(255,255,255),0,1, 8);
\r
169 // Draw convex hull for current contour.
\r
170 for(i=0; i<hullsize-1; i++)
\r
172 cvLine(image04, PointArray[hull[i]],
\r
173 PointArray[hull[i+1]],RGB(255,0,0),0, 8);
\r
175 cvLine(image04, PointArray[hull[hullsize-1]],
\r
176 PointArray[hull[0]],RGB(255,0,0),0, 8);
\r
185 // Show image. HighGUI use.
\r
186 show_iplimage(wndname02, image04);
\r
189 cvReleaseMemStorage(&stor);
\r
190 cvReleaseMemStorage(&stor03);
\r
191 cvReleaseMemStorage(&stor02);
\r
198 // Create toolbars. HighGUI use.
\r
199 create_trackbar(barname01, wndname02, &slider_pos[0], 255, func);
\r
202 // Wait for a key stroke; the same function arranges events processing
\r
204 cvReleaseImage(&image01);
\r
205 cvReleaseImage(&image02);
\r
206 cvReleaseImage(&image03);
\r
208 destroy_window(wndname01);
\r
209 destroy_window(wndname02);
\r