MATLAB Huffman Coding. Can someone offer me help with the sorting and calculating of the probabilities?
1 次查看(过去 30 天)
显示 更早的评论
Hallo, can somebody help me with my code. I am busy implementing the huffman code, but calculating the probabilities dont work right out. It has to take the two lowest probabilities and then add them together. Then it has to be sorted and done again until only two probabilities remain. This is what my program do do
0.933522222222222 0.954933333333333 0.977122222222222
0.0228777777777778 0.0228777777777778 0.0228777777777778
0.0221888888888889 0.0221888888888889
0.0214111111111111
Here is my code:
format long;
%clearing all variableas and screen
clear all;
close all;
clc;
%Reading image
a=imread('D:\face.tif');
figure,imshow(a)
%converting an image to grayscale
I=a;
%size of the image
[f,g]=size(I);
Totalcount=f*g;
%variables using to find the probability
cnt=1;
sigma=0;
%computing the cumulative probability.
for i=0:f
k=I==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:f];
%Order probabilities
for q=1:f
matrix(1,q) = pro(q);
end;
sorted = sort(transpose(matrix),'descend');
%Sort probabilities and add them together
for n=2:f-1
for m=1:f-n
sorted(m,n) = sorted(m,n-1);
if m==(f-n)
sorted(f+1-n,n) = sorted(f+2-n,n-1)+sorted(f+1-n,n-1);
final = sort(sorted,'descend');
end
end
end
2 个评论
Walter Roberson
2013-10-4
How are you maintaining the list of which symbols have been merged into which branch, so that you can do the decoding?
回答(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!