Cloudinary Blog

How-to automatically identify similar images using pHash

Image fingerprinting Identify similar images using pHas

Photos today can be easily edited by means of resizing, cropping, adjusting the contrast, or changing an image’s format. As a result, new images are created that are similar to the original ones. Websites, web applications and mobile apps that allow user generated content uploads can benefit from identifying similar images.

Image de-duplication

If your site allows users to upload images, they can also upload various processed or manipulated versions of the same image. As described above, while the versions are not exactly identical, they are quite similar.

Obviously, it’s good practice to show several different images on a single page and avoid displaying similar images. For example, travel sites might want to show different images of a hotel room, but avoid having similar images of the room on the same page.

Additionally, if your web application deals with many uploaded images, you may want to be able to automatically recognize if newly uploaded images are similar to previously uploaded images. Recognizing similar images can prevent duplicate images from being used once they are uploaded, allowing you to better organize your site’s content. The better your web application is better at identifying similar images upon upload, the more duplicated images will no longer be a thing.

Duplicated images will no longer be a thing because similar images upon upload will be identified

Image similarity identification

Cloudinary uses perceptual hash (pHash), which acts as an image fingerprint. This mathematical algorithm analyzes an image's content and represents it using a 64-bit number fingerprint. Two images’ pHash values are "close" to one another if the images’ content features are similar. By comparing two image fingerprints, you can tell if they are similar.

You can request the pHash value of an image from Cloudinary for any uploaded image, either using Cloudinary's upload API, or for any previously uploaded image in your media library using our admin API. You can simply set the phash parameter to true, which produces the image's pHash value. This image similarity algorithm is incredibly powerful and easy to use. Check out the example below:

Using the following image for example:

Original koala photo

Below is a code sample in Ruby that shows how to upload this image with a request for the pHash value:

Cloudinary::Uploader.upload("koala1.jpg", :public_id => "koala1", :phash => true)

The result below shows the returned response with the calculated pHash value:

    {
     "public_id": "koala1",
     "version": 1424266415,
     "width": 887,
     "height": 562,
     "format": "jpg",
     "etag": "6f821ea4478af3e3a183721c0755cb1b",
    ...
     "phash": "ba19c8ab5fa05a59"
    }

The examples below demonstrate multiple similar images and their pHash values. Let's compare the pHash values and find the distance between each pair. If you XOR two of the pHash values and count the “1’s” in the result, you get a value between 0-64. The lower the value, the more similar the images are. If all 64 bits are the same, the photos are very similar.

The similarity score of the examples below expresses how each image is similar to the original image. The score is calculated as 1 - (phash_distance(phash1, phash2) / 64.0) in order to give a result between 0.5 and 1 (phash_distance can be computed using bit_count(phash1 ^ phash2) in MySQL for example).

Original koala thumbnail
887x562 JPEG, 180 KB
pHash: ba19c8ab5fa05a59

Grayscale koala
887x562 JPEG, 149 KB
Difference: grayscale.
pHash: ba19caab5f205a59
Similarity score: 0.96875

Cropped koala photo with increased saturation
797x562 JPEG, 179 KB
Difference: cropped, increased color saturation.
pHash: ba3dcfabbc004a49
Similarity score: 0.78125

Cropped koala photo with lower JPEG quality
887x509 JPEG, 30.6 KB
Difference: cropped, lower JPEG quality.
pHash: 1b39ccea7d304a59
Similarity score: 0.8125

Another koala photo
1000x667 JPEG, 608 KB
Difference: a different koala photo...
pHash: 3d419c23c42eb3db
Similarity score: 0.5625

Not a koala photo
1000x688 JPEG, 569 KB
Difference: not a koala...
pHash: f10773f1cd269246
Similarity score: 0.5

v

As you can see from the results above that the three images that appear to be similar to the original received a high score when they were compared. While other comparison results showed significantly less similarity.

By using Cloudinary to upload users’ photos to your site or application, you can request the pHash values of the uploaded images and store them on your servers. That allows you to identify which images are similar and decide what the next step should be. Building image matcher type of apps would be a lot easier. You may want to keep similar images, classify them in your database, filter them out, or interactively allow users to decide which images they want to keep.

Summary

This feature is available for any Cloudinary plan, including the free tier. As explained above, you can use Cloudinary’s API to get an image’s fingerprint and start checking for similarities. In addition, it is in our roadmap to further enhance our similar image search and de-duplication capabilities.

Recent Blog Posts

10 Website Videos Mistakes and How to Solve Them

It should come as no surprise that video use on the internet is exploding. You can see the dramatic growth of video on the average site in this SpeedCurve blog post.

With the growth in video comes greater bandwidth use, which is not only costly for your IT budget, but for your visitors as well. Beyond the expense, there is the user experience to consider. The heavier the page, the longer it will take to load, and the greater likelihood visitors will abandon your site. Page load speed is also an important factor in SEO ranking, so clearly video is something we need to take seriously and get right. Video is challenging, presenting terms still unfamiliar to developers - like codecs, bitrate and adaptive bitrate streaming. As a result, mistakes are being made in video implementation.

Read more
Android Data Saver: Optimizing Mobile Data Usage with Cloudinary

Over the life of a mobile device, the cost of a cellular data plan often exceeds that of the device itself. To optimize data usage and purge useless data on their mobile devices, users can enable Data Saver from Android 7.0 (API level 24). To do so, users toggle Data Saver in quick settings under the Notification shade or under Settings > Data usage. With Data Saver enabled, apps that aren't whitelisted cannot use cellular data in the background. They are also directed to consume less data while active.

Read more
Introducing the Cloudinary Upload Widget v2

At Cloudinary, we manage the entire pipeline of media assets for thousands of customers of varying sizes from numerous verticals. Cloudinary is an end-to-end solution for all your image and video needs, including upload, storage, administration, manipulation, optimization and dynamic delivery.

Read more
Convert an Image to a 3D Canvas With Cloudinary

Note
This post was cowritten with Daniel Mendoza.
Note
This post was cowritten with Daniel Mendoza.
Note

Famed American poet Henry David Thoreau once said, “This world is but a canvas to our imagination.” And, like your imagination, the transformations you can apply to images with Cloudinary are practically endless. You can even render any flat image to appear three-dimensional and framed on a canvas.

Read more
Mobile Optimization: Optimize Your Mobile-Web User Experience

TL;DR

We live in a visual world, often while on the go, and consumers expect media-rich web content. Accordingly, the loading speed of images and videos is a big factor in user experience. To optimize customer satisfaction with your mobile content, you must focus on the quality, format, and size of your digital assets. With Cloudinary, optimization is simple, not only enhancing your mobile web and app performance, but also upping your SEO game and boosting customer experience.

Read more