@@ -1718,11 +1718,45 @@ public boolean equals(Object obj){
17181718 //**************************************************************************
17191719 /** Returns true if a given image is similar to (or equal to) this image.
17201720 * Unlike the equals() method which performs an exact match, this method
1721- * performs a fuzzy match using PHash values for the images.
1721+ * performs a fuzzy match using perceptual hash values for the images.
1722+ * Returns true if the Hamming Distance between the two images is 0.
17221723 */
17231724 public boolean isSimilarTo (Image image ){
1725+ return isSimilarTo (image , 0 );
1726+ }
1727+
1728+
1729+ //**************************************************************************
1730+ //** isSimilarTo
1731+ //**************************************************************************
1732+ /** Returns true if a given image is similar to this image. Performs a fuzzy
1733+ * match using perceptual hash values for the images.
1734+ * @param image An image to compare to
1735+ * @param threshold The minimum hamming distance between the two images
1736+ */
1737+ public boolean isSimilarTo (Image image , int threshold ){
17241738 if (image ==null ) return false ;
1725- return this .getPHash ().equals (image .getPHash ());
1739+ int d = getHammingDistance (image );
1740+ return (d <=threshold );
1741+ }
1742+
1743+
1744+ //**************************************************************************
1745+ //** getHammingDistance
1746+ //**************************************************************************
1747+ /** Returns the Hamming Distance between this image and a given image using
1748+ * perceptual hash values for the images.
1749+ */
1750+ public int getHammingDistance (Image image ){
1751+ String hash = Long .toBinaryString (getPHash ());
1752+ String target = Long .toBinaryString (image .getPHash ());
1753+ int d = 0 ;
1754+ for (int i =0 ; i <hash .length (); i ++) {
1755+ if (hash .charAt (i ) != target .charAt (i )) {
1756+ d ++;
1757+ }
1758+ }
1759+ return d ;
17261760 }
17271761
17281762
@@ -1732,10 +1766,11 @@ public boolean isSimilarTo(Image image){
17321766 /** Returns a perceptual hash value for the image. Unlike traditional
17331767 * cryptographic hashes like SHA1, PHash is not sensitive to tiny
17341768 * variations in an image. This makes it ideal to find similar images.
1735- * @return A String representing a 64-bit long value (PHash). Example:
1769+ * @return A 64-bit long value (PHash). A string representation of the
1770+ * hash (e.g. Long.toBinaryString) would look something like this:
17361771 * "1101001010111111010011111110111000011001001011110011110111001101"
17371772 */
1738- public String getPHash (){
1773+ public long getPHash (){
17391774
17401775 //Clone the image
17411776 BufferedImage bi = new BufferedImage (
@@ -1813,7 +1848,7 @@ public String getPHash(){
18131848 }
18141849 }
18151850
1816- return Long . toBinaryString ( hashBits ) ;
1851+ return hashBits ;
18171852 }
18181853
18191854
0 commit comments