2009-12-15 174 views
6

我不確定從哪裏開始,所以一些指導是好的。我需要實現的是,檢查大圖像(比如1280x1024)並檢查是否存在另一個更小的圖像(可能是50x50像素的圖像)。C#檢查一個圖像是否存在於另一個圖像

我試圖通過比較每個像素,這是非常慢,我可能需要做100次,因此它似乎不適合。我只是想知道是否有更好的方法?

感謝

+0

如何準確它必須是?你可以調整圖像大小的一半大小,並比較? – jestro 2009-12-15 22:42:41

+0

你能告訴我們關於圖像內容的任何事嗎?他們是隨機的靜態位圖,還是有一些你可以利用的結構?另外,你能告訴我們關於查詢是「垃圾」的可能性嗎?也就是說,在大圖像中找不到小圖像的可能性是多少?如果查詢具有很高的垃圾可能性,那麼您想要優化的是快速確定垃圾狀態。錯誤答案或錯誤答案的後果是什麼?它是否必須準確的所有圖像? – 2009-12-15 23:15:40

+0

哦,你是否必須確定*哪裏匹配,或者只是否存在匹配? – 2009-12-15 23:17:58

回答

3

,我只是工作在類似的東西和快速和骯髒的結果,我想出了是使用AForge.Net的實現「ExhaustiveTemplateMatching」與圖像大小的1/4。全尺寸的720p圖像花費了幾分鐘,但在1/4尺寸的情況下,我的小電腦上只有一秒左右的時間。

public static class BitmapExtensions 
{ 
    /// <summary> 
    /// See if bmp is contained in template with a small margin of error. 
    /// </summary> 
    /// <param name="template">The Bitmap that might contain.</param> 
    /// <param name="bmp">The Bitmap that might be contained in.</param>   
    /// <returns>You guess!</returns> 
    public static bool Contains(this Bitmap template, Bitmap bmp) 
    { 
     const Int32 divisor = 4; 
     const Int32 epsilon = 10; 

     ExhaustiveTemplateMatching etm = new ExhaustiveTemplateMatching(0.9f);      

     TemplateMatch[] tm = etm.ProcessImage(
      new ResizeNearestNeighbor(template.Width/divisor, template.Height/divisor).Apply(template), 
      new ResizeNearestNeighbor(bmp.Width/divisor, bmp.Height/divisor).Apply(bmp) 
      ); 

     if (tm.Length == 1) 
     { 
      Rectangle tempRect = tm[0].Rectangle; 

      if (Math.Abs(bmp.Width/divisor - tempRect.Width) < epsilon 
       && 
       Math.Abs(bmp.Height/divisor - tempRect.Height) < epsilon) 
      { 
       return true; 
      }     
     } 

     return false; 
    } 
} 

你也當然只是檢查tm.length> 0,是裏面還有一些不必要的分歧:P

+0

非常感謝您從我做的快速測試看起來很理想,明天我會進行正確測試 – Tset 2009-12-15 23:24:38