2012-06-27 36 views
0

我對圖像處理相當陌生,最近我發現了AForge庫。我一直在試圖對2幅圖像進行基本比較,我希望它能夠返回它們之間的相似百分比。但是,由於有一個空數組,我經常會得到一個Index Out Bounds異常,但我不確定ex.ProcessImage爲什麼返回一個空數組。我幾乎複製了文檔中提供的示例代碼,但我仍然有一個錯誤。這裏是我的代碼:圖像處理與AForge:空陣列?

static class Run 
{ 
    public static void go() 
    { 
     string filename1 = null; 
     string filename2 = null; 

     filename1 = "a.a.jpg"; 
     filename2 = "b.b.jpg"; 

     Bitmap image1 = new Bitmap(filename1); 
     Bitmap image2 = new Bitmap(filename2); 

     ExhaustiveTemplateMatching ex = new ExhaustiveTemplateMatching(0); 
     TemplateMatch[] match = ex.ProcessImage (image1, image2); 

     float similarity = match[0].Similarity; //where the error occurs 

     Console.WriteLine("They are" + similarity + "% similar."); 
    } 
} 

我能想象它唯一的事情就是我如何做我的位圖。但是,我確實相信我做到了這些。我也嘗試了其他初始化位圖的方式,但仍然收到相同的錯誤。

這可能是一些愚蠢的錯誤,我只是沒有意識到,但任何幫助將不勝感激。

+0

我從來沒有設法弄清楚是什麼導致了這個問題,但我創建了一個完全相同的代碼的新項目,它的工作。所以,這個問題不再需要回答。 –

回答

0

如果你得到一個IndexOutOfRangeException,這表明ex.ProcessImage已返回一個空數組。接下來要做的是找出原因。大概沒有模板相匹配...

順便說一句,這個代碼是毫無意義的長篇大論:

string filename1 = null; 
string filename2 = null; 

filename1 = "a.a.jpg"; 
filename2 = "b.b.jpg"; 

這將是更短,更清晰的:

string filename1 = "a.a.jpg"; 
string filename2 = "b.b.jpg"; 
+0

我這樣做是因爲我原來在那裏輸入文件名,然後將其刪除。 「爲什麼」是我想弄明白的。 –

+0

@NathanPrice:那麼真的你的問題不是關於IndexOutOfBoundsException - 它是「爲什麼ExhaustiveTemplateMatching返回一個空數組」。 –

+0

好的,很抱歉,我的問題對於我所尋找的內容不正確。我現在編輯它來反映這一點。 –

0

保存圖像作爲。 bmp再試一次。

string filename1 = "a.a.bmp"; 
string filename2 = "b.b.bmp";