Find Position of SubImage in biggger one

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
klaus777

Find Position of SubImage in biggger one

Post by klaus777 »

Hi Folks,

I'm a complete newbie to IM. So please bear with me. I want to find the position of a given subimage in a bigger image. Supposely, the following command was able to do this at some point in the life of IM:

Code: Select all

convert cyclops_question.png cyclops.png \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 1 -alpha off -compose copy_opacity -composite -trim \
cyclops_sep.png
As of today, however, when I run this command, I get the following error message:
convert: missing an image filename `source.png' @ error/convert.c/ConvertImageCommand/2949.
As I understand (from http://www.imagemagick.org/Usage/basics/#why), this seems to be due to change in the command line arguments and their ordering. Hence my question: how can I do this with the current version of ImageMagick?

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

Re: Find Position of SubImage in biggger one

Post by fmw42 »

Unless I have missed something like a space after the \, your command looks OK to me other than possibly adding +repage after the -trim. So I am not sure what the issue might be. Possibly missing or corrupt image? Which image is first, the larger or the smaller?

I tried this and it ran without error:

convert logo: \( logo: -gravity center -crop 128x128+0+0 \) \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 1 -alpha off -compose copy_opacity -composite -trim +repage \
logo_test.png

The above gave me the logo image with a transparent 128x128 area in the center

But when I ran this, I got the following error:

convert \( logo: -gravity center -crop 128x128+0+0 \) logo: \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 1 -alpha off -compose copy_opacity -composite -trim +repage \
logo_test.png

convert: geometry does not contain image `LOGO' @ warning/attribute.c/GetImageBoundingBox/239.


Nevertheless, I am not sure if that is really a valid way to find the offset.

However, you can find where a subimage is located in a larger image using the compare function.

see

http://www.imagemagick.org/script/compare.php
http://www.imagemagick.org/Usage/compare/#compare
http://www.imagemagick.org/Usage/compare/#sub-image
viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076

in the latter, the new convention is to use -subimage-search to force compare to consider a smaller and larger image rather than just the difference between two images of the same size. See the link above ending in /#sub-image


What version of IM. You may need to upgrade to get the subimage compare with the correct syntax as the -subimage-search option was added in 6.6.3-6, though prior to that you could do subimage search without it as far back as 6.5.0-9
Post Reply