+++ /dev/null
-/********************************************************************************\r
-*\r
-*\r
-* This program is demonstration for ellipse fitting. Program finds \r
-* contours and approximate it by ellipses.\r
-*\r
-* Trackbar specify threshold parametr.\r
-*\r
-* White lines is contours. Red lines is fitting ellipses.\r
-*\r
-*\r
-* Autor: Denis Burenkov.\r
-*\r
-*\r
-*\r
-********************************************************************************/\r
-\r
-char file_name[] = "image009.bmp";\r
-\r
-char wndname01[] = "Source image";\r
-char wndname02[] = "Thrshold image";\r
-char barname01[] = "Threshold factor";\r
-int slider_pos[1] = {70};\r
-\r
-\r
-// Load the source image. HighGUI use.\r
-IPLIMAGE image01 = load_iplimage(file_name);\r
-\r
-if(image01==NULL)\r
-{\r
- printf("File '%s' not found.\n", file_name);\r
- \r
-}\r
-\r
-// Create the destination images. HighGUI use.\r
-IPLIMAGE image02 = cvCreateImage(cvSize(image01->width, image01->height),\r
- image01->depth,\r
- 1);\r
-\r
-IPLIMAGE image03 = cvCreateImage(cvSize(image01->width, image01->height),\r
- image01->depth,\r
- 1); \r
-\r
-IPLIMAGE image04 = cvCreateImage(cvSize(image01->width, image01->height),\r
- image01->depth,\r
- 3);\r
-\r
-// Make onechannel image. IPL use.\r
-iplColorToGray(image01,image03);\r
- \r
-\r
-// Create windows. HighGUI use.\r
-named_window(wndname01, 0);\r
-named_window(wndname02, 0);\r
-\r
-// Show the image. HighGUI use.\r
-show_iplimage(wndname01, image03);\r
-\r
-\r
-// Define trackbar callback functon. This function find contours,\r
-// draw it and approximate it by ellipses.\r
-void func(int h)\r
-{\r
- CvMemStorage* stor;\r
- CvSeq* cont;\r
- CvBox2D32f* box;\r
- CvPoint* PointArray;\r
- CvPoint2D32f* PointArray2D32f;\r
- \r
- // Create dynamic structure and sequence.\r
- stor = cvCreateMemStorage(0);\r
- cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);\r
- \r
- // Threshold the source image. This needful for cvFindContours().\r
- cvThreshold( image03, image02, slider_pos[0], 255, CV_THRESH_BINARY );\r
- \r
- // Find all contours.\r
- cvFindContours( image02, stor, &cont, sizeof(CvContour), \r
- CV_RETR_LIST, CV_CHAIN_APPROX_NONE);\r
- \r
- // Clear images. IPL use.\r
- cvSetZero(image02);\r
- cvSetZero(image04);\r
- \r
- // This cycle draw all contours and approximate it by ellipses.\r
- for(;cont;cont = cont->h_next)\r
- { \r
- int i; // Indicator of cycle.\r
- int count = cont->total; // This is number point in contour\r
- CvPoint center;\r
- CvSize size;\r
- \r
- // Number point must be more than or equal to 6 (for cvFitEllipse_32f). \r
- if( count < 6 )\r
- continue;\r
- \r
- // Alloc memory for contour point set. \r
- PointArray = malloc( count*sizeof(CvPoint) );\r
- PointArray2D32f= malloc( count*sizeof(CvPoint2D32f) );\r
- \r
- // Alloc memory for ellipse data.\r
- box = malloc(sizeof(CvBox2D32f));\r
- \r
- // Get contour point set.\r
- cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);\r
- \r
- // Convert CvPoint set to CvBox2D32f set.\r
- for(i=0; i<count; i++)\r
- {\r
- PointArray2D32f[i].x = PointArray[i].x;\r
- PointArray2D32f[i].y = PointArray[i].y;\r
- }\r
- \r
- // Fits ellipse to current contour.\r
- cvFitEllipse(PointArray2D32f, count, box);\r
- \r
- // Draw current contour.\r
- cvDrawContours(image04,cont,RGB(255,255,255),RGB(255,255,255),0,1, 8);\r
- \r
- // Convert ellipse data from float to integer representation.\r
- center.x = box->center.x;\r
- center.y = box->center.y;\r
- size.width = box->size.width/2;\r
- size.height = box->size.height/2;\r
- box->angle = -box->angle;\r
- \r
- // Draw ellipse.\r
- cvEllipse(image04, \r
- center,\r
- size,\r
- box->angle,\r
- 0,\r
- 360,\r
- RGB(0,0,255),\r
- 1);\r
- \r
- // Free memory. \r
- free(PointArray);\r
- free(PointArray2D32f);\r
- free(box);\r
- }\r
- \r
- // Show image. HighGUI use.\r
- show_iplimage(wndname02, image04);\r
- \r
- }\r
-\r
-\r
-func(0);\r
-\r
-// Create toolbars. HighGUI use.\r
-create_trackbar(barname01, wndname02, &slider_pos[0], 255, func);\r
-\r
-\r
-// Wait for a key stroke; the same function arranges events processing \r
-wait_key(0);\r
-cvReleaseImage(&image01);\r
-cvReleaseImage(&image02);\r
-cvReleaseImage(&image03);\r
-\r
-destroy_window(wndname01);\r
-destroy_window(wndname02);\r
-\r
-// END
\ No newline at end of file