STEP :- 1
First of all create a table that includes three pieces of information: the symbol, its probability, and the frequency with which it appears in the given image. The formula for calculating pixel probability is as follows :
Here, num_pixel is the number of times that pixel appeared in the image and total_pixel is the total amount of pixels in the image.The table is as shown below :
Step :- 2
Sort the table's elements in descending order based on the likelihood of occurrence. Throughout the process, keep in mind that the table should be in decreasing order. The following is a table in descending order:
Step :- 3
Now the probabilities of the last two symbols, i.e. the two least probabilities, add up to form a new pseudo node that connects both the left and right initial values as its children. Even with different iterations, the sum of probability is always equal to 1. The initial iteration is displayed below :
Step :- 4
Repeat steps 2 and 3 until there is only one node remaining, which is the root node, or until we receive 1. Keep in mind that if there is no relationship between the present tree and a tree formed later, a new tree will be created, and the beauty of the algorithm is that they will find a common ancestor someplace.
Step :- 5
After the tree has been generated, the next step is to assign the code-word. According to Huffman Coding, the symbol with the highest probability should have the lowest code. As a result, we consider assigning 0 to either the left or right side of the root, and 1 to the other.
Step :- 6
Further we number all the edges of the tree and it’s subtrees but remember to not change the order of 0 and 1 that is if left side is assigned with 0 then for all the nodes left should be assigned to 0 and likewise for 1.
Step :- 7
Now, starting from the leaf nodes, backtrace all of the new keywords that will be used for decoding and encoding. Create a key table that includes all symbols as well as the codes that must be exchanged for decoding.
The image encoded string and the key table for decoding purposes would be included in the data size.
Using this given formula, we can compute the compression percentage.
And we are done, we reached our desired product.