Clear Filters
Clear Filters

hello friends, I am executing a code for compression using DPCM

2 views (last 30 days)
close all;
clear all;
clc;
A = imread('lena256.bmp');
[Height,Width,Depth] = size(A);
if Depth > 1
A1 = double(A(:,:,1));
else
A1 = double(A);
end
fileInfo = dir('lena256.bmp');
[rows, columns, numberOfColorChannels] = size(A)
fileSizeInProgram = rows * columns * numberOfColorChannels
fprintf('File size on disk = %f bytes.\n', fileInfo.bytes);
fprintf('File size in program = %f bytes.\n', fileSizeInProgram);
comp_ratio=fileInfo.bytes/fileSizeInProgram
Qtype = 'uniform'; % ’uniform’ and ’nonuniform’
B = 5; % # bits of quantization
y=DPCM_2D(A1,Qtype,B);
-----------------function DPCM_2D---------------------
function y=DPCM_2D(f,Qtype,B)
% DPCM 2D(f,Qtype,B)
% Designs and implements 2D DPCM where
% xhat = a1*x(m,n-1) + a2*x(m-1,n) + a3*x(m-1,n-1)...
% + a4*x(m-1,n+1)is the predictor
%
% Input:
% f = intensity image to be encoded
% Qtype = quantizer type: "uniform" or "nonuniform"
% B = # bits of quantization
%
% alfa = predictor coefficient
% E = unquantized prediction error image
% pe = unquantized differential image
% peq = quantized differential image
% y = DPCM reconstructed image
%disp('tena');
% This function implements only the encoder.
L = 2^B; % # levels in the quantizer
[Height,Width] = size(f);% get the image 'lena256.bmp'
%
% compute optimal predictor coefficients
[alfa,E] = LinearPredict_2D(f);%LinearPredict_2D('lena256.bmp');
%
% Design the uniform quantizer using 5*std dev as the limits
switch Qtype
case 'uniform'
dMin = mean2(E) - 5*std2(E);
dMax = mean2(E) + 5*std2(E);
q = 2*dMax/L; % step size
q2 = q/2;
dR = linspace(dMin,dMax,L+1); % decision intervals
rL = zeros(L,1); % reconstruction levels
for k = 1:L
rL(k) = dR(k)+q2;
end
case 'nonuniform'
[DR,C] = dpcmQuantizer(E,B);% design a B-bit
end
Mu = mean2(f);% mean value of the image
f = f - Mu;% remove mean value
% Implement the 2D DPCM
y = zeros(Height,Width);% array to store reconstructed image
pe = zeros(Height,Width);% array to store differential image
peq = zeros(Height,Width);% array to store quantizeddifferential image
x1 = zeros(Height+1,Width+2); % array to store reconstructed image
y(1,:) = f(1,:) + Mu;
y(:,1) = f(:,1) + Mu;
%
f = padarray(f,[1 2],'symmetric','pre');
% First row, first column no prediction
x1(1,:) = f(1,:);% store previously reconstructed pixels
x1(:,1) = f(:,1);
for r = 2:Height
for c = 2:Width
xhat = alfa(1)*x1(r,c-1) + alfa(2)*x1(r-1,c) + ...
alfa(3)*x1(r-1,c-1)+ alfa(4)*x1(r-1,c+1);
pe(r,c) = f(r,c) - xhat;
switch Qtype
case 'uniform'
for k = 1:L
if pe(r,c)>dR(k) && pe(r,c)<=dR(k+1)
peq(r,c) = rL(k);
elseif pe(r,c)<= dR(1)
peq(r,c) = rL(1);
elseif pe(r,c) > dR(L+1)
peq(r,c) = rL(L);
end
end
case 'nonuniform'
%{
for k = 1:L
if (pe(r,c)>DR(k) && pe(r,c)<=DR(k+1))
peq(r,c) = C(k);
end
end
%}
d1 = abs(pe(r,c)-C(1));
for k = 2:L
d2 = abs(pe(r,c)-C(k));
if d2<d1
d1 = d2;
J = k;
end
end
peq(r,c) = C(J);
end
x1(r,c) = peq(r,c) + xhat;% previously
y(r,c) = x1(r,c) + Mu;% mean added reconstructed pixel
%y.Height;
end
end
% Display differential and reconstructed images
figure(4),imshow(pe,[]), title('Differential image');
pause(2);
figure(5);
imshow(y,[]);
title('Compressed using DPCM');
end
*I WANT TO KNOW ,?IS MY CALCULATION FOR COMPRESSION RATIO IS CORRECT *
Output I got is--
rows = 256 columns = 256 numberOfColorChannels = 1 fileSizeInProgram = 65536 File size on disk = 66614.000000 bytes. File size in program = 65536.000000 bytes. comp_ratio = 1.01644897460938

Answers (1)

ALI NADEEM
ALI NADEEM on 23 Nov 2022
How could you store prediction error or difference of frames whose value distributed betwwen 255 and -255 value because my frames are uint8 can you tell me I have aproblem in it

Community Treasure Hunt

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

Start Hunting!