how can i do huffman encoding in image compression

11 views (last 30 days)
hi,i am doing lossy image compression using discrete cosine transform i had done all the steps of the compression(dct then quantization then zigzag scan) now i have a vector and i want to do huffman encoding i know that the code as follows
[dict,avglen] = huffmandict(symbols,p)
comp = huffmanenco(sig,dict)
i am asking now how to get the symbol and p(probability) from the large vector that i have to do the encoding
  1 Comment
shabu sathyadhas
shabu sathyadhas on 22 Mar 2016
Edited: Walter Roberson on 28 Aug 2018
%function which converts array to vector
vec_size = 1;
for p = 1:m
for q = 1:n
newvec(vec_size) = I(p,q);
vec_size = vec_size+1;
end
end
%Huffman Encodig
hcode = huffmanenco(newvec,dict);
%Huffman Decoding
dhsig1 = huffmandeco(hcode,dict);
%convertign dhsig1 double to dhsig uint8
dhsig = uint8(dhsig1);
%vector to array conversion
dec_row=sqrt(length(dhsig));
dec_col=dec_row;
%variables using to convert vector 2 array
arr_row = 1;
arr_col = 1;
vec_si = 1;
for x = 1:m
for y = 1:n
back(x,y)=dhsig(vec_si);
arr_col = arr_col+1;
vec_si = vec_si + 1;
end
arr_row = arr_row+1;
end

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Dec 2012
If your vector is uint8() then one way of doing it is
symbols = unique(YourVector(:));
counts = hist(YourVector(:), symbols);
p = double(counts) ./ sum(counts);
This is not the only way.
  19 Comments
NAGA PAVAN KALYAN KUMAR TENTU
Edited: Walter Roberson on 18 May 2021
I = imread('cameraman.tif');
I = im2double(I);
T = dctmtx(8);
dct = @(block_struct) T * block_struct.data * T';
B = blockproc(I,[8 8],dct);
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
B2 = blockproc(B,[8 8],@(block_struct) mask .* block_struct.data);
symbols = unique(B2(:));
counts = hist(B2(:), symbols);
p = double(counts) ./ sum(counts);
[dict,avglen] = huffmandict(symbols,p);
comp = huffmanenco(I,dict);
Sir there is error in above code in last command sir. sir could you help in fixing that error? Or Is that above code is for write for Huffman encoding in image compression sir.?
Walter Roberson
Walter Roberson on 18 May 2021
You are applying huffman dictionary to floating point numbers, and those are only going to be bit-for-bit identical mostly by coincidence.
You then try to do a huffman encoding of the image, instead of based upon a vector of B2 values.
... Are you sure you want to use floating point numbers as your symbols ?

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!