Let us assume we are looking for this template:

[![Stop](https://i.stack.imgur.com/PuaGa.png)](https://i.stack.imgur.com/PuaGa.png)

The corners of our template are transparent, so the background will vary, like so:

[![Stop on moon](https://i.stack.imgur.com/r40iL.png)](https://i.stack.imgur.com/r40iL.png)

[![Stop on everest](https://i.stack.imgur.com/yHtlb.png)](https://i.stack.imgur.com/yHtlb.png)

[![Stop on leaves](https://i.stack.imgur.com/XMnKt.png)](https://i.stack.imgur.com/XMnKt.png)

Assuming we could use the following mask with our template:

[![Stop](https://i.stack.imgur.com/PuaGa.png)](https://i.stack.imgur.com/PuaGa.png) [![Stop mask](https://i.stack.imgur.com/Ud5Wz.png)](https://i.stack.imgur.com/Ud5Wz.png)

It would be very easy to find it.

**What I have tried:**

I have tried `matchTemplate` 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.

I have also looked into "region of interest", 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.

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.

So, how could I do something like this with OpenCV + Python?