# can anyone please convert this codef or 512*512*3 lena image, it is working well with * lena image

16 views (last 30 days)
tina jain on 26 Apr 2015
Edited: tina jain on 27 Apr 2015
close all;
clear all;
clc;
RGB = ind2rgb(A,map);
figure(1);
imshow(RGB);
title('original image');
pause(2);*
[Height,Width,Depth] = size(RGB);
if Depth > 1
A1 = double(RGB(:,:,1));
else
A1 = double(RGB);
end
Qtype = 'uniform';
B = 5;
y=DPCM_2D(A1,Qtype,B);
%-------------function DPCM_2D-----------
function y=DPCM_2D(f,Qtype,B,map)
L = 2^B; % # levels in the quantizer%2^5=32
[Height,Width,Depth] = size(f);% get the image 'lena256.bmp'%512*512*1
%
% compute optimal predictor coefficients
[alfa,E] = LinearPredict_2D(f,map);%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;
%
% 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
%%%%function LinearPredict
function [alfa,pe] = LinearPredict_2D(I,map)%I='lena256.bmp'%512*512 double
% [alfa,pe] = LinearPredict 2D(I)
% I = input intensity image
% alfa = predictor coefficient vector
% pe = prediction error image of the same size as I
% Uses 2D linear prediction to
% create the differential image
% xhat(m,n) = alfa1(1)*x(m,n-1)+alfa(2)*x(m-1,n)+...
% alfa(3)*x(m-1,n-1)+alfa(4)*x(m-1,n+1)
[Height,Width,Depth] = size(I);
if Depth > 1
I1 = double(I(:,:,1));
else
I1 = double(I);%512*512 double
end
AAA=ind2rgb(I1,map);
Mu = mean2(I);% mean value of the image%0.4866
I = I - Mu;% mean removed input image
r00 = mean2(I .* I);
r01 = sum(sum(I(:,2:Width).* I(:,1:Width-1)))/(Height*(Width-1));
r10 = sum(sum(I(2:Height,:).* I(1:Height-1,:)))/((Height-1)*Width);
r11 = sum(sum(I(2:Height,2:Width).* I(1:Height-1,1:Width-1)))/((Height-1)*(Width-1));
r12 = sum(sum(I(2:Height,3:Width).* I(1:Height-1,1:Width-2)))/((Height-1)*(Width-2));
r02 = sum(sum(I(1:Height,3:Width).* I(1:Height,1:Width-2)))/(Height*(Width-2));
alfa = inv([r00 r11 r10 r12; r11 r00 r01 r01;r10 r01 r00 r02; r12 r01 r02 r00])/[r01 r10 r11 r11];
% Implement the Linear Predictor
pe = zeros(Height,Width,Depth);% array to store prediction errors
for r = 2:Height+1
for c = 2:Width+1
xhat = alfa(1)*I(r,c-1)+alfa(2)*I(r-1,c)+ ...
alfa(3)*I(r-1,c-1) + alfa(4)*I(r-1,c+1);
pe(r-1,c-1) = I(r,c) - xhat;% differential pixel
end
end
code in bold is changes that I made to convert indexed image to rgb but than also getting the image I attached
##### 2 CommentsShowHide 1 older comment
tina jain on 27 Apr 2015
Edited: tina jain on 27 Apr 2015
yes it is a 2D indexed image. I am trying to apply 2D DPCM on the image

### Categories

Find more on Image Segmentation and Analysis in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!