本文介绍了如何使用ImageMagick替换图像中的白色矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述:

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

The first picture is my original image. Here I want to replace the white rectangle shown with another image.

我的方法:

我有使用 floodfill 创建了一个掩码图像,它看起来像:

I have created a mask image using floodfill and it looks as:

问题:

现在我想获得第二张图像中矩形的距离或坐标,这样我就可以使用这些坐标在第一张图像的顶部叠加新图像(原始图像)在这里。

Now I would like to get the distance or co-ordinates of the rectangle in the second image so that I can use those co-ordinates to overlay a new image on top of the first (original image) here.

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

I got a little idea to use ImageMagick's chebyshev morphology operator, but don't know how I can do that.

推荐答案

我认为你可以用as非常准确地定位形状实现阈值,如下所示:

I think you can locate the shape pretty accurately with a simple threshold, like this:

convert image.jpg -threshold 90% result.jpg

然后你可以像这样做一个Canny边缘检测:

and you can then do a Canny edge detection like this:

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

接下来的事情我会正在使用 -trim 函数来查找修剪框坐标,如下所示:

The next things I would be looking at are, using the -trim function to find the trim box coordinates, like this:

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

我在下面用红色标记了。

I have marked that on in red below.

如果你真的想做修剪,请使用:

If you actually want to do the trim, use this:

convert result.jpg -trim result.jpg

此外,还有偏斜角度

convert result.jpg -deskew 40 -format "%[deskew:angle]" info:
-0.111906

霍夫线检测也可能对您有效:

A Hough line detection may also be effective for you like this:

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行

And the file lines.mvg contains the 4 lines you are looking for

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

Being a bit lazy, I didn't feel like solving for the intersections of those lines, so I thought I'd let ImageMagick do that too - by using Morphology to look for Line Junctions like this:

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替换图像中的白色矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:54