title: How to find a template in an image using a mask (or transparency) with OpenCV and Python?
body: <p>Let us assume we are looking for this template:</p>
  <p><a href="https://i.stack.imgur.com/PuaGa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PuaGa.png" alt="Stop" /></a></p>
  <p>The corners of our template are transparent, so the background will vary, like so:</p>
  <p><a href="https://i.stack.imgur.com/r40iL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r40iL.png" alt="Stop on moon" /></a></p>
  
  <p><a href="https://i.stack.imgur.com/yHtlb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yHtlb.png" alt="Stop on everest" /></a></p>
  
  <p><a href="https://i.stack.imgur.com/XMnKt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XMnKt.png" alt="Stop on leaves" /></a></p>
  
  <p>Assuming we could use the following mask with our template:</p>
  <p><a href="https://i.stack.imgur.com/PuaGa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PuaGa.png" alt="Stop" /></a>   <a href="https://i.stack.imgur.com/Ud5Wz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ud5Wz.png" alt="Stop mask" /></a></p>
  
  <p>It would be very easy to find it.</p>
  <p><strong>What I have tried:</strong></p>
  <p>I have tried <code>matchTemplate</code> but it doesn't support masks (as far as I know), and using the alpha channel (transparency) in the template does not achieve this, as it compares the alpha channels instead of ignoring those pixels.</p>
  <p>I have also looked into &quot;region of interest&quot;, which I thought would be the solution, but with it you can only specify a rectangular area. I'm not even sure if it works on the template or not.</p>
  <p>I'm sure this is possible to do by writing my own algorithm, but I was hoping this is possible via. standard OpenCV to avoid reinventing the wheel. Not to mention, it would most likely be more optimised than my own.</p>
  <p>So, how could I do something like this with OpenCV + Python?</p>