OpenCV模板匹配可将所有内容与一种颜色的参考图像进行匹配

时间:2019-05-08 13:44:01

标签: python image opencv

我要在屏幕截图上匹配参考图像。它是屏幕截图的一部分,因此必须绝对匹配。

要查找的图像:> needleImage << / p>

完整屏幕截图:

haystackImage

因此,使用OpenCV pyscreeze可以匹配以下图像:

# get all matches at once, credit: https://stackoverflow.com/questions/7670112/finding-a-subimage-inside-a-numpy-image/9253805#9253805
result = cv2.matchTemplate(haystackImage, needleImage, cv2.TM_CCOEFF_NORMED)
match_indices = numpy.arange(result.size)[(result > confidence).flatten()]
matches = numpy.unravel_index(match_indices[:limit], result.shape)

一些测试

opencv + numpy confidence 1.0: 0 matches
first ten: []
Bench passed in 0.205 seconds

opencv + numpy confidence default (0.999): 1795428 matches
first ten: [(0, 0, 13, 13), (1, 0, 13, 13), (2, 0, 13, 13), (3, 0, 13, 13), (4, 0, 13, 13), (5, 0, 13, 13), (6, 0, 13, 13), (7, 0, 13, 13), (8, 0, 13, 13), (9, 0, 13, 13)]
Bench passed in 0.852 seconds

pure python method: 6690 matches
first ten: [(1855, 208, 13, 13), (1856, 208, 13, 13), (1857, 208, 13, 13), (1858, 208, 13, 13), (1859, 208, 13, 13), (1860, 208, 13, 13), (1861, 208, 13, 13), (1862, 208, 13, 13), (1863, 208, 13, 13), (1864, 208, 13, 13)]

Bench passed in 1.344 seconds

我打的很自信,匹配方法也一样,但是使用OpenCV的结果是一样的。它与每个像素都匹配,或者与所有像素都不匹配。

pyscreeze中的纯python方法给出了最正确的结果-它正确匹配了图像。

也许我不需要像OpenCV这样的东西来匹配屏幕截图的一部分?但是纯python慢​​5到10倍。

0 个答案:

没有答案