How to Compare Two Images for Similarity using Java
Comparing digital images for similarities can be accomplished in a few common ways. While Perceptual hashing is among the most known & used methods in the world of Digital Forensics, Deep Learning methods can be just as effective — albeit in more limited use cases. Our Deep Learning Image Comparison API allows you to compare two images directly based on two image file inputs (rather than hashes), and provides a similarity score between 0.0–1.0, with lower values indicating less similarity, and higher values indicating greater likelihood of a match. It’s easy & free to use — you just need to register a free account on our website (this will supply an API key valid for 800 API calls per month) and follow instructions below to structure your API call with ready-to-run Java code examples.
To start, we need to install the SDK with Maven. Let’s first add a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, let’s complete our installation step & add a reference to the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Our next & final step is to call the function. First, let’s add the imports to the top of our file, and then let’s include our API key & file paths in their respective fields (indicated by the code comments):
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.RecognizeApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");RecognizeApi apiInstance = new RecognizeApi();
File baseImage = new File("/path/to/inputfile"); // File | Image file to compare against. Common file formats such as PNG, JPEG are supported.
File comparisonImage = new File("/path/to/inputfile"); // File | Image to compare to the base image.
String recognitionMode = "recognitionMode_example"; // String | Optional, specify the recognition mode; possible values are Normal, Basic and Advanced. Default is Normal.
try {
byte[] result = apiInstance.recognizeSimilarityCompare(baseImage, comparisonImage, recognitionMode);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling RecognizeApi#recognizeSimilarityCompare");
e.printStackTrace();
}
Below the second file input field, you’ll notice a field labeled “recognitionMode.” This field can be optionally configured with “basic” and “advanced” values in string format (default is “normal”). Electing “basic” recognition will reduce the number of API calls used per operation in exchange for reducing the depth of Deep Learning analysis. Electing “advanced” recognition will increase the number of API calls used per operation while increasing the depth of analysis.