我认为您可以使用简单的阈值非常准确地定位形状,如下所示:
convert image.jpg -threshold 90% result.jpg
然后你可以像这样做一个Canny边缘检测:
convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg
接下来我要看的是,使用-trim函数找到修剪框坐标,如下所示:
convert result.jpg -format "%@" info:
320x248+152+40
我在下面用红色标记了.
如果你真的想做修剪,请使用:
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这样做 – 通过使用Morphology来寻找像这样的Line Junction:
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