Hi Ahereza,
From what I gather you are trying to compress images of same dimensions for PSNR analysis but are getting images of different dimensions.
To achieve the desired results, you can go through the following steps along with the attached code snippets:
1. Implement a ‘try-catch’ block to handle potential errors when loading the image file, ensuring that the script does not crash if the file is missing, or the path is incorrect.
error('File "img.png" does not exist.');
2. Convert the image to grayscale using ‘rgb2gray’ function and store the dimensions in ‘row’ and ‘col’ variables.
[row, col] = size(grayIm);
3. Apply Fourier Transformation using ‘fft2’ function converting it into frequency domain and sort the Fourier coefficients by magnitude to facilitate thresholding based on size.
Btsort = sort(abs(A(:)));
4. Iterate over pre-defined compression levels, using a threshold to retain significant Fourier coefficients. Then apply ‘ifft2’ to reconstruct the image from these retained components.
for keep = [0.99, 0.05, 0.01, 0.002]
thresh = Btsort(floor((1 - keep) * length(Btsort)));
count = row * col - sum(ind(:));
per = 100 * sum(ind(:)) / (row * col);
5. Extract the real part of the reconstructed image, convert it into ‘uint8’ format and display the compressed image.
Blow = uint8(real(Blow));
title([num2str(per) '% of FFT basis']);
For better understanding, you can refer to the output attached below:
For more information, kindly go through the documentations linked below:
Happy Coding!