-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiteellipse_cam.c
198 lines (154 loc) · 5.2 KB
/
fiteellipse_cam.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/********************************************************************************
*
*
* This program is demonstration for ellipse fitting. Program finds
* contours and approximate it by ellipses.
*
* Trackbar specify threshold parametr.
*
* White lines is contours. Red lines is fitting ellipses.
*
*
* Autor: Denis Burenkov.
*
*
*
********************************************************************************/
#ifdef _CH_
#pragma package <opencv>
#endif
#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#endif
int slider_pos = 70;
// Load the source image. HighGUI use.
IplImage *image01 = 0, *image02 = 0, *image03 = 0, *image04 = 0;
void process_image(int h);
int main( int argc, char** argv )
{
int ncams, source, c, quit=0;
CvCapture *capture = NULL;
// Ouvre la webcam
ncams = (argc >= 2 ? atoi(argv[1]) : 1);
source = (argc >= 3 ? atoi(argv[2]) : 0);
capture = cvCaptureFromCAM(source);
// Create windows.
cvNamedWindow("Source", 1);
cvNamedWindow("Result", 1);
// Create toolbars. HighGUI use.
cvCreateTrackbar( "Threshold", "Result", &slider_pos, 255, process_image );
// Récupère une image de la webcam
image01 = cvQueryFrame(capture);
// Conversion en niveaux de gris
image03 = cvCreateImage(cvSize(image01->width,image01->height), IPL_DEPTH_8U, 1);
while (!quit)
{
// Récupère une image de la webcam
image01 = cvQueryFrame(capture);
// Conversion en niveaux de gris
cvCvtColor(image01, image03, CV_BGR2GRAY);
cvReleaseImage(&image02);
cvReleaseImage(&image04);
// Create the destination images
image02 = cvCloneImage( image03 );
image04 = cvCloneImage( image03 );
// Show the image.
cvShowImage("Source", image03);
process_image(0);
// Wait for a key stroke; the same function arranges events processing
c = (char)cvWaitKey(10);
switch (c)
{
case 'c':
source = (source+1) % ncams;
cvReleaseCapture(&capture);
capture = cvCaptureFromCAM(source);
cvReleaseImage(&image03);
image01 = cvQueryFrame(capture);
image03 = cvCreateImage(cvSize(image01->width,image01->height), IPL_DEPTH_8U, 1);
break;
case 'q':
quit = 1;
break;
}
}
// On release la mémoire
cvReleaseCapture(&capture);
cvReleaseImage(&image01);
cvReleaseImage(&image02);
cvReleaseImage(&image03);
cvReleaseImage(&image04);
cvDestroyWindow("Source");
cvDestroyWindow("Result");
return 0;
}
// Define trackbar callback functon. This function find contours,
// draw it and approximate it by ellipses.
void process_image(int h)
{
CvMemStorage* stor;
CvSeq* cont;
CvBox2D32f* box;
CvPoint* PointArray;
CvPoint2D32f* PointArray2D32f;
// Create dynamic memory storage and sequence.
stor = cvCreateMemStorage(0);
cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
// Threshold the source image. This needful for cvFindContours().
cvThreshold( image03, image02, slider_pos, 255, CV_THRESH_BINARY );
// Find all contours.
cvFindContours( image02, stor, &cont, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
// Clear images. IPL use.
cvZero(image02);
cvZero(image04);
// This cycle draw all contours and approximate it by ellipses.
for(;cont;cont = cont->h_next)
{
int i; // Indicator of cycle.
int count = cont->total; // This is number point in contour
CvPoint center;
CvSize size;
// Number point must be more than or equal to 6 (for cvFitEllipse_32f).
if( count < 6 )
continue;
// Alloc memory for contour point set.
PointArray = (CvPoint*)malloc( count*sizeof(CvPoint) );
PointArray2D32f= (CvPoint2D32f*)malloc( count*sizeof(CvPoint2D32f) );
// Alloc memory for ellipse data.
box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f));
// Get contour point set.
cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);
// Convert CvPoint set to CvBox2D32f set.
for(i=0; i<count; i++)
{
PointArray2D32f[i].x = (float)PointArray[i].x;
PointArray2D32f[i].y = (float)PointArray[i].y;
}
// Fits ellipse to current contour.
cvFitEllipse(PointArray2D32f, count, box);
// Draw current contour.
cvDrawContours(image04,cont,CV_RGB(255,255,255),CV_RGB(255,255,255),0,1,8,cvPoint(0,0));
// Convert ellipse data from float to integer representation.
center.x = cvRound(box->center.x);
center.y = cvRound(box->center.y);
size.width = cvRound(box->size.width*0.5);
size.height = cvRound(box->size.height*0.5);
box->angle = -box->angle;
// Draw ellipse.
/*cvEllipse(image04, center, size,
box->angle, 0, 360,
CV_RGB(0,0,255), 1, CV_AA, 0);*/
// Free memory.
free(PointArray);
free(PointArray2D32f);
free(box);
}
// Show image. HighGUI use.
cvShowImage( "Result", image04 );
cvReleaseMemStorage(&stor);
}
#ifdef _EiC
main(1,"fitellipse.c");
#endif