Ask Your Question

Revision history [back]

take a look at the code below. also see this post

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{

    Mat image = imread("Simple_RGB_color_wheel.png");

    if (image.empty())
    {
        cout << endl
             << "ERROR! Unable to read the image" << endl
             << "Press a key to terminate";
        cin.get();
        return 0;
    }
    imshow("source",image);

    Mat hsv,mask;

    cvtColor(image,hsv,COLOR_BGR2HSV);
    inRange(hsv,Scalar(30,0,0),Scalar(90,255,255),mask);
    //inRange(hsv,Scalar(40,0,0),Scalar(80,255,255),mask);

    image.setTo(Scalar(0,0,0),mask);

    imshow("result",image);
    waitKey();

    return 0;
}

input image:

source_image

output image: (inRange(hsv,Scalar(30,0,0),Scalar(90,255,255),mask);)

image description

output image: (inRange(hsv,Scalar(40,0,0),Scalar(80,255,255),mask);)

image description

as @berak said : "you'd transfer to HSV, specify a range for "green" there, and use inRange() to get a mask for that color. then you can use img.setTo(0, mask)"

take a look at the code below. also see this post

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{

    Mat image = imread("Simple_RGB_color_wheel.png");

    if (image.empty())
    {
        cout << endl
             << "ERROR! Unable to read the image" << endl
             << "Press a key to terminate";
        cin.get();
        return 0;
    }
    imshow("source",image);

    Mat hsv,mask;

    cvtColor(image,hsv,COLOR_BGR2HSV);
    inRange(hsv,Scalar(30,0,0),Scalar(90,255,255),mask);
    //inRange(hsv,Scalar(40,0,0),Scalar(80,255,255),mask);

    image.setTo(Scalar(0,0,0),mask);

    imshow("result",image);
    waitKey();

    return 0;
}

input image:

source_image

output image: (inRange(hsv,Scalar(30,0,0),Scalar(90,255,255),mask);)

image description

output image: (inRange(hsv,Scalar(40,0,0),Scalar(80,255,255),mask);)

image description