How can I replace the white rectangle within an image using ImageMagick?

老子叫甜甜 提交于 2019-12-19 17:38:38

问题


Overview:

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

My approach:

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

Problem:

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.

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


回答1:


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

convert image.jpg -threshold 90% result.jpg

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

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

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

And also, the deskew angle

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

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

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



来源:https://stackoverflow.com/questions/30973677/how-can-i-replace-the-white-rectangle-within-an-image-using-imagemagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!