OpenCV更好的检测红色? [英] OpenCV better detection of red color?

查看:656
本文介绍了OpenCV更好的检测红色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下图片:

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

I would like to detect the red rectangle using cv::inRange method and HSV color space.

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的值,期望结果。

I already created dynamic trackbars in order to change the values for HSV, but I can't get the desired result.

任何建议使用最佳值(也许过滤器)?

Any suggestion for best values (and maybe filters) to use?

推荐答案

在HSV空间中,红色大约180°。因此,您需要 H 值都在[0,10] [170,180]。

In HSV space, the red color wraps around 180. So you need the H values to be both in [0,10] and [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]:

Result adding range [170, 180]:

这篇关于OpenCV更好的检测红色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