-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatch_32.py
108 lines (97 loc) · 3.51 KB
/
match_32.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import pandas as pd
import numpy as np
import cv2
from scipy.stats import pearsonr
import heapq
import matplotlib.pyplot as plt
# 使用matplotlib展示多张图片
def matplotlib_multi_pic1():
# 先在第一行显示原图
img = cv2.imread('D:/file/image.vary.jpg/%s.jpg'%i)
title = "%s.jpg"%i
plt.subplot(3, 3, 2)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 因为OpenCV和matplotlib中的颜色通道顺序不一样
plt.imshow(img)
plt.title(title, fontsize=8)
plt.xticks([]) # 去除坐标
plt.yticks([])
# 再显示与之最相近的五张图片
j=3
for k in max_pearson_index_list:
img = cv2.imread('D:/file/image.vary.jpg/%s.jpg'%k)
title="%s.jpg"%k
# 行,列,索引
j=j+1
plt.subplot(3,3,j)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.title(title,fontsize=8)
plt.xticks([])
plt.yticks([])
plt.show()
def matplotlib_multi_pic2():
# 先在第一行显示原图
img = cv2.imread('D:/file/image.vary.jpg/%s.jpg'%i)
title = "%s.jpg"%i
plt.subplot(11, 10, 5)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 因为OpenCV和matplotlib中的颜色通道顺序不一样
plt.imshow(img)
plt.title(title, fontsize=8)
plt.xticks([]) # 去除坐标
plt.yticks([])
# 再显示与之最相近的100张图片
j=10
for k in max_pearson_index_list:
img = cv2.imread('D:/file/image.vary.jpg/%s.jpg'%k)
title="%s.jpg"%k
# 行,列,索引
j=j+1
plt.subplot(11,10,j)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.title(title,fontsize=8)
plt.xticks([])
plt.yticks([])
plt.show()
i = input("请输入要比对的图片序号:")
img = cv2.imread('D:/file/image.vary.jpg/%s.jpg'%i)
histB = cv2.calcHist([img], [0], None, [32], [0.0, 255.0])
histG = cv2.calcHist([img], [1], None, [32], [0.0, 255.0])
histR = cv2.calcHist([img], [2], None, [32], [0.0, 255.0])
arr = [0 for x in range(0,96)]
for j in range(0,32):
arr[j]=histB[j][0]
for j in range(32,64):
arr[j]=histG[j-32][0]
for j in range(64,96):
arr[j]=histR[j-64][0]
test = pd.read_excel('example32.xls',header=None)
# 对输入的图片与所有图片进行比较,将皮尔逊系数存在数组中
pearson_max = []
for j in range(0,9908):
print(j)
strDb = test.loc[[j + 1], [1]]
strDb = np.array(strDb)
strDb = strDb.tolist()
a = strDb[0][0]
arrDb = a.split(',') # 字符串转char型数组
histDb = list(map(float, arrDb)) # char型数组转float型
pearson_max.append(pearsonr(arr, histDb)[0])
max_pearson_index_list = map(pearson_max.index, heapq.nlargest(5, pearson_max))
max_pearson_index_list=list(max_pearson_index_list) # map()生成的不是list,直接print不出来
print(max_pearson_index_list)
print("与输入图片最相近的5张图片为:") # 9804、2426、1604、4982
for j in range(0,5):
x = max_pearson_index_list[j]
print("%s.jpg" %x)
print(pearson_max[x])
matplotlib_multi_pic1()
max_pearson_index_list = map(pearson_max.index, heapq.nlargest(100, pearson_max))
max_pearson_index_list=list(max_pearson_index_list)
print(max_pearson_index_list)
print("与输入图片%s.jpg最相近的100张图片为:" %i) # 1793、6959
for j in range(0,100):
x = max_pearson_index_list[j]
print("%s.jpg" %x)
print(pearson_max[x])
matplotlib_multi_pic2()