C#颜色检测多于一个结果
C# Color Detection more than one result
我有一个关于颜色检测的问题。我有代码,但我想要不止一个结果。该代码很有名,但我想让程序给我带来所有结果,而不仅仅是一个结果。我希望我说清楚了。
我错了,我做了丢失的副本
private Boolean FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
{
try
{
for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++)
{
for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++)
{
for (int innerX = 0; innerX < bmpNeedle.Width; innerX++)
{
for (int innerY = 0; innerY < bmpNeedle.Height; innerY++)
{
Color cNeedle = bmpNeedle.GetPixel(innerX, innerY);
Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY);
if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
{
goto notFound;
}
}
}
location = new Point(outerX, outerY);
listBox1.Items.Add(location);
MessageBox.Show(location.ToString());
notFound:
continue;
}
}
}
catch (Exception)
{
}
location = Point.Empty;
return false;
}
在循环之前创建一个列表并添加每个结果。
I want to the program to bring me all the results not just one result
Return 所有结果的列表。因此,您将修改您的方法以具有不同的 return 类型:
public List<Point> FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
{
List<Point> results = new List<Point>();
for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++)
{
for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++)
{
for (int innerX = 0; innerX < bmpNeedle.Width; innerX++)
{
for (int innerY = 0; innerY < bmpNeedle.Height; innerY++)
{
Color cNeedle = bmpNeedle.GetPixel(innerX, innerY);
Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY);
if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
{
goto notFound;
}
}
}
location = new Point(outerX, outerY);
// collect the result
results.Add(location);
notFound:
continue;
}
}
// when you are finished looping return it
return results;
}
您需要将找到的每个像素添加到像素集合中。这可以像这样完成:
private IEnumerable<Point> FindNeedlePixels()
{
for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++)
{
for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++)
{
for (int innerX = 0; innerX < bmpNeedle.Width; innerX++)
{
for (int innerY = 0; innerY < bmpNeedle.Height; innerY++)
{
Color cNeedle = bmpNeedle.GetPixel(innerX, innerY);
Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY);
if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
{
goto notFound;
}
}
}
yield return new Point(outerX, outerY);
notFound:
continue;
}
}
}
我有一个关于颜色检测的问题。我有代码,但我想要不止一个结果。该代码很有名,但我想让程序给我带来所有结果,而不仅仅是一个结果。我希望我说清楚了。
我错了,我做了丢失的副本
private Boolean FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
{
try
{
for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++)
{
for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++)
{
for (int innerX = 0; innerX < bmpNeedle.Width; innerX++)
{
for (int innerY = 0; innerY < bmpNeedle.Height; innerY++)
{
Color cNeedle = bmpNeedle.GetPixel(innerX, innerY);
Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY);
if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
{
goto notFound;
}
}
}
location = new Point(outerX, outerY);
listBox1.Items.Add(location);
MessageBox.Show(location.ToString());
notFound:
continue;
}
}
}
catch (Exception)
{
}
location = Point.Empty;
return false;
}
在循环之前创建一个列表并添加每个结果。
I want to the program to bring me all the results not just one result
Return 所有结果的列表。因此,您将修改您的方法以具有不同的 return 类型:
public List<Point> FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
{
List<Point> results = new List<Point>();
for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++)
{
for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++)
{
for (int innerX = 0; innerX < bmpNeedle.Width; innerX++)
{
for (int innerY = 0; innerY < bmpNeedle.Height; innerY++)
{
Color cNeedle = bmpNeedle.GetPixel(innerX, innerY);
Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY);
if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
{
goto notFound;
}
}
}
location = new Point(outerX, outerY);
// collect the result
results.Add(location);
notFound:
continue;
}
}
// when you are finished looping return it
return results;
}
您需要将找到的每个像素添加到像素集合中。这可以像这样完成:
private IEnumerable<Point> FindNeedlePixels()
{
for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++)
{
for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++)
{
for (int innerX = 0; innerX < bmpNeedle.Width; innerX++)
{
for (int innerY = 0; innerY < bmpNeedle.Height; innerY++)
{
Color cNeedle = bmpNeedle.GetPixel(innerX, innerY);
Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY);
if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
{
goto notFound;
}
}
}
yield return new Point(outerX, outerY);
notFound:
continue;
}
}
}