root / trunk / code / projects / colonet / server / vision / vision.c @ 710
History | View | Annotate | Download (4.69 KB)
1 |
/**
|
---|---|
2 |
* Robot Detection
|
3 |
* based on opencv's sample program fitellipse.c by Denis Burenkov
|
4 |
*
|
5 |
* @author Rich Hong
|
6 |
* @date 11/18/2007
|
7 |
*/
|
8 |
|
9 |
#include <vision.h> |
10 |
|
11 |
#include <cv.h> |
12 |
#include <highgui.h> |
13 |
|
14 |
#include <stdio.h> |
15 |
#include <stdlib.h> |
16 |
|
17 |
#define MINH 50 //min threshold level |
18 |
#define MAXH 75 //max threshold level |
19 |
|
20 |
#define MIN_BOX_WIDTH 10 |
21 |
#define MAX_BOX_WIDTH 20 |
22 |
#define MIN_BOX_HEIGHT 10 |
23 |
#define MAX_BOX_HEIGHT 20 |
24 |
|
25 |
#define BEST_CENTER_PADDING_X 9 |
26 |
#define BEST_CENTER_PADDING_Y 9 |
27 |
|
28 |
#define ELLIPSE_COUNT_TO_BE_OBJECT 7 |
29 |
|
30 |
#define DEBUG 0 //Debug to find threshold level |
31 |
|
32 |
struct CenterP {
|
33 |
CvPoint center; |
34 |
int count;
|
35 |
}; |
36 |
|
37 |
static char* filename; |
38 |
|
39 |
int vision_init(char* filename_) { |
40 |
filename = filename_; |
41 |
return 0; |
42 |
} |
43 |
|
44 |
int vision_get_robot_positions(VisionPosition** positions) {
|
45 |
IplImage* image03; |
46 |
|
47 |
// load image and force it to be grayscale
|
48 |
if ((image03 = cvLoadImage(filename, 0)) == 0) { |
49 |
fprintf(stderr, "Failed to load image.\n");
|
50 |
return -1; |
51 |
} |
52 |
|
53 |
// Create the destination images
|
54 |
IplImage* image02 = cvCloneImage(image03); |
55 |
IplImage* image04 = cvCloneImage(image03); |
56 |
|
57 |
if (DEBUG) cvNamedWindow("Result", 1); |
58 |
|
59 |
// Create dynamic structure and sequence.
|
60 |
CvMemStorage* stor = cvCreateMemStorage(0);
|
61 |
CvSeq* cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor); |
62 |
|
63 |
struct CenterP bestc[100]; |
64 |
int index=0; |
65 |
|
66 |
for (int h = MINH; h < MAXH; h++) { |
67 |
// Threshold the source image. This needful for cvFindContours().
|
68 |
cvThreshold(image03, image02, h, 255, CV_THRESH_BINARY);
|
69 |
|
70 |
// Find all contours.
|
71 |
cvFindContours(image02, stor, &cont, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0)); |
72 |
|
73 |
// Clear images. IPL use.
|
74 |
cvZero(image02); |
75 |
cvZero(image04); |
76 |
|
77 |
// This cycle draw all contours and approximate it by ellipses.
|
78 |
for(; cont; cont = cont->h_next) {
|
79 |
int i; // Indicator of cycle. |
80 |
int count = cont->total; // This is number point in contour |
81 |
CvPoint center; |
82 |
CvSize size; |
83 |
|
84 |
// Number point must be more than or equal to 6 (for cvFitEllipse_32f).
|
85 |
if (count < 6) continue; |
86 |
|
87 |
// Alloc memory for contour point set.
|
88 |
CvPoint* PointArray = (CvPoint*) malloc(count * sizeof(CvPoint));
|
89 |
CvPoint2D32f* PointArray2D32f= (CvPoint2D32f*) malloc(count * sizeof(CvPoint2D32f));
|
90 |
|
91 |
// Alloc memory for ellipse data.
|
92 |
CvBox2D32f* box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f));
|
93 |
|
94 |
// Get contour point set.
|
95 |
cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ); |
96 |
|
97 |
// Convert CvPoint set to CvBox2D32f set.
|
98 |
for(i=0; i<count; i++) { |
99 |
PointArray2D32f[i].x = (float)PointArray[i].x;
|
100 |
PointArray2D32f[i].y = (float)PointArray[i].y;
|
101 |
} |
102 |
|
103 |
// Fits ellipse to current contour.
|
104 |
cvFitEllipse(PointArray2D32f, count, box); |
105 |
|
106 |
// Convert ellipse data from float to integer representation.
|
107 |
center.x = cvRound(box->center.x); |
108 |
center.y = cvRound(box->center.y); |
109 |
size.width = cvRound(box->size.width*0.5); |
110 |
size.height = cvRound(box->size.height*0.5); |
111 |
box->angle = -box->angle; |
112 |
|
113 |
if (size.width > MIN_BOX_WIDTH && size.height > MIN_BOX_HEIGHT
|
114 |
&& size.width < MAX_BOX_WIDTH && size.height < MAX_BOX_HEIGHT){ |
115 |
//printf("%d %d %d %d\n",center.x,center.y,size.width,size.height);
|
116 |
|
117 |
int found=0, j; |
118 |
for (j = 0; j < index; j++) { |
119 |
if (abs(bestc[j].center.x-center.x) < BEST_CENTER_PADDING_X
|
120 |
&& abs(bestc[j].center.y-center.y) < BEST_CENTER_PADDING_Y) { |
121 |
bestc[j].count++; |
122 |
found=1;
|
123 |
break;
|
124 |
} |
125 |
} |
126 |
|
127 |
if (!found){
|
128 |
struct CenterP c;
|
129 |
c.center=center; |
130 |
c.count=1;
|
131 |
bestc[index]=c; |
132 |
index++; |
133 |
} |
134 |
} |
135 |
|
136 |
// Free memory.
|
137 |
free(PointArray); |
138 |
free(PointArray2D32f); |
139 |
free(box); |
140 |
} |
141 |
} |
142 |
|
143 |
image04 = cvCloneImage(image03); |
144 |
|
145 |
int count = 0; |
146 |
int i;
|
147 |
for (i = 0; i < index; i++) { |
148 |
if (bestc[i].count > ELLIPSE_COUNT_TO_BE_OBJECT){
|
149 |
count++; |
150 |
} |
151 |
} |
152 |
|
153 |
VisionPosition* pos_array = (VisionPosition*)malloc(sizeof(VisionPosition) * count);
|
154 |
if (pos_array == NULL) { |
155 |
fprintf(stderr, "malloc failed\n");
|
156 |
return -1; |
157 |
} |
158 |
|
159 |
int c = 0; |
160 |
for (i = 0; i < index; i++) { |
161 |
if (bestc[i].count > ELLIPSE_COUNT_TO_BE_OBJECT){
|
162 |
pos_array[c].x = bestc[i].center.x; |
163 |
pos_array[c].y = bestc[i].center.y; |
164 |
c++; |
165 |
|
166 |
if (DEBUG) cvCircle(image04, bestc[i].center, 20, CV_RGB(0,0,0), 5, 8, 0); |
167 |
} |
168 |
} |
169 |
|
170 |
if (DEBUG) cvWaitKey(0); |
171 |
|
172 |
cvReleaseImage(&image02); |
173 |
cvReleaseImage(&image03); |
174 |
|
175 |
if (DEBUG) cvDestroyWindow("Result"); |
176 |
|
177 |
// Show image. HighGUI use.
|
178 |
if (DEBUG) cvShowImage( "Result", image04 ); |
179 |
|
180 |
cvReleaseImage(&image04); |
181 |
|
182 |
cvReleaseMemStorage(&stor); |
183 |
|
184 |
*positions = pos_array; |
185 |
return count;
|
186 |
} |