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.
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
%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 Commenti
Risposte (1)
Walter Roberson
il 4 Dic 2013
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 Commenti
Walter Roberson
il 4 Dic 2013
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.
Vedere anche
Categorie
Scopri di più su Denoising and Compression in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!