Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

llama / llama_bachelor_MPiTK_2004_VM-1

.pdf
Скачиваний:
3
Добавлен:
16.04.2013
Размер:
475.19 Кб
Скачать

- 41-

Литература

1.Кочетков М.Е., Умняшкин С.В. Многопотоковая реализация алгоритма арифметического кодирования. – М.: МГИЭТ (ТУ), 1998. – 21 с. Депонировано в ВИНИТИ 25.12.98 3884-В98.

2.Лесин В.В., Лисовец Ю.П. Основы методов оптимизации: Учебное пособие. – М.: Изд-во МАИ, 1998.

3.Умняшкин С.В. Математические основы цифровой обработки сигналов. – М.:

МГИЭТ (ТУ), 2004.

4.Умняшкин С.В. О кластеризации коррелированных данных. // Информационные технологии в инновационных проектах. Международная конференция (г.Ижевск, 20-22 апреля 1999г.): Материалы докладов. – Ижевск,

ИжГТУ, 1999. – С. 59-65.

5.T. Berger. Rate Distortion Theory. – Endlewood Cliffs, NJ: Prentice Hall, 1971.

6.P. A. Chou, T. Lookabaugh, R. M. Gray. Entropy-constrained vector quantization. IEEE Transactions on ASSP, vol.37, No.1, January 1989, pp.31-42.

7.E. Fowler, S. C. Ahalt. Adaptive vector quantization using generalized threshold replenishment. Proceedings of the IEEE Data Compression Conference, J. A. Storer and M. Cohn, Eds., Snowbird, UT, March 1997, pp. 317-326.

8.A. Gersho and R. M. Gray. Vector quantization and signal compression. Kluwer Academic Publishers, Boston, 1992.

9.R. M. Gray. Vector Quantization. IEEE ASSP Magazine, April 1984, pp. 4-29.

10.C. H. Hsieh, J. C. Tsai, and P. C. Lu. Noiseless coding of VQ index using index grouping algorithm. IEEE Trans. Commun., vol. 44, pp. 1643–1648, Dec. 1996.

11.C.-M. Huang, Q. Bi, G. Stiles, and R. Harris. Fast full search equivalent encoding algorithms for image compression using vector quantization. IEEE Transactions on Image Processing, 1(3):413-416, July 1992.

12.D. A. Huffman. A method for the construction of minimum-redundancy codes. Proceedings of the I.R.E., September 1952, pp 1098-1102.

13.F. Kossentini, M. J. T. Smith, and C. F. Barnes. Finite-state residual vector quantization. J. Visual Commun. Image Rep., vol. 5, no. 1, pp. 75–87, Mar. 1994.

14.Y. Linde, A. Buzo, R. M. Gray. An Algorithm for Vector Quantizer Design. IEEE Transactions on Communication, vol. COM-28, No.1, January 1980, pp.84-95.

-42-

15.M. Nelson. The Data Compression Book. New York: M&T Books, 1996.

16.R. Rinaldo, G. Calvagno. Hybrid vector quantization for multiresolution image coding. IEEE Trans. Image Processing, vol. 6, pp. 753–758, May 1997.

17.G. K. Wallace. The JPEG still picture compression standard. IEEE Trans. Consumer Electron., vol. 38, no. 1, pp. 18–34, Feb 1992.

18.Hsien-Chung Wei, Pao-Chin Tsai, and Jia-Shung Wang. Three-Sided Side Match Finite-State Vector Quantization. IEEE transactions on circuits and systems for video technology, vol. 10, no. 1, February 2000.

19.I. H. Witten, R. Neal and J. G. Cleary. Arithmetic coding for data compression. Communications of ACM, 1987.

- 43-

Приложение 1. Тестовые изображения

Рисунок 2.1. Barbara

Рисунок 2.2. Boat

Рисунок 2.3. Cathedral

Рисунок 2.4. House

Рисунок 2.5. Lena

Рисунок 2.6. Mandrill

- 44-

Рисунок 2.7. Peppers

Рисунок 2.8. Zelda

- 45-

Приложение 2. Исходные коды программ

Файл build_codebooks.m

