Tutorial: Comparing Two Images

Learning Objectives

In this tutorial, you’ll learn:

  • How to directly compare two specific images
  • How to interpret similarity scores between images
  • Different methods for comparing images in your search context
  • When to use direct comparison vs. similarity search
  • Best practices for image comparison

Prerequisites

Before starting this tutorial, please ensure you have:

  • Completed the Detecting Duplicate Images tutorial
  • A valid search context ID from previous tutorials
  • Your Aspose Cloud credentials (Client ID and Client Secret)
  • A valid access token for the Aspose Cloud API
  • At least two images already added to your search context

Understanding Direct Image Comparison

While previous tutorials focused on finding multiple similar images or duplicate groups, this tutorial concentrates on directly comparing just two images to determine their similarity. This is useful when you:

  • Need to verify if two specific images are the same or similar
  • Want to quantify exactly how similar two images are
  • Need to determine if an image is a modified version of another
  • Are building a verification or authentication system

Image Comparison Methods

Aspose.Imaging Cloud provides several ways to compare images:

  1. Compare Two Images in Search Context: Compare two images that are already in your search context
  2. Compare Search Context Image with External Image: Compare an image in your search context with a new external image
  3. Compare Two External Images: Compare two new images that aren’t in your search context yet

We’ll focus on the first two methods in this tutorial.

Step 1: Obtain an Access Token

First, authenticate with the Aspose Cloud API:

curl -v "https://api.aspose.cloud/oauth2/token" \
-X POST \
-d 'grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET' \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Save the access token from the response for use in the next step.

Step 2: Compare Two Images in Search Context

To compare two images that are already in your search context, use this API call:

curl -v "https://api.aspose.cloud/v3/imaging/ai/imageSearch/YOUR_SEARCH_CONTEXT_ID/compare?imageId1=first_image.jpg&imageId2=second_image.jpg" \
-X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Content-Length: 0" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Replace the following placeholders:

  • YOUR_SEARCH_CONTEXT_ID with your actual search context ID
  • YOUR_ACCESS_TOKEN with the token from Step 1
  • first_image.jpg and second_image.jpg with the IDs of the images you want to compare

Step 3: Interpret the Results

You’ll receive a response like this:

{
  "Results": [
    {
      "ImageId": "",
      "Similarity": 82.5
    }
  ],
  "Code": 200,
  "Status": "OK"
}

The Similarity score (0-100) indicates how visually similar the two images are:

  • 90-100: Very high similarity (nearly identical images)
  • 70-89: High similarity (same subject with variations)
  • 50-69: Moderate similarity
  • Below 50: Low similarity (significantly different images)

Step 4: Compare a Search Context Image with an External Image

To compare an image in your search context with a new external image:

curl -v "https://api.aspose.cloud/v3/imaging/ai/imageSearch/YOUR_SEARCH_CONTEXT_ID/compare?imageId1=first_image.jpg" \
-X POST \
-T /path/to/your/external_image.jpg \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Replace the placeholders as before, and:

  • /path/to/your/external_image.jpg with the path to your local image file

Try It Yourself

Now, let’s practice comparing images:

  1. Obtain an access token using the curl command
  2. Choose two images in your search context and compare them
  3. Note the similarity score
  4. Try comparing different pairs of images to see how the scores vary
  5. Try comparing an image in your search context with a local image

Using SDK Libraries

Java SDK Example (Compare Two Images in Search Context)

import com.aspose.imaging.cloud.sdk.api.ImagingApi;
import com.aspose.imaging.cloud.sdk.model.SearchResultsSet;
import com.aspose.imaging.cloud.sdk.model.SearchResult;

public class CompareTwoImagesExample {
    
