problem with cv::inRange to detect a tennis ball

asked 2017-07-19 12:57:47 -0500

Hello everybody,

I begin to use the openCV library and I don't succed to binarize an image. I want to put a tennis ball in white and the rest of the background in black. This is my code :

int main()
{   
    int lowH = 40;    //color green
    int highH = 70;

    int lowS = 230;      
    int highS = 255;

    int lowV = 230;     
    int highV = 255;

    Mat inputFrame, hsvFrame, outputFrame;
    VideoCapture camCapture(0); 

    cvNamedWindow("Stream cam input", CV_WINDOW_NORMAL);
    cvNamedWindow("Stream cam hsv", CV_WINDOW_NORMAL);
    cvNamedWindow("Stream cam output", CV_WINDOW_NORMAL);

    if (!camCapture.isOpened()) std::cout << "error cam stream" << std::endl;

    while (1)
    {
        camCapture.read(inputFrame); 

        cvtColor(inputFrame, hsvFrame, CV_BGR2HSV); 
        cv::inRange(hsvFrame, cv::Scalar(lowH, lowS, lowV), cv::Scalar(highH, highS, highV), outputFrame);

            keyboard = waitKey(30);
        if (keyboard == 27) break;

        imshow("Stream cam input", inputFrame);
        imshow("Stream cam hsv", hsvFrame);
        imshow("Stream cam output", outputFrame);
    }
    cvDestroyWindow("Stream cam input");
    cvDestroyWindow("Stream cam hsv");
    cvDestroyWindow("Stream cam output");
}

with this code I can see the image from the webam, I can also see the converting in HSV, but the outputFrame is all black and I detect nothing, no object...

Could you help me

Thank you

edit retag flag offensive close merge delete

Comments

try with lower values for lowS and lowV, like 50, then slowly adjust & improve

berak gravatar imageberak ( 2017-07-20 01:05:20 -0500 )edit

I am new in this filed but I will suggest another way to detect the object. I think, you can subtract the current frame from previous frame and then convert resultant image to the binary image.

vps gravatar imagevps ( 2017-07-20 03:56:26 -0500 )edit