Kir*_*ash 7 php imagemagick image-processing
概述:
第一张照片是我原来的照片.在这里,我想用另一个图像替换显示的白色矩形.
我的方法:
我使用创建了一个蒙版图像floodfill
,它看起来像:
问题:
现在我想得到第二个图像中矩形的距离或坐标,这样我就可以使用这些坐标在这里叠加第一个(原始图像)顶部的新图像.
我有点想法使用ImageMagick的chebyshev形态算子,但不知道我怎么做.
我认为您可以使用简单的阈值非常准确地定位形状,如下所示:
convert image.jpg -threshold 90% result.jpg
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样做一个Canny边缘检测:
convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg
Run Code Online (Sandbox Code Playgroud)
接下来我要看的是,使用-trim
函数找到修剪框坐标,如下所示:
convert result.jpg -format "%@" info:
320x248+152+40
Run Code Online (Sandbox Code Playgroud)
我在下面用红色标记了.
如果你真的想做修剪,请使用:
convert result.jpg -trim result.jpg
Run Code Online (Sandbox Code Playgroud)
而且,偏斜角度
convert result.jpg -deskew 40 -format "%[deskew:angle]" info:
-0.111906
Run Code Online (Sandbox Code Playgroud)
霍夫线检测也可能对您有效:
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
Run Code Online (Sandbox Code Playgroud)
该文件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
Run Code Online (Sandbox Code Playgroud)
有点懒,我不想解决这些线的交叉点,所以我想我也让ImageMagick这样做 - 通过使用Morphology来寻找像这样的Line Junctions:
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
Run Code Online (Sandbox Code Playgroud)