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 */
|
---|
20 | xdo_t *xdo;
|
---|
21 |
|
---|
22 | /* Used for playing with bounderies, returned max value of the pair */
|
---|
23 | int 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 */
|
---|
28 | int min(const int x, const int y) {
|
---|
29 | return ((x < y) ? x : y);
|
---|
30 |
|
---|
31 | }
|
---|
32 |
|
---|
33 | /* Convert the virtual mapping to screen size */
|
---|
34 | int 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 |
|
---|
41 | int 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 |
|
---|
90 |
|
---|
91 | int cvalue = 0;
|
---|
92 | int xmax = -10;
|
---|
93 | int ymax = -10;
|
---|
94 | // Video run starts here ...
|
---|
95 | while (1)
|
---|
96 | {
|
---|
97 | image = cvQueryFrame(capture);
|
---|
98 |
|
---|
99 | // We need some input before running any further
|
---|
100 | if (image == NULL) {
|
---|
101 | printf("Ehm no input in the image\n");
|
---|
102 | continue;
|
---|
103 | }
|
---|
104 |
|
---|
105 | //This is needed for accessing pixel elements
|
---|
106 | int step = image->widthStep/sizeof(uchar);
|
---|
107 |
|
---|
108 | //cvCopy(image, output, NULL);
|
---|
109 | //Remove Noise from images
|
---|
110 | cvSmooth(image,output,CV_BLUR, 3, 3, 3,3);
|
---|
111 | cvCopy(image, routput, NULL);
|
---|
112 | cvCopy(image, goutput, NULL);
|
---|
113 | cvCopy(image, boutput, NULL);
|
---|
114 |
|
---|
115 |
|
---|
116 | outdata = (uchar *) output->imageData;
|
---|
117 | routdata = (uchar *) routput->imageData;
|
---|
118 | goutdata = (uchar *) goutput->imageData;
|
---|
119 | boutdata = (uchar *) boutput->imageData;
|
---|
120 | targetdata = (uchar *) target->imageData;
|
---|
121 |
|
---|
122 | cvSub(output,filter,target,NULL);
|
---|
123 | cvalue = 20;
|
---|
124 | k = 0, p = 0, q = 0;
|
---|
125 | for( m = 0; m < image->height; m++) //row
|
---|
126 | {
|
---|
127 | for( n = 0; n < image->width; n++) //column
|
---|
128 | {
|
---|
129 | //p = targetdata[RVALUE(m,n,step)] + targetdata[GVALUE(m,n,step)] + targetdata[BVALUE(m,n,step)];
|
---|
130 | p = targetdata[RVALUE(m,n,step)] + targetdata[GVALUE(m,n,step)];
|
---|
131 | if ( p > cvalue)
|
---|
132 | {
|
---|
133 | q++;
|
---|
134 | if (q > 5) {
|
---|
135 | cvalue = p;
|
---|
136 | xmax = n;
|
---|
137 | ymax = m;
|
---|
138 | //printf("Current value: %ix%i=%i\n", xmax, ymax, cvalue);
|
---|
139 | }
|
---|
140 | } else {
|
---|
141 | q = 0;
|
---|
142 | }
|
---|
143 | routdata[BVALUE(m,n,step)] = 0; // clear blue part
|
---|
144 | routdata[GVALUE(m,n,step)] = 0; // clear green part
|
---|
145 |
|
---|
146 | goutdata[BVALUE(m,n,step)] = 0; // clear blue part
|
---|
147 | goutdata[RVALUE(m,n,step)] = 0; // clear red part
|
---|
148 |
|
---|
149 | boutdata[GVALUE(m,n,step)] = 0; // clear green part
|
---|
150 | boutdata[RVALUE(m,n,step)] = 0; // clear red part
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | // Clear filter for new use
|
---|
155 | // cvSetZero(filter);
|
---|
156 | // ... or clone orginal image for mapping over it
|
---|
157 | //cvCopy(goutput, filter, NULL);
|
---|
158 |
|
---|
159 | // Make it yellow
|
---|
160 | //cvOr( routput, goutput, filter, NULL);
|
---|
161 |
|
---|
162 | // XXX: Use stuff like cvMat, cvGetRawData instead of internal hacking
|
---|
163 | // Make green 'fade' out by the presence of many red as well
|
---|
164 | // filterdata = (uchar *) filter->imageData;
|
---|
165 |
|
---|
166 | // Prepare combined output
|
---|
167 | // Some way of making the picture more pretty
|
---|
168 | //cvSmooth(filter,target, CV_MEDIAN, 3, 3, 3, 3);
|
---|
169 | cvCopy(output,filter, NULL);
|
---|
170 | cvCircle(target, cvPoint(xmax, ymax), 10, CV_RGB(255,0,0),4, 8, 0);
|
---|
171 | xdo_mousemove(xdo, ratio(xmax,width,screen_width),
|
---|
172 | ratio(ymax,height,screen_height));
|
---|
173 |
|
---|
174 |
|
---|
175 | // Show all the images
|
---|
176 | cvShowImage("RGB_Image", output);
|
---|
177 | cvShowImage("R_Image", routput);
|
---|
178 | cvShowImage("B_Image", boutput);
|
---|
179 | cvShowImage("G_Image", goutput);
|
---|
180 | cvShowImage("F_Image", target);
|
---|
181 |
|
---|
182 |
|
---|
183 | // wait for any to press to quit
|
---|
184 | // Eeks! also HACK to make it refresh properly
|
---|
185 | if (cvWaitKey(20) != -1) {
|
---|
186 | break;
|
---|
187 | }
|
---|
188 | } //for each frame ...
|
---|
189 |
|
---|
190 |
|
---|
191 | cvReleaseCapture(&capture);
|
---|
192 | cvDestroyWindow("RGB_Image");
|
---|
193 | cvDestroyWindow("R_Image");
|
---|
194 | cvDestroyWindow("G_Image");
|
---|
195 | cvDestroyWindow("B_Image");
|
---|
196 | cvDestroyWindow("F_Image");
|
---|
197 |
|
---|
198 | return 0;
|
---|
199 |
|
---|
200 | }
|
---|
201 |
|
---|
202 | // Get ourself into the highGUI main stuff
|
---|
203 | // Quick at 1.0.0, not needing in 2.0x anymore
|
---|
204 | int main(int argc, char *argv[]) {
|
---|
205 | int retval;
|
---|
206 | xdo = xdo_new(getenv("DISPLAY"));
|
---|
207 | retval = InitSystem(argc, argv);
|
---|
208 | xdo_free(xdo);
|
---|
209 | return(retval);
|
---|
210 | }
|
---|