[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 |
|
---|
[15] | 33 | int InitSystem(int argc, char *argv[]) {
|
---|
[8] | 34 | // Don't care which camera to use. Use specified number elsewise
|
---|
[14] | 35 | int cam_number = -1;
|
---|
| 36 | if (argc > 1)
|
---|
[15] | 37 | cam_number = atoi(argv[1]);
|
---|
[14] | 38 | printf("Using camera %i\n", cam_number);
|
---|
[8] | 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 |
|
---|
[14] | 44 | uchar *filterdata, *outdata, *routdata, *goutdata, *boutdata, *targetdata;
|
---|
[8] | 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);
|
---|
[9] | 66 | IplImage *target = cvCreateImage( cvSize(width,height), 8, 3);
|
---|
[8] | 67 |
|
---|
| 68 |
|
---|
| 69 | // Some temp pointers
|
---|
[9] | 70 | int i, j, k, m, n, o, p;
|
---|
| 71 | int r, g, b;
|
---|
| 72 | int q;
|
---|
[17] | 73 | int retval;
|
---|
| 74 | char arg1[4], arg2[4];
|
---|
[9] | 75 |
|
---|
[8] | 76 |
|
---|
[14] | 77 | int cvalue = 0;
|
---|
| 78 | int xmax = -10;
|
---|
| 79 | int ymax = -10;
|
---|
[8] | 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 |
|
---|
[15] | 94 | //cvCopy(image, output, NULL);
|
---|
| 95 | //Remove Noise from images
|
---|
| 96 | cvSmooth(image,output,CV_BLUR, 3, 3, 3,3);
|
---|
[8] | 97 | cvCopy(image, routput, NULL);
|
---|
| 98 | cvCopy(image, goutput, NULL);
|
---|
| 99 | cvCopy(image, boutput, NULL);
|
---|
| 100 |
|
---|
| 101 |
|
---|
[9] | 102 | outdata = (uchar *) output->imageData;
|
---|
[8] | 103 | routdata = (uchar *) routput->imageData;
|
---|
| 104 | goutdata = (uchar *) goutput->imageData;
|
---|
| 105 | boutdata = (uchar *) boutput->imageData;
|
---|
[14] | 106 | targetdata = (uchar *) target->imageData;
|
---|
[8] | 107 |
|
---|
[14] | 108 | cvSub(output,filter,target,NULL);
|
---|
| 109 | cvalue = 20;
|
---|
| 110 | k = 0, p = 0, q = 0;
|
---|
[8] | 111 | for( m = 0; m < image->height; m++) //row
|
---|
| 112 | {
|
---|
| 113 | for( n = 0; n < image->width; n++) //column
|
---|
| 114 | {
|
---|
[14] | 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 | }
|
---|
[9] | 129 | routdata[BVALUE(m,n,step)] = 0; // clear blue part
|
---|
| 130 | routdata[GVALUE(m,n,step)] = 0; // clear green part
|
---|
[8] | 131 |
|
---|
[9] | 132 | goutdata[BVALUE(m,n,step)] = 0; // clear blue part
|
---|
| 133 | goutdata[RVALUE(m,n,step)] = 0; // clear red part
|
---|
[8] | 134 |
|
---|
[9] | 135 | boutdata[GVALUE(m,n,step)] = 0; // clear green part
|
---|
| 136 | boutdata[RVALUE(m,n,step)] = 0; // clear red part
|
---|
[8] | 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | // Clear filter for new use
|
---|
[9] | 141 | // cvSetZero(filter);
|
---|
| 142 | // ... or clone orginal image for mapping over it
|
---|
| 143 | //cvCopy(goutput, filter, NULL);
|
---|
[8] | 144 |
|
---|
| 145 | // Make it yellow
|
---|
| 146 | //cvOr( routput, goutput, filter, NULL);
|
---|
| 147 |
|
---|
[9] | 148 | // XXX: Use stuff like cvMat, cvGetRawData instead of internal hacking
|
---|
| 149 | // Make green 'fade' out by the presence of many red as well
|
---|
[14] | 150 | // filterdata = (uchar *) filter->imageData;
|
---|
[9] | 151 |
|
---|
| 152 | // Prepare combined output
|
---|
[10] | 153 | // Some way of making the picture more pretty
|
---|
| 154 | //cvSmooth(filter,target, CV_MEDIAN, 3, 3, 3, 3);
|
---|
[14] | 155 | cvCopy(output,filter, NULL);
|
---|
[9] | 156 | cvCircle(target, cvPoint(xmax, ymax), 10, CV_RGB(255,0,0),4, 8, 0);
|
---|
[17] | 157 | xdo_mousemove(xdo,xmax,ymax);
|
---|
[8] | 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);
|
---|
[9] | 165 | cvShowImage("F_Image", target);
|
---|
[8] | 166 |
|
---|
| 167 |
|
---|
[9] | 168 | // wait for any to press to quit
|
---|
| 169 | // Eeks! also HACK to make it refresh properly
|
---|
[15] | 170 | if (cvWaitKey(20) != -1) {
|
---|
[8] | 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 |
|
---|
[9] | 187 | // Get ourself into the highGUI main stuff
|
---|
[15] | 188 | // Quick at 1.0.0, not needing in 2.0x anymore
|
---|
[8] | 189 | int main(int argc, char *argv[]) {
|
---|
[17] | 190 | int retval;
|
---|
| 191 | xdo = xdo_new(getenv("DISPLAY"));
|
---|
| 192 | retval = InitSystem(argc, argv);
|
---|
| 193 | xdo_free(xdo);
|
---|
[8] | 194 | }
|
---|