Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenCV template matching and transparency
    text
    copied!<p>What's the way OpenCV handles transparency in image during template matching?</p> <p>The problem is that the template image needs to have transparent parts, because in the original image there could be anything at those places.</p> <p>I tried all of the methods, and none of them yielded positive results (e.g. position of template in original image wasn't detected correctly).</p> <p><strong>Edit:</strong> OK, I see that is necessary to provide examples.</p> <p><a href="http://img35.imageshack.us/img35/3829/imageau.png" rel="noreferrer"><strong>Original image</strong></a></p> <p><a href="http://img151.imageshack.us/img151/6174/maskz.png" rel="noreferrer"><strong>Template image</strong></a></p> <p>As you can see, it's nearly impossible to match such template to image like that. The "background" around the object could have any color (like this, or white, brown...)</p> <p><a href="http://img46.imageshack.us/img46/778/sobel2.png" rel="noreferrer"><strong>Sobel on grayscaled image &amp; template + cvConvertScaleAbs</strong></a></p> <p><a href="http://img411.imageshack.us/img411/8463/image3cc.png" rel="noreferrer"><strong>Additional original image</strong></a></p> <p><strong>Edit 2:</strong> misha's solution works even with a bit of obstacles around (the "transparency" works). Example:</p> <p><a href="http://img220.imageshack.us/img220/1717/matchtemplatesuccesful.png" rel="noreferrer"><strong>Template match sucessful</strong></a></p> <p><strong>Edit 3 - multiple occurences:</strong></p> <p>I've made a quick and dirty solution of finding multiple occurences of a template, however when template is not found I get a "lot" of false positives. Mainly because of my implementation:</p> <ul> <li>iterate over image data</li> <li>if (imageData[y, x, 0] >= maxValue * 0.95f) then it counts [x,y] as a match (maxValue is from cvMinMaxLoc)</li> </ul> <p>It <a href="http://img94.imageshack.us/img94/1664/multipletemplatesok.png" rel="noreferrer">works</a> for images, when there's at least one positive match, however results in awful situation for images, on which there <a href="http://img441.imageshack.us/img441/9446/multipletemplatesbad.png" rel="noreferrer">isn't such template</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload