zoukankan      html  css  js  c++  java
  • 图片去重

    需求分析

    针对海量图片去重通常是如下思路

    1. 根据某种方法提取图片特征或者说指纹
    2. 根据提取出来的特征计算图片间的距离
    3. 根据距离调整阈值判断图片是否相似

    1、提取指纹

    常用的有如下方法:

    1. ahash
    2. phash
    3. dhash

    ahash

    1. 将图片灰度
    2. 将图片resize为(w, h)大小
    3. 取图片均值mean
    4. 将图片中每个像素值与mean比较,大于为1否则为0
    5. 得(w, h)大小的图片指纹

    phash

    1. 将图片灰度
    2. 对灰度后的图片取DCT变换
    3. 对DCT变换后的图片取左上角(w, h)大小的区域,即为新的图片
    4. 对图取mean
    5. 将图中每个元素与mean比较,大于为1否则为0
    6. 得(w, h)大小的图片指纹

    dhash

    1. 将图片resize成(w, h+1, h)大小
    2. 将图片灰度
    3. 对于图像中的每一行,相邻的两个元素后面的减去前面的元素,若大于则为1否则为0
    4. 得到(w, h)大小的图片指纹

    二、实现代码如下:

    import os
    import traceback
    from PIL import Image
    import numpy as np
    from scipy.fftpack import dct
    from scipy.spatial.distance import pdist
    
    
    class ImgHash():
        def __init__(self, path, hash_size=(8, 8)):
            self.hash_size = hash_size
            self.img = np.asarray(Image.open(path))
            self.gray = self.img2gray(self.img)
    
        @staticmethod
        def img2gray(img):
            r = img[:, :, 0]
            g = img[:, :, 1]
            b = img[:, :, 2]
            gray = 0.2989*r + 0.5807*g + 0.1140*b
            return gray
    
        def ahash(self):
            img = np.resize(self.gray, new_shape=self.hash_size)
            mean = np.mean(img)
            hashed = (img > mean).astype(int)
            return hashed
    
        def phash(self):
            img = dct(x=self.gray)[:self.hash_size[0], :self.hash_size[1]]
            mean = np.mean(img)
            hashed = (img > mean).astype(int)
            return hashed
    
        def dhash(self):
            new_shape = (self.hash_size[0], self.hash_size[1]+1, self.img.shape[-1])
            img = np.resize(self.img, new_shape)
            img = self.img2gray(img)
            hashed = (img[:, 1:] - img[:, :new_shape[1]-1] > 0).astype(int)
            return hashed
    

    参考:

  • 相关阅读:
    MORMOT数据库连接池
    TOleDBMSSQLConnectionProperties驱动MSSQL数据库
    mORMot访问远程数据库
    mormot 直接使用UNIDAC引擎操作数据库
    mormot 数据集和JSON互相转换
    Go -- 读取文件内容
    nginx -- 启动, 重启, 关闭
    JS -- 一篇文章掌握RequireJS常用知识
    用JS获取地址栏参数的方法(超级简单)
    git -- 忽略某个文件
  • 原文地址:https://www.cnblogs.com/Fosen/p/12609350.html
Copyright © 2011-2022 走看看