How to replace a white rectangle in an image using ImageMagick?

Overview:

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

enter image description here

My approach:

I created a mask image using floodfill and looks like this:

enter image description here

Problem:

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

I had a small idea to use the ImageMagick chebyshev morphology operator, but I donโ€™t know how I can do this.

+7
php image-processing imagemagick
source share
1 answer

I think you can pretty accurately determine the shape using a simple threshold, for example:

 convert image.jpg -threshold 90% result.jpg 

enter image description here

and then you can do Canny edge detection like this:

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

enter image description here

The next thing I'm looking at is to use the -trim function to find the coordinates of the trim frame, for example:

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

I noted on the red below.

enter image description here

If you really want to crop, use this:

 convert result.jpg -trim result.jpg 

enter image description here

As well as the angle of rotation

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

Hough line detection can also be effective for you as follows:

 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 

enter image description here

And the lines.mvg file contains 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 little lazy, I didnโ€™t want to solve the problems of intersecting these lines, so I thought that I would let ImageMagick do this too - using Morphology to search for Line Junctions as follows:

 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

+6
source share

All Articles