Comapre between two images?

6 次查看(过去 30 天)
Hadeel H
Hadeel H 2021-6-2
编辑: DGM 2024-2-15
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 个评论
Adam Danz
Adam Danz 2021-6-2
编辑:Adam Danz 2021-6-2
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 2021-6-3
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?

请先登录,再进行评论。

回答(1 个)

Tejas
Tejas 2024-2-15
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 个评论
DGM
DGM 2024-2-15
编辑:DGM 2024-2-15
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by