我想找到这个背景透明的图标
在这幅图中
我尝试了以下代码:
import cv2
method = cv2.TM_SQDIFF_NORMED
small_image = cv2.imread('icons/new/test.png')
large_image = cv2.imread('icons/example.png')
result = cv2.matchTemplate(small_image, large_image, method)
mn,_,mnLoc,_ = cv2.minMaxLoc(result)
MPx,MPy = mnLoc
trows,tcols = small_image.shape[:2]
cv2.rectangle(large_image, (MPx,MPy),(MPx+tcols,MPy+trows),(0,0,255),2)
cv2.imshow('output',large_image)
cv2.waitKey(0)
但是,结果如下:
我想这是因为我的小图像有透明的背景。我怎样才能解决这个问题
请注意,^{} 有一个
mask
参数,您可以在其中为模板设置掩码,该掩码将是模板的alpha通道(小图像)。此外,请记住,按以下顺序馈送图像(大图像)和模板(小图像):要保留模板的alpha通道(小图像),请使用^{} 中的^{} 标志:
通过这两个更改,我获得了所需的输出:
相关问题 更多 >
编程相关推荐