[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 */
|
---|
| 20 | xdo_t *xdo;
|
---|
| 21 |
|
---|
[9] | 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 |
|
---|
[19] | 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 |
|
---|
[15] | 41 | int 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;
|
---|
[17] | 87 | int retval;
|
---|
| 88 | char arg1[4], arg2[4];
|
---|
[20] | 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;
|
---|
[9] | 94 |
|
---|
[8] | 95 |
|
---|
[14] | 96 | int cvalue = 0;
|
---|
| 97 | int xmax = -10;
|
---|
| 98 | int ymax = -10;
|
---|
[8] | 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 |
|
---|
[15] | 113 | //cvCopy(image, output, NULL);
|
---|
| 114 | //Remove Noise from images
|
---|
| 115 | cvSmooth(image,output,CV_BLUR, 3, 3, 3,3);
|
---|
[8] | 116 | cvCopy(image, routput, NULL);
|
---|
| 117 | cvCopy(image, goutput, NULL);
|
---|
| 118 | cvCopy(image, boutput, NULL);
|
---|
| 119 |
|
---|
| 120 |
|
---|
[9] | 121 | outdata = (uchar *) output->imageData;
|
---|
[8] | 122 | routdata = (uchar *) routput->imageData;
|
---|
| 123 | goutdata = (uchar *) goutput->imageData;
|
---|
| 124 | boutdata = (uchar *) boutput->imageData;
|
---|
[14] | 125 | targetdata = (uchar *) target->imageData;
|
---|
[8] | 126 |
|
---|
[14] | 127 | cvSub(output,filter,target,NULL);
|
---|
| 128 | cvalue = 20;
|
---|
| 129 | k = 0, p = 0, q = 0;
|
---|
[8] | 130 | for( m = 0; m < image->height; m++) //row
|
---|
| 131 | {
|
---|
| 132 | for( n = 0; n < image->width; n++) //column
|
---|
| 133 | {
|
---|
[14] | 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;
|
---|
[19] | 143 | //printf("Current value: %ix%i=%i\n", xmax, ymax, cvalue);
|
---|
[14] | 144 | }
|
---|
| 145 | } else {
|
---|
| 146 | q = 0;
|
---|
| 147 | }
|
---|
[9] | 148 | routdata[BVALUE(m,n,step)] = 0; // clear blue part
|
---|
| 149 | routdata[GVALUE(m,n,step)] = 0; // clear green part
|
---|
[8] | 150 |
|
---|
[9] | 151 | goutdata[BVALUE(m,n,step)] = 0; // clear blue part
|
---|
| 152 | goutdata[RVALUE(m,n,step)] = 0; // clear red part
|
---|
[8] | 153 |
|
---|
[9] | 154 | boutdata[GVALUE(m,n,step)] = 0; // clear green part
|
---|
| 155 | boutdata[RVALUE(m,n,step)] = 0; // clear red part
|
---|
[8] | 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | // Clear filter for new use
|
---|
[9] | 160 | // cvSetZero(filter);
|
---|
| 161 | // ... or clone orginal image for mapping over it
|
---|
| 162 | //cvCopy(goutput, filter, NULL);
|
---|
[8] | 163 |
|
---|
| 164 | // Make it yellow
|
---|
| 165 | //cvOr( routput, goutput, filter, NULL);
|
---|
| 166 |
|
---|
[9] | 167 | // XXX: Use stuff like cvMat, cvGetRawData instead of internal hacking
|
---|
| 168 | // Make green 'fade' out by the presence of many red as well
|
---|
[14] | 169 | // filterdata = (uchar *) filter->imageData;
|
---|
[9] | 170 |
|
---|
| 171 | // Prepare combined output
|
---|
[10] | 172 | // Some way of making the picture more pretty
|
---|
| 173 | //cvSmooth(filter,target, CV_MEDIAN, 3, 3, 3, 3);
|
---|
[14] | 174 | cvCopy(output,filter, NULL);
|
---|
[9] | 175 | cvCircle(target, cvPoint(xmax, ymax), 10, CV_RGB(255,0,0),4, 8, 0);
|
---|
[20] | 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 | }
|
---|
[8] | 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);
|
---|
[9] | 193 | cvShowImage("F_Image", target);
|
---|
[8] | 194 |
|
---|
| 195 |
|
---|
[20] | 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 | }
|
---|
[8] | 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 |
|
---|
[9] | 224 | // Get ourself into the highGUI main stuff
|
---|
[15] | 225 | // Quick at 1.0.0, not needing in 2.0x anymore
|
---|
[8] | 226 | int main(int argc, char *argv[]) {
|
---|
[17] | 227 | int retval;
|
---|
| 228 | xdo = xdo_new(getenv("DISPLAY"));
|
---|
| 229 | retval = InitSystem(argc, argv);
|
---|
| 230 | xdo_free(xdo);
|
---|
[19] | 231 | return(retval);
|
---|
[8] | 232 | }
|
---|