2015-06-22 82 views
7

概述:如何使用ImageMagick替換圖像中的白色矩形?

第一張照片是我的原始圖像。這裏我想用另一個圖像替換顯示的白色矩形。

enter image description here

我的方法:

我已經創建使用floodfill,它看起來遮掩圖像爲:

enter image description here

問題:

現在我想在第二個圖像中獲取矩形的距離或座標,以便我可以使用這些座標在此處的第一個(原始圖像)上重疊新圖像。

我有點想法使用ImageMagick的chebyshev形態算子,但不知道我該怎麼做。

+0

嘗試'轉換image.jpg的門限90%result.jpg' –

+0

或許'轉換image.jpg的門限90%-canny爲0x1 + 10%+ 30%result.jpg' –

+0

你好馬克。 ..謝謝回覆。但我想在這裏得到矩形的座標,旋轉角度,寬度和高度以重疊另一個圖像。請在這裏看到我的其他問題,以便您瞭解我想實現的目標。 http://stackoverflow.com/questions/30971894/replacing-detected-object-in-a--frame-with-an-image-imageprocessing 希望我會以正確的方式。如果不是,請指導我。 目前我正在關注此方法: http://www.imagemagick.org/discourse-server/viewtopic.php?t=20269 –

回答

6

我想你可以用一個簡單的閾值相當準確定位的形狀,就像這樣:

convert image.jpg -threshold 90% result.jpg 

enter image description here

,然後你可以做一個Canny邊緣檢測是這樣的:

convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg 

enter image description here

下一個事情我會在看的,使用-trim功能查找裁切框座標,就像這樣:

convert result.jpg -format "%@" info: 
320x248+152+40 

我在下面的紅色標記上。

enter image description here

如果你真的想要做的裝飾,使用此:

convert result.jpg -trim result.jpg 

enter image description here

而且還可以,去偏移角度

convert result.jpg -deskew 40 -format "%[deskew:angle]" info: 
-0.111906 

霍夫線檢測也可能對你有效:

convert image.jpg -threshold 90% -canny 0x1+10%+30%  \ 
    \(+clone -background none       \ 
       -fill red -stroke red -strokewidth 2  \ 
       -hough-lines 5x5+80 -write lines.mvg  \ 
    \) -composite hough.png 

enter image description here

和文件lines.mvg包含4條線,你正在尋找

# Hough line transform: 5x5+80 
viewbox 0 0 640 360 
line 449.259,0 474.432,360 # 90 
line 0,72.5604 640,27.8072 # 143 
line 0,293.098 640,248.344 # 187 
line 153.538,0 178.712,360 # 153 

作爲一個有點懶,我不覺得解決這些線的交叉點,所以我想我會讓ImageMagick也這樣做 - 通過使用形態學來尋找像這樣的線路連接:

convert image.jpg -threshold 90% -canny 0x1+10%+30%      \ 
    \(+clone -background none -fill red -stroke red -hough-lines 5x5+80 \) \ 
    -composite -fuzz 50% -fill black -opaque white      \ 
    -morphology HMT LineJunctions hough.png 

enter image description here

+0

感謝您的努力。只要將imagemagick升級到6.9,我就會試一試。我有6.7,因此無法使用-canny在6.8以後支持我猜 –

+0

優秀的答案,馬克! :-) –

+0

偉大的工作......非常感謝Mark –

相關問題