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

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

Final commit of report to be handed in.

File size: 8.4 KB
RevLine 
[8]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>
[17]11#include "xdo.h"
[8]12
[9]13#define DEBUG 1
[8]14
[9]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
[17]19/* XXX: Global variables */
20xdo_t *xdo;
21
[9]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
[19]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
[15]41int InitSystem(int argc, char *argv[]) {
[8]42 // Don't care which camera to use. Use specified number elsewise
[19]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);
[8]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
[14]58 uchar *filterdata, *outdata, *routdata, *goutdata, *boutdata, *targetdata;
[8]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);
[9]80 IplImage *target = cvCreateImage( cvSize(width,height), 8, 3);
[8]81
82
83 // Some temp pointers
[9]84 int i, j, k, m, n, o, p;
85 int r, g, b;
86 int q;
[23]87 int x_move, y_move;
88
[17]89 int retval;
90 char arg1[4], arg2[4];
[20]91 int keycode;
92 // Actually move the pointer to it's location
[23]93 int mouse_flag = 0;
94 int r_window_flag = 1;
95 int g_window_flag = 1;
96 int b_window_flag = 1;
[20]97 // XXX: Hack to have the pointer posted on the second screen.
98 int ex_screen_flag = 0;
[23]99 int x_ajust = -114;
100 int y_ajust = -95;
101
[8]102
[14]103 int cvalue = 0;
104 int xmax = -10;
105 int ymax = -10;
[8]106 // Video run starts here ...
107 while (1)
108 {
109 image = cvQueryFrame(capture);
110
111 // We need some input before running any further
112 if (image == NULL) {
113 printf("Ehm no input in the image\n");
114 continue;
115 }
116
117 //This is needed for accessing pixel elements
118 int step = image->widthStep/sizeof(uchar);
119
[15]120 //cvCopy(image, output, NULL);
[23]121 //Remove Noise from images
122 cvSmooth(image,output,CV_BLUR, 3, 3, 3,3);
[8]123 cvCopy(image, routput, NULL);
124 cvCopy(image, goutput, NULL);
125 cvCopy(image, boutput, NULL);
126
127
[9]128 outdata = (uchar *) output->imageData;
[8]129 routdata = (uchar *) routput->imageData;
130 goutdata = (uchar *) goutput->imageData;
131 boutdata = (uchar *) boutput->imageData;
[14]132 targetdata = (uchar *) target->imageData;
[8]133
[14]134 cvSub(output,filter,target,NULL);
135 cvalue = 20;
136 k = 0, p = 0, q = 0;
[8]137 for( m = 0; m < image->height; m++) //row
138 {
139 for( n = 0; n < image->width; n++) //column
140 {
[14]141 //p = targetdata[RVALUE(m,n,step)] + targetdata[GVALUE(m,n,step)] + targetdata[BVALUE(m,n,step)];
142 p = targetdata[RVALUE(m,n,step)] + targetdata[GVALUE(m,n,step)];
143 if ( p > cvalue)
144 {
145 q++;
146 if (q > 5) {
147 cvalue = p;
148 xmax = n;
149 ymax = m;
[19]150 //printf("Current value: %ix%i=%i\n", xmax, ymax, cvalue);
[14]151 }
152 } else {
153 q = 0;
154 }
[9]155 routdata[BVALUE(m,n,step)] = 0; // clear blue part
156 routdata[GVALUE(m,n,step)] = 0; // clear green part
[8]157
[9]158 goutdata[BVALUE(m,n,step)] = 0; // clear blue part
159 goutdata[RVALUE(m,n,step)] = 0; // clear red part
[8]160
[9]161 boutdata[GVALUE(m,n,step)] = 0; // clear green part
162 boutdata[RVALUE(m,n,step)] = 0; // clear red part
[8]163 }
164 }
165
166 // Clear filter for new use
[9]167 // cvSetZero(filter);
168 // ... or clone orginal image for mapping over it
169 //cvCopy(goutput, filter, NULL);
[8]170
171 // Make it yellow
172 //cvOr( routput, goutput, filter, NULL);
173
[9]174 // XXX: Use stuff like cvMat, cvGetRawData instead of internal hacking
175 // Make green 'fade' out by the presence of many red as well
[14]176 // filterdata = (uchar *) filter->imageData;
[9]177
178 // Prepare combined output
[10]179 // Some way of making the picture more pretty
180 //cvSmooth(filter,target, CV_MEDIAN, 3, 3, 3, 3);
[14]181 cvCopy(output,filter, NULL);
[9]182 cvCircle(target, cvPoint(xmax, ymax), 10, CV_RGB(255,0,0),4, 8, 0);
[20]183 // Set to pointer to new location
[23]184 // Some Hacking to get the pointer synced
[20]185 if (mouse_flag) {
[23]186 x_move = ratio(xmax + x_ajust,width,screen_width);
187 y_move = ratio(ymax + y_ajust,height,screen_height);
[20]188 if (ex_screen_flag) {
[23]189 x_move += screen_width;
[20]190 }
[23]191 xdo_mousemove(xdo, x_move, y_move);
[20]192 }
[8]193
194
195 // Show all the images
196 cvShowImage("RGB_Image", output);
[23]197 if (r_window_flag)
198 cvShowImage("R_Image", routput);
199 if (b_window_flag)
200 cvShowImage("B_Image", boutput);
201 if (g_window_flag)
202 cvShowImage("G_Image", goutput);
[9]203 cvShowImage("F_Image", target);
[8]204
205
[20]206 // Process some keyboard input for handy selection
207 // Eeks! also HACK to make it refresh properly
208 keycode = cvWaitKey(20);
209 if (keycode != -1) {
210 printf("Keycode: %i\n", keycode);
211 if (keycode == 'q') {
212 break;
213 } else if (keycode == 'm') {
[23]214 mouse_flag ^= 1;
[20]215 } else if (keycode == 'e') {
[23]216 ex_screen_flag ^= 1;
217 // Hack to manually sync mouse pointer with laser pointer
218 } else if (keycode == 'w' || keycode == 's' || keycode == 'a' || \
219 keycode == 'd') {
220 if (keycode == 'w') {
221 y_ajust--;
222 } else if (keycode == 's') {
223 y_ajust++;
224 } else if (keycode == 'a') {
225 x_ajust--;
226 } else if (keycode == 'd') {
227 x_ajust++;
228 }
229 printf("New x,y ajust: %i,%i\n",x_ajust,y_ajust);
230 } else if (keycode == 'r' || keycode == 'g' || keycode == 'b') {
231 printf("Toggle window: %c\n", keycode);
232 if (keycode == 'r') {
233 r_window_flag ^= 1;
234 } else if (keycode == 'g') {
235 g_window_flag ^= 1;
236 } else if (keycode == 'b') {
237 b_window_flag ^= 1;
238 }
[20]239 }
[23]240 }
[8]241 } //for each frame ...
242
243
244 cvReleaseCapture(&capture);
245 cvDestroyWindow("RGB_Image");
246 cvDestroyWindow("R_Image");
247 cvDestroyWindow("G_Image");
248 cvDestroyWindow("B_Image");
249 cvDestroyWindow("F_Image");
250
251 return 0;
252
253}
254
[9]255// Get ourself into the highGUI main stuff
[15]256// Quick at 1.0.0, not needing in 2.0x anymore
[8]257int main(int argc, char *argv[]) {
[17]258 int retval;
[23]259 printf("=== INSTRUCTION == \n\
260Keybindings:\n\
261e = toggle mouse on external screen mouse pointer\n\
262m = toggle mouse tracking\n\
263q = quit\n\
264Mouse/laser pointer syncer:\n\
265w = up\n\
266s = down\n\
267a = left\n\
268f = right\n\
269");
[17]270 xdo = xdo_new(getenv("DISPLAY"));
271 retval = InitSystem(argc, argv);
272 xdo_free(xdo);
[19]273 return(retval);
[8]274}
Note: See TracBrowser for help on using the repository browser.