Compare Two Images Introduction
This article explains how to compare two images. The first image will be from search context while another image can be either from search context or loaded in request.
The API URL is: https://apireference.aspose.cloud/imaging/#/SearchContext/CompareImages
Resource URI
Swagger UI lets you call Aspose.Imaging REST APIs directly from the browser. The description of the APIs and there parameters are also given there.
cURL Example
SDKs
Using an SDK (API client) is the quickest way for a developer to speed up the development. An SDK takes care of a lot of low-level details of making requests and handling responses and lets you focus on writing code specific to your particular project. Checkout our GitHub repository for a complete list of Aspose.Imaging SDKs along with working examples, to get you started in no time.
SDK Examples
.NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-imaging-cloud/aspose-imaging-cloud-dotnet
public void CompareTwoImagesInSearchContext()
{
string ComparableImage = "ComparableImage.jpg";
string ComparingImageSimilarMore75 = "ComparingImageSimilar75.jpg";
// Create new search context
string searchContextId = this.CreateImageSearch();
// Upload both images to Cloud Storage
string[] images = { ComparableImage, ComparingImageSimilarMore75 };
foreach (string imageName in images)
{
// Upload local image to Cloud Storage
using (FileStream localInputImage = File.OpenRead(ImagingBase.PathToDataFiles + imageName))
{
var uploadFileRequest = new UploadFileRequest(imageName, localInputImage);
FilesUploadResult result = this.ImagingApi.UploadFile(uploadFileRequest);
}
}
this.CreateImageFeatures(ComparableImage, false, searchContextId);
this.CreateImageFeatures(ComparingImageSimilarMore75, false, searchContextId);
// Compare two images
string folder = null; // Folder with image to process
string storage = null; // We are using default Cloud Storage
SearchResultsSet searchResults = this.ImagingApi.CompareImages(
new CompareImagesRequest(searchContextId, ComparableImage, null, ComparingImageSimilarMore75, folder, storage));
double? similarity = searchResults.Results[0].Similarity;
Console.WriteLine("Images Similarity: " + similarity.ToString());
// Delete the search context
this.DeleteImageSearch(searchContextId);
}
Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String APP_KEY = ""; // Get AppKey and AppSID from https:dashboard.aspose.cloud/
String APP_SID = ""; // Get AppKey and AppSID from https:dashboard.aspose.cloud/
String DATA_PATH = "src/main/resources/";
ImagingApi imagingApi = new ImagingApi(APP_KEY, APP_SID);
String imageInStorage1 = "aspose-logo.jpg";
String imageInStorage2 = "aspose_logo.png";
try {
// Upload both images to Cloud Storage
String[] images = {imageInStorage1, imageInStorage2};
for (String imageName : images) {
File inputFile = new File(DATA_PATH + imageName);
FileInputStream localInputImageStream = new FileInputStream(inputFile);
byte[] localInputImage = IOUtils.toByteArray(localInputImageStream);
UploadFileRequest uploadFileRequest = new UploadFileRequest(imageName, localInputImage, null);
FilesUploadResult result = imagingApi.uploadFile(uploadFileRequest);
}
// Compare two images
String searchContextId = "5728a4ed-12bb-4b4a-8dce-38295735549f";
String folder = null; // Folder with image to process
String storage = null; // We are using default Cloud Storage
SearchResultsSet result = imagingApi.postSearchContextCompareImages(
new PostSearchContextCompareImagesRequest(
searchContextId, imageInStorage1, null, imageInStorage2, folder, storage));
Double similarity = result.getResults().get(0).getSimilarity();
System.out.println("Images Similarity: " + similarity.toString());
} catch (Exception e) {
System.out.println(e.getMessage());
}