模板匹配和透明度

模板匹配和透明度

本文介绍了OpenCV 模板匹配和透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenCV 在模板匹配过程中处理图像透明度的方式是什么?

问题是模板图像需要有透明部分,因为在原始图像中,这些地方可能有任何东西.

我尝试了所有方法,但都没有产生积极的结果(例如未正确检测到原始图像中模板的位置).

解决方案

OpenCV 似乎并没有按照您希望的方式处理 alpha.

您有两个选择:

  1. 编写您自己的将使用 Alpha 通道的互相关方法
  2. 转换您的图像,使您的 Alpha 通道变得无关紧要

由于第一个选项很简单,我将在这里探讨第二个选项.我将重新使用我提供给.

附注.这是什么游戏?

What's the way OpenCV handles transparency in image during template matching?

The problem is that the template image needs to have transparent parts, because in the original image there could be anything at those places.

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).

解决方案

It doesn't seem like OpenCV handles alpha the way you want it to.

You have two options:

  1. Write your own cross-correlation method that will use the alpha channel
  2. Transform your images so your alpha channel becomes irrelevant

Since the first option is straightforward, I will explore the second option here. I'm going to re-use the sample code I provided to a similar question earlier. If you apply cross-correlation directly to your images, the background interferes with the template matching (in particular, light background parts). If you play around with color channels, you will find that matching in the blue channel gives the correct result. This depends on the image content and isn't a consistent way to solve the problem.

Another option is to perform edge detection (e.g. Sobel) on the image and template, and perform cross-correlation then. Here are the edge detected images (I used the Sobel edge detector on the Luma channel in GIMP, and then some intensity stretching).

As you can see, the alpha channel here has become irrelevant, as most of the terrain has become zero intensity and will not contribute to the cross-correlation calculation. So now cross-correlation can be directly applied, giving the desired result:

misha@misha-desktop:~/Desktop/stackoverflow$ python cross-correlation.py map-blue.png building-maskz-blue.png
(163, 244)

Finally, here's another related question.

PS. What game is this?

这篇关于OpenCV 模板匹配和透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 14:47