我正在尝试使用 OpenCV 库在图片中圈出红色。我在谷歌看到了很多问题,但没有人帮助我。我的代码如下:
void highLightRed(const cv::Mat& inputFrame, cv::Mat& outputFrame)
{
cv::Mat gray, edges, red, blurred;
// blur will enhance edge detection
medianBlur(inputFrame, blurred, 9);
// give image in gray color space
getGray(blurred, gray);
cv::inRange(gray, cv::Scalar(0,0,112), cv::Scalar(60,0,225), red); //BGR
// using Canny algorithm to find edges
cv::Canny(red, edges, 50, 150);
std::vector< std::vector<cv::Point> > c;
// find contours of edges
cv::findContours(edges, c, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
NSLog(@"size: %lu", c.size());
inputFrame.copyTo(outputFrame);
// draw contour in green (0,200,0) with thinkness 3
//cv::drawContours(outputFrame, c, -1, cv::Scalar(0,200,0),3);
cv::drawContours(outputFrame, c, -1, cv::Scalar(0,200,0),2);
}
void getGray(const cv::Mat& input, cv::Mat& gray)
{
const int numChannes = input.channels();
if (numChannes == 4)
{
cv::cvtColor(input, gray, cv::COLOR_BGRA2GRAY);
}
}
问题:如果图片中只有红色和白色,则此代码完美运行,它成功包围了红色。但是,如果涉及多种颜色,特别是深色,则颜色检测将失败。
我需要只检测红色的精确范围。谢谢
最佳答案
找到 BGR 或 RGB 格式的确切颜色范围非常困难。您将不得不经常玩它(如果您是新手)。
一个非常简单的方法:
cvtColor()
将图像转换为 HSV 格式HSV image
分为三个 channel ,即 H、S 和 V channel 。 H-channel
中提取红色.你可以看看here找到红色的色调范围。 H-channel
与旧 S
和 V
channel 。 关于iOS:OpenCV 红色范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31423633/