2017-02-28 67 views
0

我有一个关于颜色检测的问题。我有代码,但我想要多个结果。代码很有名,但我想让程序为我带来所有结果,而不仅仅是一个结果。我希望我明确自己。C#颜色检测多个结果

我的坏我所做的漏抄

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

_“该代码是着名的”_?你是什​​么意思? – Nat

+0

https://www.youtube.com/watch?v=gEgxZrXPnzc – Agrael

+0

Youtube的名气不是真正的名气。对于代码,它甚至不是stackoverflow.com的名气,也不是真正的名声。 –

回答

2

我想的节目,给我所有的结果不只是一个结果

返回所有结果的列表。所以你会修改你的方法有不同的返回类型:

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; 

} 
+0

感谢您更新您的答案。 – Agrael

+0

@Agrael没问题 –

0

创建循环之前列表,并添加每个结果。

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