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