ImageMagick: How to Search for sub image in a bigger 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
Crazy_Bash
Posts: 3
Joined: 2012-10-12T12:14:39-07:00
Authentication code: 67789

ImageMagick: How to Search for sub image in a bigger image

Post by Crazy_Bash »

I have a forum on which users often upload images with watermark of our competitors, I would like to search for the logo on the image and replace or cut it.

I know that ImageMagick has an option called subimage-search, but i had no luck with it

what I've tried so far:

compare -subimage-search logo.png sub.png similarity.png
compare -subimage-search -metric rmse large_image.png sub-image.png result.png
compare -subimage-search -metric pae large_image.png sub-image.png result.png
compare -subimage-search -metric ncc large_image.png sub-image.png result.png
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: ImageMagick: How to Search for sub image in a bigger ima

Post by Bonzo »

I have a forum on which users often upload images with watermark of our competitors, I would like to search for the logo on the image and replace or cut it.
I assume your competitors watermark their image for this reason.

If anything you should be removing the image rather than removing the watermark.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick: How to Search for sub image in a bigger ima

Post by fmw42 »

What version of IM and platform? -subimage-search was added at version 6.6.3-6

I have used successfully for quite some time now, the following syntax.

compare -metric rmse -subimage-search large_image.png sub-image.png result.png

That should produce two result.png images, -0 and -1. The second one will be the similarity image.

see
http://www.imagemagick.org/script/comma ... age-search
http://www.imagemagick.org/Usage/compare/
http://www.imagemagick.org/script/compare.php
viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076 (though current versions of IM now require the addition of -subimage-search)

Perhaps you should post links to your two images so others can test.
Crazy_Bash
Posts: 3
Joined: 2012-10-12T12:14:39-07:00
Authentication code: 67789

Re: ImageMagick: How to Search for sub image in a bigger ima

Post by Crazy_Bash »

fmw42 wrote:What version of IM and platform? -subimage-search was added at version 6.6.3-6

I have used successfully for quite some time now, the following syntax.

compare -metric rmse -subimage-search large_image.png sub-image.png result.png

That should produce two result.png images, -0 and -1. The second one will be the similarity image.

see
http://www.imagemagick.org/script/comma ... age-search
http://www.imagemagick.org/Usage/compare/
http://www.imagemagick.org/script/compare.php
viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076 (though current versions of IM now require the addition of -subimage-search)

Perhaps you should post links to your two images so others can test.
Thanks for replay.
I'm using:
Version: ImageMagick 6.7.7-10 2012-08-27 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC

The images:
Image
sub-image to search
Image
image to replace with
Image

results:
Image
Image

I can see the two results images, but i don't know how to move forward and replace Image with Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick: How to Search for sub image in a bigger ima

Post by fmw42 »

There should be some output to the terminal that tells you the rmse error, its values scaled to the range 0 to 1 (in parenthesis) and the x,y coordinates of the best match, all in that order. Use the x,y coordinates to composite the new image at the same x,y location.


convert image newsmallimage -geometry +X+Y -compose over -composite result

see
http://www.imagemagick.org/Usage/layers/#convert
Crazy_Bash
Posts: 3
Joined: 2012-10-12T12:14:39-07:00
Authentication code: 67789

Re: ImageMagick: How to Search for sub image in a bigger ima

Post by Crazy_Bash »

Worked! Thanks a lot you are the man :)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: ImageMagick: How to Search for sub image in a bigger ima

Post by anthony »

This I don't think can be classed as a watermark. It is probbaly better classed as SPAM advertising, and you just wanted to replace that spam with your own spam. :-)

A true watermark would be very hard to locate and replace, as it is a distortion of the color values rather than a bold and obvious overlay. Direct sub-image searches of real watermarks would be very difficult.

And even in the frequency domain, it would probably be very hard to locate, though probably easier than for direct sub-image searches.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply