Interface編集部
10-1MatchTemplate_gpu.py
000: import cv2
001: import numpy as np
002:
003: def __main():
004: gpuImg = cv2.cuda_GpuMat()
005: gpuTemle = cv2.cuda_GpuMat()
006:
007: img = cv2.imread(‘IMG_1630_2.jpg’)
008: temple = cv2.imread(‘IMG_1630_2_T.jpg’)
009: h, w, _ = temple.shape
010:
011: gpuImg.upload(img)
012: gpuTemle.upload(temple)
013:
014: matcher = cv2.cuda.createTemplateMatching(srcType=cv2.CV_8UC3, method=cv2.TM_CCOEFF)
015: cuResult = matcher.match(gpuImg, gpuTemle)
016: result = cuResult.download()
017:
018: # 配列内のグローバルな最小値と最大値を見つける minLoc=最も類似する座標 maxLoc=最も類似しない座標
019: minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result)
020: x, y = maxLoc
021: color = np.array([0., 255., 255.]) # BGR表記
022: cv2.rectangle(img=img, pt1=(x, y), pt2=(x + w, y + h), color=color, thickness=3)
023:
024: cv2.imshow(‘Final result’, img)
025: cv2.imshow(‘Template’, temple)
026: cv2.waitKey(0)
027: cv2.destroyAllWindows()
028:
029: return 0
030:
031: if __name__ == ‘__main__’:
032: print(cv2.__version__)
033:
034: __main()