Ball tracking

asked 2019-05-07 10:10:59 -0600

Hi, I have the following problem: I must find a red ball on a field. I transformed my picture to HSV colorspace and used the inRange function to separate the red color. Now I'm trying to draw an rectangle at the position, where the red colored object is and to show it in the original video, but I have no plan how I can do this.

Thank you Dennis

Here is my actual code

    #include <iostream>
    #include <opencv2/opencv.hpp>

    using namespace cv;
    using namespace std;

    int main()
    {
        VideoCapture cap("test.mp4");

        // Define Color
        Scalar upperRed(180, 255, 255);
        Scalar lowerRed(125, 52, 67);

        while (1) {

            Mat frame, hsv;
            // Capture frame-by-frame
            cap >> frame;

            // If the frame is empty, break immediately
            if (frame.empty())
                break;
            GaussianBlur(frame, frame,Size(5,5) , 0);
            // Display the resulting frame in HSV
            cvtColor(frame, hsv, COLOR_BGR2HSV);
            // Color detection
            inRange(hsv, lowerRed, upperRed, frame);

            // Show images
            imshow("bye", hsv);
            imshow("hallo", frame);

            // Press  ESC on keyboard to exit
            char c = (char)waitKey(25);
            if (c == 27)
                break;
        }
        cap.release();
        destroyAllWindows;
        return 0;
    }

C:\fakepath\hallo.PNG C:\fakepath\Unbenannt.PNG

edit retag flag offensive close merge delete

Comments

1

Your call to inRange returns a binary image which is 255 where pixels meet the ball's color and 0 otherwise. Now you need to detect the location in the image with a local concentration of 255 pixel values. You could use dilation on the image to expand the bright areas until they merge, then use an equal number of dilations to get the same general size again. Then find the statistics of all the responding objects in the image using the findContours function. The largest single object is likely to be the ball. You could also use the distanceTransform and thresholding to find object centers. Or you could find the difference between dilations and erosions (which should be circular for a ball) and use the Hough Circle detection.

MikeTronix gravatar imageMikeTronix ( 2019-05-07 15:17:32 -0600 )edit