图形像素索引



我有一张白色背景的图片,我想获得不同颜色(黑色)的第一个像素索引。

public Element? GetFirstIndex(Image img)
{
Color c = new();
for(int i=0; i<300; i++)
{
for(int j=0; j <300; j++)
{
c = GetDominantColor(new(img), i, j, 1, 1);
if (c.Name != "ffffffff") return new Element() { XPosition = j, YPosition = i, Heigh = 1, Width = 1 };
}
}
return null;
}

这个代码非常慢

我想我已经解决了我的问题:我将代码分成两部分,第一部分查找颜色索引的第一个X,第二部分查找颜色索引

的第一个Y。
public Element? GetFirstIndex(Image img)
{
Element? element = null;
for(int i=0; i<img.Height; i++)
{
Color c = GetDominantColor(new(img), 0, i, img.Width, 1);
if (c.Name != "ffffffff")
{
element = new Element() { XPosition = 0, YPosition = i, Heigh = 1, Width = 1 };
break;
}
}
if (element == null) return element;
for(int i=0; i<img.Width; i++)
{
Color c = GetDominantColor(new(img), i, 0, 1, img.Height);
if (c.Name != "ffffffff")
{
element = new Element() { XPosition = i, YPosition = element.YPosition, Heigh = 1, Width = 1 };
break;
}
}
return element;
}

最新更新