int main() {
VideoCapture capture;
capture.open("pipoball.avi");
CV_Assert(capture.isOpened());
double frame_rate = capture.get(CAP_PROP_FPS);
int delay = 1000 / frame_rate;
Mat frame;
int nofrm = 0;
Point inball,accpt[30];
while (capture.read(frame))
{
float pxcnt = 0;
Point pt;
Point inball(0, 0);
if (waitKey(delay) >= 0) break;
Mat dstimg(frame.size(), CV_8UC3);
for (pt.y = 0; pt.y < frame.rows; pt.y++)
{
for (pt.x = 0; pt.x < frame.cols; pt.x++)
{
Vec3b val = frame.at<Vec3b>(pt.y, pt.x);
float luma = 0.11 * (float)val[0] + 0.59 * (float)val[1] + 0.3 * (float)val[2];
if (val[2] > 0.5 * luma && val[2] > 1.2 * val[0] && val[2] > 1.5 * val[1] && luma > 50.) {
dstimg.at<Vec3b>(pt.y, pt.x) = Vec3b(0, 255, 255); //yellow
}
nofrm++;
if (pxcnt == 0) pxcnt = 1;
inball /= pxcnt;
int radius = (int)(sqrt((double)pxcnt / 3.14));
circle(dstimg, inball, radius, CV_RGB(255, 0, 0), 2);
rectangle(dstimg, Point(inball.x - radius, inball.y - radius), Point(inball.x + radius, inball.y + radius), CV_RGB(0, 0, 0), 2);
//Show center points for more than 30 frames
accpt[(nofrm - 1) % 30] = inball;
if (nofrm >= 100) {
for (int k = 0; k < 29; k++) {
line(dstimg, accpt[(nofrm - k - 1) % 30], accpt[(nofrm - k - 2) % 30],CV_RGB(0,255,0 ));
}
}
}putText(dstimg, "A", inball, FONT_HERSHEY_SIMPLEX, 0.5, CV_RGB(0, 0, 0));
imshow ("result image", dstimg);
imshow ("Read movie file", frame);
}
waitKey(0);
return 0;
}
}
© 2024 OneMinuteCode. All rights reserved.