Simple image comparison using php

    The algorithm is probably not new, and is not ideal , but, surprisingly, it works. No graphs or correlation.

    To begin with, it is worth noting that the comparison is very approximate (at least at this stage), at times two completely different photographs turn out to be similar to 60% percent, also hard modifications (rotations, inversion, cropping) are not taken into account - for this you need to bring and bring. For me personally, the most important thing were two parameters: speed , independence of image resolution and the ability to compare "images" directly in the database.


    • Open source image
    • Scale it to the size of the mask (in my case it is 20 by 20, I do not need large sizes - the comparison is approximate, it is quite possible to make a mask and 10 by 10).
    • We calculate the main color of the mask.
    • We create an array where the value is a key of type af2 (1,2 - coordinates, as in a naval battle. 2 - discrepancy with the main brightness).
    • We generate a line - a key.
    • Compare two lines by relevance.

    The code

    In this case, the version that works through GD with PNG images is presented .
    //Генерация ключа-изображения
    function getimageid($image)
      //Размеры исходного изображения

      //Исходное изображение


      //Копируем изображение в маску

      //Будущая маска

      //Базовый цвет изображения


       //Заполняем маску и вычисляем базовый цвет

          //Вычисление яркости было подсказано хабраюзером Ryotsuke
          $colormap[$x][$y]= 0.212671 * $color['red'] + 0.715160 * $color['green'] + 0.072169 * $color['blue'];

          $average += $colormap[$x][$y];

      //Базовый цвет
      $average /= 400;

      //Генерируем ключ строку

      //Возвращаем ключ
      return join(' ',$result);

    * This source code was highlighted with Source Code Highlighter.

    //Вычисление "похожести" двух изображений
    function imagediff($image,$desc)
      $image=explode(' ',$image);
      $desc=explode(' ',$desc);


      foreach($image as $bit)

       return $result/((count($image)+count($desc))/2);

    * This source code was highlighted with Source Code Highlighter.

    * The similarity calculation function is approximate - it is better to perform this action on the base side.


    Surprisingly, 95% 87% similarity (with the addition of UPD-2)

    52% similarity

    28% similarity

    100% similarity


    All this is only approximate calculations, it is impossible to achieve a particularly advanced comparison in this way, but it will do for a quick selection from the database of similar images.

    PS Comments and advice are accepted.
    UPD Thanks MiniM - the code has been simplified.
    UPD-2 Small addition:
     //Генерируем ключ строку

    * This source code was highlighted with Source Code Highlighter.

    After that, the decrease in brightness at the point will be taken into account. Thus, the similarity in the first example will be 87% .
    UPD-3 Moved to php.

    Also popular now: