how can i do huffman encoding in image compression
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
shabu sathyadhas
2016-3-22
编辑:Walter Roberson
2018-8-28
%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
采纳的回答
Walter Roberson
2012-12-8
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 个评论
NAGA PAVAN KALYAN KUMAR TENTU
2021-5-18
编辑:Walter Roberson
2021-5-18
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
2021-5-18
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 ?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Source Coding 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!