使用 OpenCV 和 C++ 检测 RGB 颜色间隔

发布于 2024-12-29 03:49:23 字数 416 浏览 0 评论 0原文

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

我想做一个颜色层次之间的关系的比较。事实上,当亮度变化时,该比率保持不变。所以我想确定感兴趣区域颜色的可接受值的区间。

对于情况,我查看红色 R (x, y) 和 G (x, y) / R (x, y) 和 B (x, y) / R (x, y)。

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

我想找到这样的东西:

如果 minR<=R(x,y)<=maxR 且 minG<=G(x,y)<=maxG minB<=B(x,y)<=maxB 那么 颜色(x,y)=白色否则颜色(x,y)=黑色

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.

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

I would like to find something like this :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紙鸢 2025-01-05 03:49:23

使用 cv::inRange() 具有隔离红色所需的颜色边界。您可能需要转换为 HSV 或 YCbCr 等颜色空间以获得更稳定的颜色范围,因为色度和亮度可以更好地分离。您可以使用 cvtColor() 为此。在此处查看我的答案,了解一个很好的示例将 inRange()createTrackbar() 结合使用。

因此,基本模板是:

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

编辑:只需使用轨迹栏确定要隔离的颜色范围,然后使用您认为有效的颜色间隔。您不必经常使用轨迹栏。

示例:
因此,对于模板的完整示例,

我在 GIMP 中创建了一个简单(且理想)的图像,如下所示:
在此处输入图像描述

然后我创建了这个程序来过滤除红色方块之外的所有方块:

#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;
}

注意:您将无法对真实图像使用这些完全相同的过滤间隔;我只是建议你用轨迹栏调整间隔,看看什么是可以接受的。

输出如下所示:

在此处输入图像描述

瞧!只剩下红色方块了:)

享受吧:)

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().

So, the basic template would be:

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,

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

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.

The output looks like this:

enter image description here

Voila! Only the red square remains :)

Enjoy :)

享受孤独 2025-01-05 03:49:23

在这种情况下,尝试找出您所需的方块与其他方块的区别的任何独特特征。

例如,

1)方块的颜色:-如果颜色与所有其他方块不同,您可以检查每个方块的内部,并选择具有所需颜色的方块,如 mevatron。

2) 方块大小:- 如果您知道方块大小,则比较每个方块的大小并选择最佳的。

In that case, try to find out any unique feature for your required square which distinguish it from other squares.

For eg,

1) Color of square:- If color is different from all other squares, you can check inside each square, and select square with required color, as explained by mevatron.

2) Size of square :- If you know size of square, then compare size of each square and select best.

像你 2025-01-05 03:49:23

您可以使用内置函数将图像从 RGB 值转换为 HSV 类型。之后你可以发现每种颜色都有一定的HSV值范围。所以你可以找到它并将其作为阈值并将这些点与其他点区分开来。

You can convert your image from RGB value to HSV type using inbuilt function. After you can find every color has some HSV value range. So you can find that and give that as threshold and differentiate those points from others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文