OpenCV - 如何在Python中屏蔽匹配图片?

时间:2017-02-17 00:37:37

标签: python opencv image-processing

我想匹配来自我的a.jpg和b.jpg的照片。

但是有一些我不想匹配的领域。

我应该如何掩饰它?

(PS:如果将这些区域转移到黑色,则会影响cv2.matchTemplate

import cv2
import numpy as np
img1 = cv2.imread("a.jpg")
img2 = cv2.imread("b.jpg")
myROI = img2[183:374,293:408]
# here I want to mask a part of myROI .. 
# It means that I don't want to match something in my picture...
# How should I do ?
res = cv2.matchTemplate(img1,myROI,method= eval('cv2.TM_CCOEFF_NORMED'))

min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
print max_val*100 # score

A.JPG enter image description here

B.JPG

enter image description here

1 个答案:

答案 0 :(得分:0)

# -*- coding: utf-8 -*-

import cv2
import numpy as np

img1 = cv2.imread("a.jpg")
img2 = cv2.imread("b.jpg")
mymask = cv2.imread("mask.jpg") # mask shape must = template

#  mask only run in method = CV_TM_SQDIFF and CV_TM_CCORR_NORMED.
me= eval('cv2.TM_CCORR_NORMED')

res1 = cv2.matchTemplate(img1,img2,method= me, mask = mymask)
res2 = cv2.matchTemplate(img1,img2,method= eval('cv2.TM_CCORR_NORMED'))

min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res1)
print max_val*100 # score 1 -> 62

min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res2)
print max_val*100 # score 2 -> 99 

https://i.stack.imgur.com/TsqA9.jpg

b https://i.stack.imgur.com/6BemF.jpg

掩码https://i.stack.imgur.com/T2DZI.jpg