back | home | abstract | contents | next chapter
          B

          MATLAB Files
 
 
 
 
param_estim.m main batch file
image_vq_pds_param.m sub batch file
codebook_lbg.m codebook generation
quantizer_pds.m image quantization
sq_euk_dist.m distortion computation
block_to_vect.m vector generation
vect_to_block.m image generation

 

begin chapter | next chapter

param_estim.m

% parameter estimation
%
% written by Stefan Gachter, EPFL

new;

tic

N=[64];
% N size of the codebook
k=[64];
% k dimension of the quantizer
H=10;
% H number of iterations

load IMAGES.MAT lena peppers baboon
load IMG.MAT barb gold

T=[peppers baboon barb gold];
% T training images
P=lena;
% P image

for i=1:length(N)

for j=1:length(k) image_vq_pds_param(H,N(i),k(j),T,P); end end

toc
 
 
 

begin chapter | next chapter

image_vq_pds_param.m

function image_vq_pds_param(H,N,k,T,P);

% image vector quantization
%
% function image_vq_pds_param(H,N,k,T,P);
%
% codebook : exhaustive search algorithm
% quanitzation : partial distance search algorithm
%
% k dimension of the quantizer
% N size of the codebook
% H number of iterations
% T training images
% P image
%
% written by Stefan Gachter, EPFL

% trainings set
% convert into vector

vect_t=block_to_vect(T,k);

% create inital codebook
C_init=init_codebook(vect_t,N);

save(['init_codebook' num2str(H) '_' num2str(N) '_' num2str(k) '.mat'], 'C_init')

C_old=C_init;
D_init=zeros(H,1);

for h=1:H

[C_new,D(h)]=codebook_lbg(C_old,vect_t);
C_old=C_new;
end

C=C_new;

save(['codebook' num2str(H) '_' num2str(N) '_' num2str(k) '.mat'], 'C')
save(['distortion' num2str(H) '_' num2str(N) '_' num2str(k) '.mat'], 'D')

% quantize

% quantizing set
% convert into vector
vect_p=block_to_vect(P,k);

[Y,S_ops,P_ops,C_ops]=quantizer_pds(vect_p,C);

save(['operations' num2str(H) '_' num2str(N) '_' num2str(k) '.mat'], 'S_ops', 'P_ops', 'C_ops')

% convert into block

[Mp,Np]=size(P);
I=vect_to_block(Y,Mp);

save(['image' num2str(H) '_' num2str(N) '_' num2str(k) '.mat'], 'I')
 
 
 

begin chapter | next chapter

codebook_lbg.m
 
 

function [C_new,D]=codebook_lbg(C_old,X);

% [C_new,D]=codebook_lbg(C_old,X)
%
% C_old old codebook
% X trainings set
% C_new new codebook
% D mean squared error
%
% written by Stefan Gachter, EPFL
 
 

[N,k]=size(C_old);
% N size of codebook
% k dimension of the quantizer

M=size(X,1);
% M number of training vectors

Vi_sets=zeros(N,M);
% Vi Voronoi regions

Di=zeros(N,1);
% Di Distortion

for m=1:M

i=1;
d2_ref=sq_euk_dist(X(m,:),C_old(i,:));
for j=1:N d2=sq_euk_dist(X(m,:),C_old(j,:));
if d2<d2_ref d2_ref=d2;
i=j;
end
end
Vi_sets(i,m)=1;
Di(i)=Di(i)+d2_ref;
end

C_new=zeros(N,k);
for i=1:N

Vi=[];
for m=1:M if Vi_sets(i,m) Vi=[Vi;X(m,:)]; end end
g=size(Vi,1);
if g~=0 C_new(i,:)=(ones(1,g)*Vi)./g; end
end

D=(ones(1,N)*Di)/N;
% D mean squared error
 
 
 

begin chapter | next chapter

quantizer_pds.m
 
 

function [Y,S_ops,P_ops,C_ops]=quantizer_pds(X,C);

% [Y,S_ops,P_ops,C_ops]=quantizer_pds(X,C)
%
% X input vectors
% C codebook
% Y output vectors
% S_ops nb of additions per pixel
% P_ops nb of multiplications per pixel
% C_ops nb of comparisons per pixel
%
% written by Stefan Gachter, EPFL

[N,k]=size(C);
% N size of codebook
% k dimesnion of the quantizer

M=size(X,1);
% M number of input vectors

Y=zeros(M,k);
% Y output vectors

S_ops=zeros(M,N);
P_ops=zeros(M,N);
C_ops=zeros(M,N);

s_ops_k=zeros(1,k);
p_ops_k=zeros(1,k);
c_ops_k=zeros(1,k);

% operations

for m=1:M

d2_min=inf;
i_min=0;
for i=1:N
  s_ops_k=zeros(1,k);
p_ops_k=zeros(1,k);
c_ops_k=zeros(1,k);
d2=0;
j=1;
while j<=k a=X(m,j)-C(i,j);
b=a*a;
d2=d2+b;

s_ops_k(j)=2;
p_ops_k(j)=1;
c_ops_k(j)=1;

if d2>d2_min j=k; end
j=j+1;
end
if d2<=d2_min d2_min=d2;
i_min=i;
end
S_ops(m,i)=mean(s_ops_k);
P_ops(m,i)=mean(p_ops_k);
C_ops(m,i)=mean(c_ops_k);


end
Y(m,:)=C(i_min,:);

end

S_ops=S_ops./M;
P_ops=P_ops./M;
C_ops=C_ops./M;
 
 
 

begin chapter | next chapter

sq_euk_dist.m
 
 

function [d2]=sq_euk_dist(x,y);

% [d2]=sq_euk_dist(x,y)
%
% written by Stefan Gachter, EPFL

d2=(x-y)*(x-y)';
 
 
 

begin chapter | next chapter

block_to_vect.m
 
 

function [V]=block_to_vect(B,k);

% [v]=block_to_vect(B,k)
%
% written by Stefan Gachter, EPFL

[Mb,Nb]=size(B);

S=floor(Nb/k);
V=zeros(Mb*S,k);

for s=1:S

V((s-1)*Mb+1:s*Mb,1:k)=B(1:Mb,(s-1)*k+1:s*k); end
 
 
 

begin chapter | next chapter

vect_to_block.m
 
 

function [B]=vect_to_block(V,M);

% [B]=vect_to_block(V,M)
%
% written by Stefan Gachter, EPFL

[Mv,Nv]=size(V);

T=floor(Mv/M);
B=zeros(M,Nv*T);

for t=1:T

B(1:M,(t-1)*Nv+1:t*Nv)=V((t-1)*M+1:t*M,1:Nv); end
 
 
 
 

begin chapter | next chapter