im trying huffman encoding in matlab and reached the part where i get a table with huffman codes, what's next ?

1 visualización (últimos 30 días)
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"

Respuestas (1)

charan
charan el 12 de Feb. de 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)
img = 4×4
15 4 11 3 13 8 1 13 1 5 1 1 7 1 4 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
symbols = unique(img);
counts = histc(img, symbols);
p = counts / sum(counts);
dict = huffmandict(symbols, p)
dict = 9x2 cell array
{[ 1]} {[ 1]} {[ 3]} {[0 0 0 0 1]} {[ 4]} {[ 0 0 1]} {[ 5]} {[0 0 0 0 0]} {[ 7]} {[0 0 0 1 1]} {[ 8]} {[0 0 0 1 0]} {[11]} {[ 0 1 0 1]} {[13]} {[ 0 1 1]} {[15]} {[ 0 1 0 0]}
img_enc = huffmanenco(img(:), dict)'
img_enc = 1×46
0 1 0 0 0 1 1 1 0 0 0 1 1 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 1 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
img2=reshape(huffmandeco(img_enc,dict),4,4)
img2 = 4×4
15 4 11 3 13 8 1 13 1 5 1 1 7 1 4 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The "img_enc" shows the encoded image representation. With the dictionary available the image can be decoded from the binary representation.

Categorías

Más información sobre Denoising and Compression en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by