+++ /dev/null
-/********************************************************************************\r
-*\r
-*\r
-* This program is demonstration for functions: cvConvexHull() and\r
-* cvConvexityDefects. Program finds contours, convex hull \r
-* and defects of convexity of this contours.\r
-*\r
-* Trackbar specify threshold parametr.\r
-*\r
-* White lines is contours. Blue lines is convex hull.\r
-* Red triangles marks defects of convexity.\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[] = "Output 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
-\r
-\r
-// Define trackbar callback functon. This function find contours, convex hull\r
-// and defects of convexity. Then draw it all.\r
-void func(int h)\r
-{\r
- CvMemStorage* stor;\r
- CvMemStorage* stor02;\r
- CvMemStorage* stor03;\r
- CvSeq* cont;\r
- CvSeq* seqhull;\r
- CvSeq* defects;\r
- int* hull;\r
- int hullsize;\r
- CvPoint* PointArray;\r
- CvConvexityDefect* defectArray;\r
- \r
- \r
- // Create memory storage for sequence.\r
- stor = cvCreateMemStorage(0);\r
- stor02 = cvCreateMemStorage(0);\r
- stor03 = cvCreateMemStorage(0);\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 image\r
- cvSetZero(image04);\r
- \r
- // This cycle draw all contours and find convex hull\r
- // and defects of convexity.\r
- for(;cont;cont = cont->h_next)\r
- { \r
- int i; // Indicator of cycles.\r
- int count = cont->total; // This is number point in contour\r
- CvPoint center;\r
- CvSize size;\r
- \r
- \r
- // Alloc memory for contour point set. \r
- PointArray = malloc( count*sizeof(CvPoint) );\r
- \r
- // Alloc memory for indices of convex hull vertices.\r
- hull = malloc(sizeof(int)*count);\r
- \r
- // Get contour point set.\r
- cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);\r
- \r
- \r
- // Find convex hull for curent contour.\r
- cvConvexHull( PointArray,\r
- count,\r
- NULL,\r
- CV_COUNTER_CLOCKWISE,\r
- hull,\r
- &hullsize);\r
- \r
- // Find convex hull for current contour.\r
- // This required for cvConvexityDefects().\r
- seqhull = cvContourConvexHull( cont,\r
- CV_COUNTER_CLOCKWISE,\r
- stor02);\r
- \r
- // This required for cvConvexityDefects().\r
- // Otherwise cvConvexityDefects() falled.\r
- if( hullsize < 4 )\r
- continue;\r
- \r
- // Find defects of convexity of current contours. \r
- defects = cvConvexityDefects( cont,\r
- seqhull,\r
- stor03);\r
- \r
- // This cycle marks all defects of convexity of current contours.\r
- for(;defects;defects = defects->h_next)\r
- {\r
- int nomdef = defects->total; // defect amount\r
- \r
- if(nomdef == 0)\r
- continue;\r
- \r
- // Alloc memory for defect set. \r
- defectArray = malloc(sizeof(CvConvexityDefect)*nomdef);\r
- \r
- // Get defect set.\r
- cvCvtSeqToArray(defects,defectArray, CV_WHOLE_SEQ);\r
- \r
- // Draw marks for all defects.\r
- for(i=0; i<nomdef; i++)\r
- {\r
- cvLine(image04, *(defectArray[i].start), \r
- *(defectArray[i].depth_point),RGB(0,0,255), 0, 8);\r
- cvLine(image04, *(defectArray[i].depth_point),\r
- *(defectArray[i].end),RGB(0,0,255), 0, 8);\r
- }\r
- \r
- // Free memory. \r
- free(defectArray);\r
- }\r
- \r
- // Draw current contour.\r
- cvDrawContours(image04,cont,RGB(255,255,255),RGB(255,255,255),0,1, 8);\r
- \r
- // Draw convex hull for current contour. \r
- for(i=0; i<hullsize-1; i++)\r
- {\r
- cvLine(image04, PointArray[hull[i]], \r
- PointArray[hull[i+1]],RGB(255,0,0),0, 8);\r
- }\r
- cvLine(image04, PointArray[hull[hullsize-1]],\r
- PointArray[hull[0]],RGB(255,0,0),0, 8);\r
- \r
- \r
- // Free memory. \r
- free(PointArray);\r
- free(hull);\r
- \r
- }\r
- \r
- // Show image. HighGUI use. \r
- show_iplimage(wndname02, image04);\r
- \r
- // Free memory.\r
- cvReleaseMemStorage(&stor);\r
- cvReleaseMemStorage(&stor03);\r
- cvReleaseMemStorage(&stor02);\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