【问题标题】:How can I replace the white rectangle within an image using ImageMagick?如何使用 ImageMagick 替换图像中的白色矩形?
【发布时间】:2015-06-22 06:54:56
【问题描述】:

概述:

第一张图是我的原图。这里我想用另一个图像替换显示的白色矩形。

我的方法:

我使用floodfill 创建了一个蒙版图像,它看起来像:

问题:

现在我想获取第二个图像中矩形的距离或坐标,以便我可以使用这些坐标在此处的第一个(原始图像)之上叠加一个新图像。

我对使用 ImageMagick 的 chebyshev 形态运算符有一点想法,但不知道该怎么做。

【问题讨论】:

  • 试试convert image.jpg -threshold 90% result.jpg
  • 或者convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg
  • 你好马克...谢谢你的回复。但我在这里想要的是获取矩形的坐标、旋转角度以及宽度和高度,以便与另一个图像重叠。请在此处查看我的另一个问题,以便您了解我想要实现的目标。 stackoverflow.com/questions/30971894/…希望我走的是正确的道路。如果没有请指导我。目前我正在遵循这种方式:imagemagick.org/discourse-server/viewtopic.php?t=20269
  • 我会做霍夫线来得到 4 个边,然后求解交叉点来得到拐角。

标签: php image-processing imagemagick


【解决方案1】:

我认为您可以使用简单的阈值非常准确地定位形状,如下所示:

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 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

【讨论】:

  • 感谢您的努力。将 imagemagick 升级到 6.9 后,我会尝试一下。我有 6.7,因此无法使用支持 6.8 的 -canny 我猜
  • 优秀的答案,马克! :-)
  • 为您再标记一个问题。如何将用于显示目的的点(交点)的坐标存储在变量或文件中,就像使用线条一样。
  • 应该使用convert hough.png txt:- | grep "red"
猜你喜欢
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 2019-05-31
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
相关资源
最近更新 更多