小伙伴们写论文时有没有把插图贴在文中,结果忘记保存文件名?在文件夹里找图片好痛苦啊。交给电脑找吧
import cv2
import numpy as np
import os
def aHash(img):
img = cv2.resize(img, (8, 8), interpolation=cv2.INTER_CUBIC)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
s = 0
hash_str = ''
for i in range(8):
for j in range(8):
s = s + gray[i, j]
avg = s / 64
for i in range(8):
for j in range(8):
if gray[i, j] > avg:
hash_str = hash_str + '1'
else:
hash_str = hash_str + '0'
return hash_str
def dHash(img):
img = cv2.resize(img, (9, 8), interpolation=cv2.INTER_CUBIC)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hash_str = ''
for i in range(8):
for j in range(8):
if gray[i, j] > gray[i, j + 1]:
hash_str = hash_str + '1'
else:
hash_str = hash_str + '0'
return hash_str
def cmpHash(hash1, hash2):
n = 0
if len(hash1) != len(hash2):
return -1
for i in range(len(hash1)):
if hash1[i] != hash2[i]:
n = n + 1
return n
def found_f(path_1,path2):
img1 = cv2.imread(path_1)
img2 = cv2.imread(path2)
img1 = cv2.resize(img1,(100,100))
img2 = cv2.resize(img2,(100,100))
hash1 = aHash(img1)
hash2 = aHash(img2)
n =cmpHash(hash1, hash2)
return n
folder_to_search = r'F:\3. The cropped image tiles and raster labels\test\image'
aim_photo = r'E:\temporary\7.tif'
scores =[[100,'name']]
f_ns = os.listdir(folder_to_search)
for f_n in f_ns:
f_dir = os.path.join(folder_to_search,f_n)
score = found_f(f_dir,aim_photo)
if score <= scores[0][0]:
del scores[0]
scores.append([score,f_n])
print(scores)
print('相似度 文件名',scores[0])