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