Changeset 9
- Timestamp:
- Nov 30, 2009, 10:18:39 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
liacs/mss/project/colour-tracking.c
r8 r9 10 10 #include <stdio.h> 11 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 } 12 28 13 29 int cvInitSystem(int argc, char *argv[]) { … … 19 35 20 36 21 uchar * routdata, *goutdata, *boutdata, *target;37 uchar *filterdata, *outdata, *routdata, *goutdata, *boutdata; 22 38 23 39 … … 41 57 IplImage *goutput = cvCreateImage( cvSize(width,height), 8, 3); 42 58 IplImage *filter = cvCreateImage( cvSize(width,height), 8, 3); 59 IplImage *target = cvCreateImage( cvSize(width,height), 8, 3); 43 60 44 61 45 62 // Some temp pointers 46 int j, k, m, n, p; 63 int i, j, k, m, n, o, p; 64 int r, g, b; 65 int q; 66 47 67 48 68 // Video run starts here ... … … 66 86 67 87 88 outdata = (uchar *) output->imageData; 68 89 routdata = (uchar *) routput->imageData; 69 90 goutdata = (uchar *) goutput->imageData; … … 75 96 for( n = 0; n < image->width; n++) //column 76 97 { 77 routdata[ m*step+n*3+0] = 0; // clear blue part78 routdata[ m*step+n*3+1] = 0; // clear green part79 80 goutdata[ m*step+n*3+0] = 0; // clear blue part81 goutdata[ m*step+n*3+2] = 0; // clear red part82 83 boutdata[ m*step+n*3+1] = 0; // clear green part84 boutdata[ m*step+n*3+2] = 0; // clear red part98 routdata[BVALUE(m,n,step)] = 0; // clear blue part 99 routdata[GVALUE(m,n,step)] = 0; // clear green part 100 101 goutdata[BVALUE(m,n,step)] = 0; // clear blue part 102 goutdata[RVALUE(m,n,step)] = 0; // clear red part 103 104 boutdata[GVALUE(m,n,step)] = 0; // clear green part 105 boutdata[RVALUE(m,n,step)] = 0; // clear red part 85 106 } 86 107 } 87 108 88 109 // Clear filter for new use 89 cvSetZero(filter); 110 // cvSetZero(filter); 111 // ... or clone orginal image for mapping over it 112 //cvCopy(goutput, filter, NULL); 90 113 91 114 // Make it yellow 92 115 //cvOr( routput, goutput, filter, NULL); 93 116 117 // XXX: Use stuff like cvMat, cvGetRawData instead of internal hacking 118 // Make green 'fade' out by the presence of many red as well 119 filterdata = (uchar *) filter->imageData; 120 for( m = 0; m < image->height; m++) //row 121 { 122 for( n = 0; n < image->width; n++) //column 123 { 124 r = outdata[RVALUE(m,n,step)]; 125 g = outdata[GVALUE(m,n,step)]; 126 b = outdata[BVALUE(m,n,step)]; 127 // Many red, less blue and green. Good for laser pointer tracing 128 p = (r < 220) ? 0 : r; 129 p = (b < 180 || g < 180) ? 0 : p; 130 //p = (abs(p - filterdata[GVALUE(m,n,step)]) > 20) ? p : 0; 131 filterdata[GVALUE(m,n,step)] = p; 132 } 133 } 134 94 135 95 136 int xmin = 0, xmax = 0; 96 137 int ymin = 0, ymax = 0; 138 int value = 0, cmax = 0; 97 139 98 140 // Find the 'very' green point and mark it's surroundings 99 for(m = 0; m < image->height; m++) 141 const int stepSize = 5; 142 for(m = 0; m < image->height; m = m+stepSize) 100 143 { 101 for(n = 0; n < image->width; n ++)144 for(n = 0; n < image->width; n = n+stepSize) 102 145 { 103 146 // Make it stop processing all points, pick the ones with high value 104 if(goutdata[m*step+n*3+1] > 200) 105 { 106 if(n < xmin) 107 xmin = n; 108 if(n > xmax) 109 xmax = n; 110 if(m < ymin) 111 ymin = m; 112 if(m > ymax) 113 ymax = m; 147 value = filterdata[GVALUE(m,n,step)]; 148 if (value > cmax) 149 { 150 // detect 'square' 151 p = 0; 152 const int SIZE = 2; 153 for (i = m; i < min(m+SIZE,height); i++) 154 { 155 for (j = n; j < min(n+SIZE,width); j++) 156 { 157 value = filterdata[GVALUE(i,j,step)]; 158 if (value > cmax) 159 { 160 p++; 161 } 162 } 163 } 164 165 /* Make sure envirionment is 'low' */ 166 for (i = max(m-SIZE,0); i < m; i++) 167 { 168 for (j = max(n-SIZE,0); j < n; j++) 169 { 170 q = filterdata[GVALUE(i,j,step)]; 171 if (q > value) 172 { 173 p--; 174 } 175 } 176 } 177 for (i = min(m+SIZE,height); i < min(m+(2*SIZE),height); i++) 178 { 179 for (j= min(m+SIZE,width); j < min(m+(2*SIZE),width); j++) 180 { 181 q = filterdata[GVALUE(i,j,step)]; 182 if (q > value) 183 { 184 p--; 185 } 186 } 187 } 188 189 if (p > SIZE) 190 { 191 printf("%ix%i=%i\n",n,m,value); 192 cmax = value; 193 xmax = n; 194 ymax = m; 195 } 114 196 } 115 197 } 116 198 } 117 118 cvRectangle(output, cvPoint(xmin, ymin), cvPoint(xmax,ymax), CV_RGB(0,255,0),2, 8, 0); 119 cvCircle(filter, cvPoint((xmin+xmax)/2, (ymin+ymax)/2), 0, CV_RGB(255,0,0),2, 8, 0); 199 200 // Prepare combined output 201 cvCopy(filter, target, NULL); 202 cvCircle(target, cvPoint(xmax, ymax), 10, CV_RGB(255,0,0),4, 8, 0); 120 203 121 122 123 204 124 205 // Show all the images … … 127 208 cvShowImage("B_Image", boutput); 128 209 cvShowImage("G_Image", goutput); 129 cvShowImage("F_Image", filter);210 cvShowImage("F_Image", target); 130 211 131 212 132 //wait for a key to press, Eeks! HACK to make it refresh properly 213 // wait for any to press to quit 214 // Eeks! also HACK to make it refresh properly 133 215 if (cvWaitKey(20) != -1) { 134 216 break; … … 148 230 } 149 231 232 // Get ourself into the highGUI main stuff 150 233 int main(int argc, char *argv[]) { 151 234 return cvInitSystem(argc, argv);
Note:
See TracChangeset
for help on using the changeset viewer.