function res = build_codebooks(clusters, training, L_fin, lambda, epsilon) % Алгоритм с условным разделением 1 ячейки за итерацию.

n_clust = max(max(clusters));

[M N]= size(training); book = cell(1,n_clust+1); nu = cell(1,n_clust);

%Создание массива книг, в каждой ячейке которого находится другой

%массив, в k-й ячейке которого лежит книга для k-го кластера library = cell(1,length(L_fin)+1);

for k=1:length(L_fin)

library{k} = cell(1,n_clust+1); library{k}{end} = cell(1,n_clust); % для nu

end

disp([sprintf('Processing: C=%d L=[',n_clust), sprintf('%d ',L_fin), sprintf('] lambda=%.1f...',lambda)]);

for k=1:n_clust

i_clust = find(clusters == k); l_clust = length(i_clust);

A = training(:,i_clust); Atransp = A';

tic;

cnt = 0; cnt_lib = 1;

L_storage = zeros(1,length(L_fin)); L = 1;

Jval = inf; book{k} = mean(A); nu{k} = 1;

omegaE = cell(1,1); omegaE{1} = 1;

while L < max(L_fin) & cnt < floor(log2(max(L_fin))) Err = [0 inf];

% ELBG block i = 0;

while i < L

if L >= max(L_fin),break,end

% generate new cell and run simple LBG i = i + 1;

bE = book{k}; nuE = nu{k};

delta = randn(1,l_clust);

delta = epsilon * delta / norm(delta); bE = [bE; bE(i,:)-delta];

bE(i,:) = bE(i,:)й; iE = i;

nuE = [nuE, nuE(i)/2]; nuE(i) = nuE(end); ErrE = [0 inf];

LE = size(bE,1);

while abs(ErrE(1) / ErrE(2) - 1) > epsilon

