10-1MultiMatchTemplate.py

000: import cv2
001: import numpy as np
002:
003: def __main():
004: img = cv2.imread(“IMG_1618_2.JPG”)
005: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
006: temple = cv2.imread(“IMG_1621_T.jpg”)
007: temple = cv2.cvtColor(temple, cv2.COLOR_BGR2GRAY)
008: h1, w1 = temple.shape
009:
010: # ここから処理を実行する
011: result = cv2.matchTemplate(gray, temple, cv2.TM_CCOEFF_NORMED)
012:
013: threshold = 0.8
014: location = np.where(result >= threshold)
015: for x, y in zip(*location[::-1]):
016: cv2.rectangle(img, pt1=(x, y), pt2=(x + w1, y + h1), color=(0, 255, 255), thickness=1)
017:
018: cv2.imshow(‘res.png’, img)
019: cv2.imshow(‘template’, temple)
020: cv2.waitKey(0)
021: cv2.destroyAllWindows()
022: if __name__ == ‘__main__’:
023: print(cv2.__version__)
024:
025: __main()