Update the changelog
[opencv] / apps / Hawk / demos / DemConvex.c
1 /********************************************************************************\r
2 *\r
3 *\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
7 *\r
8 *  Trackbar specify threshold parametr.\r
9 *\r
10 *  White lines is contours. Blue lines is convex hull.\r
11 *  Red triangles marks defects of convexity.\r
12 *\r
13 *\r
14 *  Autor:  Denis Burenkov.\r
15 *\r
16 *\r
17 *\r
18 ********************************************************************************/\r
19 \r
20 char file_name[] = "image009.bmp";\r
21 \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
26 \r
27 \r
28 // Load the source image. HighGUI use.\r
29 IPLIMAGE image01 = load_iplimage( file_name );\r
30 \r
31 if(image01==NULL)\r
32 {\r
33     printf("File '%s' not found.\n", file_name);\r
34     \r
35 }\r
36 \r
37 // Create the destination images. HighGUI use.\r
38 IPLIMAGE image02 = cvCreateImage(cvSize(image01->width, image01->height),\r
39                                 image01->depth,\r
40                                 1);\r
41 \r
42 IPLIMAGE image03 = cvCreateImage(cvSize(image01->width, image01->height),\r
43                                 image01->depth,\r
44                                 1);\r
45                                               \r
46 IPLIMAGE image04 = cvCreateImage(cvSize(image01->width, image01->height),\r
47                                 image01->depth,\r
48                                 3);\r
49 \r
50 // Make onechannel image. IPL use.\r
51 iplColorToGray(image01,image03);\r
52                          \r
53 \r
54 // Create windows. HighGUI use.\r
55 named_window(wndname01, 0);\r
56 named_window(wndname02, 0);\r
57 \r
58 // Show the image. HighGUI use.\r
59 show_iplimage(wndname01, image03);\r
60 \r
61 \r
62 \r
63 \r
64 // Define trackbar callback functon. This function find contours, convex hull\r
65 // and defects of convexity. Then draw it all.\r
66 void func(int h)\r
67 {\r
68     CvMemStorage* stor;\r
69     CvMemStorage* stor02;\r
70     CvMemStorage* stor03;\r
71     CvSeq* cont;\r
72     CvSeq* seqhull;\r
73     CvSeq* defects;\r
74     int* hull;\r
75     int hullsize;\r
76     CvPoint* PointArray;\r
77     CvConvexityDefect* defectArray;\r
78     \r
79     \r
80     // Create memory storage for sequence.\r
81     stor = cvCreateMemStorage(0);\r
82     stor02 = cvCreateMemStorage(0);\r
83     stor03 = cvCreateMemStorage(0);\r
84             \r
85     // Threshold the source image. This needful for cvFindContours().\r
86     cvThreshold( image03, image02, slider_pos[0], 255, CV_THRESH_BINARY );\r
87     \r
88     // Find all contours.\r
89     cvFindContours( image02, stor, &cont, sizeof(CvContour), \r
90                     CV_RETR_LIST, CV_CHAIN_APPROX_NONE);\r
91     \r
92     // Clear image\r
93     cvSetZero(image04);\r
94     \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
98     {   \r
99         int i;                   // Indicator of cycles.\r
100         int count = cont->total; // This is number point in contour\r
101         CvPoint center;\r
102         CvSize size;\r
103         \r
104         \r
105         // Alloc memory for contour point set.    \r
106         PointArray = malloc( count*sizeof(CvPoint) );\r
107                 \r
108         // Alloc memory for indices of convex hull vertices.\r
109         hull = malloc(sizeof(int)*count);\r
110         \r
111         // Get contour point set.\r
112         cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);\r
113         \r
114                 \r
115         // Find convex hull for curent contour.\r
116         cvConvexHull( PointArray,\r
117                       count,\r
118                       NULL,\r
119                       CV_COUNTER_CLOCKWISE,\r
120                       hull,\r
121                       &hullsize);\r
122         \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
127                              stor02);\r
128         \r
129         // This required for cvConvexityDefects().\r
130         // Otherwise cvConvexityDefects() falled.\r
131         if( hullsize < 4 )\r
132             continue;\r
133          \r
134         // Find defects of convexity of current contours.                        \r
135         defects = cvConvexityDefects( cont,\r
136                             seqhull,\r
137                             stor03);\r
138         \r
139         // This cycle marks all defects of convexity of current contours.\r
140         for(;defects;defects = defects->h_next)\r
141         {\r
142             int nomdef = defects->total; // defect amount\r
143             \r
144             if(nomdef == 0)\r
145                 continue;\r
146              \r
147             // Alloc memory for defect set.   \r
148             defectArray = malloc(sizeof(CvConvexityDefect)*nomdef);\r
149             \r
150             // Get defect set.\r
151             cvCvtSeqToArray(defects,defectArray, CV_WHOLE_SEQ);\r
152             \r
153             // Draw marks for all defects.\r
154             for(i=0; i<nomdef; i++)\r
155             {\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
160             }\r
161              \r
162             // Free memory.       \r
163             free(defectArray);\r
164         }\r
165         \r
166         // Draw current contour.\r
167         cvDrawContours(image04,cont,RGB(255,255,255),RGB(255,255,255),0,1, 8);\r
168         \r
169         // Draw convex hull for current contour.        \r
170         for(i=0; i<hullsize-1; i++)\r
171         {\r
172             cvLine(image04, PointArray[hull[i]], \r
173                             PointArray[hull[i+1]],RGB(255,0,0),0, 8);\r
174         }\r
175         cvLine(image04, PointArray[hull[hullsize-1]],\r
176                              PointArray[hull[0]],RGB(255,0,0),0, 8);\r
177         \r
178           \r
179         // Free memory.          \r
180         free(PointArray);\r
181         free(hull);\r
182         \r
183     }\r
184     \r
185     // Show image. HighGUI use. \r
186     show_iplimage(wndname02, image04);\r
187     \r
188     // Free memory.\r
189     cvReleaseMemStorage(&stor);\r
190     cvReleaseMemStorage(&stor03);\r
191     cvReleaseMemStorage(&stor02);\r
192 \r
193     }\r
194 \r
195 \r
196 func(0);\r
197 \r
198 // Create toolbars. HighGUI use.\r
199 create_trackbar(barname01, wndname02, &slider_pos[0], 255, func);\r
200 \r
201 \r
202 // Wait for a key stroke; the same function arranges events processing                \r
203 wait_key(0);\r
204 cvReleaseImage(&image01);\r
205 cvReleaseImage(&image02);\r
206 cvReleaseImage(&image03);\r
207 \r
208 destroy_window(wndname01);\r
209 destroy_window(wndname02);\r
210 \r
211 //                              END