I understand that you want to compare two images in MATLAB. Here are some methods for the same:
- Structural Similarity Index (SSIM):
- This method evaluates images based on luminance, contrast, and structure, providing an index that measures image quality. A higher SSIM value indicates greater similarity between images. You can set a threshold to determine if the images are similar.
- Feature Matching:
- This involves detecting, extracting, and matching features between images. MATLAB's “Computer Vision Toolbox” includes algorithms such as “FAST”, “Harris”, and “Shi & Tomasi” corner detectors. It also includes, blob detectors like “SIFT”, “SURF” etc.. These methods help identify corresponding points or regions in the images.
- Histogram of Oriented Gradients (HOG):
- This feature descriptor is used in computer vision and image processing to capture the distribution of gradient orientations in localized portions of an image. It can be used to compare the structural content of images.
Here is a MATLAB script demonstrating these methods using an example:
1. Preparing the data
close all;
% Read the input image 'cameraman.tif'
originalImage = imread("cameraman.tif");
% Create a slightly brighter version of the original image
modifiedImage = imadd(originalImage, 20);
% Display the original and modified images
figure();
set(gcf, 'Position', [100, 100, 400, 200]);
subplot(1, 2, 1); imshow(originalImage); title('Original Image');
subplot(1, 2, 2); imshow(modifiedImage); title('Modified Image');
2. SSIM method:
% Calculate the SSIM
[ssimValue, ssimMap] = ssim(originalImage, modifiedImage);
fprintf("SSIM: %f\n", ssimValue);
% Display the SSIM map
figure;
imshow(ssimMap, []);
colorbar;
title(['SSIM Map (SSIM Value: ', num2str(ssimValue), ')']);
set(gcf, 'Position', [100, 100, 400, 200]);
% SSIM threshold check
ssimThreshold = 0.90;
if ssimValue > ssimThreshold
disp('The images are similar based on SSIM.');
else
disp('The images are different based on SSIM.');
end
3. SURF features:
% Detect and display SURF features
surfPointsOriginal = detectSURFFeatures(originalImage);
surfPointsModified = detectSURFFeatures(modifiedImage);
figure;
subplot(1, 2, 1); imshow(originalImage); hold on; plot(surfPointsOriginal.selectStrongest(50));
title('SURF Features - Original Image');
subplot(1, 2, 2); imshow(modifiedImage); hold on; plot(surfPointsModified.selectStrongest(50));
title('Modified Image');
set(gcf, 'Position', [100, 100, 500, 200]);
% Extract and match SURF features
[surfFeaturesOriginal, validSurfPointsOriginal] = extractFeatures(originalImage, surfPointsOriginal);
[surfFeaturesModified, validSurfPointsModified] = extractFeatures(modifiedImage, surfPointsModified);
surfIndexPairs = matchFeatures(surfFeaturesOriginal, surfFeaturesModified);
% Display matched SURF features
matchedSurfPointsOriginal = validSurfPointsOriginal(surfIndexPairs(:, 1));
matchedSurfPointsModified = validSurfPointsModified(surfIndexPairs(:, 2));
figure;
showMatchedFeatures(originalImage, modifiedImage, matchedSurfPointsOriginal, matchedSurfPointsModified, 'montage');
title('Matched SURF Features');
set(gcf, 'Position', [100, 100, 500, 200]);
4. HOG features:
% Extract and compare HOG features
[hogFeaturesOriginal, ~] = extractHOGFeatures(originalImage);
[hogFeaturesModified, ~] = extractHOGFeatures(modifiedImage);
% Compare HOG features
hogSimilarity = 1 - pdist2(hogFeaturesOriginal, hogFeaturesModified, 'cosine');
fprintf('HOG Similarity: %f\n', hogSimilarity);
% HOG similarity threshold check
hogThreshold = 0.97;
if hogSimilarity > hogThreshold
disp('The images are similar based on HOG features.');
else
disp('The images are different based on HOG features.');
end
You can observe the differences between the two images using these methods.
For further details on detectors and descriptors in MATLAB, please refer to the following resources:
- Feature Detection and Extraction Documentation:
- https://www.mathworks.com/help/vision/feature-detection-and-extraction.html
- Structural Similarity Index (SSIM):
- https://www.mathworks.com/help/images/ref/ssim.html
- Feature Matching Documentation
- https://www.mathworks.com/help/vision/ref/matchfeatures.html
These resources provide comprehensive insights into the various methods and tools available in MATLAB for image comparison and analysis.