[JvalE JE] = minJ(bE',Atransp,-log2(nuE),lambda);

- 46-

omegaE = cell(1,LE); for m = 1 : LE

omegaE{m} = find(JE == m);

end

ErrE(1) = ErrE(2);

ErrE(2) = sum(JvalE) / (size(A,1) * l_clust);

tmp = cellfun('length',omegaE); nuE = tmp ./ sum(tmp);

del = []; for m=1:LE

if nuE(m) > 0

bE(m,:) = mean(A(omegaE{m},:));

else

del = [del, m];

end

end

bE(del,:) = []; nuE(del) = []; omegaE(del) = []; JvalE(del) = []; iE = iE - length(find(del < iE)); if iE < 1, error('figaaa'),end

LE = size(bE,1);

end

if sum(JvalE) < sum(Jval) lib_ind = find(L_fin == L);

book{k} = bE; nu{k} = nuE;

Jval = JvalE; i = iE;

 

L = size(book{k},1);

 

if ~isempty(lib_ind)

 

% Sort codebook by descending frequency

 

[nu{k} I] = sort(nu{k});

 

nu{k} = nu{k}(end:-1:1);

 

book{k} = book{k}(I(end:-1:1),:);

 

L_storage(lib_ind) = L;

 

library{lib_ind}{k} = book{k};

 

library{lib_ind}{end}{k} = nu{k};

 

end

%

end

disp(sprintf('i=%d L=%d',i,L));

 

end % while i <= L

 

cnt = cnt + 1;

 

end % while L < max(L_fin) & cnt < max(L_fin)/2

 

t(k) = toc;

%Заполнить незаполненные ячейки в случае, если книга не смогла дорасти

%до заданного размера

[nu{k} I] = sort(nu{k}); nu{k} = nu{k}(end:-1:1);

book{k} = book{k}(I(end:-1:1),:);

for i=1:length(L_fin)

if isempty(library{i}{k}) L_storage(i) = L; library{i}{k} = book{k}; library{i}{end}{k} = nu{k};

end

end

disp([sprintf('\tCluster %d built, cells=[',k), sprintf('%d ',L_storage), sprintf('] T=%.1fs Iter=%d T_avg=%.1fs',t(k),cnt,t(k)/cnt)]);

end

- 47-

disp(sprintf('Codebook built in T=%.1f min',sum(t)/60));

% All algorithm parameters are stored in book{end} param{1} = clusters;

param{2} = lambda; library{end} = param;

res = library;

Файл build_upd_stat.m

function freq = build_upd_stat(im_spectre, clusters, Q, freq_size)

M = size(im_spectre,1); n_clust = max(max(clusters));

Q = repmat(Q, M, 1);

quant = round(im_spectre ./ Q); quant = quant + freq_size/2;

for k=1:n_clust

i_clust = find(clusters == k); q = quant(:,i_clust);

freq{k} = zeros(1, freq_size); for i=0:freq_size-1

freq{k}(i+1) = sum(sum(q == i));

end end

Файл decode.m

function image = decode(index,update,upd_mtx,imsize,Q,Quant_mtx,cb_param,tau)

book = index{end}{2}; Lmax = index{end}{3}; nu = book{end};

M = length(index{1}); clusters = cb_param{1};

dct_size = size(clusters,1) * size(clusters,2); blksize = size(clusters,1);

lambda = cb_param{2}; n_clust = max(max(clusters));

blk_in_row = imsize(2) / blksize; image = zeros(M,dct_size);

for k = 1 : n_clust

i_clust = find(clusters == k);

L = size(book{k},1); updcnt = 0;

%nu{k} = ones(1,L);

nu{k} = nu{k} * M / 48 + 1;

for i=1:M ii=index{k}(i); if ii ~= 0

image(i,i_clust) = book{k}(ii,:); nu{k}(ii) = nu{k}(ii) + 1;

else

updcnt = updcnt + 1;

if L < Lmax(k) % Добавить в конец кодовой книги

- 48-

book{k} = [book{k}; update{k}(updcnt,:) .* Quant_mtx(upd_mtx{k}(updcnt),i_clust)];

image(i,i_clust) = book{k}(end,:); L = L + 1;

nu{k} = [nu{k}, 1];

else % Заменить самый редкий вектор кодовой книги

[tmp I] = min(nu{k}(1:L));

book{k}(I,:) = update{k}(updcnt,:) .* Quant_mtx(upd_mtx{k}(updcnt),i_clust);

image(i,i_clust) = book{k}(I,:); nu{k}(I) = 1;

end

end

nu{k} = nu{k} * tau;

end

% disp('---'); end

image(:,1) = Q * cumsum(index{end-1});

for i = blk_in_row+1 : 2*blk_in_row : size(image,1); image(i:iY_in_row-1,:) = image(iY_in_row-1:-1:i,:);

end

%image = col2im(image', [blksize blksize], imsize, 'distinct');

%%image = blkproc(image, [blksize blksize], 'quantize_scalar',2/9,'dequant');

%image = blkproc(image, [blksize blksize], 'idct2');

for i=1:size(image,1)

image(i,:) = megadct2(1,image(i,:));

end

image = col2im(image', [blksize blksize], imsize, 'distinct');

Файл display_graphs.m

function res = display_graphs(data, filename,filepath)

im_name = data{1}{1};

lambda = []; clusters=[]; book = []; psnr = []; bpp = [];

for i=1:length(data)

book = [book data{i}{2}]; clusters = [clusters data{i}{3}]; lambda = [lambda data{i}{4}]; psnr = [psnr data{i}{5}];

bpp = [bpp data{i}{7}];

end

book = unique(book); clusters = unique(clusters); lambda = unique(lambda);

[bpp_jpg, psnr_jpg] = jpeg_psnr_plot(filename,filepath);

for i=1:length(lambda) figure;

x = reshape(bpp(1:length(data)/length(lambda)),length(data)/length(lambda)/length(clus ters),length(clusters));

- 49-

y = reshape(psnr(1:length(data)/length(lambda)),length(data)/length(lambda)/length(clu sters),length(clusters));

plot(x,y,'*-',bpp_jpg,psnr_jpg,'ko-'),grid; leg = {};

for j=1:length(clusters)

leg{j} = sprintf('Clusters=%d',clusters(j));

end

leg{end+1} = 'JPEG';

title([sprintf('Compression of %s, \\lambda=%.1f L=[',im_name,lambda(i)), sprintf('%d ',book), sprintf(']')]);

xlabel('BPP'); ylabel('PSNR, dB'); legend(leg,4);

bpp(1:length(data)/length(lambda)) = []; psnr(1:length(data)/length(lambda)) = [];

end

res = [];

Файл encode_dc.m

function res = encode_dc(dc, Q) % dc is a column vector

res = diff(round([0; dc ./ Q]));

Файл img_prepare.m

function image = img_prepare(image_file)

blksize = 8; % if != 8, apply ordinary blkproc & dct2 instead of megadct

% Read image and compute DCT for all blocks image = double(imread(image_file));

%image = blkproc(image, [blksize blksize], 'dct2');

blk_in_row = size(image,2) / blksize;

image = im2col(image, [blksize blksize], 'distinct')'; for i=1:size(image,1)

image(i,:) = megadct2(0,image(i,:));

end

% Quantize all blocks

%image = blkproc(image, [blksize blksize], 'quantize_scalar',2/9,'quant');

% Unroll block set to the vector set

%image = im2col(image, [blksize blksize], 'distinct')';

% Reverse some vectors to make 'labirynth' image scanning for i = blk_in_row+1 : 2*blk_in_row : size(image,1);

image(i:iY_in_row-1,:) = image(iY_in_row-1:-1:i,:);

end

%image = image(:,zigzug);

Файл jpeg_psnr_plot.m

function [bpp,psnr] = jpeg_psnr_plot(pic_name, jpegs_dir)

filez = dir([jpegs_dir, '*.jpg']);

- 50-

filez_ari = dir(['img/_arith_jpg/', pic_name(1:end-4),'/','*.jpg']);

x = double(imread([jpegs_dir,'../',pic_name])); for i=1:length(filez)

fname = filez(i).name;

y = imread([jpegs_dir, fname]); y = double(y(:,:,1));

inf = imfinfo([jpegs_dir, fname]); %imshow(y)

S = sqrt(sum(sum((x - y).^2)) / (size(x,1)*size(x,2))); psnr(i) = 20 * log10(255 / S);

% bpp(i) = 8 * (inf.FileSize - 54) / inf.Width / inf.Height; bpp(i) = 8 * (filez_ari(i).bytes - 54) / inf.Width / inf.Height;

%str = sprintf('%s:: psnr=%.3f bpp=%.3f',filez(i).name,psnr(i),bpp(i)); %disp(str);

end

[psnr I] = sort(psnr); bpp = bpp(I);

Файл main.m

clc;

clear;

format compact;

lambda = [1.2 1.7 2 2.5 3]; clusters = [10 12 14];

L = [16 32 64 96 128 192 256];

%L = [16 32 48 64 96 128 160 192 256];

%L = [16 32 48 64 96 128 160 192 256 384 512];

book_epsilon = 0.06; train_file = 'all2.bmp';

encode_dir = 'img/all/'; % do not forget ending slash!

log_file = 'result/log.txt'; result_file = 'result.mat'; result_out_dir = 'result/tmp/'; Q = 25; % DC quantizer

Quant_coef = [0.5 1 2];%[0.5 0.8 1 1.5 2]; % Pennebaker mtx coefficient for quantizing updates

tau = 0.991; % decay constant for frequency tables upd_freq_size = 256; % Max symbols in update context

% Ускорение работы за счет кэширования кодовых книг и спектров тестовых изображений

use_cached_book = 1; use_cached_train = 1; use_cached_quant = 1; use_cached_update_stat = 1;

showpic_after_calc = 0; % показывать декодированные картинки (с паузой)

diary(log_file);

disp(sprintf('\nLog started at %d.•d.•d •d:•d:•d\n',floor(clock)));

disp(sprintf('Preparing %s for training...',train_file)); if ~use_cached_train

I = img_prepare(['img/', train_file]); save(sprintf('img/img_%s.mat',train_file),'I');

end

im_train = load(sprintf('img/img_%s.mat',train_file)); im_train = im_train.I;

Quant_mtx = quant_mtx(Quant_coef);

Соседние файлы в папке llama