我该如何使用ImageMagick替换图像中的白色矩形?

7

概述:

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

enter image description here

我的方法:

我使用floodfill创建了一个掩码图像,如下所示:

enter image description here

问题:

现在我想获取第二张图片中矩形的距离或坐标,以便我可以使用这些坐标将新图像覆盖在第一张(原始)图像上。

我有一个小想法,可以使用ImageMagick的切比雪夫形态学算子,但不知道如何操作。


尝试使用命令convert image.jpg -threshold 90% result.jpg进行图像处理。 - Mark Setchell
或者是 convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg - Mark Setchell
你好Mark,谢谢回复。但是我想要的是获取矩形的坐标、旋转角度、宽度和高度,以便重叠另一张图片。请看看我的其他问题,这样你就能了解我想要实现什么。http://stackoverflow.com/questions/30971894/replacing-detected-object-in-a-frame-with-an-image-imageprocessing希望我走的是正确的方向。如果不是,请指导我。 目前我正在按照以下方式进行:http://www.imagemagick.org/discourse-server/viewtopic.php?t=20269 - Kiran Dash
我会使用霍夫线来获取四条边,然后解决交点以获取角落。 - Mark Setchell
1个回答

6

我认为您可以通过简单的阈值来相当准确地定位形状,就像这样:

convert image.jpg -threshold 90% result.jpg

enter image description here

然后,您可以像这样进行Canny边缘检测:

convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg

图片描述

接下来,我将使用-trim函数查找修剪框坐标,如下所示:

convert result.jpg -format "%@" info:
320x248+152+40

我已经在下面的红框标出来了。

enter image description here

如果您实际上想要进行修剪,请使用以下内容:

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

enter image description here


感谢您的努力。我会尽快升级我的ImageMagick到6.9版本,然后尝试一下。我现在使用的是6.7版本,因此无法使用-canny命令,该命令从6.8版本开始支持。 - Kiran Dash
马克,你的回答太棒了! :-) - Kurt Pfeifle
再问您一个问题。我该如何将您用于显示目的的点(交叉点)的坐标存储在变量或文件中,就像您对线条所做的那样。 - Kiran Dash
应该能够与 convert hough.png txt:- | grep "red" 一起使用。 - wittich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接