# A Mixed Method with Effective Color Reduction
## 2 Materials and Methods
### Overview of the BS+ATCQ Algorithm
#### 2.1.2 The Ant-Tree for Color Quantization Method  
ATCQ is a clustering-based method that is used to reduce the colors of an image [39]. This method represents the pixels of the original image by means of ants and subsequently builds a tree using them based on the similarity among the pixels represented by the ants. The ant \(h_{i}\) represents the pixel \(p_{i}\) of the image, with \(1\leq i\leq n\). At the beginning of the algorithm, all the ants are on the root of the tree, denoted \(a_{0}\). The operations of the algorithm allow all the ants to connect to the structure, becoming new nodes of the tree. The number of children of \(a_{0}\), denoted as \(q\), is equal to \(0\) before starting the operations and can increase to a limit defined by \(Q_{max}\). These children, denoted \(\{S_{1},...,S_{q}\}\), define the root nodes of \(q\) subtrees of ants. The node \(S_{j}\) defines a color of the quantized palette, computed as \(color_{j}=sum_{j}/nc_{j}\), where \(sum_{j}\) is defined by Equation (8) and \(nc_{j}\) is the number of said ants. When the algorithm concludes, the color of the subtree \(j\) is used to represent in the quantized image the pixels of the original image associated with the ants of this subtree. \[sum_{j}=\sum_{ \begin{subarray}{c}h_{i}\text{ connected}\\ \text{to subtree }j\end{subarray} }p_{i} \tag{8}\]  
The ATCQ operations define an iterative process to connect all the ants to the tree (Algorithm 2). Each iteration takes an ant \(h_{i}\) that is not connected to the structure and operates according to the current position of this ant, denoted \(a_{pos}\). ```
1:while there are moving ants do
2: Take a moving ant, \(h_{i}\), now on \(a_{pos}\)
3:if\((a_{pos}=a_{0})\)then
4: Support case operations (\(h_{i}\))
5:else
6: Ant case operations (\(h_{i}\))
7:endif
8:endwhile
9:Generate the quantized image
```  
**Algorithm 2** ATCQ algorithm. If the ant is on the node \(a_{0}\), the number of children of this node determines the next operation (Algorithm 3). If \(a_{0}\) has no children (\(q=0\)), the first one is created, and the ant is connected to it. In another case, the selected ant can move to an existing subtree or to a new subtree. To determine the destination of the ant, it is necessary to determine the node \(S_{c}\) with the color most similar to \(h_{i}\). Let \(d_{ic}\) denote this similarity (\(d_{ic}=Sim(p_{i},color_{c})\)). In order to make a decision, this value is compared to a threshold computed by Equation (9), where \(\alpha\) is a parameter in \((0,1]\) and \(e_{c}\) is the sum of the similarities between each ant \(h_{i}\) connected to the subtree \(c\) and the value of \(color_{c}\) when the ant was included in the subtree (Equation (10)). If \(d_{ic}<T_{c}\), a new child of \(a_{0}\) must be created to connect \(h_{i}\) to it. If \(d_{ic}\geq T_{c}\), the ant is included in the subtree \(c\). This last operation is also performed when a new child of \(a_{0}\) should be created, but it is impossible because this node already has \(Q_{max}\) children. \[T_{c}=\frac{e_{c}}{nc_{c}}\alpha \tag{9}\]  
\[e_{c}=\sum_{ \begin{subarray}{c}h_{i}\text{ }connected\\ \text{ to }subree\ j\end{subarray} }d_{ic} \tag{10}\]  
```
1:if (\(q=0\)) then
2: Create a new child of \(a_{0}\) and connect \(h_{i}\)
3:else
4: Select the node \(S_{c}\) with the color most similar to \(h_{i}\)
5:if (\(d_{ic}<T_{c}\)) and (\(q<Q_{max}\)) then
6: Create a new child of \(a_{0}\) and connect \(h_{i}\)
7:else
8: Update the subtree with root in \(S_{c}\)
9:endif
10:endif
```  
**Algorithm 3** Support case operations (\(h_{i}\))-ATCQ. The similarity between two colors \(x\) and \(y\) used in [39] is computed as a function of the Euclidean distance, \(E(x,y)\), between both colors: \(Sim(x,y)=1/(1+E(x,y))\). Before creating a new child of \(a_{0}\), \(q\) is increased by one. Then, the three variables associated with the new node \(S_{q}\) are initialized: \(nc_{q}=1,e_{q}=0\) and \(sum_{q}=p_{i}\).