# A Low-Complexity Lossless Compression Method Based on a Code Table for Infrared Images
## 4 Proposed Method  
### The Creation and Coding of General Code Table  
#### 4.1.1 Canonical Huffman Coding  
The first step in Huffman coding is counting the frequency of each pixel symbol. The frequency is weight, and \(n\) pixel symbols can be used to construct an \(n\)-th Huffman tree based on the weights. A \(3\times n\) array \(\vec{N}\) is created to store the differential pixels, frequency of occurrence, and code length. The code length is known after building the Huffman tree in Algorithm 1 and calculating the code lengths in Algorithm 2, which is initialized to 0. After counting the pixels of an image, a frequency table of \(n=188\) pixel symbols is obtained, with data from image B used for illustration. The pixel frequency statistics are shown in Table 3.  
\begin{table}
\begin{tabular}{c c c c c c c} \hline \multirow{2}{*}{**Image**} & **Original** & \multirow{2}{*}{**Entropy1**} & **Entropy2** & \multirow{2}{*}{**Image**} & **Original** & \multirow{2}{*}{**Entropy1**} & **Entropy2** \\  & **Image** & & & & & **Image** & \\ \hline
1  
The canonical Huffman tree only needs to obtain the code length of pixel symbols, and an array \(\vec{H}\) of size \(2\times(2n-1)\) can describe the Huffman tree to obtain the code length. The two columns of \(\vec{H}\) are used to record the weight and parent of each node, respectively. The first n rows of the \(\vec{H}\) record the leaf nodes and the last \(n-1\) rows record the new synthesized nodes, setting the parent of the root node to 0. The canonical Huffman tree construction algorithm is shown in Algorithm 1. \(\vec{H}\) after running Algorithm 1 is shown in Table 4.  
```
1:\(\vec{N}\)
2:\(\vec{H}\)
3:\(\vec{H}\)\(\leftarrow\) 0, \(i=0\)
4:while\(i\
eq n\)do
5:\(\vec{H}\)(\(i\),1) = \(\vec{N}\)(\(i\),2);
6:\(i=i+1\)
7:endwhile
8:while\(i\
eq 2\times n-1\)do
9:\((index1,index2)=find2 minweight(\vec{H},i-1)\)
10:\(\vec{H}(index1,2)=i\)
11:\(\vec{H}(index2,2)=i\)
12:\(\vec{H}(index,1)=\vec{H}(index1,1)+\vec{H}(index2,1)\)
13:\(i=i+1\)
14:endwhile
```  
**Algorithm 1** Canonical Huffman tree creation. \(\vec{H}\)\(\leftarrow\) represents a synthesized node, which is a non-leaf node, so it does not have corresponding pixels. \begin{table}
\begin{tabular}{c c c c} \hline \hline
**Pixel** & **Index** & **Weight** & **Parent** \\ \hline... &... &... &... \\ \(-3\) & 77 & 14,253 & 364 \\ \(-2\) & 78 & 17,124 & 366 \\ \(-1\) & 79 & 19,885 & 368 \\ \(0\) & 80 & 106,708 & 374 \\ \(1\) & 81 & 17,535 & 366 \\ \(2\) & 82 & 14,818 & 365 \\ \(3\) & 83 & 12,135 & 363 \\... &... &... &... \\ \(\backslash\)\({}^{1}\) & 356 & 12,674 & 363 \\ \(\backslash\) & 357 & 14,755 & 364 \\ \(\backslash\) & 358 & 15,480 & 365 \\ \(\cdot\) &... &... &... \\ \(\backslash\) & 361 & 21,246 & 368 \\ \(\backslash\) & 362 & 23,294 & 369 \\ \(\backslash\) & 363 & 24,809 & 369 \\ \(\backslash\) & 364 & 29,008 & 370 \\ \(\backslash\) & 365 & 30,298 & 370 \\ \(\backslash\) & 366 & 34,659 & 371 \\ \(\backslash\) & 367 & 37,261 & 371 \\ \(\backslash\) & 368 & 41,131 & 372 \\ \(\backslash\) & 369 & 48,103 & 372 \\ \(\backslash\) & 370 & 59,306 & 373 \\ \(\backslash\) & 371 & 71,920 & 373 \\ \(\backslash\) & 372 & 89,234 & 374 \\ \(\backslash\) & 373 & 131,226 & 375 \\ \(\backslash\) & 374 & 195,942 & 375 \\ \(\backslash\) & 375 & 327,168 & 0 \\ \hline \hline \end{tabular}
\end{table} 
Table 4: \(\vec{H}\) describing the Huffman tree. The Huffman tree, also known as an optimal binary tree, has the shortest path length with weights. Higher weights in a Huffman tree correspond to closer proximity to the root node. In image coding, higher weights signify a higher frequency of occurrence of pixel symbols, while being closer to the root node indicates shorter coded symbols. The shortest weighted path length represents optimal compression for images by the Huffman tree among binary trees.