這裏是鏈接,我覺得 How do I find an image contained within an image如何在圖像之間進行完美匹配?
而我得到的代碼在上面的鏈接。
這裏是我的代碼
import re
import os
import time
import cv2
from cv2 import cv
METHOD = cv.CV_TM_SQDIFF_NORMED
ROOTDIR = 'GeneralIMG/'
# Load The Deck Img
LARGE_IMAGE = cv2.imread('deck.bmp')
for subdir, dirs, files in os.walk(ROOTDIR):
for myfile in files:
filepath = os.path.join(subdir, myfile)
# Read the images from the file
# small_image = cv2.imread(myfile)
# small_image = cv2.imread('GeneralIMG/cfrB.bmp')
Ffile = subdir + myfile
small_image = cv2.imread(Ffile)
result = cv2.matchTemplate(small_image, LARGE_IMAGE, METHOD)
# We want the minimum squared difference
mn,_,mnLoc,_ = cv2.minMaxLoc(result)
MPx,MPy = mnLoc
if MPx == 0 and MPy == 0:
print 'Match failure!'
else:
print MPx, MPy
print re.sub(r"B|D", "", os.path.splitext(myfile)[0])
# Draw the rectangle:
# Extract the coordinates of our best match
MPx,MPy = mnLoc
# Step 2: Get the size of the template. This is the same size as the match.
trows,tcols = small_image.shape[:2]
# Step 3: Draw the rectangle on LARGE_IMAGE
cv2.rectangle(LARGE_IMAGE, (MPx,MPy),(MPx+tcols,MPy+trows),(0,0,255),2)
# Display the original image with the rectangle around the match.
cv2.imshow('output',LARGE_IMAGE)
# The image is only displayed if we call this
cv2.waitKey(0)
# time.sleep(5)
這是小圖片我想要的大圖像,但是大圖像不包含小圖像中找到。令我驚訝的是matchTemplate仍然得到了結果。
12~07_00-03-03.bmp http://7xpvdr.com1.z0.glb.clouddn.com/12~07_00-03-03.bmp
但這場比賽的結果我得到的是不是像下面的PIC不夠準確。 紅色矩形是匹配結果。
12~07_00-01-58.bmp http://7xpvdr.com1.z0.glb.clouddn.com/12~07_00-01-58.bmp
在這裏我要找到確切的一個大相同照片。如果不包含它,只要返回一個false就沒關係。我不想模糊搜索,我該怎麼辦?
--- ---更新
這是小IMG:
這是原來的較大bmp圖象
當圖像被上傳的格式將被更改。小img是png文件,更大的是bmp。
事情是我不知道匹配模板是否使用模糊搜索。所以這裏的問題爲什麼我得到錯誤的搜索結果似乎是合理的。我想知道如何準確地搜索。 THX〜
更新:
我檢查的[matchtemplate]
的doc和我覺得這意味着它是一個最好的匹配搜索不完美匹配。
那麼我怎樣才能使它完美匹配?
什麼是模板匹配? 模板匹配是一種用於查找匹配(與模板圖像相似)圖像區域的技術(補丁)。
我不相信你的最終圖像中有確切的子圖像 - 我相信你可能已經將該文件保存爲JPEG或在發佈之前做了其他事情 - 事實上,你必須編輯它以繪製紅色框。記住JPEG文件不準確 - 它們是有損的,所以只需打開並重新保存JPEG就可能將內容更改爲不再是原件的完美副本。你有**更大圖像的**原始**嗎? –
@MarkSetchell結果顯示找到了subimg,並給出了它在較大圖像中的確切座標。我不知道匹配模板在這裏使用模糊搜索而不是精確搜索。 – Lee