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 |
|
---|
12 | #define DEBUG 1
|
---|
13 |
|
---|
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 |
|
---|
29 | int InitSystem(int argc, char *argv[]) {
|
---|
30 | // Don't care which camera to use. Use specified number elsewise
|
---|
31 | int cam_number = -1;
|
---|
32 | if (argc > 1)
|
---|
33 | cam_number = atoi(argv[1]);
|
---|
34 | printf("Using camera %i\n", cam_number);
|
---|
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 |
|
---|
40 | uchar *filterdata, *outdata, *routdata, *goutdata, *boutdata, *targetdata;
|
---|
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);
|
---|
62 | IplImage *target = cvCreateImage( cvSize(width,height), 8, 3);
|
---|
63 |
|
---|
64 |
|
---|
65 | // Some temp pointers
|
---|
66 | int i, j, k, m, n, o, p;
|
---|
67 | int r, g, b;
|
---|
68 | int q;
|
---|
69 |
|
---|
70 |
|
---|
71 | int cvalue = 0;
|
---|
72 | int xmax = -10;
|
---|
73 | int ymax = -10;
|
---|
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 |
|
---|
88 | //cvCopy(image, output, NULL);
|
---|
89 | //Remove Noise from images
|
---|
90 | cvSmooth(image,output,CV_BLUR, 3, 3, 3,3);
|
---|
91 | cvCopy(image, routput, NULL);
|
---|
92 | cvCopy(image, goutput, NULL);
|
---|
93 | cvCopy(image, boutput, NULL);
|
---|
94 |
|
---|
95 |
|
---|
96 | outdata = (uchar *) output->imageData;
|
---|
97 | routdata = (uchar *) routput->imageData;
|
---|
98 | goutdata = (uchar *) goutput->imageData;
|
---|
99 | boutdata = (uchar *) boutput->imageData;
|
---|
100 | targetdata = (uchar *) target->imageData;
|
---|
101 |
|
---|
102 | cvSub(output,filter,target,NULL);
|
---|
103 | cvalue = 20;
|
---|
104 | k = 0, p = 0, q = 0;
|
---|
105 | for( m = 0; m < image->height; m++) //row
|
---|
106 | {
|
---|
107 | for( n = 0; n < image->width; n++) //column
|
---|
108 | {
|
---|
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 | }
|
---|
123 | routdata[BVALUE(m,n,step)] = 0; // clear blue part
|
---|
124 | routdata[GVALUE(m,n,step)] = 0; // clear green part
|
---|
125 |
|
---|
126 | goutdata[BVALUE(m,n,step)] = 0; // clear blue part
|
---|
127 | goutdata[RVALUE(m,n,step)] = 0; // clear red part
|
---|
128 |
|
---|
129 | boutdata[GVALUE(m,n,step)] = 0; // clear green part
|
---|
130 | boutdata[RVALUE(m,n,step)] = 0; // clear red part
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | // Clear filter for new use
|
---|
135 | // cvSetZero(filter);
|
---|
136 | // ... or clone orginal image for mapping over it
|
---|
137 | //cvCopy(goutput, filter, NULL);
|
---|
138 |
|
---|
139 | // Make it yellow
|
---|
140 | //cvOr( routput, goutput, filter, NULL);
|
---|
141 |
|
---|
142 | // XXX: Use stuff like cvMat, cvGetRawData instead of internal hacking
|
---|
143 | // Make green 'fade' out by the presence of many red as well
|
---|
144 | // filterdata = (uchar *) filter->imageData;
|
---|
145 |
|
---|
146 | // Prepare combined output
|
---|
147 | // Some way of making the picture more pretty
|
---|
148 | //cvSmooth(filter,target, CV_MEDIAN, 3, 3, 3, 3);
|
---|
149 | cvCopy(output,filter, NULL);
|
---|
150 | cvCircle(target, cvPoint(xmax, ymax), 10, CV_RGB(255,0,0),4, 8, 0);
|
---|
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);
|
---|
158 | cvShowImage("F_Image", target);
|
---|
159 |
|
---|
160 |
|
---|
161 | // wait for any to press to quit
|
---|
162 | // Eeks! also HACK to make it refresh properly
|
---|
163 | if (cvWaitKey(20) != -1) {
|
---|
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 |
|
---|
180 | // Get ourself into the highGUI main stuff
|
---|
181 | // Quick at 1.0.0, not needing in 2.0x anymore
|
---|
182 | int main(int argc, char *argv[]) {
|
---|
183 | return InitSystem(argc, argv);
|
---|
184 | }
|
---|