get signed result from -subimage-search ?

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
adrda
Posts: 5
Joined: 2016-05-15T17:25:09-07:00
Authentication code: 1151

get signed result from -subimage-search ?

Post by adrda »

Hi,

-subimage-search of two grayscale images provides a correlation map, but correlation and anti-correlation cannot be distinguished. I would like a way to keep the sign of the ressemblance.

To give an example, here is a search for a bright disc (p.png) in an image containing a white and a black disc (test.png):

Code: Select all

convert -size 15x15 xc:black -fill white -draw 'circle 7,7 11,7' p.png
convert -size 100x50 xc:grey -fill white -draw 'circle 25,25 29,25' -fill black -draw 'circle 75,25 79,25' test.png
compare test.png p.png -metric NCC -subimage-search tiff:- 2>/dev/null | convert tiff:- -delete 0 show
As you can see the result of -subimage-search shows the same value at both (25,25) and (75,25). My search sample (p.png) was a bright disc on a darker background, so I'd expect not to find the right spot at (75,25). In technical terms, I want the correlation coefficient either to be clipped to positive values, or to be offset so that zero maps to grey (the equivalent of -bias 50%, which is not an option recognized by compare).

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

Re: get signed result from -subimage-search ?

Post by fmw42 »

I do not know what IM does for NCC in terms of anti-correlation values. But if you are using non-HDRI compile of IM, the results will be clipped to the range 0 to quantumrange (0 to 1). You do not say what versions and Q level compile of IM you are using.

If I do

Code: Select all

convert -size 15x15 xc:black -fill white -draw 'circle 7,7 11,7' p.png
im6937hdri compare -metric NCC p.png p.png null:
1.00446

Code: Select all

convert -size 15x15 xc:black -fill white -draw 'circle 7,7 11,7' p.png
convert p.png -negate pneg.png
im6937hdri compare -metric NCC p.png pneg.png null:
1.00446

So it does look like IM may be taking the absolute value of the result.

The fractional part greater than 1 is likely a rounding issue.
adrda
Posts: 5
Joined: 2016-05-15T17:25:09-07:00
Authentication code: 1151

Re: get signed result from -subimage-search ?

Post by adrda »

Yes precisely, it appears IM takes the absolute value of the NCC, which is not what I want (who ever does, I wonder ?).

In the meantime I turned to the -morphology Convolve operator as a replacement for -subimage-search, because the object I want to detect can be described as linear combination of available kernels (and both images are grayscale).
In the general case it's a pity that this -morphology operator does not accept a (grayscale) image as a kernel, it's quite messy to convert an image into the text representation that the operator expects for a free-form kernel.

Thanks for your help,
Adrian

My apologies for forgetting the IM version information:

$> convert -version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2016-04-08 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff wmf x xml zlib
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: get signed result from -subimage-search ?

Post by snibgo »

adrda wrote:In the general case it's a pity that this -morphology operator does not accept a (grayscale) image as a kernel, ...
Agreed.

I've written a process module for this purpose: it takes an image and spits out the text for the kernel. But this is a messy solution. A kernel is a raster image, and IM is a processor for raster images, so I've never understood why we have to specify kernels with text.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: get signed result from -subimage-search ?

Post by fmw42 »

I suspect the problem is that kernel images in non-hdri IM would preclude negative values.
adrda
Posts: 5
Joined: 2016-05-15T17:25:09-07:00
Authentication code: 1151

Re: get signed result from -subimage-search ?

Post by adrda »

Right, but one could introduce an offset parameter, declare QuantumRange/2 as zero, or accept an optional second raster image representing the negative part.
Post Reply