How to Crop and get SSIM Value apply on two images
2 views (last 30 days)
Show older comments
shubham kumar gupta
on 21 Feb 2022
Commented: shubham kumar gupta
on 27 Feb 2022
I was struggling to get ssim value with two images of coins, as the placement position of coin is different little bit
like I have two coin images, 1st image 401x401,2nd image 441x441, on comparision
due to positions of images at different positions ssim(img1,img2) is giving incorrect output, I tried cropping but that didnot worked
Thank You
2 Comments
DGM
on 21 Feb 2022
Edited: DGM
on 21 Feb 2022
If you were to register the two images as best you could, what value would the error metric retain? How do you propose to separate the influence of the spatial transformations from the differences in the unregistered images? Why would SSIM be a particularly relevant error metric?
I should add that the fact that the data range of the two images varies by a few orders of magnitude means that everything you do to try to put them on the same scale will become another inseparable component of the useless number you get out of ssim().
Accepted Answer
DGM
on 21 Feb 2022
Edited: DGM
on 21 Feb 2022
If it's assumed that there is no scale or rotation transformation, then you can go ahead and try to align the images.
load coin_ssim.mat
% pad reference image since object is so close to edges
refpict = padarray(mat2gray(clean_img),[20 20],'replicate','both');
% crop test image down to extract the object alone
badpict = imcrop(mat2gray(img2),[2.5 61.5 357 363]);
% maximize normalized cross-correlation to find offset
szb = size(badpict);
c = normxcorr2(badpict,refpict);
[idxy idxx] = find(c == max(c(:)));
osy = idxy-szb(1);
osx = idxx-szb(2);
% crop the reference pict to the ROI
refpict = refpict(osy:idxy-1,osx:idxx-1);
imshow(imfuse(badpict,refpict,'checkerboard'));
ssim(badpict,refpict) % a number
7 Comments
DGM
on 27 Feb 2022
You're not going to get significantly higher (e.g. 80%) SSIM through any amount of extra registration tweaking. As I've said already, there's little meaning to the SSIM when you cannot know the error contribution. If your goal is to measure the quality of your denoising, then you can't isolate that information from the error caused by the transformations required for registration. Neither is that number independent of the accuracy of the registration estimation itself. Overfitting the registration (e.g. trying to remove scale/rotation/skew when none was originally applied) only makes the number less meaningful.
The SSIM you get describes the end-to-end fidelity of the entire transformation -> noising -> denoising -> registration -> transformation process. If that's what you want, then I guess that the number would be appropriate.
I imagine the vast majority of the error has nothing to do with the remaining registration error. It has to do with the fact that the denoised image is so much darker and blurrier than the source. If that's the image you need to measure, then measuring it is what you get. If it needs to yet be adjusted, then maybe you can play with levels to get closer, or you can apply the filtering suggested in prior threads. I recall @yanqi liu posted a good noise masking example.
load coin_ssim.mat
load coin_ssim_reg.mat
refpict = mat2gray(clean_img);
badpict = regobj.RegisteredImage;
% adjust levels
badpict = imadjust(badpict,[0.15 0.55],[0 1],0.30);
imshow([badpict refpict])
ssim(badpict,refpict)
More Answers (1)
yanqi liu
on 22 Feb 2022
yes,sir,may be make them to same size,and then compare
load coin_ssim.mat
% get location
bw1 = im2bw(mat2gray(clean_img));
bw2 = imclose(im2bw(mat2gray(img2),0.3),strel('disk',9));
bw2 = bwareafilt(bw2,1);
% make same size
[r,c] = find(bw1);
clean_img = clean_img(min(r):max(r),min(c):max(c));
[r,c] = find(bw2);
img2 = img2(min(r):max(r),min(c):max(c));
img2 = imresize(img2, size(clean_img),'bilinear');
ssim(mat2gray(clean_img),mat2gray(img2))
% compare
figure; imshowpair(clean_img,img2);
figure; montage({mat2gray(clean_img),mat2gray(img2)}, 'Size', [1 2], 'BackgroundColor', 'w', 'BorderSize', [2 2]);
6 Comments
See Also
Categories
Find more on Geometric Transformation and Image Registration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!