-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathimage_detect_02.py
50 lines (41 loc) · 1.46 KB
/
image_detect_02.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import cv2
import numpy as np
from matplotlib import pyplot as plt
import argparse
def matchAB(fileA, fileB):
# 读取图像数据
imgA = cv2.imread(fileA)
imgB = cv2.imread(fileB)
# 转换成灰色
grayA = cv2.cvtColor(imgA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imgB, cv2.COLOR_BGR2GRAY)
# 获取图片A的大小
height, width = grayA.shape
# 取局部图像,寻找匹配位置
result_window = np.zeros((height, width), dtype=imgA.dtype)
for start_y in range(0, height-100, 10):
for start_x in range(0, width-100, 10):
window = grayA[start_y:start_y+100, start_x:start_x+100]
match = cv2.matchTemplate(grayB, window, cv2.TM_CCOEFF_NORMED)
_, _, _, max_loc = cv2.minMaxLoc(match)
matched_window = grayB[max_loc[1]:max_loc[1]+100, max_loc[0]:max_loc[0]+100]
result = cv2.absdiff(window, matched_window)
result_window[start_y:start_y+100, start_x:start_x+100] = result
plt.imshow(result_window)
plt.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--source_image',
type=str,
default='img/image01-0.png',
help='source image'
)
parser.add_argument(
'--target_image',
type=str,
default='img/image01-1.png',
help='target image'
)
FLAGS, unparsed = parser.parse_known_args()
matchAB(FLAGS.source_image, FLAGS.target_image)