2011-12-19 122 views
13

最近我有機會與圖像處理技術合作,作爲我的一個項目的一部分,我的任務是在給出新圖像時從圖像存儲庫中找到匹配的圖像。我開始使用Google搜索「如何使用Java比較圖像」來開始我的項目,並且我找到了一些關於找到兩幅圖像相似性的好文章。幾乎所有的人都是基於四個基本步驟,它們分別是:如何使用java比較圖像的相似性

1.Locating the Region of Interest (Where the Objects appear in the given image), 
2.Re-sizing the ROIs in to a common size, 
3.Substracting ROIs, 
4.Calculating the Black and White Ratio of the resultant image after subtraction. 

雖然這聽起來是一個很好的算法來比較圖像,它需要在我的項目中使用JAI實施之後相當長的時間。因此我必須找到一種替代方法。

有什麼建議嗎?

+0

你有沒有想過使用的OpenCV的百分比是多少? http://stackoverflow.com/questions/2037579/java-opencv-bindings – 2011-12-19 21:43:58

回答

5

根據不同的圖像,你可以做這樣的事情(僞代碼)。這是非常原始的,但應該是非常有效的。你可以通過採用隨機或圖案像素而不是每個像素來加速它。

for x = 0 to image.size: 
    for y = 0 to image.size: 
     diff += abs(image1.get(x,y).red - image2.get(x,y).red) 
     diff += abs(image1.get(x,y).blue - image2.get(x,y).blue) 
     diff += abs(image1.get(x,y).green - image2.get(x,y).green) 
    end 
end 

return ((float)(diff))/(x * y * 3) 
+0

這是否真的有足夠的精度? – sum2000 2011-12-19 21:45:46

+0

嗯,我不認爲它是你正在尋找的精度,但準確性。例如,如果您拍攝圖像並向左和向下偏移3個像素,則這將顯示圖像幾乎沒有任何相似之處。但是,那麼你必須問自己,應該嗎? – corsiKa 2011-12-19 21:50:17

+0

此方法適用於黑白圖像嗎? – manu 2014-05-08 09:09:20

10
**// This API will compare two image file // 
// return true if both image files are equal else return false//** 
public static boolean compareImage(File fileA, File fileB) {   
    try { 
     // take buffer data from botm image files // 
     BufferedImage biA = ImageIO.read(fileA); 
     DataBuffer dbA = biA.getData().getDataBuffer(); 
     int sizeA = dbA.getSize();      
     BufferedImage biB = ImageIO.read(fileB); 
     DataBuffer dbB = biB.getData().getDataBuffer(); 
     int sizeB = dbB.getSize(); 
     // compare data-buffer objects // 
     if(sizeA == sizeB) { 
      for(int i=0; i<sizeA; i++) { 
       if(dbA.getElem(i) != dbB.getElem(i)) { 
        return false; 
       } 
      } 
      return true; 
     } 
     else { 
      return false; 
     } 
    } 
    catch (Exception e) { 
     System.out.println("Failed to compare image files ..."); 
     return false; 
    } 
} 
+0

這將比較兩幅圖像是否相同,我要求找出兩幅圖像之間的相似度。 – sum2000 2012-06-22 07:24:31

+0

只是想檢查,如果兩個不同的圖像符合相同的大小,會返回什麼? – ChanGan 2013-01-18 11:31:11

+0

嗨Sandip,如何通過兩個傳遞兩個圖像到位fileA和fileB ...都是文件的位置?你能給我簡短的一點嗎? – ChanGan 2013-01-18 12:10:12

3

這個API將比較兩個圖像文件,並返回相似的

public float compareImage(File fileA, File fileB) { 

    float percentage = 0; 
    try { 
     // take buffer data from both image files // 
     BufferedImage biA = ImageIO.read(fileA); 
     DataBuffer dbA = biA.getData().getDataBuffer(); 
     int sizeA = dbA.getSize(); 
     BufferedImage biB = ImageIO.read(fileB); 
     DataBuffer dbB = biB.getData().getDataBuffer(); 
     int sizeB = dbB.getSize(); 
     int count = 0; 
     // compare data-buffer objects // 
     if (sizeA == sizeB) { 

      for (int i = 0; i < sizeA; i++) { 

       if (dbA.getElem(i) == dbB.getElem(i)) { 
        count = count + 1; 
       } 

      } 
      percentage = (count * 100)/sizeA; 
     } else { 
      System.out.println("Both the images are not of same size"); 
     } 

    } catch (Exception e) { 
     System.out.println("Failed to compare image files ..."); 
    } 
    return percentage; 
} 
+0

請在代碼中找到帶有第一行頂端參數的方法名,在那裏我們可以傳遞兩個帶有文件路徑的圖像(例如:C://Documents//image1.jpg) – 2015-12-11 05:59:47

+2

您應該提到這是基於Sandip甘古利的回答。 – 2017-06-15 17:17:54