下面的代码需要多秒时间,我想通过颜色更快地检测到一个对象,这样它就可以实时显示。
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";
}发布于 2013-10-15 17:20:14
首先,如果你想找到一个颜色,我建议你分裂你的图像在HSV模式。用这种方式跟踪颜色更容易。
然后,不要执行这个双重for/循环,只需使用以下简单函数:CountNonZero
不要在循环中将东西写到控制台,除非是为了调试,因为它太慢了。
这是最终的管道,应该是实时的。
https://stackoverflow.com/questions/19380531
复制相似问题