23 |
23 |
// Load the source image. HighGUI use.
|
24 |
24 |
IplImage *image02 = 0, *image03 = 0, *image04 = 0;
|
25 |
25 |
|
26 |
|
void loop_image();
|
|
26 |
void loop_image(void);
|
27 |
27 |
|
28 |
28 |
struct CenterP {
|
29 |
|
CvPoint center;
|
30 |
|
int count;
|
|
29 |
CvPoint center;
|
|
30 |
int count;
|
31 |
31 |
};
|
32 |
32 |
|
33 |
33 |
int main( int argc, char** argv ) {
|
34 |
|
const char* filename = argc == 2 ? argv[1] : (char*)"colonet.jpg";
|
|
34 |
const char* filename = argc == 2 ? argv[1] : (char*)"colonet.jpg";
|
35 |
35 |
|
36 |
|
// load image and force it to be grayscale
|
37 |
|
if( (image03 = cvLoadImage(filename, 0)) == 0 ) return -1;
|
|
36 |
// load image and force it to be grayscale
|
|
37 |
if ((image03 = cvLoadImage(filename, 0)) == 0) {
|
|
38 |
fprintf(stderr, "Failed to load image.\n");
|
|
39 |
return -1;
|
|
40 |
}
|
38 |
41 |
|
39 |
|
// Create the destination images
|
40 |
|
image02 = cvCloneImage( image03 );
|
41 |
|
image04 = cvCloneImage( image03 );
|
|
42 |
// Create the destination images
|
|
43 |
image02 = cvCloneImage( image03 );
|
|
44 |
image04 = cvCloneImage( image03 );
|
42 |
45 |
|
43 |
|
if (DEBUG) cvNamedWindow("Result", 1);
|
44 |
|
|
45 |
|
loop_image();
|
|
46 |
if (DEBUG) cvNamedWindow("Result", 1);
|
46 |
47 |
|
47 |
|
if (DEBUG) cvWaitKey(0);
|
|
48 |
loop_image();
|
48 |
49 |
|
49 |
|
cvReleaseImage(&image02);
|
50 |
|
cvReleaseImage(&image03);
|
|
50 |
if (DEBUG) cvWaitKey(0);
|
51 |
51 |
|
52 |
|
if (DEBUG){
|
53 |
|
cvDestroyWindow("Result");
|
54 |
|
}
|
55 |
|
return 0;
|
|
52 |
cvReleaseImage(&image02);
|
|
53 |
cvReleaseImage(&image03);
|
|
54 |
|
|
55 |
if (DEBUG) {
|
|
56 |
cvDestroyWindow("Result");
|
|
57 |
}
|
|
58 |
|
|
59 |
return 0;
|
56 |
60 |
}
|
57 |
61 |
|
58 |
|
void loop_image(){
|
59 |
|
CvMemStorage* stor;
|
60 |
|
CvSeq* cont;
|
61 |
|
CvBox2D32f* box;
|
62 |
|
CvPoint* PointArray;
|
63 |
|
CvPoint2D32f* PointArray2D32f;
|
64 |
|
|
65 |
|
// Create dynamic structure and sequence.
|
66 |
|
stor = cvCreateMemStorage(0);
|
67 |
|
cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
|
68 |
|
|
69 |
|
struct CenterP bestc[100];
|
70 |
|
int index=0;
|
|
62 |
void loop_image() {
|
|
63 |
CvMemStorage* stor;
|
|
64 |
CvSeq* cont;
|
|
65 |
CvBox2D32f* box;
|
|
66 |
CvPoint* PointArray;
|
|
67 |
CvPoint2D32f* PointArray2D32f;
|
71 |
68 |
|
72 |
|
int h;
|
73 |
|
for (h=MINH;h<MAXH;h++){
|
74 |
|
// Threshold the source image. This needful for cvFindContours().
|
75 |
|
cvThreshold( image03, image02, h, 255, CV_THRESH_BINARY );
|
76 |
|
|
77 |
|
// Find all contours.
|
78 |
|
cvFindContours( image02, stor, &cont, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
|
79 |
|
|
80 |
|
// Clear images. IPL use.
|
81 |
|
cvZero(image02);
|
82 |
|
cvZero(image04);
|
83 |
|
|
84 |
|
// This cycle draw all contours and approximate it by ellipses.
|
85 |
|
for(;cont;cont = cont->h_next) {
|
86 |
|
int i; // Indicator of cycle.
|
87 |
|
int count = cont->total; // This is number point in contour
|
88 |
|
CvPoint center;
|
89 |
|
CvSize size;
|
90 |
|
|
91 |
|
// Number point must be more than or equal to 6 (for cvFitEllipse_32f).
|
92 |
|
if( count < 6 ) continue;
|
93 |
|
|
94 |
|
// Alloc memory for contour point set.
|
95 |
|
PointArray = (CvPoint*)malloc( count*sizeof(CvPoint) );
|
96 |
|
PointArray2D32f= (CvPoint2D32f*)malloc( count*sizeof(CvPoint2D32f) );
|
97 |
|
|
98 |
|
// Alloc memory for ellipse data.
|
99 |
|
box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f));
|
100 |
|
|
101 |
|
// Get contour point set.
|
102 |
|
cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);
|
103 |
|
|
104 |
|
// Convert CvPoint set to CvBox2D32f set.
|
105 |
|
for(i=0; i<count; i++) {
|
106 |
|
PointArray2D32f[i].x = (float)PointArray[i].x;
|
107 |
|
PointArray2D32f[i].y = (float)PointArray[i].y;
|
108 |
|
}
|
109 |
|
|
110 |
|
// Fits ellipse to current contour.
|
111 |
|
cvFitEllipse(PointArray2D32f, count, box);
|
112 |
|
|
113 |
|
// Convert ellipse data from float to integer representation.
|
114 |
|
center.x = cvRound(box->center.x);
|
115 |
|
center.y = cvRound(box->center.y);
|
116 |
|
size.width = cvRound(box->size.width*0.5);
|
117 |
|
size.height = cvRound(box->size.height*0.5);
|
118 |
|
box->angle = -box->angle;
|
|
69 |
// Create dynamic structure and sequence.
|
|
70 |
stor = cvCreateMemStorage(0);
|
|
71 |
cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
|
119 |
72 |
|
120 |
|
if (size.width>10&&size.height>10&&size.width<20&&size.height<20){
|
121 |
|
//printf("%d %d %d %d\n",center.x,center.y,size.width,size.height);
|
122 |
|
|
123 |
|
int found=0;
|
124 |
|
int j;
|
125 |
|
for (j=0;j<index;j++)
|
126 |
|
if (abs(bestc[j].center.x-center.x)<9&&abs(bestc[j].center.y-center.y)<9){
|
127 |
|
bestc[j].count++;
|
128 |
|
found=1;
|
129 |
|
break;
|
130 |
|
}
|
131 |
|
if (!found){
|
132 |
|
struct CenterP c;
|
133 |
|
c.center=center;
|
134 |
|
c.count=1;
|
135 |
|
bestc[index]=c;
|
136 |
|
index++;
|
137 |
|
}
|
138 |
|
}
|
139 |
|
// Free memory.
|
140 |
|
free(PointArray);
|
141 |
|
free(PointArray2D32f);
|
142 |
|
free(box);
|
143 |
|
}
|
144 |
|
}
|
145 |
|
image04 = cvCloneImage( image03 );
|
146 |
|
int i;
|
147 |
|
for (i=0;i<index;i++){
|
148 |
|
if (bestc[i].count>7){
|
149 |
|
printf("%d,%d\n",bestc[i].center.x,bestc[i].center.y);
|
150 |
|
if (DEBUG) cvCircle(image04,bestc[i].center, 20, CV_RGB(0,0,0),5,8,0);
|
151 |
|
}
|
152 |
|
}
|
153 |
|
// Show image. HighGUI use.
|
154 |
|
if (DEBUG) cvShowImage( "Result", image04 );
|
|
73 |
struct CenterP bestc[100];
|
|
74 |
int index=0;
|
|
75 |
|
|
76 |
int h;
|
|
77 |
for (h = MINH; h < MAXH; h++) {
|
|
78 |
// Threshold the source image. This needful for cvFindContours().
|
|
79 |
cvThreshold(image03, image02, h, 255, CV_THRESH_BINARY);
|
|
80 |
|
|
81 |
// Find all contours.
|
|
82 |
cvFindContours( image02, stor, &cont, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
|
|
83 |
|
|
84 |
// Clear images. IPL use.
|
|
85 |
cvZero(image02);
|
|
86 |
cvZero(image04);
|
|
87 |
|
|
88 |
// This cycle draw all contours and approximate it by ellipses.
|
|
89 |
for(; cont; cont = cont->h_next) {
|
|
90 |
int i; // Indicator of cycle.
|
|
91 |
int count = cont->total; // This is number point in contour
|
|
92 |
CvPoint center;
|
|
93 |
CvSize size;
|
|
94 |
|
|
95 |
// Number point must be more than or equal to 6 (for cvFitEllipse_32f).
|
|
96 |
if (count < 6) continue;
|
|
97 |
|
|
98 |
// Alloc memory for contour point set.
|
|
99 |
PointArray = (CvPoint*) malloc(count * sizeof(CvPoint));
|
|
100 |
PointArray2D32f= (CvPoint2D32f*) malloc(count * sizeof(CvPoint2D32f));
|
|
101 |
|
|
102 |
// Alloc memory for ellipse data.
|
|
103 |
box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f));
|
|
104 |
|
|
105 |
// Get contour point set.
|
|
106 |
cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);
|
|
107 |
|
|
108 |
// Convert CvPoint set to CvBox2D32f set.
|
|
109 |
for(i=0; i<count; i++) {
|
|
110 |
PointArray2D32f[i].x = (float)PointArray[i].x;
|
|
111 |
PointArray2D32f[i].y = (float)PointArray[i].y;
|
|
112 |
}
|
|
113 |
|
|
114 |
// Fits ellipse to current contour.
|
|
115 |
cvFitEllipse(PointArray2D32f, count, box);
|
|
116 |
|
|
117 |
// Convert ellipse data from float to integer representation.
|
|
118 |
center.x = cvRound(box->center.x);
|
|
119 |
center.y = cvRound(box->center.y);
|
|
120 |
size.width = cvRound(box->size.width*0.5);
|
|
121 |
size.height = cvRound(box->size.height*0.5);
|
|
122 |
box->angle = -box->angle;
|
|
123 |
|
|
124 |
if (size.width>10&&size.height>10&&size.width<20&&size.height<20){
|
|
125 |
//printf("%d %d %d %d\n",center.x,center.y,size.width,size.height);
|
|
126 |
|
|
127 |
int found=0;
|
|
128 |
int j;
|
|
129 |
for (j=0;j<index;j++)
|
|
130 |
if (abs(bestc[j].center.x-center.x)<9&&abs(bestc[j].center.y-center.y)<9){
|
|
131 |
bestc[j].count++;
|
|
132 |
found=1;
|
|
133 |
break;
|
|
134 |
}
|
|
135 |
if (!found){
|
|
136 |
struct CenterP c;
|
|
137 |
c.center=center;
|
|
138 |
c.count=1;
|
|
139 |
bestc[index]=c;
|
|
140 |
index++;
|
|
141 |
}
|
|
142 |
}
|
|
143 |
// Free memory.
|
|
144 |
free(PointArray);
|
|
145 |
free(PointArray2D32f);
|
|
146 |
free(box);
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
image04 = cvCloneImage( image03 );
|
|
151 |
int i;
|
|
152 |
for (i=0; i < index; i++) {
|
|
153 |
if (bestc[i].count>7){
|
|
154 |
printf("%d,%d\n",bestc[i].center.x,bestc[i].center.y);
|
|
155 |
if (DEBUG) cvCircle(image04,bestc[i].center, 20, CV_RGB(0,0,0),5,8,0);
|
|
156 |
}
|
|
157 |
}
|
|
158 |
|
|
159 |
// Show image. HighGUI use.
|
|
160 |
if (DEBUG) cvShowImage( "Result", image04 );
|
155 |
161 |
}
|
156 |
|
|
|
162 |
/*
|
157 |
163 |
#ifdef _EiC
|
158 |
164 |
main(1,"vision.c");
|
159 |
165 |
#endif
|
160 |
|
|
|
166 |
*/
|