im detecting coloured balls in a video feed using opencv. i am recording the circles centre points. when the white ball moves i would like to do a detection for 10 seconds while displaying the video feed. after the 10 seconds i would like the next piece of code to run. im having trouble with this. code follows
while(1)
{
img = cvQueryFrame( capture );
CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 1, 7, 45, 40,0,25);
float* p;
CvScalar s,t;
int num_red = 15;
for (int i = 0; i < circles->total; i++)
{
// round the floats to an int
p = (float*)cvGetSeqElem(circles, i);
cv::Point center(cvRound(p[0]), cvRound(p[1]));
int radius = cvRound(p[2]);
//uchar* ptr;
//ptr = cvPtr2D(img, center.y, center.x, NULL);
//printf("B: %d G: %d R: %d\n", ptr[0],ptr[1],ptr[2]);
s = cvGet2D(img,center.y-6, center.x);
t = cvGet2D(img,center.y, center.x);//colour of circle
printf("\nB: %f G: %f R: %f\n",s.val[0],s.val[1],s.val[2]);
if (s.val[2]<50 && s.val[1]<50 && s.val[0] <50)
{
printf("Black Ball\n");
}
else if(s.val[2]>200 && s.val[1]>200 && s.val[0]>200)
{
printf("White Ball\n");
xpos=center.x;
ypos=center.y;
}
else if(s.val[2]>=250 && s.val[1]>=250 && s.val[0] <200)
printf("Yellow Ball\n");
else if(s.val[2]<70 && s.val[1]>80 && s.val[0] <70)
printf("Green Ball\n");
else if(s.val[2]>100 && s.val[1]<100 && s.val[0] <100)
printf("Brown Ball\n");
else if(s.val[2]<100 && s.val[1]<100 && s.val[0] >100)
printf("Blue Ball\n");
else if(s.val[2]>=250 && s.val[1]<250 && s.val[0] >=250)
printf("Pink Ball\n");
else if (s.val[2]<=255 && s.val[2]>=0 && s.val[1]<=255 && s.val[1]>=0 && s.val[0] <=255 && s.val[0]>=0)
{
printf("Red Ball\n");
//num_red++;
}
// draw the circle center
cvCircle(img, center, 3, CV_RGB(0,255,0), -1, 8, 0 );
// draw the circle outline
cvCircle(img, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );
//display coordinates
printf("x: %d y: %d r: %d\n",center.x,center.y, radius);
}
printf("\nDetected %d balls\n", circles->total);
if (xpos >= oldx + 10 || xpos<= oldx - 10 //wait for white ball movement here.
&& ypos >= oldy + 10 || ypos<= oldy - 10)
{
oldx = xpos;
oldy = ypos;
printf("White Ball Moving");
//start 10second counter where balls are tracked and video displayed
//after 10 seconds next code runs
}
{
//code to be run after 10 seconds counter but ony then
}
}//end of while there is frame incoming
cvShowImage( "Vid", img );
Tip: if you are going to share code, make sure it works. The idea is to always share a minimal example that reproduces the problem you are facing. This makes it easier for us to help you.
Since your code is incomplete I had to fill the missing pieces.
The code below retrieves frames from the camera, but I imagine your original code was reading from a video file. Anyway, the program below starts a 2nd thread (using pthreads) when a white ball is detected. It writes “White Ball detected” for 10 seconds and when the timer is done it writes “10sec passed”.
Due to the lack of information, the timer will only be fired once during the lifetime of the application (after the the FIRST white ball is detected). I tried to make this procedure as simple as possible so you can change it if you want it.
Compiled with:
EDIT:
Windows version: