问题描述
我有一个要在OpenCV中达到阈值的彩色图像.我想要的是,如果任何RGB通道的值都在某个值以下,请将所有通道中的值设置为零(即黑色).
I have a color image that I want to a threshold in OpenCV. What I would like is that if any of the RGB channels in under a certain value, to set the value in all the channels to zero (i.e. black).
因此,我将opencv阈值函数用作:
So, I use the opencv threshold function as:
cv::Mat frame, thresholded
// read frame somewhere, it is a BGR image.
cv::threshold(frame, thresholded, 5, 255, cv::THRESH_BINARY);
所以,我想这样做是,如果任何一个通道小于5,我都认为会将其设置为零.但是,它似乎并没有以这种方式工作.例如,我看到其中一些区域只有绿色通道通过,这表明并非所有通道都设置为0.
So, what I thought this would do is that if any of the channels is less than 5, I thought it would set them to zero. However, it does not seem to work that way. For example, I see only the green channel come through for some of these regions, indicating not all channels are set to 0.
有没有一种快速使用OpenCV实现此目标的方法?
Is there a way to achieve this using OpenCV in a fast way?
推荐答案
可以使用功能 cv::inRange
.
It's possible to threshold a colored image using the function cv::inRange
.
void inRange(InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst)
例如,您只能允许介于(0,125,0)和(255,200,255)之间的值,或单个通道的任何值:
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;
}
cv::Mat output;
cv::inRange(image, cv::Scalar(0, 125, 0), cv::Scalar(255, 200, 255), output);
cv::imshow("output", output);
这篇关于在OpenCV中对RGB图像进行阈值处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!