source: liacs/mms/project/colour-tracking.c@ 22

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

Code presented

File size: 7.0 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#include "xdo.h"
12
13#define DEBUG 1
14
15#define RVALUE(x,y,step) (x*step+y*3+2)
16#define GVALUE(x,y,step) (x*step+y*3+1)
17#define BVALUE(x,y,step) (x*step+y*3+0)
18
19/* XXX: Global variables */
20xdo_t *xdo;
21
22/* Used for playing with bounderies, returned max value of the pair */
23int max(const int x, const int y) {
24 return ((x > y) ? x : y);
25}
26
27/* Used for playing with bounderies, returned min value of the pair */
28int min(const int x, const int y) {
29 return ((x < y) ? x : y);
30
31}
32
33/* Convert the virtual mapping to screen size */
34int ratio (const int size, const int current_size, const int orig_size) {
35 double t = (double)size / current_size;
36 t *= orig_size;
37 // printf("%i/%i -> %i\n", size, current_size, (int)t);
38 return((int)t);
39}
40
41int InitSystem(int argc, char *argv[]) {
42 // Don't care which camera to use. Use specified number elsewise
43 if (argc < 3) {
44 printf("Usage: %s <cam number> <screen width> <screen height>\n",
45 argv[0]);
46 return(64);
47 }
48 const int cam_number = atoi(argv[1]);
49 const int screen_width = atoi(argv[2]);
50 const int screen_height = atoi(argv[3]);
51 printf("INFO Using camera %i\n", cam_number);
52 printf("INFO Screen size %ix%i\n", screen_width, screen_height);
53 CvCapture *capture = cvCaptureFromCAM(cam_number);
54 int width = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
55 int height = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
56
57
58 uchar *filterdata, *outdata, *routdata, *goutdata, *boutdata, *targetdata;
59
60
61 //Create a window
62 cvNamedWindow("RGB_Image", CV_WINDOW_AUTOSIZE);
63 cvMoveWindow("RGB_Image",0,0);
64 cvNamedWindow("R_Image", CV_WINDOW_AUTOSIZE);
65 cvMoveWindow("R_Image",0,height);
66 cvNamedWindow("G_Image", CV_WINDOW_AUTOSIZE);
67 cvMoveWindow("G_Image",width,0);
68 cvNamedWindow("B_Image", CV_WINDOW_AUTOSIZE);
69 cvMoveWindow("B_Image",width,height);
70 cvNamedWindow("F_Image", CV_WINDOW_AUTOSIZE);
71
72
73 //Create Image Skeletons
74 IplImage *image = NULL;
75 IplImage *output = cvCreateImage( cvSize(width,height), 8, 3);
76 IplImage *boutput = cvCreateImage( cvSize(width,height), 8, 3);
77 IplImage *routput = cvCreateImage( cvSize(width,height), 8, 3);
78 IplImage *goutput = cvCreateImage( cvSize(width,height), 8, 3);
79 IplImage *filter = cvCreateImage( cvSize(width,height), 8, 3);
80 IplImage *target = cvCreateImage( cvSize(width,height), 8, 3);
81
82
83 // Some temp pointers
84 int i, j, k, m, n, o, p;
85 int r, g, b;
86 int q;
87 int retval;
88 char arg1[4], arg2[4];
89 int keycode;
90 // Actually move the pointer to it's location
91 int mouse_flag = 1;
92 // XXX: Hack to have the pointer posted on the second screen.
93 int ex_screen_flag = 0;
94
95
96 int cvalue = 0;
97 int xmax = -10;
98 int ymax = -10;
99 // Video run starts here ...
100 while (1)
101 {
102 image = cvQueryFrame(capture);
103
104 // We need some input before running any further
105 if (image == NULL) {
106 printf("Ehm no input in the image\n");
107 continue;
108 }
109
110 //This is needed for accessing pixel elements
111 int step = image->widthStep/sizeof(uchar);
112
113 //cvCopy(image, output, NULL);
114 //Remove Noise from images
115 cvSmooth(image,output,CV_BLUR, 3, 3, 3,3);
116 cvCopy(image, routput, NULL);
117 cvCopy(image, goutput, NULL);
118 cvCopy(image, boutput, NULL);
119
120
121 outdata = (uchar *) output->imageData;
122 routdata = (uchar *) routput->imageData;
123 goutdata = (uchar *) goutput->imageData;
124 boutdata = (uchar *) boutput->imageData;
125 targetdata = (uchar *) target->imageData;
126
127 cvSub(output,filter,target,NULL);
128 cvalue = 20;
129 k = 0, p = 0, q = 0;
130 for( m = 0; m < image->height; m++) //row
131 {
132 for( n = 0; n < image->width; n++) //column
133 {
134 //p = targetdata[RVALUE(m,n,step)] + targetdata[GVALUE(m,n,step)] + targetdata[BVALUE(m,n,step)];
135 p = targetdata[RVALUE(m,n,step)] + targetdata[GVALUE(m,n,step)];
136 if ( p > cvalue)
137 {
138 q++;
139 if (q > 5) {
140 cvalue = p;
141 xmax = n;
142 ymax = m;
143 //printf("Current value: %ix%i=%i\n", xmax, ymax, cvalue);
144 }
145 } else {
146 q = 0;
147 }
148 routdata[BVALUE(m,n,step)] = 0; // clear blue part
149 routdata[GVALUE(m,n,step)] = 0; // clear green part
150
151 goutdata[BVALUE(m,n,step)] = 0; // clear blue part
152 goutdata[RVALUE(m,n,step)] = 0; // clear red part
153
154 boutdata[GVALUE(m,n,step)] = 0; // clear green part
155 boutdata[RVALUE(m,n,step)] = 0; // clear red part
156 }
157 }
158
159 // Clear filter for new use
160 // cvSetZero(filter);
161 // ... or clone orginal image for mapping over it
162 //cvCopy(goutput, filter, NULL);
163
164 // Make it yellow
165 //cvOr( routput, goutput, filter, NULL);
166
167 // XXX: Use stuff like cvMat, cvGetRawData instead of internal hacking
168 // Make green 'fade' out by the presence of many red as well
169 // filterdata = (uchar *) filter->imageData;
170
171 // Prepare combined output
172 // Some way of making the picture more pretty
173 //cvSmooth(filter,target, CV_MEDIAN, 3, 3, 3, 3);
174 cvCopy(output,filter, NULL);
175 cvCircle(target, cvPoint(xmax, ymax), 10, CV_RGB(255,0,0),4, 8, 0);
176 // Set to pointer to new location
177 if (mouse_flag) {
178 if (ex_screen_flag) {
179 xdo_mousemove(xdo, ratio(xmax,width,screen_width) + screen_width - 250,
180 ratio(ymax,height,screen_height));
181 } else {
182 xdo_mousemove(xdo, ratio(xmax,width,screen_width),
183 ratio(ymax,height,screen_height));
184 }
185 }
186
187
188 // Show all the images
189 cvShowImage("RGB_Image", output);
190 cvShowImage("R_Image", routput);
191 cvShowImage("B_Image", boutput);
192 cvShowImage("G_Image", goutput);
193 cvShowImage("F_Image", target);
194
195
196 // Process some keyboard input for handy selection
197 // Eeks! also HACK to make it refresh properly
198 keycode = cvWaitKey(20);
199 if (keycode != -1) {
200 printf("Keycode: %i\n", keycode);
201 if (keycode == 'q') {
202 break;
203 } else if (keycode == 'm') {
204 mouse_flag = (mouse_flag == 1) ? 0 : 1;
205 } else if (keycode == 'e') {
206 ex_screen_flag = (ex_screen_flag == 1) ? 0 : 1;
207
208 }
209 }
210 } //for each frame ...
211
212
213 cvReleaseCapture(&capture);
214 cvDestroyWindow("RGB_Image");
215 cvDestroyWindow("R_Image");
216 cvDestroyWindow("G_Image");
217 cvDestroyWindow("B_Image");
218 cvDestroyWindow("F_Image");
219
220 return 0;
221
222}
223
224// Get ourself into the highGUI main stuff
225// Quick at 1.0.0, not needing in 2.0x anymore
226int main(int argc, char *argv[]) {
227 int retval;
228 xdo = xdo_new(getenv("DISPLAY"));
229 retval = InitSystem(argc, argv);
230 xdo_free(xdo);
231 return(retval);
232}
Note: See TracBrowser for help on using the repository browser.