thresholding RGB image in OpenCV

In short, you have to slipt your image in three images containing the three channels, threeshold them independantly and then merge them again.

Mat frame,thresholded;
vector<Mat> splited_frame;
//Read your frame
split(frame, splited_frame);
for (size_t i = 0; i < splited_frame.size(); i++)
   threshold(splited_frame[i], splited_frame[i], 5, 255, cv::THRESH_BINARY);
merge(splited_frame,thresholded);

This code should do it.

Sorry, I read to fast. Then, you should modify the code slightly after the for

thresholded = splited_frame[0].clone();
for(size_t i = 1; i < splited_frame.size(); i++) thresholded &= splited_frame[i];
frame &= thresholded;

You create a mask from the three thresholded images, then apply this mask to your input image.


It's possible to threshold a colored image using the function cv::inRange.

void inRange(InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst)

For example, you can allow only values between (0, 125, 0) and (255, 200, 255), or any values for individual channels:

cv::Mat image = cv::imread("bird.jpg");

if (image.empty())
{
    std::cout << "> This image is empty" << std::endl;
    return 1;
}

Original image

cv::Mat output;
cv::inRange(image, cv::Scalar(0, 125, 0), cv::Scalar(255, 200, 255), output);
cv::imshow("output", output);

Output image

Tags:

Opencv