Php 如何使用ImageMagick替换图像中的白色矩形?
概述: 第一张照片是我的原始图像。在这里,我想用另一个图像替换显示的白色矩形 我的方法: 我使用Php 如何使用ImageMagick替换图像中的白色矩形?,php,image-processing,imagemagick,Php,Image Processing,Imagemagick,概述: 第一张照片是我的原始图像。在这里,我想用另一个图像替换显示的白色矩形 我的方法: 我使用泛光填充创建了一个遮罩图像,它看起来如下所示: 问题: 现在我想得到第二幅图像中矩形的距离或坐标,这样我就可以使用这些坐标在第一幅(原始图像)的顶部覆盖一幅新图像 我有了一个使用ImageMagick的形态学操作符的小主意,但不知道如何做到这一点。我认为你可以用一个简单的阈值非常准确地定位形状,如下所示: convert image.jpg -threshold 90% result.jpg c
泛光填充创建了一个遮罩图像,它看起来如下所示:
问题:
现在我想得到第二幅图像中矩形的距离或坐标,这样我就可以使用这些坐标在第一幅(原始图像)的顶部覆盖一幅新图像
我有了一个使用ImageMagick的形态学操作符的小主意,但不知道如何做到这一点。我认为你可以用一个简单的阈值非常准确地定位形状,如下所示:
convert image.jpg -threshold 90% result.jpg
convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg
convert result.jpg -format "%@" info:
320x248+152+40
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
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
然后你可以像这样做一个精明的边缘检测:
convert image.jpg -threshold 90% result.jpg
convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg
convert result.jpg -format "%@" info:
320x248+152+40
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
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
接下来我要看的是,使用-trim
函数查找修剪框坐标,如下所示:
convert image.jpg -threshold 90% result.jpg
convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg
convert result.jpg -format "%@" info:
320x248+152+40
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
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
我已经在下面用红色标出了
如果确实要进行修剪,请使用以下方法:
convert result.jpg -trim result.jpg
还有,倾斜角度
convert result.jpg -deskew 40 -format "%[deskew:angle]" info:
-0.111906
Hough线检测也可能对您有效,如下所示:
convert image.jpg -threshold 90% result.jpg
convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg
convert result.jpg -format "%@" info:
320x248+152+40
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
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
文件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% result.jpg
convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg
convert result.jpg -format "%@" info:
320x248+152+40
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
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
尝试convert image.jpg-阈值90%结果。jpg
或者convert image.jpg-阈值90%-canny 0x1+10%+30%结果。jpg
Hello Mark…感谢您的回复。但我想在这里得到的是坐标,旋转角度,矩形的宽度和高度,以便与另一个图像重叠。请看我在这里的另一个问题,这样你就知道我在努力实现什么。希望我走对了路。如果没有,请引导我。目前我采用的是这样的方法:我会用Hough线得到4条边,然后求解交叉点得到拐角。谢谢你的努力。一旦我将imagemagick升级到6.9,我就会尝试一下。我有6.7,因此无法使用-canny,它支持6.8以后的版本,我猜答案很好,Mark!:-)再给你标记一个问题。如何将用于显示目的的点(交点)坐标存储在变量或文件中,存储方式与线条相同。应使用convert hough.png txt:-| grep“red”