Hello, am trying to compress images of the same dimensions using Fourier transform but i get the result with totally different dimensions which has made it hard to get psnr

2 Ansichten (letzte 30 Tage)
close all
clear all
clc
a=imread('C:\Users\hp\Desktop\research\New folder\Image5.jpg');
Error using imread>get_full_filename
File "C:\Users\hp\Desktop\research\New folder\Image5.jpg" does not exist.

Error in imread (line 372)
fullname = get_full_filename(filename);
grayIm =rgb2gray(a);
[row col] = size(grayIm);
figure;
imshow(grayIm);title('original image')
A=fft2(grayIm);
count_pic=2;
Btsort = sort(abs(A(:)));
for keep=[.99 .05 .01 .002];
thresh = Btsort(floor((1-keep)*length(Btsort)));
ind=abs(A)>thresh;
count=row*col-sum(sum(ind));
Alow=A.*ind;
per=100-count/(row*col)*100
Blow=uint8(ifft2(Alow));
figure;
imshow(Blow);
count_pic=count_pic+1;
title([num2str(per) '% of fft basis'])
end

Antworten (1)

Suraj Kumar
Suraj Kumar am 28 Aug. 2024
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.
try
a = imread('img.png');
catch
error('File "img.png" does not exist.');
end
2. Convert the image to grayscale using ‘rgb2gray’ function and store the dimensions in ‘row’ and ‘col’ variables.
grayIm = rgb2gray(a);
[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.
% Compute the 2D Fourier Transform
A = fft2(grayIm);
% Sort the Fourier coefficients by magnitude
Btsort = sort(abs(A(:)));
4. Iterate over pre-defined compression levels, using a threshold to retain significant Fourier coefficients. Then applyifft2 to reconstruct the image from these retained components.
% Loop over different levels of compression
for keep = [0.99, 0.05, 0.01, 0.002]
thresh = Btsort(floor((1 - keep) * length(Btsort)));
ind = abs(A) > thresh;
count = row * col - sum(ind(:));
Alow = A .* ind;
per = 100 * sum(ind(:)) / (row * col);
Blow = ifft2(Alow);
5. Extract the real part of the reconstructed image, convert it into ‘uint8’ format and display the compressed image.
Blow = uint8(real(Blow));
figure;
imshow(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!

Community Treasure Hunt

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

Start Hunting!

Translated by