C#颜色检测不止一个结果

时间:2017-02-28 10:27:34

标签: c# color-detection

我有一个关于颜色检测的问题。我有代码,但我想要不止一个结果。代码很有名,但我想让程序给我带来所有结果而不仅仅是一个结果。我希望我能说清楚。

我做错了我制作了丢失的副本

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;
        }

3 个答案:

答案 0 :(得分:3)

  

我希望程序能够为我带来所有结果而不只是一个结果

返回所有结果的列表。因此,您可以修改方法以使其具有不同的返回类型:

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;

}

答案 1 :(得分:0)

在循环之前创建一个List并添加每个结果。

答案 2 :(得分:0)

您需要将每个找到的像素添加到像素集合中。这可以这样做:

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;
        }
    }
}