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

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

Bit hacky, but uinput did not do the trick. Using the X11Test does the trick

File size: 5.6 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
33int InitSystem(int argc, char *argv[]) {
34 // Don't care which camera to use. Use specified number elsewise
35 int cam_number = -1;
36 if (argc > 1)
37 cam_number = atoi(argv[1]);
38 printf("Using camera %i\n", cam_number);
39 CvCapture *capture = cvCaptureFromCAM(cam_number);
40 int width = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
41 int height = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
42
43
44 uchar *filterdata, *outdata, *routdata, *goutdata, *boutdata, *targetdata;
45
46
47 //Create a window
48 cvNamedWindow("RGB_Image", CV_WINDOW_AUTOSIZE);
49 cvMoveWindow("RGB_Image",0,0);
50 cvNamedWindow("R_Image", CV_WINDOW_AUTOSIZE);
51 cvMoveWindow("R_Image",0,height);
52 cvNamedWindow("G_Image", CV_WINDOW_AUTOSIZE);
53 cvMoveWindow("G_Image",width,0);
54 cvNamedWindow("B_Image", CV_WINDOW_AUTOSIZE);
55 cvMoveWindow("B_Image",width,height);
56 cvNamedWindow("F_Image", CV_WINDOW_AUTOSIZE);
57
58
59 //Create Image Skeletons
60 IplImage *image = NULL;
61 IplImage *output = cvCreateImage( cvSize(width,height), 8, 3);
62 IplImage *boutput = cvCreateImage( cvSize(width,height), 8, 3);
63 IplImage *routput = cvCreateImage( cvSize(width,height), 8, 3);
64 IplImage *goutput = cvCreateImage( cvSize(width,height), 8, 3);
65 IplImage *filter = cvCreateImage( cvSize(width,height), 8, 3);
66 IplImage *target = cvCreateImage( cvSize(width,height), 8, 3);
67
68
69 // Some temp pointers
70 int i, j, k, m, n, o, p;
71 int r, g, b;
72 int q;
73 int retval;
74 char arg1[4], arg2[4];
75
76
77 int cvalue = 0;
78 int xmax = -10;
79 int ymax = -10;
80 // Video run starts here ...
81 while (1)
82 {
83 image = cvQueryFrame(capture);
84
85 // We need some input before running any further
86 if (image == NULL) {
87 printf("Ehm no input in the image\n");
88 continue;
89 }
90
91 //This is needed for accessing pixel elements
92 int step = image->widthStep/sizeof(uchar);
93
94 //cvCopy(image, output, NULL);
95 //Remove Noise from images
96 cvSmooth(image,output,CV_BLUR, 3, 3, 3,3);
97 cvCopy(image, routput, NULL);
98 cvCopy(image, goutput, NULL);
99 cvCopy(image, boutput, NULL);
100
101
102 outdata = (uchar *) output->imageData;
103 routdata = (uchar *) routput->imageData;
104 goutdata = (uchar *) goutput->imageData;
105 boutdata = (uchar *) boutput->imageData;
106 targetdata = (uchar *) target->imageData;
107
108 cvSub(output,filter,target,NULL);
109 cvalue = 20;
110 k = 0, p = 0, q = 0;
111 for( m = 0; m < image->height; m++) //row
112 {
113 for( n = 0; n < image->width; n++) //column
114 {
115 //p = targetdata[RVALUE(m,n,step)] + targetdata[GVALUE(m,n,step)] + targetdata[BVALUE(m,n,step)];
116 p = targetdata[RVALUE(m,n,step)] + targetdata[GVALUE(m,n,step)];
117 if ( p > cvalue)
118 {
119 q++;
120 if (q > 5) {
121 cvalue = p;
122 xmax = n;
123 ymax = m;
124 printf("Current value: %ix%i=%i\n", xmax, ymax, cvalue);
125 }
126 } else {
127 q = 0;
128 }
129 routdata[BVALUE(m,n,step)] = 0; // clear blue part
130 routdata[GVALUE(m,n,step)] = 0; // clear green part
131
132 goutdata[BVALUE(m,n,step)] = 0; // clear blue part
133 goutdata[RVALUE(m,n,step)] = 0; // clear red part
134
135 boutdata[GVALUE(m,n,step)] = 0; // clear green part
136 boutdata[RVALUE(m,n,step)] = 0; // clear red part
137 }
138 }
139
140 // Clear filter for new use
141 // cvSetZero(filter);
142 // ... or clone orginal image for mapping over it
143 //cvCopy(goutput, filter, NULL);
144
145 // Make it yellow
146 //cvOr( routput, goutput, filter, NULL);
147
148 // XXX: Use stuff like cvMat, cvGetRawData instead of internal hacking
149 // Make green 'fade' out by the presence of many red as well
150 // filterdata = (uchar *) filter->imageData;
151
152 // Prepare combined output
153 // Some way of making the picture more pretty
154 //cvSmooth(filter,target, CV_MEDIAN, 3, 3, 3, 3);
155 cvCopy(output,filter, NULL);
156 cvCircle(target, cvPoint(xmax, ymax), 10, CV_RGB(255,0,0),4, 8, 0);
157 xdo_mousemove(xdo,xmax,ymax);
158
159
160 // Show all the images
161 cvShowImage("RGB_Image", output);
162 cvShowImage("R_Image", routput);
163 cvShowImage("B_Image", boutput);
164 cvShowImage("G_Image", goutput);
165 cvShowImage("F_Image", target);
166
167
168 // wait for any to press to quit
169 // Eeks! also HACK to make it refresh properly
170 if (cvWaitKey(20) != -1) {
171 break;
172 }
173 } //for each frame ...
174
175
176 cvReleaseCapture(&capture);
177 cvDestroyWindow("RGB_Image");
178 cvDestroyWindow("R_Image");
179 cvDestroyWindow("G_Image");
180 cvDestroyWindow("B_Image");
181 cvDestroyWindow("F_Image");
182
183 return 0;
184
185}
186
187// Get ourself into the highGUI main stuff
188// Quick at 1.0.0, not needing in 2.0x anymore
189int main(int argc, char *argv[]) {
190 int retval;
191 xdo = xdo_new(getenv("DISPLAY"));
192 retval = InitSystem(argc, argv);
193 xdo_free(xdo);
194}
Note: See TracBrowser for help on using the repository browser.