    public static void main(String[] args) {
        // Get your clientId and clientSecret from https://dashboard.aspose.cloud/
        String clientId = "YOUR_CLIENT_ID";
        String clientSecret = "YOUR_CLIENT_SECRET";
        
        // Create instance of the API
        ImagingApi api = new ImagingApi(clientId, clientSecret);
        
        // Your search context ID from previous operations
        String searchContextId = "YOUR_SEARCH_CONTEXT_ID";
        
        // IDs of the images to compare
        String imageId1 = "first_image.jpg";
        String imageId2 = "second_image.jpg";
        
        try {
            // Compare the two images
            SearchResultsSet results = api.compareImages(searchContextId, imageId1, imageId2, null, null, null);
            
            // Get the similarity score
            if (results.getResults() != null && !results.getResults().isEmpty()) {
                double similarity = results.getResults().get(0).getSimilarity();
                System.out.println("Similarity between " + imageId1 + " and " + imageId2 + ": " + similarity);
                
                // Interpret the similarity score
                if (similarity >= 90) {
                    System.out.println("Images are nearly identical");
                } else if (similarity >= 70) {
                    System.out.println("Images are very similar");
                } else if (similarity >= 50) {
                    System.out.println("Images have moderate similarity");
                } else {
                    System.out.println("Images are significantly different");
                }
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Java SDK Example (Compare with External Image)

import com.aspose.imaging.cloud.sdk.api.ImagingApi;
import com.aspose.imaging.cloud.sdk.model.SearchResultsSet;

import java.io.File;
import java.nio.file.Files;

public class CompareWithExternalImageExample {
    
    public static void main(String[] args) {
        // Get your clientId and clientSecret from https://dashboard.aspose.cloud/
        String clientId = "YOUR_CLIENT_ID";
        String clientSecret = "YOUR_CLIENT_SECRET";
        
        // Create instance of the API
        ImagingApi api = new ImagingApi(clientId, clientSecret);
        
        // Your search context ID from previous operations
        String searchContextId = "YOUR_SEARCH_CONTEXT_ID";
        
        // ID of the image in search context
        String imageId1 = "first_image.jpg";
        
        // Path to your local image file
        String imagePath = "/path/to/your/external_image.jpg";
        
        try {
            // Read the image into a byte array
            File imageFile = new File(imagePath);
            byte[] imageData = Files.readAllBytes(imageFile.toPath());
            
            // Compare with external image
            SearchResultsSet results = api.compareImages(searchContextId, imageId1, null, imageData, null, null);
            
            // Get the similarity score
            if (results.getResults() != null && !results.getResults().isEmpty()) {
                double similarity = results.getResults().get(0).getSimilarity();
                System.out.println("Similarity between " + imageId1 + " and external image: " + similarity);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

.NET SDK Example

using Aspose.Imaging.Cloud.Sdk.Api;
using Aspose.Imaging.Cloud.Sdk.Model;
using System;

namespace AsposeImagingCloudTutorials
{
    class CompareTwoImagesExample
    {
        static void Main(string[] args)
        {
            // Get your clientId and clientSecret from https://dashboard.aspose.cloud/
            string clientId = "YOUR_CLIENT_ID";
            string clientSecret = "YOUR_CLIENT_SECRET";
            
            // Create instance of the API
            ImagingApi api = new ImagingApi(clientId, clientSecret);
            
            // Your search context ID from previous operations
            string searchContextId = "YOUR_SEARCH_CONTEXT_ID";
            
            // IDs of the images to compare
            string imageId1 = "first_image.jpg";
            string imageId2 = "second_image.jpg";
            
            try
            {
                // Compare the two images
                SearchResultsSet results = api.CompareImages(searchContextId, imageId1, imageId2);
                
                // Get the similarity score
                if (results.Results != null && results.Results.Count > 0)
                {
                    double similarity = results.Results[0].Similarity;
                    Console.WriteLine($"Similarity between {imageId1} and {imageId2}: {similarity}");
                    
                    // Interpret the similarity score
                    if (similarity >= 90)
                    {
                        Console.WriteLine("Images are nearly identical");
                    }
                    else if (similarity >= 70)
                    {
                        Console.WriteLine("Images are very similar");
                    }
                    else if (similarity >= 50)
                    {
                        Console.WriteLine("Images have moderate similarity");
                    }
                    else
                    {
                        Console.WriteLine("Images are significantly different");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }
    }
}

Real-World Applications of Image Comparison

Direct image comparison has many practical applications:

  1. Copyright Verification: Determine if an image is a copy or derivative of a protected work
  2. Quality Control: Compare image versions before and after processing
  3. Security Systems: Verify if a captured image matches a reference image
  4. Product Authentication: Check if a product image matches an authentic reference
  5. Version Control: Track and quantify changes between image versions

Understanding Similarity vs. Identical

It’s important to understand that even exact duplicates may not score 100 on similarity:

  • Format Differences: Converting between formats (e.g., PNG to JPEG) introduces small changes
  • Compression Artifacts: Different compression levels affect pixel values
  • Metadata Variations: Different metadata doesn’t affect visual similarity but makes files different
  • Minimal Edits: Tiny crops or brightness adjustments can lower the score slightly

For most practical applications, a score of 95+ can be considered essentially identical.

What You’ve Learned

In this tutorial, you’ve learned:

  • How to directly compare two specific images for similarity
  • How to interpret similarity scores
  • Different methods for comparing images (in context vs. external)
  • Real-world applications for image comparison
  • Why even identical images may not score exactly 100

Troubleshooting Tips

  • Low Scores for Similar Images: Check for format differences, compression, or subtle edits
  • High Scores for Different Images: Very similar subjects might score high even when they’re different images
  • Zero Score: This usually indicates an error in the image IDs or that features couldn’t be extracted
  • API Errors: Ensure both images exist in your search context when using the two-ID method

Next Steps

Now that you know how to compare specific images, you’re ready to move on to the next tutorial in our series:

Tutorial: Working with Image Tags

In the next tutorial, you’ll learn how to enhance your image search capabilities by working with tags.

Further Practice

To reinforce what you’ve learned:

  • Create a script that compares multiple pairs of images and ranks them by similarity
  • Try comparing modified versions of the same image to see how different modifications affect similarity
  • Implement a simple verification system that checks if an image matches a reference
  • Experiment with comparing different types of content to understand what visual elements affect similarity scores

Helpful Resources

Have questions about this tutorial? Please feel free to post your questions on our support forum.