On Tue, 9 Apr 2013, at 17:42, Lewis Haley wrote:
> Hi everyone,
>
> I have successfully prototyped a method for template matching with
> transparent template,
> i.e. templates which aren't square or even regular shapes, and which
> part(s) of the template
> have zero alpha.
>
> This is the message from an internal commit, which outlines the process:
>
> The general idea is:
> > What if we want to match a template that isn't square, or otherwise
> > might
> > have background content that we wish to ignore?
> > This prototype allows for template with an alpha channel (transparency)
> > to be submitted to the template match algorithm.
> >
> > Note that the cv.imread which loads the images from disk uses -1
> > instead
> > of 1 as the flag. This means that all channel information is kept
> > as-is,
> > whereas 1 would remove the alpha and just use RGB.
I like the general approach. It would be good to work through the maths
of it to ensure the results are consistent with both the current results
with non-transparent templates and that we could generalise the approach
to semi-transparent templates.
> > * The process does not affect the first pass... which means that in
> > general
> > the match threshold (certainty, best_res, etc) has to be decreased as
> > there is literally less image to match... I'm uncertain how
> > cvTemplateMatch deals with alpha channels... I think it probably
> > ignores
> > it and just reads the RGB as (0, 0, 0) or black. Might want to
> > consider cloning the template for the template match and converting
> > every
> > pixel with alpha = 0 to be mid-gray because this is would maximize
> > the
> > potential difference to any other colour.
I like that idea. Rather than grey, filling the space with noise
(random pixel values) might give better results. This is because nothing
should correlate with random noise well, including other random noise.
This could even work with semi-transparent templates. We could blend
the noise behind the semi-transparent image.
We would have to take care to adjust the certainties appropriately. Also
blending could be tricky as it's colourspace dependant and we might have
to be careful to take gamma into account. But certainly it should be
easier with a simple mask rather than semi-transparency, so we can start
there much like you have :).
> > * The mask_from_alpha function creates a mask by parsing over the
> > template
> > and creating a new image where I[x,y] = white if alpha == 255, else
> > black. We do it like this so that only 100% opaque pixels contribute
> > to
> > the mask, which eliminates anti-aliasing problems. Note that the
> > convert
> > to grayscale must come after the alpha parse so we do not lose the
> > alpha
> > information!
For now I think I'd prefer to raise an exception on partial transparency
for forward compatibility with support for partial transparency.
> > * The normalize still works with the addition of giving the normalize
> > the
> > mask as an option. Note that OpenCV already supported this.
> >
> > * After the absdiff, threshold, erode, we do a bitwise and between the
> > resultant image and the mask. This means that only white pixels on
> > the
> > image which occur within the white areas of the mask are counted when
> > assessing the final match/no match.
> >
>
> At present we are not looking to immediately implement this idea, however
> feel free
> to attempt this idea in gst/gsttemplatematch.c/h and contribute it to the
> stb-tester
> repo:
https://github.com/drothlis/stb-tester/.
>
> If this is of interest or use to you, let us know so we can raise the
> priority of its implementation.
I also have increased interest in transparent templates recently so if
you could show your working that would be great. Even if it wasn't a
patch to stbt it would be interesting as a discussion point so we can
see more concretely the details of how first_pass_result and
confirmation thresholds are affected.
Thanks
Will