1 /********************************************************************************
\r
4 * This program is demonstration for ellipse fitting. Program finds
\r
5 * contours and approximate it by ellipses.
\r
7 * Trackbar specify threshold parametr.
\r
9 * White lines is contours. Red lines is fitting ellipses.
\r
12 * Autor: Denis Burenkov.
\r
16 ********************************************************************************/
\r
18 char file_name[] = "image009.bmp";
\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
26 // Load the source image. HighGUI use.
\r
27 IPLIMAGE image01 = load_iplimage(file_name);
\r
31 printf("File '%s' not found.\n", file_name);
\r
35 // Create the destination images. HighGUI use.
\r
36 IPLIMAGE image02 = cvCreateImage(cvSize(image01->width, image01->height),
\r
40 IPLIMAGE image03 = cvCreateImage(cvSize(image01->width, image01->height),
\r
44 IPLIMAGE image04 = cvCreateImage(cvSize(image01->width, image01->height),
\r
48 // Make onechannel image. IPL use.
\r
49 iplColorToGray(image01,image03);
\r
52 // Create windows. HighGUI use.
\r
53 named_window(wndname01, 0);
\r
54 named_window(wndname02, 0);
\r
56 // Show the image. HighGUI use.
\r
57 show_iplimage(wndname01, image03);
\r
60 // Define trackbar callback functon. This function find contours,
\r
61 // draw it and approximate it by ellipses.
\r
67 CvPoint* PointArray;
\r
68 CvPoint2D32f* PointArray2D32f;
\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
74 // Threshold the source image. This needful for cvFindContours().
\r
75 cvThreshold( image03, image02, slider_pos[0], 255, CV_THRESH_BINARY );
\r
77 // Find all contours.
\r
78 cvFindContours( image02, stor, &cont, sizeof(CvContour),
\r
79 CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
\r
81 // Clear images. IPL use.
\r
85 // This cycle draw all contours and approximate it by ellipses.
\r
86 for(;cont;cont = cont->h_next)
\r
88 int i; // Indicator of cycle.
\r
89 int count = cont->total; // This is number point in contour
\r
93 // Number point must be more than or equal to 6 (for cvFitEllipse_32f).
\r
97 // Alloc memory for contour point set.
\r
98 PointArray = malloc( count*sizeof(CvPoint) );
\r
99 PointArray2D32f= malloc( count*sizeof(CvPoint2D32f) );
\r
101 // Alloc memory for ellipse data.
\r
102 box = malloc(sizeof(CvBox2D32f));
\r
104 // Get contour point set.
\r
105 cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);
\r
107 // Convert CvPoint set to CvBox2D32f set.
\r
108 for(i=0; i<count; i++)
\r
110 PointArray2D32f[i].x = PointArray[i].x;
\r
111 PointArray2D32f[i].y = PointArray[i].y;
\r
114 // Fits ellipse to current contour.
\r
115 cvFitEllipse(PointArray2D32f, count, box);
\r
117 // Draw current contour.
\r
118 cvDrawContours(image04,cont,RGB(255,255,255),RGB(255,255,255),0,1, 8);
\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
128 cvEllipse(image04,
\r
139 free(PointArray2D32f);
\r
143 // Show image. HighGUI use.
\r
144 show_iplimage(wndname02, image04);
\r
151 // Create toolbars. HighGUI use.
\r
152 create_trackbar(barname01, wndname02, &slider_pos[0], 255, func);
\r
155 // Wait for a key stroke; the same function arranges events processing
\r
157 cvReleaseImage(&image01);
\r
158 cvReleaseImage(&image02);
\r
159 cvReleaseImage(&image03);
\r
161 destroy_window(wndname01);
\r
162 destroy_window(wndname02);
\r