Comapre between two images?

6 views (last 30 days)
Hadeel H
Hadeel H on 2 Jun 2021
Edited: DGM on 15 Feb 2024
I resized my image using
I= imread('my_image.jpg');
J = imresize(I, 2, 'bicubic')
But I want to know how to compare between my original image(I) and the resized image( J), and then display the errors and fix if there is any corrupted pixel?
  2 Comments
Adam Danz
Adam Danz on 2 Jun 2021
Edited: Adam Danz on 2 Jun 2021
What do you mean by compare? Their image sizes will differ; they will have a different number of pixels, and depending on the resolution, they will likely have a visually noticeable difference in quality. As noted in the documention, with bicubic interpolation, the output pixel value is a weighted average of pixels in the nearest 4-by-4 neighborhood and can produce pixel values outside the original range.
What would you define as an error or a corrupted pixel? Remember that you will not have a 1:1 correspondence of pixels between the original and resized image.
Seeing the original image and the resized image may help with conveying the goal.
Hadeel H
Hadeel H on 3 Jun 2021
I know there will be differences in number of pixels between the two images (the original and the resized one), but how can I show theses differences in matlab like how to show the number of pixels of each and how to show if there is any error or any corrupted pixel between the original and the resized image?

Sign in to comment.

Answers (1)

Tejas
Tejas on 15 Feb 2024
Hello Hadeel,
The number of pixels for an image can be found using “size” function of MALTAB. To compare both the images, which are of different dimensions, there is no straight-forward approach for pixel-wise comparison. I would like to suggest a work-around, for comparing two images, one image can be converted to dimensions of the other. Now pixels-wise comparison can be done based on some desired threshold.
I am assuming that by "corrupted pixels," you are referring to whether the pixel values deviate from their expected values after they are resized. In other words, you are inquiring whether the resize function results in any unexpected pixel values. The following solution is based on this assumption.
Below is the solution code snippet and screenshot of desired result.
I = imread('Sample_Image.jpg');
J = imresize(I, 2, 'bicubic');
disp(['Total number of pixels in image I: ', num2str(NumOfPixels(I)) ]);
disp(['Total number of pixels in image J: ', num2str(NumOfPixels(J)) ]);
% Resizing J for comparison
K = imresize(J, 0.5, 'bicubic');
disp(['Total number of pixels in image K: ', num2str(NumOfPixels(K)) ]);
% Give both images and desired threshold as input arguments
threshold = 5;
disp(['Difference in pixels : ', num2str(Compare(I, K, threshold)) ]);
function Total_Pixels = NumOfPixels(img)
[n_rows, n_cols, n_channels] = size(img);
Total_Pixels = n_rows * n_cols * n_channels;
end
function Num_Corrupt_Px = Compare(I, K, threshold)
diff_in_pixels = abs(double(I) - double(K));
Num_Corrupt_Px = diff_in_pixels > threshold;
Num_Corrupt_Px = sum(Num_Corrupt_Px(:));
end
Hope it helps!
  1 Comment
DGM
DGM on 15 Feb 2024
Edited: DGM on 15 Feb 2024
Testing a round-trip transformation still doesn't really tell us anything meaningful about the forward operation alone. The result is strongly determined by the second transformation.
inpict = imread('cameraman.tif');
inpict = im2double(inpict); % don't want integer rounding obfuscating things
% downsample with bicubic with antialiasing
k = 5;
outpict = imresize(inpict,k,'bicubic');
outpict = imresize(outpict,1/k,'bicubic');
nnz(inpict ~= outpict)
ans = 65536
% downsample with bicubic without antialiasing
k = 5;
outpict = imresize(inpict,k,'bicubic');
outpict = imresize(outpict,1/k,'bicubic','antialiasing',false);
nnz(inpict ~= outpict)
ans = 0
% downsample with nearest-neighbor with antialiasing
k = 5;
outpict = imresize(inpict,k,'bicubic');
outpict = imresize(outpict,1/k,'nearest');
nnz(inpict ~= outpict)
ans = 0
% just do it without imresize()
k = 5;
os = ceil(k/2);
outpict = imresize(inpict,k,'bicubic');
outpict = outpict(os:k:end,os:k:end);
nnz(inpict ~= outpict)
ans = 0
Now try it with k = 4 and see what happens. Is the image quality actually 60 thousand times worse? No. Our sampling is just off by half a pixel. In other words, the magnitude of observed error is determined largely by our method of observation, not the actual thing we're measuring.
Note that's a count of all pixels which differ. It's not a class-sensitive approach which presumes a uint8 input.
FWIW, I don't really know what OP was after, so I have no idea what would be a better answer.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!