Explaination of following code

3 visualizaciones (últimos 30 días)
sonali bandal
sonali bandal el 6 de En. de 2017
Respondida: Hari el 4 de Feb. de 2025
clear all;
close all;
clc;
I=imread('IMG1.jpg');
imshow(I);
title('INPUT IMAGE');
pause(.2);
%%RESIZING
I=imresize(I,[512,512]);
imshow(I);
title('RESIZED IMAGE');
pause(.2);
%%GRAY CONVERSION
[m,n,z]=size(I);
% m:Number of rows
% n:Number of coloumns
% z:Number of channeles i.r R,G,B
if z==3
I=rgb2gray(I);
imshow(I);
title('GRAY IMAGE');
pause(.2);
end
%%DESIGN BIORTHOGONAL FILTER
% Find the two scaling filters associated with bior3.5.
[Rf,Df] = biorwavf('bior3.5');
figure,
subplot(1,2,1);stem(Rf,'r','Linewidth',2);title('SCALING FILTER:RECONSTRUCTION');grid on
subplot(1,2,2);stem(Df,'g','Linewidth',2);title('SCALING FILTER:DECOMPOSITION');grid on
% Compute the four filters needed.
[Lo_D,Hi_D,Lo_R,Hi_R] = biorfilt(Df,Rf);
figure,
subplot(1,2,1);stem(Lo_D,'r','Linewidth',2);title('DECOMPOSITION LPF');grid on
subplot(1,2,2);stem(Hi_D,'g','Linewidth',2);title('DECOMPOSITION LPF');grid on
figure,
% High and low frequency illustration.
fftld = fft(Lo_D);
ffthd = fft(Hi_D);
freq = [1:length(Lo_D)]/length(Lo_D);
plot(freq,abs(fftld),'r','Linewidth',2);
hold on
plot(freq,abs(ffthd),'g','Linewidth',2);grid on
legend('LPF FREQ','HPF,FREQ')
title('TRANSFER MODULUS FOR DECOMPOSITION FILTER')
%%APPLY FOR FIRST LEVEL DECOMPOSITION
% FIRST LEVEL
[CA1,CH1,CV1,CD1] = dwt2(I,Lo_D,Hi_D);
figure,
subplot(2,2,1);imshow(uint8(CA1));title('APPROXIMATE COEFF:1');
subplot(2,2,2);imshow(uint8(CH1));title('DETAIL COEFF:1');
subplot(2,2,3);imshow(uint8(CV1));title('DETAIL COEFF:2');
subplot(2,2,4);imshow(uint8(CD1));title('DETAIL COEFF:3');
% % SECOND LEVEL
% [CA1,CH1,CV1,CD1] = dwt2(CA1,Lo_D,Hi_D);
% figure,
% subplot(2,2,1);imshow(uint8(CA1));title('APPROXIMATE COEFF:1');
% subplot(2,2,2);imshow(uint8(CH1));title('DETAIL COEFF:1');
% subplot(2,2,3);imshow(uint8(CV1));title('DETAIL COEFF:2');
% subplot(2,2,4);imshow(uint8(CD1));title('DETAIL COEFF:3');
% % THIRD LEVEL
% [CA1,CH1,CV1,CD1] = dwt2(CA1,Lo_D,Hi_D);
% figure,
% subplot(2,2,1);imshow(uint8(CA1));title('APPROXIMATE COEFF:1');
% subplot(2,2,2);imshow(uint8(CH1));title('DETAIL COEFF:1');
% subplot(2,2,3);imshow(uint8(CV1));title('DETAIL COEFF:2');
% subplot(2,2,4);imshow(uint8(CD1));title('DETAIL COEFF:3');
%%APPLY FOR FIRST LEVEL RECONSTRUCTION
I1= idwt2(CA1,CH1,CV1,CD1,Lo_R,Hi_R);
clc
OriImg=double(I);
DesImg=double(I1);
ImgDims = size( OriImg );
ImgSize = ImgDims( 1 ) * ImgDims( 2 );
ImgDif = DesImg - OriImg;
MSE = sum( sum( ImgDif .* ImgDif ) ) ./ ImgSize;
% MSE2 = mean((OriImg-DesImg)^2)
if ( MSE == 0 )
PSNR = 100;
else
PSNR = 10 * log10( ( 255.^ 2- 1 ) .^ 2 ./ MSE );
% PSNR2 = 10*log10(255^2/mean((OriImg-DesImg)^2))
PSNR=mod(PSNR,100);
end
fprintf('The mean square error is %.2f.\nThe PSNR = %.2f', MSE, PSNR);

Respuestas (1)

Hari
Hari el 4 de Feb. de 2025
Hi Sonali,
I understand that you want an explanation of the given MATLAB code, which involves image processing operations such as resizing, grayscale conversion, wavelet decomposition, and reconstruction, followed by calculating the Mean Square Error (MSE) and Peak Signal-to-Noise Ratio (PSNR).
Please find the explanation here:
The code begins by clearing the workspace, closing all figures, and clearing the command window. It reads an image "IMG1.jpg", displays it, and then resizes it to 512x512 pixels.
clear all;
close all;
clc;
I = imread('IMG1.jpg');
imshow(I);
title('INPUT IMAGE');
pause(.2);
I = imresize(I, [512, 512]);
imshow(I);
title('RESIZED IMAGE');
pause(.2);
The code checks the number of channels in the image. If the image is RGB (3 channels), it converts the image to grayscale and displays it.
[m, n, z] = size(I);
if z == 3
I = rgb2gray(I);
imshow(I);
title('GRAY IMAGE');
pause(.2);
end
It designs a biorthogonal wavelet filter using "bior3.5" and plots the scaling filters for reconstruction and decomposition. It then computes the four filters needed for decomposition and reconstruction.
[Rf, Df] = biorwavf('bior3.5');
figure,
subplot(1,2,1); stem(Rf, 'r', 'Linewidth', 2); title('SCALING FILTER:RECONSTRUCTION'); grid on;
subplot(1,2,2); stem(Df, 'g', 'Linewidth', 2); title('SCALING FILTER:DECOMPOSITION'); grid on;
[Lo_D, Hi_D, Lo_R, Hi_R] = biorfilt(Df, Rf);
The code applies the first level of wavelet decomposition using the designed filters and displays the approximation and detail coefficients. It also computes the transfer modulus for the decomposition filter.
[CA1, CH1, CV1, CD1] = dwt2(I, Lo_D, Hi_D);
figure,
subplot(2,2,1); imshow(uint8(CA1)); title('APPROXIMATE COEFF:1');
subplot(2,2,2); imshow(uint8(CH1)); title('DETAIL COEFF:1');
subplot(2,2,3); imshow(uint8(CV1)); title('DETAIL COEFF:2');
subplot(2,2,4); imshow(uint8(CD1)); title('DETAIL COEFF:3');
Finally, the code reconstructs the image using inverse wavelet transform, calculates the MSE and PSNR, and prints them.
I1 = idwt2(CA1, CH1, CV1, CD1, Lo_R, Hi_R);
OriImg = double(I);
DesImg = double(I1);
ImgDims = size(OriImg);
ImgSize = ImgDims(1) * ImgDims(2);
ImgDif = DesImg - OriImg;
MSE = sum(sum(ImgDif .* ImgDif)) ./ ImgSize;
if (MSE == 0)
PSNR = 100;
else
PSNR = 10 * log10((255.^2 - 1).^2 ./ MSE);
PSNR = mod(PSNR, 100);
end
fprintf('The mean square error is %.2f.\nThe PSNR = %.2f', MSE, PSNR);
Refer to the documentation for the following functions:

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by