Simple Image Matching

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
billbell52

Simple Image Matching

Post by billbell52 »

I am trying to determine if a sub image exists in a main image. Both images are black and white. Both are small. Main image is 50x50. Sub is 12x12 pixels. There is a 100% match. I want to know if the sub image exists in the main image and the location of the match. I am running this on a PC. I have read the docs and I cannot figure it out.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Simple Image Matching

Post by fmw42 »

see example at viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076

if necessary add -dissimilarity-threshold 1 to the command to force it to continue to the end if it stops prematurely due to to much difference
billbell52

Re: Simple Image Matching

Post by billbell52 »

Thanks. just tried it and it appears to work. For what I want it is fast enough.

IM is amazing!!!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: (not so) Simple Image Matching

Post by anthony »

Other methods for binary symbols is to use morphology, or correlation, both of which is currently on the IM Examples page
http://www.imagemagick.org/Usage/morphology/

The disadvantage is that you currently can not generate a 'search pattern' using an image. However
I can see ways to generate a 'kernel' from an image. For example...
first a image of many symbols to search...

Code: Select all

convert label:"AWNSuj4CPqQ4w1XfBkmUMT-7
G,FyRtWIyvmmSfigTj5dYp25
Jnkfh,fxghFkGtTdVSu,1,u8
M-eXsDl1UIeeOMUoOskASTCG
QN,z0IstoiNtnNOBg5o7Rx,y"  -threshold 50% -negate  text.gif
now lets generate a 'symbol' image to search can convert it into a kernel string....

Code: Select all

convert label:"5" -threshold 50%  -trim +repage -negate  symbol.gif

convert symbol.gif -format '%wx%h:' info:  > kernel.txt
convert symbol.gif  -compress none pgm: | tail -n +4 | sed 's/255/1/g' >> kernel.txt
the 'kernel.txt' file now contains the search pattern...

Code: Select all

5x8:
1 1 1 1 0 1 0 0 0 0 1 1 1 1 0 1 0 0 1 1 0 0 0 0 1 
0 0 0 0 1 1 0 0 1 1 1 1 1 1 0 
Now you can search for that symbol in the above, using 'erode'

Code: Select all

convert text.gif -morphology erode  "`cat kernel.txt`" show:
And it works... the result is a black image with 3 white pixels showing the locations
of the '5' characters.

NOTE the use of '@file' is not yet available for -morphology, just as generating a kernel from image is also not yet available.

NOTE When 'hit-n-miss' morphology development has finished that will not just match
the 'on' pixels as in the above, but also 'off' pixels.

NOTE: Correlation would not just find exact matches, but also 'near' matches, but by replacing the zeros in the kernel with -1 you can also match backgrounds ( as hit-n-mis morphology would.

Correlation can also be done using very LARGE search images (directly) using Fast Fourier Transform Techniques that are still being developed and added. However 'negative' correlations would need a HDRI version of IM.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply