PHP Implementation image similar search

Source: Internet
Author: User

I QQ group also has a lot of technical documents, I hope to provide you with some help (non-technical not to add).

QQ Group: 281442983 (click the link to join the group: http://jq.qq.com/?_wv=1027&k=29LoD19) qq:1542385235

A few days ago, I saw Nanyi's blog about the principle of similar image search, which introduced the general principles of Google's similar image search, mainly from the looks like it translation from Neal Krawetz. This article introduces the related steps of similar image search processing in detail, and gives a python implementation, because I am relatively familiar with PHP, so I wrote a PHP version.

Simple principle of image similarity search

According to the article description, in fact, the principle is relatively simple, roughly the following steps:

1, reduce the size. Reduce the image to 8x8 's size, a total of 64 pixels. The role of this step is to remove the details of the picture, only the structure, shading and other basic information, discard the different sizes, proportions of the picture differences.

2, simplify the color. Converts the zoomed-in image to a level 64 grayscale. That is, all pixels have a total of 64 colors.

3, calculate the average. Calculates a grayscale average of all 64 pixels.

4. Compare the grayscale of pixels. The grayscale of each pixel is compared to the average. Greater than or equal to the average, recorded as 1, less than the average, recorded as 0.

5. Calculate the hash value. By combining the results of the previous step, you make up a 64-bit integer, which is the fingerprint of the image. The order of the combinations is not important, just make sure all the pictures are in the same order. After getting the fingerprint, you can compare different pictures and see how many of the 64 bits are not the same.

The advantages of this algorithm are simple and fast, not affected by the size of the picture, the disadvantage is that the contents of the picture can not be changed. In practical applications, more powerful phash algorithms and sift algorithms are often used to identify the deformation of images. As long as the degree of deformation does not exceed 25%, they can match the original image.

PHP implementation of image similarity search

The implementation of a Python version is given in the original, with only 53 lines of code. I wrote a corresponding PHP version, because it is written directly in the class, so the code has 100 lines, as follows:

hash ($f);
}
Return $isString? $result [0]: $result;
}
Public Function checkissimilarimg ($imgHash, $otherImgHash) {
if (file_exists ($imgHash) && file_exists ($otherImgHash)) {
$imgHash = $this->run ($imgHash);
$otherImgHash = $this->run ($otherImgHash);
}
if (strlen ($imgHash)!== strlen ($otherImgHash)) return false;
$count = 0;
$len = strlen ($imgHash);
for ($i =0; $i < $len; $i + +) {
if ($imgHash {$i}!== $otherImgHash {$i}) {
$count + +;
}
}
Return $count <= (5 * $rate * $rate)? True:false;
}
Public Function hash ($file) {
if (!file_exists ($file)) {
return false;
}
$height = 8 * $this->rate;
$width = 8 * $this->rate;
$img = Imagecreatetruecolor ($width, $height);
List ($w, $h) = getimagesize ($file);
$source = $this->createimg ($file);
Imagecopyresampled ($img, $source, 0, 0, 0, 0, $width, $height, $w, $h);
$value = $this->gethashvalue ($img);
Imagedestroy ($IMG);
return $value;
}
Public Function Gethashvalue ($img) {
$width = Imagesx ($img);
$height = Imagesy ($img);
$total = 0;
$array = Array ();
for ($y =0; $y < $height; $y + +) {
for ($x =0; $x < $width; $x + +) {
$gray = (Imagecolorat ($img, $x, $y) >> 8) & 0xFF;
if (!is_array ($array [$y])) {
$array [$y] = array ();
}
$array [$y] [$x] = $gray;
$total + = $gray;
}
}
$average = Intval ($total/(* $this->rate * $this->rate));
$result = ";
for ($y =0; $y < $height; $y + +) {
for ($x =0; $x < $width; $x + +) {
if ($array [$y] [$x] >= $average) {
$result. = ' 1 ';
}else{
$result. = ' 0 ';
}
}
}
return $result;
}
Public Function createimg ($file) {
$ext = $this->getfileext ($file);
if ($ext = = = ' jpeg ') $ext = ' jpg ';
$img = null;
Switch ($ext) {
Case ' png ': $img = Imagecreatefrompng ($file);
Case ' jpg ': $img = Imagecreatefromjpeg ($file);
Case ' gif ': $img = Imagecreatefromgif ($file);
}
return $img;
}
Public Function Getfileext ($file) {
$infos = Explode ('. ', $file);
$ext = Strtolower ($infos [Count ($infos)-1]);
return $ext;
}
}
The method is called as follows:

Require_once "Imghash.class.php";
$instance = Imghash::getinstance ();
$result = $instance->checkissimilarimg (' chenyin/img_3214.png ', ' chenyin/img_3212.jpg ');
If the $result value is true, it indicates that 2 pictures are similar, otherwise not similar.



Other

In the actual similar image search, the fingerprint of the image is not a difficult problem, but in how to find a large amount of image fingerprints and similar fingerprints.

I QQ group also has a lot of technical documents, I hope to provide you with some help (non-technical not to add).

QQ Group: 281442983 (click the link to join the group: http://jq.qq.com/?_wv=1027&k=29LoD19) qq:1542385235

PHP Implementation image similar search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.