Fast and simple image hashing algorithm

前端 未结 2 1392
孤街浪徒
孤街浪徒 2020-12-25 09:18

I need a (preferably simple and fast) image hashing algorithm. The hash value is used in a lookup table, not for cryptography.

Some of the images are \"computer grap

相关标签:
2条回答
  • 2020-12-25 09:23

    If you want to make it very fast, you should consider taking a random subset of the pixels to avoid reading the entire image. Next, compute a hash function on the sequence of values at those pixels. The random subset should be selected by a deterministic pseudo-random number generator with fixed seed so that identical images produce identical subsets and consequently identical hash values.

    This should work reasonably well even for artificial images. However, if you have images which differ from each other by a small number of pixels, this is going to give hash collisions. More iterations give better reliability. If that is the case, for instance, if your images set is likely to have pairs with one different pixel, you must read every pixel to compute the hash value. Taking a simple linear combination with pseudo-random coefficients would be good enough even for artificial images.

    pseudo-code of a simple algorithm

    Random generator = new generator(2847)  // Initialized with fixed seed
    int num_iterations = 100
    
    int hash(Image image) {
        generator.reset()   //To ensure consistency on each evaluation
        int value = 0
        for num_iteration steps {
            int nextValue = image.getPixel(generator.nextInt()%image.getSize()).getValue()
            value = value + nextValue*generator.nextInt()
        }
        return value
    }
    
    0 讨论(0)
  • 2020-12-25 09:43

    Have a look at this tutorial on the phash algorithm http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html which is used to find closely matching images.

    0 讨论(0)
提交回复
热议问题