猿问

OpenCV更好地检测红色?

我有以下图像:


我想使用cv::inRange方法和HSV颜色空间来检测红色矩形。


int H_MIN = 0;

int H_MAX = 10;

int S_MIN = 70; 

int S_MAX = 255;

int V_MIN = 50;

int V_MAX = 255;


cv::cvtColor( input, imageHSV, cv::COLOR_BGR2HSV );


cv::inRange( imageHSV, cv::Scalar( H_MIN, S_MIN, V_MIN ), cv::Scalar( H_MAX, S_MAX, V_MAX ), imgThreshold0 );

我已经创建了动态跟踪栏以更改HSV的值,但无法获得理想的结果。


有建议使用最佳值(或过滤器)的建议吗?


智慧大石
浏览 421回答 1
1回答

一只斗牛犬

在HSV空间,红色包裹周围180所以,你需要的H值,既在[0,10]和[170,180]。尝试这个:#include <opencv2\opencv.hpp>using namespace cv;int main(){&nbsp; &nbsp; Mat3b bgr = imread("path_to_image");&nbsp; &nbsp; Mat3b hsv;&nbsp; &nbsp; cvtColor(bgr, hsv, COLOR_BGR2HSV);&nbsp; &nbsp; Mat1b mask1, mask2;&nbsp; &nbsp; inRange(hsv, Scalar(0, 70, 50), Scalar(10, 255, 255), mask1);&nbsp; &nbsp; inRange(hsv, Scalar(170, 70, 50), Scalar(180, 255, 255), mask2);&nbsp; &nbsp; Mat1b mask = mask1 | mask2;&nbsp; &nbsp; imshow("Mask", mask);&nbsp; &nbsp; waitKey();&nbsp; &nbsp; return 0;}您先前的结果:结果添加范围[170,180]:另一个仅需要检查单个范围的有趣方法是:反转BGR图像转换为HSV寻找青色颜色这个想法已经由fmw42提出,并由Mark Setchell指出。非常感谢你。#include <opencv2\opencv.hpp>using namespace cv;int main(){&nbsp; &nbsp; Mat3b bgr = imread("path_to_image");&nbsp; &nbsp; Mat3b bgr_inv = ~bgr;&nbsp; &nbsp; Mat3b hsv_inv;&nbsp; &nbsp; cvtColor(bgr_inv, hsv_inv, COLOR_BGR2HSV);&nbsp; &nbsp; Mat1b mask;&nbsp;&nbsp; &nbsp; inRange(hsv_inv, Scalar(90 - 10, 70, 50), Scalar(90 + 10, 255, 255), mask); // Cyan is 90&nbsp; &nbsp; imshow("Mask", mask);&nbsp; &nbsp; waitKey();&nbsp; &nbsp; return 0;}
随时随地看视频慕课网APP
我要回答