首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有一种更快的方法,用openCV包装器( Emgu )彩色检测物体?

是否有一种更快的方法,用openCV包装器( Emgu )彩色检测物体?
EN

Stack Overflow用户
提问于 2013-10-15 11:52:45
回答 1查看 2.5K关注 0票数 1

下面的代码需要多秒时间,我想通过颜色更快地检测到一个对象,这样它就可以实时显示。

代码语言:javascript
复制
grayImg = input.InRange(new Bgr(selectionRangeSlider1.SelectedMin,
                                selectionRangeSlider2.SelectedMin,
                                selectionRangeSlider3.SelectedMin),
                                new Bgr(selectionRangeSlider1.SelectedMax,
                                        selectionRangeSlider2.SelectedMax,
                                        selectionRangeSlider3.SelectedMax));  

selectionRangeSlider是一个自定义控件,它在1条价值线上有2个滑块

代码语言:javascript
复制
Rectangle roi; //this rectangle is the product of rectangle recognition, now I want to check if the color of this recangle is at least 50% yellow

int whitePixels = 0;

for (int i = roi.X; (i < (roi.X + roi.Width)); i++)
{
    for (int j = roi.Y; (j < (roi.Y + roi.Height)); j++)
    {
        Byte currentVal = g.Data[i, j, 0];

        if (currentVal == 255) //255 means true: this pixel is yellow
        {
            Console.WriteLine(i + "," + j + " is yellow");
            whitePixels++;
        }
    }
}

if (whitePixels > ((roi.Width * roi.Height) / 2))
{
    // "more that half is yellow";
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-15 17:20:14

首先,如果你想找到一个颜色,我建议你分裂你的图像在HSV模式。用这种方式跟踪颜色更容易。

然后,不要执行这个双重for/循环,只需使用以下简单函数:CountNonZero

不要在循环中将东西写到控制台,除非是为了调试,因为它太慢了。

这是最终的管道,应该是实时的。

  1. 将图像转换为HSV模式
  2. 分三道
  3. 根据您想要跟踪的颜色(使用您的UI)执行InRange操作。
  4. 做ROI
  5. CountNonZero。
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19380531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档