感知哈希算法原理与实现

今天忽然想做一个图像识别的APP,但是在两张图片相似度的问题上产生了问题,感知哈希算法并不能解决这个问题,只是我在试着解决问题的过程中学到的一点知识。

这里的关键技术叫做”感知哈希算法”(Perceptual hash algorithm),它的作用是对每张图片生成一个”指纹”(fingerprint)字符串,然后比较不同图片的指纹。结果越接近,就说明图片越相似。

下面是一个最简单的实现:

  1. 第一步,缩小尺寸。
    将图片缩小到8x8的尺寸,总共64个像素。这一步的作用是去除图片的细节,只保留结构、明暗等基本信息,摒弃不同尺寸、比例带来的图片差异。

  2. 第二步,简化色彩。
    将缩小后的图片,转为64级灰度。也就是说,所有像素点总共只有64种颜色。

  3. 第三步,计算平均值。
    计算所有64个像素的灰度平均值。

  4. 第四步,比较像素的灰度。
    将每个像素的灰度,与平均值进行比较。大于或等于平均值,记为1;小于平均值,记为0。

  5. 第五步,计算哈希值。
    将上一步的比较结果,组合在一起,就构成了一个64位的整数,这就是这张图片的指纹。组合的次序并不重要,只要保证所有图片都采用同样次序就行了。

得到指纹以后,就可以对比不同的图片,看看64位中有多少位是不一样的。在理论上,这等同于计算”汉明距离”(Hamming distance)。如果不相同的数据位不超过5,就说明两张图片很相似;如果大于10,就说明这是两张不同的图片。

这种算法的优点是简单快速,不受图片大小缩放的影响,缺点是图片的内容不能变更。如果在图片上加几个文字,它就认不出来了。所以,它的最佳用途是根据缩略图,找出原图。

实际应用中,往往采用更强大的pHash算法和SIFT算法,它们能够识别图片的变形。只要变形程度不超过25%,它们就能匹配原图。这些算法虽然更复杂,但是原理与上面的简便算法是一样的,就是先将图片转化成Hash字符串,然后再进行比较。

具体实现

工具类

/**
 * 图片工具类,主要针对图片水印处理
 * 
 * @author WANGHONG
 * 
 */
public class ImageHelper {

    // 项目根目录路径
    public static final String path = System.getProperty("user.dir");

    /**
     * 生成缩略图 <br/>
     * 保存:ImageIO.write(BufferedImage, imgType[jpg/png/...], File);
     * 
     * @param source
     *            原图片
     * @param width
     *            缩略图宽
     * @param height
     *            缩略图高
     * @param b
     *            是否等比缩放
     * */
    public static BufferedImage thumb(BufferedImage source, int width, int height, boolean b) {
        // targetW,targetH分别表示目标长和宽
        int type = source.getType();
        BufferedImage target = null;
        double sx = (double) width / source.getWidth();
        double sy = (double) height / source.getHeight();

        if (b) {
            if (sx > sy) {
                sx = sy;
                width = (int) (sx * source.getWidth());
            } else {
                sy = sx;
                height = (int) (sy * source.getHeight());
            }
        }

        if (type == BufferedImage.TYPE_CUSTOM) { // handmade
            ColorModel cm = source.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();
            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
        } else
            target = new BufferedImage(width, height, type);
        Graphics2D g = target.createGraphics();
        // smoother than exlax:
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
        g.dispose();
        return target;
    }

