在文件夹中寻找与例图最相似的图片

小伙伴们写论文时有没有把插图贴在文中,结果忘记保存文件名?在文件夹里找图片好痛苦啊。交给电脑找吧

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cv2
import numpy as np
import os
# 均值哈希算法
def aHash(img):
    # 缩放为8*8
    img = cv2.resize(img, (8, 8), interpolation=cv2.INTER_CUBIC)
    # 转换为灰度图
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # s为像素和初值为0,hash_str为hash值初值为''
    s = 0
    hash_str = ''
    # 遍历累加求像素和
    for i in range(8):
        for j in range(8):
            s = s + gray[i, j]
    # 求平均灰度
    avg = s / 64
    # 灰度大于平均值为1相反为0生成图片的hash值
    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):
    # 缩放8*8
    img = cv2.resize(img, (9, 8), interpolation=cv2.INTER_CUBIC)
    # 转换灰度图
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    hash_str = ''
    # 每行前一个像素大于后一个像素为1,相反为0,生成哈希
    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
# Hash值对比
def cmpHash(hash1, hash2):
    n = 0
    # hash长度不同则返回-1代表传参出错
    if len(hash1) != len(hash2):
        return -1
    # 遍历判断
    for i in range(len(hash1)):
        # 不相等则n计数+1,n最终为相似度
        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)
    # print(hash1)
    # print(hash2)
    # n = cmpHash(hash1, hash2)
    # print('均值哈希算法相似度:' + str(n))
    n =cmpHash(hash1, hash2)
    return n
    #
    # hash1 = dHash(img1)
    # hash2 = dHash(img2)
    # print(hash1)
    # print(hash2)
    # n = cmpHash(hash1, hash2)
    # print
    # '差值哈希算法相似度:' + str(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)
    # print(f_dir)
    score = found_f(f_dir,aim_photo)
    # print(scores[0][0],'scores[0][0]')
    if score <= scores[0][0]:
        del scores[0]
        scores.append([score,f_n])
        print(scores)

print('相似度 文件名',scores[0])


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值