2014-11-06 131 views
0

我並不總是有正方形/長方形圖像,有時我也應該匹配圓形圖像。例如下面是2張圖片。 Ball是模板圖像,第二個是Source應該搜索模板的Source。我可以將模板的背景透明化,但是這會產生錯誤,使得白色會降低比賽分數,因爲您在源圖像上看到的球體周圍沒有白色。這些只是2個示例圖片。你有建議/解決方案嗎?Java中圓形圖像的OpenCV模板匹配

Template image to be searched

Source image

回答

0

如果你知道屬於模板的像素,可以寫你的匹配

總和的絕對差異試驗(僞代碼)

Mat I, T // image and template 
vector<Point> template_pixels 
Rect sliding_window 
vector<double> match_rates 

for all rows in image 
update sliding_window 
    for all cols in image 
    update sliding_window 
    Mat W = I(sliding_window) 
    sum = 0 
     for all rows in template 
       for all cols in template 
       if(template_pixels contains pixel i) 
        sum += abs(W(i) - T(i)) 
       end for 
     end for 
    match_rates.pushback(sum) 
    end for 
end for 

minMaxLoc(match_rates) 

和在圖像行上使用多線程優化它

+0

我不知道python如果這是python我可能會試試:)我在berky寫這個。它是非常簡單但功能強大的語言 - 真棒,因爲你已經知道了 - 只是很難適應。8) – baci 2014-11-07 01:00:31

+0

你是對的,Python有點不同。如果這是Java代碼,我肯定會+1。 – karlphillip 2014-11-07 01:01:44

1

沒關係,你仍然可以使用matchTemplate()並取得了優異的成績:

enter image description here

You can find a decent tutorial on OpenCV's documentation。順便說一句,這是在那裏共享的演示的輸出。

+0

模板匹配找到最佳結果,即使模板不在源中,它也會標記一個正方形。如果搜索到的圖像真的在源中,那麼得分就像0.9987 ...但在這種情況下,白人的得分將爲0.7 ...我會讓我們說1000個不同圖像的比較,一些會有綠色的一些會有藍色,一些灰色的背景。這將始終影響匹配分數。根據找到或未找到我的情況和具有隨機閾值的分數圖像設置閾值將是一個問題。我知道它會始終高於0.6 ...但仍然... – Anarkie 2014-11-07 13:24:00

2

我認爲你也可以使用直方圖反投影。在那裏你可以使用任意形狀的面具。用反投影圖像旋轉蒙版,您將在圖像中出現對象的區域檢測到峯值,如下面的圖像(顏色映射和縮放)所示。

背投影:

enter image description here

卷積:

enter image description here

編輯:

這是在此基礎上paper。我正在試驗它,並希望在博客中發佈。 這是用C++編寫的。

// model histogram: this is the football template 
calcHist(&model32fc3, 1, (const int*)channels, modelMask, histModel, 3, (const int*)histSize, (const float**)ranges, true, false); 
// image histogram 
calcHist(&image32fc3, 1, (const int*)channels, Mat(), histImage, 3, (const int*)histSize, (const float**)ranges, true, false); 
// ratio histogram 
divide(histModel, histImage, histRatio); 
cv::min(histRatio, 1.0, histRatio); 
// backproject ratio histogram 
calcBackProject(&image32fc3, 1, (const int*)channels, histRatio, backprj, (const float**)ranges); 
// convolve the kernel with the backprojected image 
filter2D(backprj, conv, CV_32F, modelMask); 
+0

您能舉出代碼示例嗎?或者是一個指向這個地方的教程的鏈接? – Anarkie 2014-11-07 13:26:22

+0

添加了代碼。我仍在試驗它。如果我有機會在未來的博客中發佈此代碼,我會更新代碼或提供鏈接。 – dhanushka 2014-11-07 14:11:37

+0

我知道C++,但在Java中工作不是,你可以發佈它在Java?或者在完全完成後我會自己嘗試。 – Anarkie 2014-11-07 14:15:10