如何使用 ImageMagick 替换图像中的白色矩形?
How can I replace the white rectangle within an image using ImageMagick?
概览:
第一张图是我的原图。这里我想用另一个图像替换显示的白色矩形。
我的做法:
我使用 floodfill
创建了一个蒙版图像,它看起来像:
问题:
现在我想获取第二张图像中矩形的距离或坐标,以便我可以使用这些坐标在此处的第一张(原始图像)之上叠加一个新图像。
我有点想使用 ImageMagick 的 chebyshev 形态学运算符,但不知道该怎么做。
我认为您可以使用简单的阈值非常准确地定位形状,如下所示:
convert image.jpg -threshold 90% result.jpg
然后您可以像这样进行 Canny 边缘检测:
convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg
接下来我要看的是,使用 -trim
函数找到 trim 框坐标,如下所示:
convert result.jpg -format "%@" info:
320x248+152+40
我在下面用红色标记了它。
如果你真的想做 trim,使用这个:
convert result.jpg -trim result.jpg
还有去偏角
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
并且文件 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
概览:
第一张图是我的原图。这里我想用另一个图像替换显示的白色矩形。
我的做法:
我使用 floodfill
创建了一个蒙版图像,它看起来像:
问题:
现在我想获取第二张图像中矩形的距离或坐标,以便我可以使用这些坐标在此处的第一张(原始图像)之上叠加一个新图像。
我有点想使用 ImageMagick 的 chebyshev 形态学运算符,但不知道该怎么做。
我认为您可以使用简单的阈值非常准确地定位形状,如下所示:
convert image.jpg -threshold 90% result.jpg
然后您可以像这样进行 Canny 边缘检测:
convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg
接下来我要看的是,使用 -trim
函数找到 trim 框坐标,如下所示:
convert result.jpg -format "%@" info:
320x248+152+40
我在下面用红色标记了它。
如果你真的想做 trim,使用这个:
convert result.jpg -trim result.jpg
还有去偏角
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
并且文件 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