Remove object from Image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Caracks
Posts: 3
Joined: 2019-10-08T11:28:24-07:00
Authentication code: 1152

Remove object from Image

Post by Caracks » 2019-10-08T13:38:22-07:00

Hello everyone, I'm fairly new to IM and to coding.

I have 2000 images which some have the seller's logo and some don't, as the following image shows: https://i.imgur.com/xnXtZa6.png
Since I can't have the logo, I need IM to either remove the object or replace it with a white object, like this: https://i.imgur.com/kCC7DWH.png

I haven't found the solution yet,
Thank you very much

User avatar
fmw42
Posts: 25546
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove object from Image

Post by fmw42 » 2019-10-08T14:04:33-07:00

How does one identify the logo and distinguish it from the main subject? Is it always one color? Is it always the smallest object? How is the computer going to know what is logo and what is not logo?

Please always provide your ImageMagick version and platform/OS, since syntax may differ.

Caracks
Posts: 3
Joined: 2019-10-08T11:28:24-07:00
Authentication code: 1152

Re: Remove object from Image

Post by Caracks » 2019-10-08T14:13:38-07:00

Thanks for replying in such a short notice!
fmw42 wrote:
2019-10-08T14:04:33-07:00
How does one identify the logo and distinguish it from the main subject? Is it always one color? Is it always the smallest object? How is the computer going to know what is logo and what is not logo?

Please always provide your ImageMagick version and platform/OS, since syntax may differ.
It's always that purple logo. The Imagemagick version is the 7.0.3-4 running in Win 10

User avatar
fmw42
Posts: 25546
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove object from Image

Post by fmw42 » 2019-10-08T15:01:08-07:00

Will the logo always be in a white area and not cover the main object? Will it always be the same size?

Caracks
Posts: 3
Joined: 2019-10-08T11:28:24-07:00
Authentication code: 1152

Re: Remove object from Image

Post by Caracks » 2019-10-08T15:19:10-07:00

Yes

User avatar
fmw42
Posts: 25546
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove object from Image

Post by fmw42 » 2019-10-08T17:35:23-07:00

OK. One way to do that is to use compare to search the larger image with logo to find the best matching location. This is a slow process so one should resize both the image and logo to get a close matching location. Then crop the large image with a bit of padding and search it for the exact match.

Unfortunately, I do not use Windows and the syntax for doing computations is not something I know how to program. But I can give you an outline.

The information returned from a compare is the metric score of how well it matched in the range 0 to quantum range, the score in the range 0 to 1 in parenthesis and the x,y pixel location of the top left corner of where the logo is matched in the large image.

See
https://imagemagick.org/script/compare.php
https://imagemagick.org/Usage/compare/

Input:
Image

Logo:
Image

Code: Select all

magick compare -metric rmse -subimage-search ( witch.png purplelogo.png -resize 25% ) null:
2739.99 (0.0418096) @ 188,1

Compute the location in the full resolution image
x=188*4=752 subtract 10 = 742
y=1*4=4 subtract 4 = 0

purplelogo.png has dimensions 142 × 137 so we pad by 20 in each direction

So now process the original image but crop crop it at 162x157+0+742 so we only search a small region

Code: Select all

magick compare -metric rmse -subimage-search ( witch.png -crop 162x157+742+0 +repage ) purplelogo.png  null:
0 (0) @ 11,2

So the location in the uncrossed image is x=11+742=753 and y=2+0=2

So now just change the logo to be pure white and place it over the location

Code: Select all

magick witch.png -alpha off ( purplelogo.png -alpha opaque -fill white -colorize 100 ) -geometry +753+2 -compose over -composite witch_white.png
Image


Another method would be to convert the image to binary and use connected-components to get the bounding box where the smallest region is located, assuming it is the logo. Then use the bounding box to create a white region and place it over the original image. See https://imagemagick.org/script/connected-components.php

Code: Select all

magick witch_binary.png -type bilevel ^
-define connected-components:verbose=true ^
-define connected-components:mean-color=true ^
-define connected-components:area-threshold=5000 ^
-connected-components 4 null:
Objects (id: bounding-box centroid area mean-color):
6: 754x855+386+0 867.1,435.7 446758 gray(255)
0: 463x855+0+0 196.9,416.5 331007 gray(255)
3: 423x855+249+0 496.1,453.8 181988 gray(0)
8: 143x147+749+0 821.0,71.7 14947 gray(0)

Get the bounding box of the smallest remaining black region
143x147+749+0

Code: Select all

magick witch.png -alpha off ( -size 143x147 xc:white ) -geometry +749+0 -compose over -composite witch_white2.png
Image

I leave it to you to script either approach in Windows.

snibgo
Posts: 12145
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove object from Image

Post by snibgo » 2019-10-09T06:04:59-07:00

For a Windows BAT script that searches quickly, see Searching an image.

For a process module that searches even more quickly, see Process modules: multi-scale image search. This is available for all platforms, but needs IM to be re-built.

For the OP problem, perhaps the logo is always the smallest non-white connected component. If so, then "-connected-components" may be the fastest way to search for it.
snibgo's IM pages: im.snibgo.com

Post Reply