Update the changelog
[opencv] / apps / Hawk / demos / cvFitLine.c
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
5 \r
6 \r
7 // Load the source image. HighGUI use.\r
8 IPLIMAGE image01 = load_iplimage("image009.bmp");\r
9 \r
10 // Create the destination images. HighGUI use.\r
11 IPLIMAGE image02 = cvCreateImage(cvSize(image01->width, image01->height),\r
12                                 image01->depth,\r
13                                 1);\r
14 \r
15 IPLIMAGE image03 = cvCreateImage(cvSize(image01->width, image01->height),\r
16                                 image01->depth,\r
17                                 1);                           \r
18 // Make onechannel image.\r
19 cvCvtColor(image01,image03, CV_BGR2GRAY);\r
20                          \r
21 \r
22 // Create windows. HighGUI use.\r
23 named_window(wndname01, 0);\r
24 named_window(wndname02, 0);\r
25 \r
26 // Show the image. HighGUI use.\r
27 show_iplimage(wndname01, image03);\r
28 \r
29 \r
30 // Define trackbar callback functon. This function find contours,\r
31 // draw it and approximate it by lines.\r
32 void func(int h)\r
33 {\r
34     CvMemStorage* stor;\r
35     CvSeq* cont;\r
36     float * line;\r
37     CvPoint* PointArray;\r
38     CvPoint2D32f* PointArray2D32f;\r
39     CvPoint FirstPoint;\r
40     CvPoint LastPoint;\r
41         \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
45     \r
46     // Threshold the source image. This needful for cvFindContours().\r
47     cvThreshold( image03, image02, slider_pos[0], 255, CV_THRESH_BINARY );\r
48     \r
49     // Find all contours.\r
50     cvFindContours( image02, stor, &cont, sizeof(CvContour), \r
51                     CV_RETR_LIST, CV_CHAIN_APPROX_NONE);\r
52     \r
53     // Clear image. IPL use.\r
54     cvSetZero(image02);\r
55     \r
56     // This cycle draw all contours and approximate it by lines.\r
57     for(;cont;cont = cont->h_next)\r
58     {   \r
59         int i; // Indicator of cycle.\r
60         int count = cont->total; // This is number point in contour\r
61         \r
62         \r
63         // Alloc memory for contour point set.    \r
64         PointArray = malloc( count*sizeof(CvPoint) );\r
65         PointArray2D32f= malloc( count*sizeof(CvPoint2D32f) );\r
66         \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
71         \r
72         // Convert CvPoint set to CvBox2D32f set.\r
73         for(i=0; i<count; i++)\r
74         {\r
75             PointArray2D32f[i].x = PointArray[i].x;\r
76             PointArray2D32f[i].y = PointArray[i].y;\r
77         }\r
78         \r
79         // Fits line to current contour.\r
80         cvFitLine2D(PointArray2D32f,cont->total,CV_DIST_L2,NULL,0.01,0.01,line);\r
81         \r
82         FirstPoint.x=line[2]-100*line[0];\r
83         FirstPoint.y=line[3]-100*line[1];\r
84         \r
85         LastPoint.x=line[2]+100*line[0];\r
86         LastPoint.y=line[3]+100*line[1];\r
87         \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
93         \r
94         // Free memory.          \r
95         free(PointArray);\r
96         free(PointArray2D32f);\r
97         free(line);\r
98     }\r
99     \r
100     // Show image. HighGUI use. \r
101     show_iplimage(wndname02, image02);\r
102     }\r
103 \r
104 \r
105 func(0);\r
106 \r
107 // Create toolbars. HighGUI use.\r
108 create_trackbar(barname01, wndname02, &slider_pos[0], 255, func);\r
109 \r
110 \r
111 // Wait for a key stroke; the same function arranges events processing                \r
112 wait_key(0);\r
113 cvReleaseImage(&image01);\r
114 cvReleaseImage(&image02);\r
115 cvReleaseImage(&image03);\r
116 \r
117 destroy_window(wndname01);\r
118 destroy_window(wndname02);\r
119 \r
120