This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
/
colour-tracking.c
171 lines (119 loc) · 4.87 KB
/
colour-tracking.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* http://home.iitk.ac.in/~swagatk/faq/opencv.html#Colar_Based_Tracking */
#include<cv.h>
#include<cxcore.h>
#include<cvaux.h>
#include<highgui.h>
int main(int argc, char *argv[]) {
CvCapture *capture = cvCaptureFromAVI(argv[1]);
int frames = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
uchar *routdata, *goutdata, *target;
//Create a window
cvNamedWindow("Image", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Final_RG", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Filtered_RG", CV_WINDOW_AUTOSIZE);
//Create Image Skeletons
IplImage *image = 0;
IplImage *routput = cvCreateImage( cvSize(640,480), 8, 3);
IplImage *goutput = cvCreateImage( cvSize(640,480), 8, 3);
IplImage *final_rg = cvCreateImage( cvSize(640,480), 8, 3);
IplImage *filtered_rg = cvCreateImage( cvSize(640,480), 8, 3);
int nFrames = 300;
int j, k, m, n, p;
// These are used for storing pixel indices
int rindex[500][2];
int gindex[500][2];
// Threshold for R, G, B channels. This depend on the object to be tracked.
// These are for a pink object.
int red = 112;//200;
int blue = 66;//210;
int green = 79;//190;
// Video run starts here ...
for(j = 0; j < nFrames; j++)
{
image = cvQueryFrame(capture);
//This is needed for accessing pixel elements
int step = image->widthStep/sizeof(uchar);
cvCopy(image, routput, NULL);
cvCopy(image, goutput, NULL);
//Otherwise the image appears inverted
routput->origin = image->origin;
goutput->origin = image->origin;
final_rg->origin = image->origin;
filtered_rg->origin = image->origin;
routdata = (uchar *) routput->imageData;
goutdata = (uchar *) goutput->imageData;
k = 0, p = 0;
for( m = 0; m < image->height; m++) //row
{
for( n = 0; n < image->width; n++) //column
{
// only show those pixels with RG component specified below
if(routdata[m*step+n*3+2] > red && routdata[m*step+n*3+0] > blue)
{
rindex[k][0] = m;
rindex[k][1] = n; // save the pixel indices in a vector for later use.
k = k + 1;
}
else
{
//black out this pixel
routdata[m*step+n*3+0] = 0; //clear blue part
routdata[m*step+n*3+1] = 0; //clear green part
routdata[m*step+n*3+2] = 0; //clear red part
}
// Only show pixels with RB component as specified below
if(routdata[m*step+n*3+2] > red && routdata[m*step+n*3+1] < blue)
{
gindex[p][0] = m;
gindex[p][1] = n; // save the pixel indices in a vector for later use.
p = p + 1;
}
else
{
//black out this pixel
goutdata[m*step+n*3+0] = 0; //clear blue part
goutdata[m*step+n*3+1] = 0; //clear green part
goutdata[m*step+n*3+2] = 0; //clear red part
}
}
}
// Perform 'AND' Operation between routput and goutput images
cvAnd( routput, goutput, final_rg, NULL);
//Apply an averaging filter to remove noise from final_rg image
cvSmooth(final_rg, filtered_rg, CV_MEDIAN, 5, 5, 0, 0);
target = (uchar *) filtered_rg->imageData;
int xmin = 400, xmax = 0;
int ymin = 400, ymax = 0;
for(m = 0; m < filtered_rg->height; m++)
{
for(n = 0; n < filtered_rg->width; n++)
{
if(target[m*step+n*3+2] > 100)
{
if(n < xmin)
xmin = n;
if(n > xmax)
xmax = n;
if(m < ymin)
ymin = m;
if(m > ymax)
ymax = m;
}
}
}
cvRectangle(image, cvPoint(xmin, ymin), cvPoint(xmax,ymax), CV_RGB(0,255,0),2, 8, 0);
cvCircle(image, cvPoint((xmin+xmax)/2, (ymin+ymax)/2), 0, CV_RGB(255,0,0),2, 8, 0);
// Show all the images
cvShowImage("Image", image);
cvShowImage( "Final_RG", final_rg);
cvShowImage ( "Filtered_RG", filtered_rg);
//wait for a key to press.
cvWaitKey(0);
} //for each frame ...
cvReleaseCapture(&capture);
// cvReleaseImage(&image); // it gives error ... find out why?
cvDestroyWindow("Image");
cvDestroyWindow("Final_RG");
cvDestroyWindow("Filtered_RG");
return 0;
}