返回顶部

[学习经验] CV系列之用图像相似度去重

[复制链接]
dongguaziLv.1 显示全部楼层 发表于 2023-9-8 15:46:59 |阅读模式 打印 上一主题 下一主题

马上注册,享用更多功能,让你轻松玩转AIHIA梦工厂!

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Andrew 于 2023-9-14 01:59 PM 编辑

当我们处理一批图片时,这批图片可能会有重复的图片,下面分享一个具备图片相似度去重功能的脚本,欢迎大家对该脚本进行改进。

  1. import os
  2. import cv2
  3. from PIL import Image
  4. import imagehash
  5. import shutil
  6. import numpy as np
  7. from tqdm import tqdm

  8. def calculate_phash(image):
  9.     # 使用感知哈希算法计算图像的哈希值
  10.     pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
  11.     phash = imagehash.phash(pil_image)
  12.     return str(phash)

  13. def calculate_mse(image1, image2):
  14.     # 计算均方误差
  15.     mse = ((image1 - image2) ** 2).mean()
  16.     return mse

  17. def move_similar_images(source_folder, destination_folder, near_num=10, mse_thred = 100):
  18.     # 获取源文件夹中所有图片的路径
  19.     image_paths = [os.path.join(source_folder, image_name) for image_name in os.listdir(source_folder)]

  20.     # 按照图片名称排序
  21.     image_paths.sort()

  22.     # 遍历图片列表,判断相邻的图片是否相似
  23.     img_num = len(image_paths)-near_num
  24.     progress_bar = tqdm(total=img_num, unit='files', desc='processing files')
  25.     for i in range(len(image_paths)-near_num):
  26.         progress_bar.set_postfix(file=i)
  27.         progress_bar.update(1)

  28.         # current_image = cv2.imread(image_paths)
  29.         current_image = cv2.imdecode(np.fromfile(image_paths[i], dtype=np.uint8), -1)

  30.         # 比较当前图像和后续 near_num 张图像
  31.         for j in range(i+1, i+near_num+1):
  32.             # next_image = cv2.imread(image_paths[j])
  33.             next_image = cv2.imdecode(np.fromfile(image_paths[j], dtype=np.uint8), -1)

  34.             #
  35.             if current_image.shape != next_image.shape:
  36.                 continue      

  37.             # # 计算当前图像和下一张图像的哈希值,如果相同,移动
  38.             # current_hash = calculate_phash(current_image)
  39.             # next_hash = calculate_phash(next_image)
  40.             # if current_hash == next_hash:
  41.             #     destination_path = os.path.join(destination_folder, os.path.basename(image_paths[i]))
  42.             #     shutil.move(image_paths[i], destination_path)
  43.             #     break
  44.         
  45.             # 计算mse相似度,如果低于阈值,移动
  46.             try:
  47.                 mse = calculate_mse(current_image, next_image)
  48.             except IOError as e:
  49.                 print('erro:', e)            
  50.             if mse < mse_thred:
  51.                 destination_path = os.path.join(destination_folder, os.path.basename(image_paths[i]))
  52.                 shutil.move(image_paths[i], destination_path)
  53.                 break

  54.     progress_bar.close()   


  55. if __name__ == '__main__':
  56.     # 源文件夹和目标文件夹的路径
  57.     src_dir = r'F:\data\xxx'
  58.     des_dir = src_dir + '_chongfu'
  59.     if not os.path.exists(des_dir):
  60.         os.makedirs(des_dir)

  61.     # 移动相似图片到目标文件夹
  62.     move_similar_images(src_dir, des_dir, near_num=20, mse_thred = 70)
复制代码


AIHIA梦工厂,共建AI人脉圈,共享AI时代美好生活!
回复

使用道具 举报

精彩评论1

blacksheepxLv.1 显示全部楼层 发表于 2023-9-11 14:44:55
除了计算哈希值,也可以通过一些网络模型提取特征值来进行比对,相似度还可以用余弦相似度,计算向量内积就可以
又不是不能用
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

AIHIA梦工厂旨在建立涵盖广泛人工智能行业,包括AI芯片、AI工业应用、AI电商、AI自动驾驶、AI智慧城市、智慧农业等人工智能应用领域。梦工厂为每位AI人提供技术交流、需求对接、行业资源、招聘求职、人脉拓展等多个方面交流学习平台促进人工智能的发展和应用。
  • 官方手机版

  • 联盟公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2023, AIHIA梦工厂
  • 苏ICP备2023025400号-1 | 苏公网安备32021402002407 | 电信增值许可证:苏B2-20231396 | 无锡腾云驾数技术服务有限公司 QQ