2017-02-28 10 views
0

色の検出に関する質問があります。私はコードを持っていますが、私は複数の結果が必要です。コードは有名ですが、私はすべての結果を1つの結果だけではなく私に伝えたいと思っています。私は自分自身を明確にしたいと思う。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の名声は本当の名声ではありません。コードのためにも、それは本当の名声ではありません。 –

答えて

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; 
     } 
    } 
} 
関連する問題