Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect basket ball Hoops and ball tracking

Detect the hoop(basket).To see the samples of "hoop".

Count the no of successful attempts(shoot) and the failure attempts. I am using opencv.

Input:

  1. Camera position will be static.
  2. The Portrait mode videos from any mobile device.

ref:

What have i tried:

  1. Able to track the basket ball. Still, seeking for a better solution.

results:

enter image description here

My code:

int main () {

VideoCapture vid(path);

    if (!vid.isOpened())
        exit(-1);
    int i_frame_height = vid.get(CV_CAP_PROP_FRAME_HEIGHT);
    i_height_basketball = i_height_basketball * I_HEIGHT / i_frame_height;
    int fps = vid.get(CV_CAP_PROP_FPS);
    Mat mat_black(640, 480, CV_8UC3, Scalar(0, 0, 0));
    vector <Mat> vec_frames;
    for (int i_push = 0; i_push < I_NO_FRAMES_STORE; i_push++)
        vec_frames.push_back(mat_black);

    vector <Mat> vec_mat_result;
    for (int i_push = 0; i_push < I_RESULT_STORE; i_push++)
        vec_mat_result.push_back(mat_black);

    int count_frame = 0;
    while (true) {
        int clk_start = clock();
        Mat image, result;
        vid >> image;
        if (image.empty())
            break;

        resize(image, image, Size(I_WIDTH, I_HEIGHT));
        image.copyTo(vec_mat_result[count_frame % I_RESULT_STORE]);
        if (count_frame >= 1)
            vec_mat_result[(count_frame - 1) % I_RESULT_STORE].copyTo(result);
        GaussianBlur(image, image, Size(9, 9), 2, 2);
        image.copyTo(vec_frames[count_frame % I_NO_FRAMES_STORE]);

        if (count_frame >= I_NO_FRAMES_STORE - 1) {
            Mat mat_diff_temp(I_HEIGHT, I_WIDTH, CV_32S, Scalar(0));
            for (int i_diff = 0; i_diff < I_NO_FRAMES_STORE; i_diff++) {

                Mat mat_rgb_diff_temp = abs(vec_frames[ (count_frame - 1) % I_NO_FRAMES_STORE ] - vec_frames[ (count_frame - i_diff) % I_NO_FRAMES_STORE ]);
                cvtColor(mat_rgb_diff_temp, mat_rgb_diff_temp, CV_BGR2GRAY);
                mat_rgb_diff_temp = mat_rgb_diff_temp > I_THRESHOLD;
                mat_rgb_diff_temp.convertTo(mat_rgb_diff_temp, CV_32S);
                mat_diff_temp = mat_diff_temp + mat_rgb_diff_temp;

            }
            mat_diff_temp = mat_diff_temp > I_THRESHOLD_2;
            //            mat_diff_temp.convertTo(mat_diff_temp, CV_8U);

            Mat mat_roi = mat_diff_temp.rowRange(0, i_height_basketball);
//            imshow("ROI", mat_roi);
            Moments mm = cv::moments(mat_roi, true); 
            Point p_center = Point(mm.m10 / mm.m00, mm.m01 / mm.m00);
            circle(result, p_center, 3, CV_RGB(0, 255, 0), -1);
            line(result, Point(0, i_height_basketball), Point(result.cols, i_height_basketball), Scalar(225, 0, 0), 1);

        }
        count_frame = count_frame + 1;
        int clk_processing_time = (clock() - clk_start);
        if (count_frame > 1)
            imshow("image", result);
        //        waitKey(0);

        int delay = (1000 / fps) - clk_processing_time;
        if (delay <= 0)
            delay = 2;
        if (waitKey(delay) >= 27)
            break;

    }
    vid.release();
    return 0;
}

Questions:

  1. How to detect the hoop? I thought of doing with Square detection to detect the square regions around the hoop.
  2. What is the best way of counting the successful shoots? Or How to count ?
like image 839
user2727765 Avatar asked Nov 12 '22 21:11

user2727765


1 Answers

I have what I suspect will be a fairly strong baseline: once the ball has commenced its downward arc, if the ball demonstrates significant upward movement again, its a miss. Otherwise, its a basket. This won't catch airballs, but I suspect they're relatively few anyway.

I think you could get a whole lot of mileage out of learning the ball trajectory of a successful shot and not worry too much about the hoop. Furthermore, didn't you say the camera was fixed-position? Doesn't that mean the hoop's always in the same place, and so you could just specify its location?

EDIT:

If you absolutely did have to find the hoop, I'd look for an object (sub-region of the image) of about the same size as the ball (which you say you can track) that's orange. More generally, you could learn a classifier for the hoop based on the training images you linked to, and apply it at a mixture of locations and scales, searching for the best match. You should know its approximate location, i.e. that it's in the upper portion of the image and likely to be to one side or the other. Then you could use proximity features to this identified region in addition to trajectory features to build a classifier for whether the shot succeeded or not.

like image 164
Ben Allison Avatar answered Nov 15 '22 08:11

Ben Allison