AIHIA梦工厂

标题: CV系列之用图像相似度去重 [打印本页]

作者: dongguazi    时间: 2023-9-8 03:46 PM
标题: CV系列之用图像相似度去重
本帖最后由 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)
复制代码



作者: blacksheepx    时间: 2023-9-11 02:44 PM
除了计算哈希值,也可以通过一些网络模型提取特征值来进行比对,相似度还可以用余弦相似度,计算向量内积就可以




欢迎光临 AIHIA梦工厂 (https://aihiamgc.com/) Powered by Discuz! X3.5