    /**
     * 图片水印
     * 
     * @param imgPath
     *            待处理图片
     * @param markPath
     *            水印图片
     * @param x
     *            水印位于图片左上角的 x 坐标值
     * @param y
     *            水印位于图片左上角的 y 坐标值
     * @param alpha
     *            水印透明度 0.1f ~ 1.0f
     * */
    public static void waterMark(String imgPath, String markPath, int x, int y, float alpha) {
        try {
            // 加载待处理图片文件
            Image img = ImageIO.read(new File(imgPath));

            BufferedImage image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.drawImage(img, 0, 0, null);

            // 加载水印图片文件
            Image src_biao = ImageIO.read(new File(markPath));
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            g.drawImage(src_biao, x, y, null);
            g.dispose();

            // 保存处理后的文件
            FileOutputStream out = new FileOutputStream(imgPath);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(image);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文字水印
     * 
     * @param imgPath
     *            待处理图片
     * @param text
     *            水印文字
     * @param font
     *            水印字体信息
     * @param color
     *            水印字体颜色
     * @param x
     *            水印位于图片左上角的 x 坐标值
     * @param y
     *            水印位于图片左上角的 y 坐标值
     * @param alpha
     *            水印透明度 0.1f ~ 1.0f
     */

    public static void textMark(String imgPath, String text, Font font, Color color, int x, int y, float alpha) {
        try {
            Font Dfont = (font == null) ? new Font("宋体", 20, 13) : font;

            Image img = ImageIO.read(new File(imgPath));

            BufferedImage image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();

            g.drawImage(img, 0, 0, null);
            g.setColor(color);
            g.setFont(Dfont);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            g.drawString(text, x, y);
            g.dispose();
            FileOutputStream out = new FileOutputStream(imgPath);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(image);
            out.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    /**
     * 读取JPEG图片
     * 
     * @param filename
     *            文件名
     * @return BufferedImage 图片对象
     */
    public static BufferedImage readJPEGImage(String filename) {
        try {
            InputStream imageIn = new FileInputStream(new File(filename));
            // 得到输入的编码器,将文件流进行jpg格式编码
            JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
            // 得到编码后的图片对象
            BufferedImage sourceImage = decoder.decodeAsBufferedImage();

            return sourceImage;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ImageFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 读取JPEG图片
     * 
     * @param filename
     *            文件名
     * @return BufferedImage 图片对象
     */
    public static BufferedImage readPNGImage(String filename) {
        try {
            File inputFile = new File(filename);
            BufferedImage sourceImage = ImageIO.read(inputFile);
            return sourceImage;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ImageFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 灰度值计算
     * 
     * @param pixels
     *            像素
     * @return int 灰度值
     */
    public static int rgbToGray(int pixels) {
        // int _alpha = (pixels >> 24) & 0xFF;
        int _red = (pixels >> 16) & 0xFF;
        int _green = (pixels >> 8) & 0xFF;
        int _blue = (pixels) & 0xFF;
        return (int) (0.3 * _red + 0.59 * _green + 0.11 * _blue);
    }

    /**
     * 计算数组的平均值
     * 
     * @param pixels
     *            数组
     * @return int 平均值
     */
    public static int average(int[] pixels) {
        float m = 0;
        for (int i = 0; i < pixels.length; ++i) {
            m += pixels[i];
        }
        m = m / pixels.length;
        return (int) m;
    }
}

程序入口

package com.test.image;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public class ImageSearch {

    /**
     * @param args
     */
    public static void main(String[] args) {
        List<String> hashCodes = new ArrayList<String>();

        String filename = ImageHelper.path + "\\images\\";
        String hashCode = null;

        for (int i = 0; i < 6; i++) {
            hashCode = produceFingerPrint(filename + "example" + (i + 1) + ".jpg");
            hashCodes.add(hashCode);
        }
        System.out.println("Resources: ");
        System.out.println(hashCodes);
        System.out.println();

        String sourceHashCode = produceFingerPrint(filename + "source.jpg");
        System.out.println("Source: ");
        System.out.println(sourceHashCode);
        System.out.println();

        for (int i = 0; i < hashCodes.size(); i++) {
            int difference = hammingDistance(sourceHashCode, hashCodes.get(i));
            if (difference == 0) {
                System.out.print("source.jpg图片跟example" + (i + 1) + ".jpg一样");
            } else if (difference <= 5) {
                System.out.print("source.jpg图片跟example" + (i + 1) + ".jpg非常相似");
            } else if (difference <= 10) {
                System.out.print("source.jpg图片跟example" + (i + 1) + ".jpg有点相似");
            } else if (difference > 10) {
                System.out.print("source.jpg图片跟example" + (i + 1) + ".jpg完全不一样");
            }
            System.out.println("\t汉明距离\t" + difference);
        }

    }

    /**
     * 计算"汉明距离"(Hamming distance)。 如果不相同的数据位不超过5,就说明两张图片很相似;如果大于10,就说明这是两张不同的图片。
     * 
     * @param sourceHashCode
     *            源hashCode
     * @param hashCode
     *            与之比较的hashCode
     */
    public static int hammingDistance(String sourceHashCode, String hashCode) {
        int difference = 0;
        int len = sourceHashCode.length();

        for (int i = 0; i < len; i++) {
            if (sourceHashCode.charAt(i) != hashCode.charAt(i)) {
                difference++;
            }
        }
        return difference;
    }

    /**
     * 生成图片指纹
     * 
     * @param filename
     *            文件名
     * @return 图片指纹
     */
    public static String produceFingerPrint(String filename) {
        BufferedImage source = ImageHelper.readPNGImage(filename);// 读取文件

        int width = 8;
        int height = 8;

        // 第一步,缩小尺寸。
        // 将图片缩小到8x8的尺寸,总共64个像素。这一步的作用是去除图片的细节,只保留结构、明暗等基本信息,摒弃不同尺寸、比例带来的图片差异。
        BufferedImage thumb = ImageHelper.thumb(source, width, height, false);

        // 第二步,简化色彩。
        // 将缩小后的图片,转为64级灰度。也就是说,所有像素点总共只有64种颜色。
        int[] pixels = new int[width * height];
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                pixels[i * height + j] = ImageHelper.rgbToGray(thumb.getRGB(i, j));
            }
        }

        // 第三步,计算平均值。
        // 计算所有64个像素的灰度平均值。
        int avgPixel = ImageHelper.average(pixels);

        // 第四步,比较像素的灰度。
        // 将每个像素的灰度,与平均值进行比较。大于或等于平均值,记为1;小于平均值,记为0。
        int[] comps = new int[width * height];
        for (int i = 0; i < comps.length; i++) {
            if (pixels[i] >= avgPixel) {
                comps[i] = 1;
            } else {
                comps[i] = 0;
            }
        }

        // 第五步,计算哈希值。
        // 将上一步的比较结果,组合在一起,就构成了一个64位的整数,这就是这张图片的指纹。组合的次序并不重要,只要保证所有图片都采用同样次序就行了。
        StringBuffer hashCode = new StringBuffer();
        for (int i = 0; i < comps.length; i += 4) {
            int result = comps[i] * (int) Math.pow(2, 3) + comps[i + 1] * (int) Math.pow(2, 2) + comps[i + 2] * (int) Math.pow(2, 1) + comps[i + 2];
            hashCode.append(binaryToHex(result));
        }

        // 得到指纹以后,就可以对比不同的图片,看看64位中有多少位是不一样的。
        return hashCode.toString();
    }

    /**
     * 二进制转为十六进制
     * 
     * @param int binary
     * @return char hex
     */
    private static char binaryToHex(int binary) {
        char ch = ' ';
        switch (binary) {
        case 0:
            ch = '0';
            break;
        case 1:
            ch = '1';
            break;
        case 2:
            ch = '2';
            break;
        case 3:
            ch = '3';
            break;
        case 4:
            ch = '4';
            break;
        case 5:
            ch = '5';
            break;
        case 6:
            ch = '6';
            break;
        case 7:
            ch = '7';
            break;
        case 8:
            ch = '8';
            break;
        case 9:
            ch = '9';
            break;
        case 10:
            ch = 'a';
            break;
        case 11:
            ch = 'b';
            break;
        case 12:
            ch = 'c';
            break;
        case 13:
            ch = 'd';
            break;
        case 14:
            ch = 'e';
            break;
        case 15:
            ch = 'f';
            break;
        default:
            ch = ' ';
        }
        return ch;
    }
}

完成,但要实现同一个物体两张图片的内容识别出来并判断相似度的道路还是很远啊,哪种算法可以实现这种功能,我还不知道,有知道的同学可以在评论区告诉我一声,多谢。

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
感知哈希算法(Perceptual Hash Algorithm,简称PHash)是一种用于图片相似度比较的算法,通过对图像进行降维处理,将图像转化为一个唯一的hash值,然后通过计算hash值的差异度来判断图像的相似程度。下面是用Python实现PHash算法的步骤: 1. 图像预处理:首先,将图像转为灰度图像,这样可以减少计算复杂度。使用Python的PIL库可以方便地实现这一步骤。 2. 图像缩放:为了降低计算复杂度,将图像缩小到一个固定的大小,如8x8像素。这一步骤也可以使用PIL库来实现。 3. 离散余弦变换(Discrete Cosine Transform,简称DCT):对缩小后的图像进行DCT变换,得到频域信息。可以使用Python的numpy库中的dct函数来计算DCT。 4. 量化:将DCT系数量化,得到一个8x8的二进制矩阵,其中大于平均值为1,小于平均值为0。 5. 生成hash值:将量化后的二进制矩阵转为一个唯一的hash值。可以将矩阵展平,并将每个元素按顺序拼接成一个长字符串,然后使用Python的hash函数计算出一个hash值。 通过上述步骤,就可以实现PHash算法。使用Python中的PIL库和numpy库可以方便地进行图像处理和计算操作。具体实现的代码可以参考以下示例: ```python from PIL import Image import numpy as np def perceptual_hash(image_path): # 图像预处理 image = Image.open(image_path).convert('L') # 图像缩放 image = image.resize((8, 8), Image.ANTIALIAS) # 离散余弦变换 dct_coeffs = np.asarray(image, dtype=float) dct_coeffs = np.round(dct_coeffs) - 128 dct_coeffs = np.fft.dct(dct_coeffs) dct_coeffs = dct_coeffs[:8, :8] # 量化 avg = np.mean(dct_coeffs) hash_value = np.where(dct_coeffs > avg, 1, 0) # 生成hash值 hash_value = hash_value.flatten().tolist() hash_value = ''.join(map(str, hash_value)) hash_value = hash(hash_value) return hash_value # 使用示例 image1_path = 'image1.jpg' image2_path = 'image2.jpg' hash1 = perceptual_hash(image1_path) hash2 = perceptual_hash(image2_path) print("Image1 hash value:", hash1) print("Image2 hash value:", hash2) ``` 以上代码中的`image1.jpg`和`image2.jpg`分别为待比较的两张图片,根据输出的hash值可以判断两张图片的相似性。如果hash值越接近,则表示图片越相似。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值