1 char wndname01[] = "Source image";
\r
2 char wndname02[] = "Thrshold image";
\r
3 char barname01[] = "Threshold factor";
\r
4 int slider_pos[1] = {0};
\r
7 // Load the source image. HighGUI use.
\r
8 IPLIMAGE image01 = load_iplimage("image009.bmp");
\r
10 // Create the destination images. HighGUI use.
\r
11 IPLIMAGE image02 = cvCreateImage(cvSize(image01->width, image01->height),
\r
15 IPLIMAGE image03 = cvCreateImage(cvSize(image01->width, image01->height),
\r
18 // Make onechannel image.
\r
19 cvCvtColor(image01,image03, CV_BGR2GRAY);
\r
22 // Create windows. HighGUI use.
\r
23 named_window(wndname01, 0);
\r
24 named_window(wndname02, 0);
\r
26 // Show the image. HighGUI use.
\r
27 show_iplimage(wndname01, image03);
\r
30 // Define trackbar callback functon. This function find contours,
\r
31 // draw it and approximate it by lines.
\r
37 CvPoint* PointArray;
\r
38 CvPoint2D32f* PointArray2D32f;
\r
42 // Create dynamic structure and sequence.
\r
43 stor = cvCreateMemStorage(0);
\r
44 cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
\r
46 // Threshold the source image. This needful for cvFindContours().
\r
47 cvThreshold( image03, image02, slider_pos[0], 255, CV_THRESH_BINARY );
\r
49 // Find all contours.
\r
50 cvFindContours( image02, stor, &cont, sizeof(CvContour),
\r
51 CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
\r
53 // Clear image. IPL use.
\r
56 // This cycle draw all contours and approximate it by lines.
\r
57 for(;cont;cont = cont->h_next)
\r
59 int i; // Indicator of cycle.
\r
60 int count = cont->total; // This is number point in contour
\r
63 // Alloc memory for contour point set.
\r
64 PointArray = malloc( count*sizeof(CvPoint) );
\r
65 PointArray2D32f= malloc( count*sizeof(CvPoint2D32f) );
\r
67 // Alloc memory for line data.
\r
68 line = malloc(4*sizeof(float));
\r
69 // Get contour point set.
\r
70 cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);
\r
72 // Convert CvPoint set to CvBox2D32f set.
\r
73 for(i=0; i<count; i++)
\r
75 PointArray2D32f[i].x = PointArray[i].x;
\r
76 PointArray2D32f[i].y = PointArray[i].y;
\r
79 // Fits line to current contour.
\r
80 cvFitLine2D(PointArray2D32f,cont->total,CV_DIST_L2,NULL,0.01,0.01,line);
\r
82 FirstPoint.x=line[2]-100*line[0];
\r
83 FirstPoint.y=line[3]-100*line[1];
\r
85 LastPoint.x=line[2]+100*line[0];
\r
86 LastPoint.y=line[3]+100*line[1];
\r
88 // Draw current contour.
\r
89 cvDrawContours(image02,cont,255,255,0,1,8);
\r
90 //Fits lines only to sufficiently large contours
\r
91 if (cont->total > 50 )
\r
92 cvLine(image02,FirstPoint,LastPoint,150,1,0);
\r
96 free(PointArray2D32f);
\r
100 // Show image. HighGUI use.
\r
101 show_iplimage(wndname02, image02);
\r
107 // Create toolbars. HighGUI use.
\r
108 create_trackbar(barname01, wndname02, &slider_pos[0], 255, func);
\r
111 // Wait for a key stroke; the same function arranges events processing
\r
113 cvReleaseImage(&image01);
\r
114 cvReleaseImage(&image02);
\r
115 cvReleaseImage(&image03);
\r
117 destroy_window(wndname01);
\r
118 destroy_window(wndname02);
\r