how can we recover huffman encoded image?

i have written the following code for encoding and decoding but i am unable to obtain the recovered image.can anyone please help??
data=double(data);
symbols= unique(data);
counts = hist(data,symbols);
p = counts / sum(counts);
dict = huffmandict(symbols,p);
comp = huffmanenco(data,dict);
datanew= huffmandeco(comp,dict);

 Accepted Answer

You need to reshape datanew to the original image size, and you need to cast() it back to the original data type. Probably
uint8(reshape(datanew, size(data)))

14 Comments

Anu  Sri
Anu Sri on 17 Sep 2018
Edited: Anu Sri on 17 Sep 2018
i have to get the coloured image but i got the black and white image
What part is not working?
What is size(data) ? What is size(datanew) ? What is class(data) before the first line of code you show here? What is min(datanew) and max(datanew) ?
Size of data and datanew is 904×1 Class of data is double Max(datanew)=961 Max(data)=961
Before the line
data=double(data);
insert
sz = size(data);
cl = class(data);
Then you can have
data=double(data);
symbols= unique(data);
counts = hist(data,symbols);
p = counts / sum(counts);
dict = huffmandict(symbols,p);
comp = huffmanenco(data,dict);
datanew= huffmandeco(comp,dict);
and then
newimg = cast( reshape(datanew, sz), cl );
However, those sizes do not make sense. That would be an 8 x 113 grayscale image, and it would be strange for a non-RGB image to have a maximum of 961, especially when color output is expected. There is something wrong with your data. The input might potentially be pseudocolor instead of RGB, but even then the size and value does not make sense.
since i am working on satellite image so maximum size is 961.i am getting result upto 2nd last line of the code.I am actually facing problem in last line
[i,j,blksz]=find(S);
blkcount=length(j);
avg=zeros(blkcount,1);
for k=1:blkcount
avg(k)=mean2(I(i(k):i(k)+blksz(k)-1,j(k):j(k)+blksz(k)-1));%find mean
%value
end
avg=uint8(avg);
data=[i;j;blksz;double(avg)];%record total information
f=class(data);
data=double(data);
symbols= unique(data);
counts = hist(data,symbols);
figure,stem(counts);
p = counts / sum(counts);
sp=round(p*1000);% scaled probabilities
dict = huffmandict(symbols,p);
comp = huffmanenco(data,dict);
t=toc;
datanew= huffmandeco(comp,dict);
x=length(i);
disp(x);
y=length(j);
z=length(blksz);
w=length(avg);
inew=double(datanew(1:x));
disp(inew);
jnew=double(datanew(x+1:(x+y)));
blksznew=double(datanew((x+y+1):(x+y+z)));
avgnew=double(datanew((x+y+z+1):(x+y+z+w)));
blkcount=length(inew);
for k=1:blkcount
outim(inew(k):inew(k)+blksznew(k)-1,jnew(k):jnew(k)+blksznew(k)-1)=avgnew(k);
end
figure,imshow(outim);title('Decompressed Image');
What is S ? How does it relate to I ? It looks to me as if I is not even your original image.
S is the sparse matrix which i got on performing quadtree decomposition on original image I
When you did that quadtree decomposition, what values did you store in S ?
Corodinate values of the block and block size was stored in it in the form [(x,y) blksize]
quadtree will not generally produce square cells, except in the case where the original image was exactly square.
It is not clear why you are calculating an average over the block: often you would continue decomposing until all of the elements in a block were the same. It is valid, though, to continue only until the elements are "close enough" to being the same, and then a mean could be meaningful for that.
However, if I is your original RGB image, then it does not make sense to be taking a single mean instead of doing something to find a representative RGB value as a triple.
The information you store in data involves a bunch of row and column numbers. A high level of duplication might not be common, so your dictionary might include a lot of symbols, leading to some long representations. There is probably another layer of compression available by encoding differences in coordinates (at least in one direction) instead of absolute coordinates.
Anyhow, reconstruction will take a lot more work than just reshaping and type casting. You need to be assigning values into
reconstructed_I(i(k):i(k)+blksz(k)-1,j(k):j(k)+blksz(k)-1)
The second output of qt3ddecom is already the RGB values associated with each block.
Why are you coding all of that directly when qt3ddecom will do most of that for you ?
You should be calling vec2mat(valRGB1, 2) to reconstruct your valRGB .
Its done..thankyou for your support!!

Sign in to comment.

More Answers (0)

Asked:

on 16 Sep 2018

Reopened:

on 18 Sep 2018

Community Treasure Hunt

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

Start Hunting!