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 cvInitSystem(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]) > 0 ? atoi(argv[1]) : -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 | cvCopy(image, routput, NULL);
|
---|
90 | cvCopy(image, goutput, NULL);
|
---|
91 | cvCopy(image, boutput, NULL);
|
---|
92 |
|
---|
93 |
|
---|
94 | outdata = (uchar *) output->imageData;
|
---|
95 | routdata = (uchar *) routput->imageData;
|
---|
96 | goutdata = (uchar *) goutput->imageData;
|
---|
97 | boutdata = (uchar *) boutput->imageData;
|
---|
98 | targetdata = (uchar *) target->imageData;
|
---|
99 |
|
---|
100 | cvSub(output,filter,target,NULL);
|
---|
101 | cvalue = 20;
|
---|
102 | k = 0, p = 0, q = 0;
|
---|
103 | for( m = 0; m < image->height; m++) //row
|
---|
104 | {
|
---|
105 | for( n = 0; n < image->width; n++) //column
|
---|
106 | {
|
---|
107 | //p = targetdata[RVALUE(m,n,step)] + targetdata[GVALUE(m,n,step)] + targetdata[BVALUE(m,n,step)];
|
---|
108 | p = targetdata[RVALUE(m,n,step)] + targetdata[GVALUE(m,n,step)];
|
---|
109 | if ( p > cvalue)
|
---|
110 | {
|
---|
111 | q++;
|
---|
112 | if (q > 5) {
|
---|
113 | cvalue = p;
|
---|
114 | xmax = n;
|
---|
115 | ymax = m;
|
---|
116 | printf("Current value: %ix%i=%i\n", xmax, ymax, cvalue);
|
---|
117 | }
|
---|
118 | } else {
|
---|
119 | q = 0;
|
---|
120 | }
|
---|
121 | routdata[BVALUE(m,n,step)] = 0; // clear blue part
|
---|
122 | routdata[GVALUE(m,n,step)] = 0; // clear green part
|
---|
123 |
|
---|
124 | goutdata[BVALUE(m,n,step)] = 0; // clear blue part
|
---|
125 | goutdata[RVALUE(m,n,step)] = 0; // clear red part
|
---|
126 |
|
---|
127 | boutdata[GVALUE(m,n,step)] = 0; // clear green part
|
---|
128 | boutdata[RVALUE(m,n,step)] = 0; // clear red part
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | // Clear filter for new use
|
---|
133 | // cvSetZero(filter);
|
---|
134 | // ... or clone orginal image for mapping over it
|
---|
135 | //cvCopy(goutput, filter, NULL);
|
---|
136 |
|
---|
137 | // Make it yellow
|
---|
138 | //cvOr( routput, goutput, filter, NULL);
|
---|
139 |
|
---|
140 | // XXX: Use stuff like cvMat, cvGetRawData instead of internal hacking
|
---|
141 | // Make green 'fade' out by the presence of many red as well
|
---|
142 | // filterdata = (uchar *) filter->imageData;
|
---|
143 |
|
---|
144 | // Prepare combined output
|
---|
145 | // Some way of making the picture more pretty
|
---|
146 | //cvSmooth(filter,target, CV_MEDIAN, 3, 3, 3, 3);
|
---|
147 | cvCopy(output,filter, NULL);
|
---|
148 | cvCircle(target, cvPoint(xmax, ymax), 10, CV_RGB(255,0,0),4, 8, 0);
|
---|
149 |
|
---|
150 |
|
---|
151 | // Show all the images
|
---|
152 | cvShowImage("RGB_Image", output);
|
---|
153 | cvShowImage("R_Image", routput);
|
---|
154 | cvShowImage("B_Image", boutput);
|
---|
155 | cvShowImage("G_Image", goutput);
|
---|
156 | cvShowImage("F_Image", target);
|
---|
157 |
|
---|
158 |
|
---|
159 | // wait for any to press to quit
|
---|
160 | // Eeks! also HACK to make it refresh properly
|
---|
161 | if (cvWaitKey(200) != -1) {
|
---|
162 | break;
|
---|
163 | }
|
---|
164 | } //for each frame ...
|
---|
165 |
|
---|
166 |
|
---|
167 | cvReleaseCapture(&capture);
|
---|
168 | cvDestroyWindow("RGB_Image");
|
---|
169 | cvDestroyWindow("R_Image");
|
---|
170 | cvDestroyWindow("G_Image");
|
---|
171 | cvDestroyWindow("B_Image");
|
---|
172 | cvDestroyWindow("F_Image");
|
---|
173 |
|
---|
174 | return 0;
|
---|
175 |
|
---|
176 | }
|
---|
177 |
|
---|
178 | // Get ourself into the highGUI main stuff
|
---|
179 | int main(int argc, char *argv[]) {
|
---|
180 | return cvInitSystem(argc, argv);
|
---|
181 | }
|
---|