Comapre between two images?
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Adam Danz
il 2 Giu 2021
Modificato: Adam Danz
il 2 Giu 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.
Risposte (1)
Tejas
il 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
This solution has been tested in MATLAB R2023b. To know more about “size” function in MATLAB , please refer to this documentation : https://www.mathworks.com/help/matlab/ref/size.html?&s_tid=srchtitle_support_results_1_size#:~:text=2%0A%0A%20%20%20%20%205%20%20%20%20%204-,Dimension%20Lengths%20as%20Separate%20Arguments,numCols%20%3D%203,-Input%20Arguments .
Hope it helps!
1 Commento
DGM
il 15 Feb 2024
Modificato: DGM
il 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)
% downsample with bicubic without antialiasing
k = 5;
outpict = imresize(inpict,k,'bicubic');
outpict = imresize(outpict,1/k,'bicubic','antialiasing',false);
nnz(inpict ~= outpict)
% downsample with nearest-neighbor with antialiasing
k = 5;
outpict = imresize(inpict,k,'bicubic');
outpict = imresize(outpict,1/k,'nearest');
nnz(inpict ~= outpict)
% 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)
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.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!