im trying huffman encoding in matlab and reached the part where i get a table with huffman codes, what's next ?
1 view (last 30 days)
Show older comments
Hello, so i have being trying to implement huffman encoding for an image compression project i got, and in my code im able to reach where i get a table of each symbol and it's code such as the one im posting below, but im not quite sure what i do after this, should i replace each pixel in my image with the new code as a string, or should i combine multiple codes and store each 8 bits on it own, or what ? since you cant save a value as binary in matlab im not quite sure where to go from this
Symbol Probablity Code
1 0.947429656982422 "0"
8 0.0165405273437500 "100"
7 0.00766372680664063 "101"
9 0.00628662109375000 "1100"
15 0.00466156005859375 "11010"
6 0.00458145141601563 "11011"
5 0.00319671630859375 "11100"
10 0.00265502929687500 "11101"
4 0.00210571289062500 "111100"
11 0.00152206420898438 "111101"
3 0.00105667114257813 "1111100"
12 0.00102996826171875 "1111101"
13 0.000568389892578125 "1111110"
2 0.000389099121093750 "11111110"
14 0.000308990478515625 "111111110"
0 3.81469726562500e-06 "111111111"
0 Comments
Answers (1)
charan
on 12 Feb 2025
Hi,
If you replace the pixel values with the code and store them in order you get a binary representation of image that is shorter than the original image representation, in this way image compression can be achieved. You can refer to the below code that shows similar workflow on a matrix:
img = randi(15,4)
symbols = unique(img);
counts = histc(img, symbols);
p = counts / sum(counts);
dict = huffmandict(symbols, p)
img_enc = huffmanenco(img(:), dict)'
img2=reshape(huffmandeco(img_enc,dict),4,4)
The "img_enc" shows the encoded image representation. With the dictionary available the image can be decoded from the binary representation.
0 Comments
See Also
Categories
Find more on Denoising and Compression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!