下面的代码需要几秒钟,我想更快地通过颜色检测对象,以便可以实时显示。
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";
}