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