Why does ssim only return values of 1?

6 views (last 30 days)
Warren Boschen
Warren Boschen on 18 Sep 2023
Answered: Siraj on 27 Sep 2023
I'm attempting to use ssim to compare two images. The idea is to compare every slice and "dynamic" along the third and fourth dimensions of the 4D array dynamics respectively with every slice from the very last entry of dynamics, but whenever I run this I'm returning only values of 1, which would make no sense unless I'm comparing an image with itself. Here is my code:
mon_struc = dir('*.mat');
for i = 1:length(mon_struc)
dynamics(:, :, :, i) = importdata(mon_struc(i).name);
end % dynamics ends up being 100x100x24x7.
nums = size(dynamics, 3); % 24
numd = size(dynamics, 4); % 7
ss = zeros(numd - 1, nums); % Don't include the last dynamic since that's the reference.
for mon = 1:numd-1
for slice = 1:nums
ss(mon, slice) = ssim(dynamics(:, :, slice, mon), dynamics(:, :, slice, end));
end
end
ss_mean = mean(ss, 2);
How do I fix this? Any insight you have would be greatly appreciated please.
Thanks,
Warren
  1 Comment
Matt J
Matt J on 18 Sep 2023
Here is my code:
We can't run your code without input data to go with it.

Sign in to comment.

Answers (1)

Siraj
Siraj on 27 Sep 2023
Hi!
It seems that you are loading data from various '.mat' files into the 'dynamics' 4D array, each file representing a unique 3D image. However, you consistently get SSIM values of 1, suggesting that the image slices you're comparing are extremely similar, which might indicate a problem with either your data or the SSIM calculation.
First ensure that the data loaded from the ".mat" files is accurately representing meaningful images. It's important to check whether the pixel values are appropriately normalized, as this can significantly affect SSIM calculations. Additionally, take a closer look at the parameters used with the "ssim" function, including "DataFormat" and "DynamicRange," as these parameters can have an impact on the SSIM result. Keep in mind that when dealing with highly similar images, SSIM values tend to approach one, as demonstrated in the following code.
% Create a 3D image with dimensions 5x5x5 and initialize it with ones.
image = ones(5, 5, 5);
% Set the values in the first slice (z = 1) to be slightly different.
image(:,:,1) = 1.005;
% Calculate the Structural Similarity Index (SSIM) between the first and second slices.
similarity = ssim(image(:,:,1), image(:,:,2))
similarity = 1.0000
For additional information regarding the parameters of the "ssim" function, refer to the following link.
To investigate further, you can print out the actual pixel values of the slices you are comparing and visually inspect them to confirm if they are indeed very similar.
Hope this helps.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!