source: liacs/mss/project/colour-tracking.c@ 13

Last change on this file since 13 was 10, checked in by Rick van der Zwet, 15 years ago

Some more playing around. But sleep is on it's way

File size: 6.8 KB
Line 
1/* OpenCV Hello world, with filtering of various filters
2 * Based on http://home.iitk.ac.in/~swagatk/faq/opencv.html#Colar_Based_Tracking
3 * Rick van der Zwet <info@rickvanderzwet.nl>
4 */
5
6#include <cv.h>
7#include <cxcore.h>
8#include <cvaux.h>
9#include <highgui.h>
10#include <stdio.h>
11
12#define DEBUG 1
13
14#define RVALUE(x,y,step) (x*step+y*3+2)
15#define GVALUE(x,y,step) (x*step+y*3+1)
16#define BVALUE(x,y,step) (x*step+y*3+0)
17
18/* Used for playing with bounderies, returned max value of the pair */
19int max(const int x, const int y) {
20 return ((x > y) ? x : y);
21}
22
23/* Used for playing with bounderies, returned min value of the pair */
24int min(const int x, const int y) {
25 return ((x < y) ? x : y);
26
27}
28
29int cvInitSystem(int argc, char *argv[]) {
30 // Don't care which camera to use. Use specified number elsewise
31 int cam_number = atoi(argv[1]) > 0 ? atoi(argv[1]) : -1;
32 CvCapture *capture = cvCaptureFromCAM(cam_number);
33 int width = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
34 int height = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
35
36
37 uchar *filterdata, *outdata, *routdata, *goutdata, *boutdata;
38
39
40 //Create a window
41 cvNamedWindow("RGB_Image", CV_WINDOW_AUTOSIZE);
42 cvMoveWindow("RGB_Image",0,0);
43 cvNamedWindow("R_Image", CV_WINDOW_AUTOSIZE);
44 cvMoveWindow("R_Image",0,height);
45 cvNamedWindow("G_Image", CV_WINDOW_AUTOSIZE);
46 cvMoveWindow("G_Image",width,0);
47 cvNamedWindow("B_Image", CV_WINDOW_AUTOSIZE);
48 cvMoveWindow("B_Image",width,height);
49 cvNamedWindow("F_Image", CV_WINDOW_AUTOSIZE);
50
51
52 //Create Image Skeletons
53 IplImage *image = NULL;
54 IplImage *output = cvCreateImage( cvSize(width,height), 8, 3);
55 IplImage *boutput = cvCreateImage( cvSize(width,height), 8, 3);
56 IplImage *routput = cvCreateImage( cvSize(width,height), 8, 3);
57 IplImage *goutput = cvCreateImage( cvSize(width,height), 8, 3);
58 IplImage *filter = cvCreateImage( cvSize(width,height), 8, 3);
59 IplImage *target = cvCreateImage( cvSize(width,height), 8, 3);
60
61
62 // Some temp pointers
63 int i, j, k, m, n, o, p;
64 int r, g, b;
65 int q;
66
67
68 // Video run starts here ...
69 while (1)
70 {
71 image = cvQueryFrame(capture);
72
73 // We need some input before running any further
74 if (image == NULL) {
75 printf("Ehm no input in the image\n");
76 continue;
77 }
78
79 //This is needed for accessing pixel elements
80 int step = image->widthStep/sizeof(uchar);
81
82 cvCopy(image, output, NULL);
83 cvCopy(image, routput, NULL);
84 cvCopy(image, goutput, NULL);
85 cvCopy(image, boutput, NULL);
86
87
88 outdata = (uchar *) output->imageData;
89 routdata = (uchar *) routput->imageData;
90 goutdata = (uchar *) goutput->imageData;
91 boutdata = (uchar *) boutput->imageData;
92
93 k = 0, p = 0;
94 for( m = 0; m < image->height; m++) //row
95 {
96 for( n = 0; n < image->width; n++) //column
97 {
98 routdata[BVALUE(m,n,step)] = 0; // clear blue part
99 routdata[GVALUE(m,n,step)] = 0; // clear green part
100
101 goutdata[BVALUE(m,n,step)] = 0; // clear blue part
102 goutdata[RVALUE(m,n,step)] = 0; // clear red part
103
104 boutdata[GVALUE(m,n,step)] = 0; // clear green part
105 boutdata[RVALUE(m,n,step)] = 0; // clear red part
106 }
107 }
108
109 // Clear filter for new use
110 // cvSetZero(filter);
111 // ... or clone orginal image for mapping over it
112 //cvCopy(goutput, filter, NULL);
113
114 // Make it yellow
115 //cvOr( routput, goutput, filter, NULL);
116
117 // XXX: Use stuff like cvMat, cvGetRawData instead of internal hacking
118 // Make green 'fade' out by the presence of many red as well
119 filterdata = (uchar *) filter->imageData;
120 for( m = 0; m < image->height; m++) //row
121 {
122 for( n = 0; n < image->width; n++) //column
123 {
124 r = outdata[RVALUE(m,n,step)];
125 g = outdata[GVALUE(m,n,step)];
126 b = outdata[BVALUE(m,n,step)];
127 // Many red, less blue and green. Good for laser pointer tracing
128 p = (r < 220) ? 0 : r;
129 p = (b < 180 || g < 180) ? 0 : p;
130 //p = (abs(p - filterdata[GVALUE(m,n,step)]) > 20) ? p : 0;
131 filterdata[GVALUE(m,n,step)] = p;
132 }
133 }
134
135
136 int xmin = 0, xmax = 0;
137 int ymin = 0, ymax = 0;
138 int value = 0, cmax = 0;
139
140 // Find the 'very' green point and mark it's surroundings
141 const int stepSize = 5;
142 for(m = 0; m < image->height; m = m+stepSize)
143 {
144 for(n = 0; n < image->width; n = n+stepSize)
145 {
146 // Make it stop processing all points, pick the ones with high value
147 value = filterdata[GVALUE(m,n,step)];
148 if (value > cmax)
149 {
150 // detect 'square'
151 p = 0;
152 int SIZE = 3;
153 for (i = m; i < min(m+SIZE,height); i++)
154 {
155 for (j = n; j < min(n+SIZE,width); j++)
156 {
157 value = filterdata[GVALUE(i,j,step)];
158 if (value > cmax)
159 {
160 p++;
161 }
162 }
163 }
164
165
166 SIZE = 6;
167 /* Make sure envirionment is 'low' */
168 for (i = max(m-SIZE,0); i < m; i++)
169 {
170 for (j = max(n-SIZE,0); j < n; j++)
171 {
172 q = filterdata[GVALUE(i,j,step)];
173 if (q > value)
174 {
175 p--;
176 }
177 }
178 }
179 for (i = min(m+SIZE,height); i < min(m+(2*SIZE),height); i++)
180 {
181 for (j= min(m+SIZE,width); j < min(m+(2*SIZE),width); j++)
182 {
183 q = filterdata[GVALUE(i,j,step)];
184 if (q > value)
185 {
186 p--;
187 }
188 }
189 }
190
191 if (p > SIZE)
192 {
193 //printf("%ix%i=%i\n",n,m,value);
194 cmax = value;
195 xmax = n;
196 ymax = m;
197 }
198 }
199 }
200 }
201
202 // Prepare combined output
203 // Some way of making the picture more pretty
204 //cvSmooth(filter,target, CV_MEDIAN, 3, 3, 3, 3);
205 cvCopy(filter,target, NULL);
206 cvCircle(target, cvPoint(xmax, ymax), 10, CV_RGB(255,0,0),4, 8, 0);
207
208
209 // Show all the images
210 cvShowImage("RGB_Image", output);
211 cvShowImage("R_Image", routput);
212 cvShowImage("B_Image", boutput);
213 cvShowImage("G_Image", goutput);
214 cvShowImage("F_Image", target);
215
216
217 // wait for any to press to quit
218 // Eeks! also HACK to make it refresh properly
219 if (cvWaitKey(20) != -1) {
220 break;
221 }
222 } //for each frame ...
223
224
225 cvReleaseCapture(&capture);
226 cvDestroyWindow("RGB_Image");
227 cvDestroyWindow("R_Image");
228 cvDestroyWindow("G_Image");
229 cvDestroyWindow("B_Image");
230 cvDestroyWindow("F_Image");
231
232 return 0;
233
234}
235
236// Get ourself into the highGUI main stuff
237int main(int argc, char *argv[]) {
238 return cvInitSystem(argc, argv);
239}
Note: See TracBrowser for help on using the repository browser.