2017-02-28 1 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

유튜브 명성이 진짜 명성이 아닙니다. 코드를 위해서도 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; 
     } 
    } 
} 
관련 문제