Page 1 of 1

how to search subimage in a region of a larger image

Posted: 2015-09-17T09:52:20-07:00
by jesse1
Hi.
I want to search a small image in a big one. It works well when I use:

Code: Select all

compare -metric mse -subimage-search "big.png" "small.png" null:
But it rather slow and I want to specify region in big image where subimage should exist.
I tried:

Code: Select all

compare -metric mse -extract 64x150+0+118 -subimage-search "big.png" "small.png" null:
And I get error:

0 (0) @ 0,8compare: geometry does not contain image `small.png' @ warning/transform.c/CropImage/666.
compare: geometry does not contain image `big.png' @ warning/transform.c/CropImage/666.

The bigger image is 300x500, small is 20x20.

What is wrong?

Re: how to search subimage in a region of a larger image

Posted: 2015-09-17T09:57:59-07:00
by fmw42
try

Code: Select all

compare -metric mse -subimage-search "big.png[64x150+0+118]" "small.png" null:
If you want a very fast compare and are on a Unix platform (Linux, Mac OSX or Windows with Cygwin) and are willing to compile IM in Q16 HDRI, then you could try my script normcrosscorr at my link below. It uses FFT and is explained at http://www.fmwconcepts.com/imagemagick/ ... mcrosscorr and
http://www.fmwconcepts.com/imagemagick/ ... SFORMS.pdf

Alternately, do a multi-resolution search by resizing both images to be smaller say by 1/10 and do the compare, then take a full resolution subsection of the larger image near the match point and do the compare with that region and full smaller image. User snibgo does this and may be able to give better details. See his web site section at http://im.snibgo.com/srchimg.htm

Re: how to search subimage in a region of a larger image

Posted: 2015-09-17T10:05:00-07:00
by snibgo
As Fred says, but I'll add:

Extract, like other operators, processes all the images in the current list, not just one. Unlike other operators, it seems to only work when placed before the image, not after it. (Its purpose is to speed up reading.)

I would use crop, with parentheses. And I would use convert, not compare:

Code: Select all

convert -metric MSE ( "big.png" -crop 64x150+0+118 +repage ) "small.png" -subimage-search NULL:

Re: how to search subimage in a region of a larger image

Posted: 2015-09-18T00:49:46-07:00
by jesse1
Thanks for your answers.
fmw42 wrote: compare -metric mse -subimage-search "big.png[64x150+0+118]" "small.png" null:
I tried it and still got error:
20398.4 (0.311259) @ 0,129compare: geometry does not contain image `big.png' @ warning/transform.c/CropImage/666.
snibgo wrote:convert -metric MSE ( "big.png" -crop 64x150+0+118 +repage ) "small.png" -subimage-search NULL:
In this case I receive:
convert: unrecognized option `-subimage-search' @ error/convert.c/ConvertImageCommand/2950.

I use Mac OSX version of imagemagick:
Version: ImageMagick 6.9.1-10 Q16 x86_64 2015-08-20 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib

Re: how to search subimage in a region of a larger image

Posted: 2015-09-18T01:32:55-07:00
by snibgo
Sorry, I had a brainstorm there. "-subimage-search" is not available in convert. (I think it should be, but it isn't.) And "-crop" is not available in compare.

The "geometry does not contain image" message usually arises when your offsets are larger than the image. What size is big.png?

Re: how to search subimage in a region of a larger image

Posted: 2015-09-18T02:41:28-07:00
by snibgo
I'll add generally: searching needs O(n^4) pixel comparisons, where (n) is the linear dimension.

If the approximate location of the small image within the large image is known, it is always worth shrinking the large image to the search area before doing the actual search.

If the small image could be anywhere in the large image, I generally do as Fred suggests: shrink both images, and an initial search of these gives an approximate location, so a cropped area of the large image can then be searched for the small image. Instead of one very slow search, I do two or more very fast searches. (In theory, a search of resized images may return the wrong result, but I have never noticed this in practice.) I explore this in my page Searching an image.

Of course, both techniques can be used: first crop the large image to exclude the area where the search is certain to fail, and then use a resizing technique.

In the OP problem, if the small image is larger than 20x20 pixels, the resizing technique will probably be faster.

Re: how to search subimage in a region of a larger image

Posted: 2015-09-18T03:41:33-07:00
by jesse1
snibgo wrote:The "geometry does not contain image" message usually arises when your offsets are larger than the image. What size is big.png?
It is 300x500.

I understand I can do this in two steps: first crop, then compare with -subimage-search, but it would be better if it was possible in single command, without worrying about intermediate files.

Re: how to search subimage in a region of a larger image

Posted: 2015-09-18T03:48:28-07:00
by jesse1
While I want offset to be applied to big image only, I noticed that error reported when offset is bigger than small image size.
Small image is 20x20, so when offset is "+20+20" or higher, it gives me error.

Re: how to search subimage in a region of a larger image

Posted: 2015-09-18T09:48:12-07:00
by fmw42
Can you upload your two images to dropbox.com and put the URLs here, so we can test. I see no reason that my command should not work.