In my problem, i have taken rgb image of size 512*512,then converted it to greyscale and applied dct2 on it.now i want to do huffman coding of this.Plz tell me how to do this and also tell me if i am wrong.
2 次查看(过去 30 天)
显示 更早的评论
%clear all variables and screen
clear all;
close all;
clc;
% Reads the image whose name is lena & format is jpg.
I=imread('lena.jpg');
figure;imshow(I);
T1=I;
title('coloured image of lena');
T1=im2double(T1);
T2=rgb2gray(T1);
%size of the image
[m,n]=size(T2);
Totalcount=m*n;
figure;imshow(T2);
title('grayscale image of lena');
%performing the 2D-dct to grayscale image T2.
Z=dct2(T2);
figure;imshow(Z);
title('dct transformed');
%variables using to find the probability
cnt=1;
sigma=0;
%computing the cumulative probability.
for i=0:255;
k=Z==i;
count(cnt)=sum(k(:));
%pro array is having the probabilities
pro(cnt)=count(cnt)/Totalcount;
%sigma=sigma+pro(cnt);
%cumpro(cnt)=sigma;
cnt=cnt+1;
end;
%Symbols for an image
symbols =0:255;
%Huffman code Dictionary
dict = huffmandict(symbols,pro);
%function which converts array to vector
vec_size = 1;
for p = 1:m;
for q = 1:n;
newvec(vec_size) = Z(p,q);
vec_size = vec_size+1;
end
end
%Huffman Encodig
hcode = huffmanenco(newvec,dict);
%Huffman Decoding
dhsig1 = huffmandeco(hcode,dict);
%converting 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
%converting image from grayscale to rgb
[deco, map] = gray2ind(back,256);
T3 = ind2rgb(deco,map);
figure;imshow(T3);title('decoded.JPG');
i am getting an error:
??? Error using ==> huffmandict at 108
The sum of elements of the probability vector must be 1
Error in ==> lena1 at 46
dict = huffmandict(symbols,pro);
0 个评论
回答(1 个)
Walter Roberson
2013-12-4
Your expression count(cnt)/Totalcount is not going to result in values that are exactly representable unless Totalcount is a power of 2.
Note: If you are trying to encode images as in JPEG, then you need to do a zig-zag transformation, and you need to do modified huffman encoding, not regular huffman encoding.
2 个评论
Walter Roberson
2013-12-4
No, this code is for lossy compression only.
Caution: if you look at the source code referred to there, remember you will need to rewrite the huffman part of it, to avoid plagerism in submitting your assignment.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Denoising and Compression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!