2017-02-17 57 views
-1

我想匹配來自我的a.jpg和b.jpg的圖片。OpenCV - 如何在Python中掩蓋匹配圖片?

但是有一些地方我不想匹配。

我應該如何掩飾它?

(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

+0

你可以上傳兩張圖片以及 –

+0

@JeruLuke,編輯! – eason

+0

首先嚐試'cv2.matchTemplate()'然後執行屏蔽 –

回答

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