retrieving image after encoding

my code is
A=imread('imagcp.bmp');
A=A(:);
YourVector=double(A);
symbols = unique(YourVector(:));
counts = hist(YourVector(:), symbols);
p = double(counts) ./ sum(counts)
[dict,avglen]=huffmandict(symbols,p);
comp=huffmanenco(A,dict);
I want to dispaly image after encoding,plz help

 Accepted Answer

Walter Roberson
Walter Roberson on 26 Dec 2012
"comp" is not something that can be displayed. You need to do a huffman decoding of "comp" with "dict" in order to restore the image.

7 Comments

walter i have displayed decoded image but the thing is i need encoded image to be displayed and find compression ratio
my code
clc
clear all
a=imread('cameraman.tif')
I=a;
[m,n]=size(I);
Totalcount=m*n;
cnt=1;
sigma=0;
%computing the cumulative probability.
for i=0:255
k=I==i;
count(cnt)=sum(k(:))
pro(cnt)=count(cnt)/Totalcount;
sigma=sigma+pro(cnt);
cumpro(cnt)=sigma;
cnt=cnt+1;
end;
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) = 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
imshow(back),title('decoded_Image')
Notice that your code never resets arr_col to 1 when the row changes. Not that it matters in your code as you never use arr_col or arr_row for anything.
I don't know why you don't use reshape() to construct "back", but anyhow...
Assuming that your image is square is a bad idea. You should take my earlier advice of including the sizes along with the data being logically transmitted (that is, include the sizes at the beginning of hcode, and strip them out of hcode before decoding.
Does the assignment say anything about having to display the encoded image in the form of an image ? If it does, then
imagesc(hcode)
and prepare to be underwhelmed (and to get a warning about how the image has been scaled down to fit in the window.)
shows the encoded image,why i get like this,
please as per ur advice can u send the code for dislaying encoded and decoded image as u said the code above is not sufficient as u said to use reshape,plz assist in sending some codes
Instead of your "for x" "for y" loop,
back = reshape(dhsig, n, m) .';
As to why the encoded image shows up like it does, what were you expecting when you display a vector of binary values?
ok walter thanks
walter but why the size increases after encoding
Each element of the output represents one bit. The original data was 8 bits per element.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!