Detect RGB color interval with OpenCV and C++

后端 未结 3 1567
北荒
北荒 2020-12-09 00:43

I would like to detect a red colored object in a video or image, with OpenCV and C++. What algorithms are available to do this?

I would like to do a comparison of th

相关标签:
3条回答
  • 2020-12-09 00:52

    You can convert your image from RGB value to HSV type using inbuilt function. After you can find every color has some HSV value range. So you can find that and give that as threshold and differentiate those points from others.

    0 讨论(0)
  • 2020-12-09 01:07

    Preprocess the image using cv::inRange() with the necessary color bounds to isolate red. You may want to transform to a color-space like HSV or YCbCr for more stable color bounds because chrominance and luminance are better separated. You can use cvtColor() for this. Check out my answer here for a good example of using inRange() with createTrackbar().

    So, the basic template would be:

    Mat redColorOnly;
    inRange(src, Scalar(lowBlue, lowGreen, lowRed), Scalar(highBlue, highGreen, highRed), redColorOnly);
    detectSquares(redColorOnly);
    

    EDIT : Just use the trackbars to determine the color range you want to isolate, and then use the color intervals you find that work. You don't have to constantly use the trackbars.

    EXAMPLE :
    So, for a complete example of the template here you go,

    I created a simple (and ideal) image in GIMP, shown below: enter image description here

    Then I created this program to filter all but the red squares:

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <iostream>
    
    using namespace std;
    using namespace cv;
    
    Mat redFilter(const Mat& src)
    {
        assert(src.type() == CV_8UC3);
    
        Mat redOnly;
        inRange(src, Scalar(0, 0, 0), Scalar(0, 0, 255), redOnly);
    
        return redOnly;
    }
    
    int main(int argc, char** argv)
    {
        Mat input = imread("colored_squares.png");
    
        imshow("input", input);
        waitKey();
    
        Mat redOnly = redFilter(input);
    
        imshow("redOnly", redOnly);
        waitKey();
    
        // detect squares after filtering...
    
        return 0;
    }
    

    NOTE : You will not be able to use these exact same filter intervals for your real imagery; I just suggest you tune the intervals with trackbars to see what is acceptable.

    The output looks like this:

    enter image description here

    Voila! Only the red square remains :)

    Enjoy :)

    0 讨论(0)
  • 2020-12-09 01:12

    In that case, try to find out any unique feature for your required square which distinguish it from other squares.

    For eg,

    1) Color of square:- If color is different from all other squares, you can check inside each square, and select square with required color, as explained by mevatron.

    2) Size of square :- If you know size of square, then compare size of each square and select best.

    0 讨论(0)
提交回复
热议问题