使用模板匹配查找类似的Opencv图像

2024-05-18 23:29:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Opencv中的模板匹配来查找相似的图像。到目前为止,我已经开始工作了,但我现在还不知道如何比较这些图像

我的想法是使用cv2.matchTemplate,然后使用阈值检查是否有任何部分匹配,并删除所有不匹配的部分。 但是,当我看一看产生类似图像的阵列时

(array([0], dtype=int64), array([0], dtype=int64))

作为输出

一个不同的形象出现了

(array([], dtype=int64), array([], dtype=int64))

因为我认为输出是完美的,没有匹配,所以空数组将是如何判断它们是不同的还是相同的。但是我使用了与模板相同的图像,这就是输出结果

(array([], dtype=int64), array([], dtype=int64))

我认为这应该与相似的图像相同

我的问题是如何确定哪些相似,哪些不相似? 这是我现在正在使用的代码

def matcher(template, img, match_type, threshold):
    # Match operation
    res = cv2.matchTemplate(img, template, match_type)
    # store coordinates of matched areas
    loc = np.where( res >= threshold)
    return loc

locsame = matcher(template, same, cv2.TM_SQDIFF, 0.8)    
loc1 = matcher(template, sim, cv2.TM_SQDIFF, 0.8)
loc2 = matcher(template, di, cv2.TM_CCOEFF_NORMED, 0.8)

Tags: 图像模板imgthresholdtypematchmatcherres

热门问题