Page 1 of 1
get signed result from -subimage-search ?
Posted: 2016-05-22T14:24:48-07:00
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
Re: get signed result from -subimage-search ?
Posted: 2016-05-22T16:10:13-07:00
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.
Re: get signed result from -subimage-search ?
Posted: 2016-05-22T17:59:42-07:00
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
Re: get signed result from -subimage-search ?
Posted: 2016-05-22T18:23:03-07:00
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.
Re: get signed result from -subimage-search ?
Posted: 2016-05-22T19:14:21-07:00
by fmw42
I suspect the problem is that kernel images in non-hdri IM would preclude negative values.
Re: get signed result from -subimage-search ?
Posted: 2016-05-23T01:24:48-07:00
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.