Tutorial: Extracting Image Features

Learning Objectives

In this tutorial, you’ll learn:

  • What image features are and why they’re essential for reverse image search
  • How to extract features from images with and without adding them to your search context
  • The difference between various feature extraction methods
  • How to use extracted features for advanced image search scenarios

Prerequisites

Before starting this tutorial, please ensure you have:

  • Completed the Adding Images to Your Search Context 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 one image ready for feature extraction

Understanding Image Features

Image features are numerical representations of visual elements in an image. They capture distinctive aspects like:

  • Edges and corners
  • Color distributions
  • Texture patterns
  • Shape information
  • Local image gradients

These features create a “visual fingerprint” that allows the system to compare images based on their visual similarity rather than pixel-by-pixel matching, which is more robust for real-world applications.

Two Approaches to Feature Extraction

Aspose.Imaging Cloud provides two ways to extract features from images:

  1. Extract and Add: Extract features and immediately add them to your search context
  2. Extract Only: Extract features without adding them to your search context (useful for one-time comparisons)

We’ll explore both approaches in this tutorial.

Method 1: Extract Features and Add Them to Search Context

This method is useful when building up your search database with images that you want to be findable in future searches.

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: Extract Features and Add to Search Context

Use the following API call to extract features from an image and add them to your search context:

curl -v "https://api.aspose.cloud/v3/imaging/ai/imageSearch/YOUR_SEARCH_CONTEXT_ID/features" \
-X POST \
-T /path/to/your/image.jpg \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-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
  • /path/to/your/image.jpg with the local path to your image file

Step 3: Verify the Operation

If successful, you’ll receive a response like this:

{
  "Code": 200,
  "Status": "OK"
}

This confirms that the features were successfully extracted and added to your search context.

Method 2: Extract Features Without Adding to Search Context

This method is useful when you want to analyze or compare images without permanently adding them to your search database.

Step 1: Obtain an Access Token (Same as Above)

Step 2: Extract Features Only

Use the following API call to extract features from an image without adding them to your search context:

curl -v "https://api.aspose.cloud/v3/imaging/ai/imageSearch/YOUR_SEARCH_CONTEXT_ID/image2features?imageId=my_temp_image.jpg" \
-X GET \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Content-Length: 0" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Replace the placeholders as in the previous method. The imageId parameter is just for reference and doesn’t need to match an existing image.

Step 3: Examine the Extracted Features

You’ll receive a response containing the extracted features:

{
  "ImageId": "my_temp_image.jpg",
  "FeaturesCount": 508,
  "FeatureLengthInBits": 488,
  "Features": "ot6WQAEAAAAAAAAAlVD6OoeptkDV2K9CpC8qQj0AAAD...",
  "Code": 200,
  "Status": "OK"
}

The Features field contains a base64-encoded representation of the image features, and the FeaturesCount tells you how many distinct features were identified.

Try It Yourself

Now, let’s practice extracting features from images:

  1. Obtain an access token using the curl command
  2. Try both methods of feature extraction on the same image
  3. Compare the response times and result formats
  4. Extract features from different types of images (photos, graphics, etc.) to see how the feature counts vary

Using SDK Libraries

Java SDK Example (Method 1: Extract and Add)

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

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

public class ExtractAndAddFeaturesExample {
    
    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";
        
        // Path to your local image file
        String imagePath = "/path/to/your/image.jpg";
        
        try {
            // Read the image into a byte array
            File imageFile = new File(imagePath);
            byte[] imageData = Files.readAllBytes(imageFile.toPath());
            
            // Extract features and add to search context
            api.createImageFeatures(searchContextId, imageData, null, null, null);
            
            System.out.println("Features successfully extracted and added to search context");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Java SDK Example (Method 2: Extract Only)

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

public class ExtractFeaturesOnlyExample {
    
    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";
        
        // A reference ID for the image
        String imageId = "my_temp_image.jpg";
        
        try {
            // Extract features only
            ImageFeatures features = api.extractImageFeatures(searchContextId, imageId, null, null, null);
            
            // Print the extracted features information
            System.out.println("Image ID: " + features.getImageId());
            System.out.println("Features Count: " + features.getFeaturesCount());
            System.out.println("Feature Length In Bits: " + features.getFeatureLengthInBits());
            System.out.println("Features (truncated): " + features.getFeatures().substring(0, 50) + "...");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

.NET SDK Example (Method 1: Extract and Add)

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

namespace AsposeImagingCloudTutorials
{
    class ExtractAndAddFeaturesExample
    {
        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";
            
            // Path to your local image file
            string imagePath = @"C:\path\to\your\image.jpg";
            
            try
            {
                // Read the image into a byte array
                byte[] imageData = File.ReadAllBytes(imagePath);
                
                // Extract features and add to search context
                api.CreateImageFeatures(searchContextId, imageData);
                
                Console.WriteLine("Features successfully extracted and added to search context");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }
    }
}

Advanced Use Cases for Image Features

Feature extraction enables several advanced use cases:

  1. Custom Similarity Metrics: By having access to the raw features, you can implement custom similarity calculations.
  2. Feature Analysis: You can analyze which types of images generate more features (usually more detailed images).
  3. Incremental Updates: You can extract features from new images and add them to your search index without rebuilding the entire database.
  4. Feature Comparison: Extract features from two images to directly compare them without adding them to your search context.

What You’ve Learned

In this tutorial, you’ve learned:

  • What image features are and their role in reverse image search
  • How to extract features and add them to your search context
  • How to extract features without adding them to your search context
  • How to use SDK libraries for feature extraction
  • Advanced use cases for image feature extraction

Troubleshooting Tips

  • Low Feature Count: Some simple images may have very few features. This is normal and not an error.
  • Large Images: Very large images may timeout during feature extraction. Consider resizing them first.
  • Binary vs. Text Features: The features are returned in a binary format encoded as base64 text. Don’t attempt to parse them manually.
  • Feature Storage: Features are already stored efficiently in the search context; you don’t need to store them separately.

Next Steps

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

Tutorial: Finding Similar Images

In the next tutorial, you’ll learn how to use the features you’ve extracted to find similar images in your search context.

Further Practice

To reinforce what you’ve learned:

  • Extract features from a diverse set of images and compare the feature counts
  • Create a script that extracts features from a batch of images
  • Try extracting features from the same image in different formats to see if there are differences
  • Experiment with downloading images from a URL, extracting features, and adding them to your search context

Helpful Resources

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