Update the changelog
[opencv] / apps / Hawk / demos / DemGeom.c
1 /********************************************************************************\r
2 *\r
3 *\r
4 *  This program is demonstration for ellipse fitting. Program finds \r
5 *  contours and approximate it by ellipses.\r
6 *\r
7 *  Trackbar specify threshold parametr.\r
8 *\r
9 *  White lines is contours. Red lines is fitting ellipses.\r
10 *\r
11 *\r
12 *  Autor:  Denis Burenkov.\r
13 *\r
14 *\r
15 *\r
16 ********************************************************************************/\r
17 \r
18 char file_name[] = "image009.bmp";\r
19 \r
20 char wndname01[] = "Source image";\r
21 char wndname02[] = "Thrshold image";\r
22 char barname01[] = "Threshold factor";\r
23 int slider_pos[1] = {70};\r
24 \r
25 \r
26 // Load the source image. HighGUI use.\r
27 IPLIMAGE image01 = load_iplimage(file_name);\r
28 \r
29 if(image01==NULL)\r
30 {\r
31     printf("File '%s' not found.\n", file_name);\r
32     \r
33 }\r
34 \r
35 // Create the destination images. HighGUI use.\r
36 IPLIMAGE image02 = cvCreateImage(cvSize(image01->width, image01->height),\r
37                                 image01->depth,\r
38                                 1);\r
39 \r
40 IPLIMAGE image03 = cvCreateImage(cvSize(image01->width, image01->height),\r
41                                 image01->depth,\r
42                                 1);                           \r
43 \r
44 IPLIMAGE image04 = cvCreateImage(cvSize(image01->width, image01->height),\r
45                                 image01->depth,\r
46                                 3);\r
47 \r
48 // Make onechannel image. IPL use.\r
49 iplColorToGray(image01,image03);\r
50                          \r
51 \r
52 // Create windows. HighGUI use.\r
53 named_window(wndname01, 0);\r
54 named_window(wndname02, 0);\r
55 \r
56 // Show the image. HighGUI use.\r
57 show_iplimage(wndname01, image03);\r
58 \r
59 \r
60 // Define trackbar callback functon. This function find contours,\r
61 // draw it and approximate it by ellipses.\r
62 void func(int h)\r
63 {\r
64     CvMemStorage* stor;\r
65     CvSeq* cont;\r
66     CvBox2D32f* box;\r
67     CvPoint* PointArray;\r
68     CvPoint2D32f* PointArray2D32f;\r
69     \r
70     // Create dynamic structure and sequence.\r
71     stor = cvCreateMemStorage(0);\r
72     cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);\r
73     \r
74     // Threshold the source image. This needful for cvFindContours().\r
75     cvThreshold( image03, image02, slider_pos[0], 255, CV_THRESH_BINARY );\r
76     \r
77     // Find all contours.\r
78     cvFindContours( image02, stor, &cont, sizeof(CvContour), \r
79                     CV_RETR_LIST, CV_CHAIN_APPROX_NONE);\r
80     \r
81     // Clear images. IPL use.\r
82     cvSetZero(image02);\r
83     cvSetZero(image04);\r
84     \r
85     // This cycle draw all contours and approximate it by ellipses.\r
86     for(;cont;cont = cont->h_next)\r
87     {   \r
88         int i; // Indicator of cycle.\r
89         int count = cont->total; // This is number point in contour\r
90         CvPoint center;\r
91         CvSize size;\r
92         \r
93         // Number point must be more than or equal to 6 (for cvFitEllipse_32f).        \r
94         if( count < 6 )\r
95             continue;\r
96         \r
97         // Alloc memory for contour point set.    \r
98         PointArray = malloc( count*sizeof(CvPoint) );\r
99         PointArray2D32f= malloc( count*sizeof(CvPoint2D32f) );\r
100         \r
101         // Alloc memory for ellipse data.\r
102         box = malloc(sizeof(CvBox2D32f));\r
103         \r
104         // Get contour point set.\r
105         cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);\r
106         \r
107         // Convert CvPoint set to CvBox2D32f set.\r
108         for(i=0; i<count; i++)\r
109         {\r
110             PointArray2D32f[i].x = PointArray[i].x;\r
111             PointArray2D32f[i].y = PointArray[i].y;\r
112         }\r
113         \r
114         // Fits ellipse to current contour.\r
115         cvFitEllipse(PointArray2D32f, count, box);\r
116         \r
117         // Draw current contour.\r
118         cvDrawContours(image04,cont,RGB(255,255,255),RGB(255,255,255),0,1, 8);\r
119         \r
120         // Convert ellipse data from float to integer representation.\r
121         center.x = box->center.x;\r
122         center.y = box->center.y;\r
123         size.width = box->size.width/2;\r
124         size.height = box->size.height/2;\r
125         box->angle = -box->angle;\r
126         \r
127         // Draw ellipse.\r
128         cvEllipse(image04, \r
129                   center,\r
130                   size,\r
131                   box->angle,\r
132                   0,\r
133                   360,\r
134                   RGB(0,0,255),\r
135                   1);\r
136         \r
137         // Free memory.          \r
138         free(PointArray);\r
139         free(PointArray2D32f);\r
140         free(box);\r
141     }\r
142     \r
143     // Show image. HighGUI use.\r
144     show_iplimage(wndname02, image04);\r
145     \r
146     }\r
147 \r
148 \r
149 func(0);\r
150 \r
151 // Create toolbars. HighGUI use.\r
152 create_trackbar(barname01, wndname02, &slider_pos[0], 255, func);\r
153 \r
154 \r
155 // Wait for a key stroke; the same function arranges events processing                \r
156 wait_key(0);\r
157 cvReleaseImage(&image01);\r
158 cvReleaseImage(&image02);\r
159 cvReleaseImage(&image03);\r
160 \r
161 destroy_window(wndname01);\r
162 destroy_window(wndname02);\r
163 \r
164 //                              END