Algorithm: Reduce size. The fastest way to remove high frequencies and detail is to shrink the image. In this case, shrink it to 9x8 so that there are 72 total pixels. Reduce color. Convert the image to a grayscale picture. This changes the hash from 72 pixels to a total of 72 colors. Compute the difference. The algorithm works on the difference between adjacent pixels. This identifies the relative gradient direction. In this case, the 9 pixels per row yields 8 differences between adjacent pixels. Eight rows of eight differences becomes 64 bits. Assign bits. Each bit is simply set based on whether the left pixel is brighter than the right pixel. http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html
Beispiel #1
0
 /**
  * Compare two images and returns a hamming distance. A value of 0 indicates a likely similar picture. A value between 1 and 10 is potentially a variation. A value greater than 10 is likely a different image.
  *
  * @param ImageInterface|string $image1
  * @param ImageInterface|string $image2
  *
  * @return int Hamming distance. Note: This breaks the chain if you are doing fluent api calls as it does not return an Editor.
  * @throws \Exception
  */
 public function compare($image1, $image2)
 {
     if (is_string($image1)) {
         // If string passed, turn it into a Image object
         $image1 = Image::createFromFile($image1);
         $this->flatten($image1);
     }
     if (is_string($image2)) {
         // If string passed, turn it into a Image object
         $image2 = Image::createFromFile($image2);
         $this->flatten($image2);
     }
     $hash = new DifferenceHash();
     $bin1 = $hash->hash($image1, $this);
     $bin2 = $hash->hash($image2, $this);
     $str1 = str_split($bin1);
     $str2 = str_split($bin2);
     $distance = 0;
     foreach ($str1 as $i => $char) {
         if ($char !== $str2[$i]) {
             $distance++;
         }
     }
     return $distance;
 }