mardi 31 mars 2015

Im trying to compress an image using huffman coding "no built in func allowed"

I've generated the codes for each probability but i cant encode them in the image matrix A, as unit8 cant be transformed to cell matrix. any help please?


< clc; clear all; global CODE A1 = imread('fig2.jpg');



A1=rgb2gray(A1);

A1=imresize(A1,[48 48]);

%imshow(A1)
[M, N]=size(A1);
A = A1(:);
count = imhist(A);
p = count / numel(A);
CODE = cell(length(p), 1);
s = cell(length(p), 1);


for i = 1:length(p) % Generate a starting tree with symbol nodes 1, 2, 3, ... to
% reference the symbol probabilities.
s{i} = i ;
end
while numel(s) > 2
[p, i] = sort(p); % Sort the symbol probabilities
p(2) = p(1) + p(2); % Merge the 2 lowest probabilities
p(1) = []; % and prune the lowest one

s = s(i); % Reorder tree for new probabilities
s{2} = {s{1}, s{2}}; % and merge & prune its nodes
s(1) = [] ; % to match the probabilities
end
makecode(s, [])


for i = 1:numel(CODE)
c = CODE{i};
t = c; c(c=='1') = '0'; c(t=='0') = '1';
CODE{i} = c;

end
%----------------------
function makecode(sc, codeword)
% Scan the nodes of a Huffman source reduction tree recursively to
% generate the indicated variable length code words.

% Global variable surviving all recursive calls
global CODE

if isa(sc,'cell') % For cell array nodes,
makecode(sc{1}, [codeword 0]); % add a 0 if the 1st element
makecode(sc{2}, [codeword 1]); % or a 1 if the 2nd
else % For leaf (numeric) nodes,
CODE{sc} = char('0' + codeword); % create a char code string
end>

Aucun commentaire:

Enregistrer un commentaire