Revision 431
tmp commit for vision changes
vision.c | ||
---|---|---|
5 | 5 |
* @author Rich Hong |
6 | 6 |
* @date 11/18/2007 |
7 | 7 |
*/ |
8 |
#ifdef _CH_ |
|
9 |
#pragma package <opencv> |
|
10 |
#endif |
|
11 | 8 |
|
12 |
#ifndef _EiC |
|
13 |
#include "cv.h" |
|
14 |
#include "highgui.h" |
|
15 |
#endif |
|
9 |
#include <vision.h> |
|
10 |
|
|
11 |
#include <cv.h> |
|
12 |
#include <highgui.h> |
|
13 |
|
|
16 | 14 |
#include <stdio.h> |
17 | 15 |
#include <stdlib.h> |
18 | 16 |
|
... | ... | |
20 | 18 |
#define MAXH 150 //max threshold level |
21 | 19 |
#define DEBUG 0 //Debug to find threshold level |
22 | 20 |
|
23 |
// Load the source image. HighGUI use. |
|
24 |
IplImage *image02 = 0, *image03 = 0, *image04 = 0; |
|
25 |
|
|
26 |
void loop_image(void); |
|
27 |
|
|
28 | 21 |
struct CenterP { |
29 | 22 |
CvPoint center; |
30 | 23 |
int count; |
31 | 24 |
}; |
32 | 25 |
|
33 |
int main( int argc, char** argv ) { |
|
34 |
const char* filename = argc == 2 ? argv[1] : (char*)"colonet.jpg"; |
|
26 |
static IplImage *image02, *image03, *image04; |
|
35 | 27 |
|
28 |
int vision_init(const char* filename) { |
|
29 |
if ((image03 = cvLoadImage(filename, 0)) == 0) { |
|
30 |
fprintf(stderr, "Failed to load image.\n"); |
|
31 |
return -1; |
|
32 |
} |
|
33 |
|
|
36 | 34 |
// load image and force it to be grayscale |
37 | 35 |
if ((image03 = cvLoadImage(filename, 0)) == 0) { |
38 | 36 |
fprintf(stderr, "Failed to load image.\n"); |
... | ... | |
40 | 38 |
} |
41 | 39 |
|
42 | 40 |
// Create the destination images |
43 |
image02 = cvCloneImage( image03 );
|
|
44 |
image04 = cvCloneImage( image03 );
|
|
41 |
image02 = cvCloneImage(image03);
|
|
42 |
image04 = cvCloneImage(image03);
|
|
45 | 43 |
|
46 | 44 |
if (DEBUG) cvNamedWindow("Result", 1); |
47 | 45 |
|
48 |
loop_image(); |
|
46 |
return 0; |
|
47 |
} |
|
49 | 48 |
|
49 |
void vision_destroy() { |
|
50 | 50 |
if (DEBUG) cvWaitKey(0); |
51 | 51 |
|
52 | 52 |
cvReleaseImage(&image02); |
53 | 53 |
cvReleaseImage(&image03); |
54 | 54 |
|
55 |
if (DEBUG) { |
|
56 |
cvDestroyWindow("Result"); |
|
57 |
} |
|
58 |
|
|
59 |
return 0; |
|
55 |
if (DEBUG) cvDestroyWindow("Result"); |
|
60 | 56 |
} |
61 | 57 |
|
62 |
void loop_image() {
|
|
58 |
int vision_get_robot_positions(VisionPosition** positions) {
|
|
63 | 59 |
CvMemStorage* stor; |
64 | 60 |
CvSeq* cont; |
65 | 61 |
CvBox2D32f* box; |
... | ... | |
124 | 120 |
if (size.width>10&&size.height>10&&size.width<20&&size.height<20){ |
125 | 121 |
//printf("%d %d %d %d\n",center.x,center.y,size.width,size.height); |
126 | 122 |
|
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){ |
|
123 |
int found=0, j; |
|
124 |
for (j = 0; j < index; j++) { |
|
125 |
if (abs(bestc[j].center.x-center.x)<9&&abs(bestc[j].center.y-center.y)<9) { |
|
131 | 126 |
bestc[j].count++; |
132 | 127 |
found=1; |
133 | 128 |
break; |
134 | 129 |
} |
130 |
} |
|
131 |
|
|
135 | 132 |
if (!found){ |
136 | 133 |
struct CenterP c; |
137 | 134 |
c.center=center; |
... | ... | |
140 | 137 |
index++; |
141 | 138 |
} |
142 | 139 |
} |
140 |
|
|
143 | 141 |
// Free memory. |
144 | 142 |
free(PointArray); |
145 | 143 |
free(PointArray2D32f); |
... | ... | |
147 | 145 |
} |
148 | 146 |
} |
149 | 147 |
|
150 |
image04 = cvCloneImage( image03 ); |
|
148 |
image04 = cvCloneImage(image03); |
|
149 |
|
|
150 |
int count = 0; |
|
151 |
|
|
151 | 152 |
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); |
|
153 |
for (i = 0; i < index; i++) { |
|
154 |
if (bestc[i].count > 7){ |
|
155 |
count++; |
|
156 |
} |
|
157 |
} |
|
158 |
|
|
159 |
VisionPosition* pos = (VisionPosition*)malloc(sizeof(VisionPosition) * count); |
|
160 |
for (i = 0; i < index; i++) { |
|
161 |
if (bestc[i].count > 7){ |
|
162 |
pos[i].x = bestc[i].center.x; |
|
163 |
pos[i].y = bestc[i].center.y; |
|
164 |
|
|
155 | 165 |
if (DEBUG) cvCircle(image04,bestc[i].center, 20, CV_RGB(0,0,0),5,8,0); |
156 | 166 |
} |
157 | 167 |
} |
158 | 168 |
|
159 | 169 |
// Show image. HighGUI use. |
160 | 170 |
if (DEBUG) cvShowImage( "Result", image04 ); |
171 |
|
|
172 |
*positions = pos; |
|
173 |
return count; |
|
161 | 174 |
} |
162 |
/* |
|
163 |
#ifdef _EiC |
|
164 |
main(1,"vision.c"); |
|
165 |
#endif |
|
166 |
*/ |
Also available in: Unified diff