Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 973b782

Browse files
committed
matchTemplate
1 parent 27f47fc commit 973b782

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

my08-opencv玩游戏game/连连看/compare_photos.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
lenY = 8 # 列
1919

2020

21-
def get_image_difference(image_1, image_2):#这个函数不行
21+
def get_image_difference(image_1, image_2): # 这个函数不行
2222
first_image_hist = cv2.calcHist([image_1], [0], None, [256], [0, 256])
2323
second_image_hist = cv2.calcHist([image_2], [0], None, [256], [0, 256])
2424

@@ -42,11 +42,15 @@ def compare(i, j, img):
4242
# 图片相似度
4343
y1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
4444
z1 = cv2.cvtColor(z, cv2.COLOR_BGR2GRAY)
45-
image_difference = get_image_difference(y1, z1)
46-
print(i, j, x, y, image_difference)
45+
# image_difference = get_image_difference(y1, z1)
46+
res = cv2.matchTemplate(z1, y1, cv2.TM_CCOEFF_NORMED)
47+
# print(i, j, x, y, image_difference)
48+
print(i, j, x, y, res)
4749
# if abs(image_difference-1)>0.5:
48-
if image_difference < 0.1:
49-
pairs.append((i, j, x, y, image_difference))
50+
# if image_difference < 0.1:
51+
# pairs.append((i, j, x, y, image_difference))
52+
if res[0][0] > 0.9 and (i != x and j != y):
53+
pairs.append((i, j, x, y, res[0][0]))
5054
print('--------')
5155

5256

@@ -55,14 +59,14 @@ def compare(i, j, img):
5559
compare(i, j, y)
5660

5761
print('--------')
58-
# print(pairs)
59-
60-
62+
print(pairs)#33对?
63+
#[(0, 0, 5, 3, 0.93773538), (0, 3, 2, 6, 0.94418496), (0, 3, 3, 4, 0.97784418), (0, 3, 5, 6, 0.91531861), (0, 3, 7, 4, 0.90034771), (0, 4, 1, 5, 0.9859665), (0, 4, 2, 5, 0.97593749), (0, 4, 5, 7, 0.92510819), (1, 0, 2, 4, 0.95577663), (1, 0, 2, 5, 0.93438679), (1, 0, 3, 2, 0.98244762), (1, 0, 5, 1, 0.95950162), (1, 0, 5, 7, 0.9012484), (1, 0, 7, 3, 0.91213149), (1, 2, 4, 3, 0.97030866), (1, 3, 7, 5, 0.90350145), (1, 4, 3, 5, 0.92840946), (1, 4, 3, 6, 0.92976296), (1, 5, 5, 7, 0.93449652), (2, 1, 6, 4, 0.91386253), (2, 4, 5, 7, 0.95080549), (2, 5, 5, 7, 0.95228308), (3, 2, 5, 7, 0.94026983), (3, 2, 7, 3, 0.95594138), (3, 4, 5, 6, 0.96190572), (3, 4, 8, 7, 0.90763825), (4, 1, 7, 2, 0.95110172), (4, 3, 6, 6, 0.95759535), (5, 1, 7, 3, 0.97735828), (5, 2, 8, 3, 0.96606308), (5, 6, 8, 7, 0.92764288), (6, 0, 7, 3, 0.96886152), (7, 4, 8, 7, 0.93397516)]
6164

6265
# 1, 0, 1, 5
6366
a = mat[1][0]
6467
b = mat[1][5]
6568
y1 = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
6669
z1 = cv2.cvtColor(b, cv2.COLOR_BGR2GRAY)
67-
image_difference = get_image_difference(y1, z1)
68-
print(1, 0, 1, 5, image_difference)
70+
# image_difference = get_image_difference(y1, z1)
71+
res = cv2.matchTemplate(z1, y1, cv2.TM_CCOEFF_NORMED)
72+
print(1, 0, 1, 5, res)

0 commit comments

Comments
 (0)