使用数据第一个是有1000张图像的自然图像数据集
第二个是牛津大学建筑数据集
"""
@File : 颜色直方图图像检索.py
@Time : 2021-04-18 10:56
@Author : XD
@Email : gudianpai@qq.com
@Software: PyCharm
"""
from tqdm import tqdm
import glob
from sklearn.preprocessing import normalize
import matplotlib.pyplot as plt
import numpy as np
import cv2
def show_img(ori_path, aim_path):
plt.figure(figsize=(20, 10))
img = cv2.imread(ori_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.subplot(3, 5, 1)
plt.imshow(img)
plt.xticks([])
plt.yticks([])
plt.title('Original Image')
for idx in range(10):
img = cv2.imread(aim_path[idx])
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.subplot(3, 5, 6 + idx)
plt.imshow(img)
plt.xticks([])
plt.yticks([])
plt.show()
def main():
jpgs = glob.glob("./ox/*.jpg")
hist_feat = []
for path in tqdm(jpgs):
img = cv2.imread(path)
feat = cv2.calcHist([img], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
hist_feat.append(feat.flatten())
hist_feat = np.vstack(hist_feat)
hist_feat = normalize(hist_feat)
search_idx = 777
ids = np.dot(hist_feat[search_idx], hist_feat.T)
ids = np.argsort(ids)[::-1][1:]
show_img(jpgs[search_idx], [jpgs[x] for x in ids[:10]])
if __name__ == '__main__':
main()