有没有一种更快的方法可以使用Emgu CV(openCV包装器)按颜色检测对象
本文关键字:包装 CV openCV 颜色 对象 检测 Emgu 可以使 一种 有没有 方法 | 更新日期: 2023-09-27 18:35:30
下面的代码需要几秒钟,我想更快地通过颜色检测对象,以便可以实时显示。
grayImg = input.InRange(new Bgr(selectionRangeSlider1.SelectedMin,
selectionRangeSlider2.SelectedMin,
selectionRangeSlider3.SelectedMin),
new Bgr(selectionRangeSlider1.SelectedMax,
selectionRangeSlider2.SelectedMax,
selectionRangeSlider3.SelectedMax));
selectionRangeSlider 是一个自定义控件,在 1 个值行上有 2 个滑块
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";
}
首先,如果你想找到一种颜色,我建议你在HSV模式下拆分你的图像。以这种方式跟踪颜色更容易。
然后,不要做这个双 for/循环,只需使用这个简单的函数:CountNonZero
永远不要循环将内容写入控制台,除非用于调试,因为它非常慢。
所以这是最终的管道,应该是实时的
- 将图像转换为 HSV 模式
- 分为 3 个通道
- 根据要跟踪的颜色执行 InRange (使用您的 UI)
- 提高投资回报率
- 计数非零。