使用OpenCV和C ++检测RGB颜色间隔 [英] Detect RGB color interval with OpenCV and C++

查看:101
本文介绍了使用OpenCV和C ++检测RGB颜色间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OpenCV和C ++在视频或图像中检测红色物体.有什么算法可以做到这一点?

I would like to detect a red colored object in a video or image, with OpenCV and C++. What algorithms are available to do this?

我想比较颜色之间的关系.实际上,当亮度变化时,该比率保持恒定.所以我想确定感兴趣区域的颜色可接受值的间隔.

I would like to do a comparison of the relationship between levels of color. Indeed, when the brightness varies, the ratio remains constant. So I want to determine the interval of acceptable values ​​for the colors of zone of interest.

在某些情况下,我查看红色的R(x,y)和G(x,y)/R(x,y)和B(x,y)/R(x,y).

For cases I look at the red R (x, y) and G (x, y) / R (x, y) and B (x, y) / R (x, y).

然后我将找到可接受值的范围:首先得到一个想法, 它将释放调色板图像中每个报告的最大值和最小值 红色

I will then find the ranges of acceptable values​​: to get a first idea, it releases the maximum and minimum for each report from a palette image red

我想找到这样的东西:

如果minR≤R(x,y)≤maxR且minG≤G(x,y)≤maxG minB≤B(x,y)≤maxB couleur(x,y)= blanc else couleur(x,y)= NOIR

if minR<=R(x,y)<=maxR and minG<=G(x,y)<=maxG minB<=B(x,y)<=maxB so couleur(x,y)=blanc else couleur(x,y)=NOIR

推荐答案

使用此处看看我的答案,将inRange()createTrackbar()一起使用.

Preprocess the image using cv::inRange() with the necessary color bounds to isolate red. You may want to transform to a color-space like HSV or YCbCr for more stable color bounds because chrominance and luminance are better separated. You can use cvtColor() for this. Check out my answer here for a good example of using inRange() with createTrackbar().

因此,基本模板将是:

Mat redColorOnly;
inRange(src, Scalar(lowBlue, lowGreen, lowRed), Scalar(highBlue, highGreen, highRed), redColorOnly);
detectSquares(redColorOnly);

:只需使用轨迹栏确定要隔离的颜色范围,然后使用发现该效果的颜色间隔即可.您不必经常使用轨迹栏.

EDIT : Just use the trackbars to determine the color range you want to isolate, and then use the color intervals you find that work. You don't have to constantly use the trackbars.

示例:
因此,这里有模板的完整示例,

EXAMPLE :
So, for a complete example of the template here you go,

我在GIMP中创建了一个简单(理想的)图像,如下所示:

I created a simple (and ideal) image in GIMP, shown below:

然后,我创建了此程序来过滤除红色方块以外的所有内容:

Then I created this program to filter all but the red squares:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

Mat redFilter(const Mat& src)
{
    assert(src.type() == CV_8UC3);

    Mat redOnly;
    inRange(src, Scalar(0, 0, 0), Scalar(0, 0, 255), redOnly);

    return redOnly;
}

int main(int argc, char** argv)
{
    Mat input = imread("colored_squares.png");

    imshow("input", input);
    waitKey();

    Mat redOnly = redFilter(input);

    imshow("redOnly", redOnly);
    waitKey();

    // detect squares after filtering...

    return 0;
}

注意::您将无法在实际图像中使用这些完全相同的滤镜时间间隔;我只是建议您使用轨迹栏调整间隔,以了解可接受的范围.

NOTE : You will not be able to use these exact same filter intervals for your real imagery; I just suggest you tune the intervals with trackbars to see what is acceptable.

输出看起来像这样:

Voila!仅剩下红色方块:)

Voila! Only the red square remains :)

享受:)

这篇关于使用OpenCV和C ++检测RGB颜色间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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