Clear Filters
Clear Filters

How to compare between two images ?

17 views (last 30 days)
Sally Sakr
Sally Sakr on 14 May 2022
Edited: Umeshraja on 21 Sep 2024 at 10:32
I want to compare an image with another one knowing if they are identical or not, the project is about welding defects using ultrasonic, so I want to make a model telling if this image shows a defect or not ( has no defect )

Accepted Answer

Umeshraja
Umeshraja on 18 Sep 2024 at 10:37
Edited: Umeshraja on 21 Sep 2024 at 10:32
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);
SSIM: 0.938800
% 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.');
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: 0.999982
% HOG similarity threshold check
hogThreshold = 0.97;
if hogSimilarity > hogThreshold
disp('The images are similar based on HOG features.');
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:
These resources provide comprehensive insights into the various methods and tools available in MATLAB for image comparison and analysis.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!