Class RotPHash

  • All Implemented Interfaces:
    Serializable

    public class RotPHash
    extends HashingAlgorithm
    A rotational invariant hashing algorithm which is mostly immune to rotation attacks. The hash wraps the pixels around a circle and computes a discrete cosine transformation on each subsection. Ring partition. Pixels are mapped to buckets according to their distance to the center
    Since:
    2.0.0
    Author:
    Kilian
    See Also:
    Serialized Form
    • Constructor Detail

      • RotPHash

        public RotPHash​(int bitResolution)
        Create a Rotational Invariant Perceptive Hasher
        Parameters:
        bitResolution - The desired bit resolution of the created hash
      • RotPHash

        public RotPHash​(int bitResolution,
                        boolean truncateKey)
        Create a Rotational Invariant Perceptive Hasher
        Parameters:
        bitResolution - The desired bit resolution of the created hash
        truncateKey - if true the resulting key will exactly have bitResolution length at the cost of truncating some information in the last layer. This may result in tiny bit of accuracy loss.

        If false the keys length will at least be bitResolution bits long, but most likely longer. All keys produced with the same settings will have the same length.

    • Method Detail

      • hash

        protected BigInteger hash​(BufferedImage image,
                                  HashBuilder hash)
        Description copied from class: HashingAlgorithm
        Calculate a hash for the given image. Invoking the hash function on the same image has to return the same hash value. A comparison of the hashes relates to the similarity of the images. The lower the value the more similar the images are. Equal images will produce a similarity of 0.

        This method is intended to be overwritten by implementations and takes a baseHash argument to allow concatenating multiple hashes as well to be able to compute the effective hash length in HashingAlgorithm.getKeyResolution(). Preceding 0's are omitted in big integer objects, while the usual hamming distance can be calculated due to xoring without issue the normalized distance requires the potential length of the key to be known.

        Specified by:
        hash in class HashingAlgorithm
        Parameters:
        image - Image whose hash will be calculated
        hash - a hash builder used to construct the hash
        Returns:
        the hash encoded as a big integer
      • computePartition

        protected int computePartition​(double originalX,
                                       double originalY)
        Compute the ring partition this specific pixel will fall into.
        Parameters:
        originalX - the x pixel index in the picture
        originalY - the y pixel index in the picture
        Returns:
        the bucket index
      • precomputeAlgoId

        protected int precomputeAlgoId()
        Description copied from class: HashingAlgorithm
        A unique id identifying the settings and algorithms used to generate the output result. This method shall contain a hash code for the object which
        • Stays consistent throughout restart of the jvm
        • Value does not change after the constructor finished
        • Must return the same value if two instances compute the same hashes for identical input

        Even if different bitResolutions are used in the constructor HashingAlgorithm(int) the algorithId MUST return the same id for two instances if the returned hashes for the same input will always be equal. Therefore instead of checking against the bitResolution the actual resolution as returned by HashingAlgorithm.getKeyResolution() should be used. This method algorithm id's as information available to the child class and will be extended by the hashcode of the kernels.

        Specified by:
        precomputeAlgoId in class HashingAlgorithm
        Returns:
        the preliminary algorithm id identifying this hashing algorithm