Skip to content

Commit 27f47fc

Browse files
committed
compare_photos
1 parent 1224820 commit 27f47fc

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2018/3/13 08:30
3+
# @Author : play4fun
4+
# @File : compare_photos.py
5+
# @Software: PyCharm
6+
7+
"""
8+
compare_photos.py:
9+
"""
10+
11+
import cv2, pickle
12+
13+
with open('photo_mat', 'rb') as f:
14+
mat = pickle.load(f)
15+
16+
pairs = [] # 配对好的
17+
lenX = 9 # 行
18+
lenY = 8 # 列
19+
20+
21+
def get_image_difference(image_1, image_2):#这个函数不行
22+
first_image_hist = cv2.calcHist([image_1], [0], None, [256], [0, 256])
23+
second_image_hist = cv2.calcHist([image_2], [0], None, [256], [0, 256])
24+
25+
img_hist_diff = cv2.compareHist(first_image_hist, second_image_hist, cv2.HISTCMP_BHATTACHARYYA)
26+
img_template_probability_match = cv2.matchTemplate(first_image_hist, second_image_hist, cv2.TM_CCOEFF_NORMED)[0][0]
27+
img_template_diff = 1 - img_template_probability_match
28+
29+
# taking only 10% of histogram diff, since it's less accurate than template method
30+
commutative_image_diff = (img_hist_diff / 10) + img_template_diff
31+
return commutative_image_diff
32+
33+
34+
def compare(i, j, img):
35+
for x in range(lenX):
36+
if x < i:
37+
continue
38+
for y in range(lenY):
39+
if y < j:
40+
continue
41+
z = mat[x][y]
42+
# 图片相似度
43+
y1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
44+
z1 = cv2.cvtColor(z, cv2.COLOR_BGR2GRAY)
45+
image_difference = get_image_difference(y1, z1)
46+
print(i, j, x, y, image_difference)
47+
# if abs(image_difference-1)>0.5:
48+
if image_difference < 0.1:
49+
pairs.append((i, j, x, y, image_difference))
50+
print('--------')
51+
52+
53+
for i, x in enumerate(mat):
54+
for j, y in enumerate(x):
55+
compare(i, j, y)
56+
57+
print('--------')
58+
# print(pairs)
59+
60+
61+
62+
# 1, 0, 1, 5
63+
a = mat[1][0]
64+
b = mat[1][5]
65+
y1 = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
66+
z1 = cv2.cvtColor(b, cv2.COLOR_BGR2GRAY)
67+
image_difference = get_image_difference(y1, z1)
68+
print(1, 0, 1, 5, image_difference)

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,27 @@
2626
y1 = 0
2727
xp = int(height / 9)
2828
yp = int(width / 8)
29+
mat=[]
2930
for x2 in range(xp, height, xp):
31+
pl=[]
3032
for y2 in range(yp, width, yp):
3133
cut = img[x1:x2, y1:y2]
3234
cv2.imshow('cut', cut)
3335
cv2.waitKey(10)
3436

3537
y1 = y2
36-
cv2.waitKey(1000)
38+
#
39+
pl.append(cut)
40+
cv2.waitKey(100)
3741
y1 = 0
3842
x1 = x2
43+
#
44+
mat.append(pl)
3945

4046
cv2.waitKey(0)
4147
cv2.destroyAllWindows()
48+
49+
#
50+
import pickle
51+
with open('photo_mat','wb') as f:
52+
pickle.dump(mat,f)
1010 KB
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2018/3/13 08:26
3+
# @Author : play4fun
4+
# @File : show_photos.py
5+
# @Software: PyCharm
6+
7+
"""
8+
show_photos.py:
9+
"""
10+
11+
import cv2,pickle
12+
13+
with open('photo_mat','rb') as f:
14+
mat=pickle.load(f)
15+
16+
for x in mat:
17+
for y in x:
18+
cv2.imshow('mat',y)
19+
cv2.waitKey(10)
20+
cv2.waitKey(100)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy