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 |
% 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)
toc
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=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')
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
C_new=zeros(N,k);
for i=1:N
D=(ones(1,N)*Di)/N;
% D mean
squared error
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
s_ops_k(j)=2;
p_ops_k(j)=1;
c_ops_k(j)=1;
end
Y(m,:)=C(i_min,:);
S_ops=S_ops./M;
P_ops=P_ops./M;
C_ops=C_ops./M;
function [d2]=sq_euk_dist(x,y);
% [d2]=sq_euk_dist(x,y)
%
% written
by Stefan Gachter, EPFL
d2=(x-y)*(x-y)';
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
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