Superpixel-Based PSO Algorithms for Color Image Quantization

Nature-inspired artificial intelligence algorithms have been applied to color image quantization (CIQ) for some time. Among these algorithms, the particle swarm optimization algorithm (PSO-CIQ) and its numerous modifications are important in CIQ. In this article, the usefulness of such a modification, labeled IDE-PSO-CIQ and additionally using the idea of individual difference evolution based on the emotional states of particles, is tested. The superiority of this algorithm over the PSO-CIQ algorithm was demonstrated using a set of quality indices based on pixels, patches, and superpixels. Furthermore, both algorithms studied were applied to superpixel versions of quantized images, creating color palettes in much less time. A heuristic method was proposed to select the number of superpixels, depending on the size of the palette. The effectiveness of the proposed algorithms was experimentally verified on a set of benchmark color images. The results obtained from the computational experiments indicate a multiple reduction in computation time for the superpixel methods while maintaining the high quality of the output quantized images, slightly inferior to that obtained with the pixel methods.


Introduction
Color image quantization (CIQ) is a transformation of a true color image (usually 8 bits for each RGB component) into an image with some colors, e.g., 8,16,32,64,128, and 256, which make up the color palette. Such a significant reduction in the number of colors in an image can enable faster, further image processing for such tasks as image compression [1], image segmentation [2], content-based image retrieval [3], etc. It is important to keep maximal similarity between the original image and the quantized image. Therefore, color image quantization can be considered a useful preprocessing operation.Although there are different CIQ methods, they consist of two main operations, which are the design of the color palette and the mapping of the pixels of the original image to the colors of the palette.
A few decades of development of the CIQ subject matter have resulted in several different approaches. The first methods can be called splitting methods. They split the solid color into smaller and smaller boxes, whose representatives (e.g., mean colors) are included in the final color palette. A good example of these is the classic method of median cut [4]. Splitting methods are fast, but quantized images often differ significantly from the original images. The second category of methods utilizes pixel clustering algorithms such as K-means (KM) [5] and fuzzy C-means (FCM) [6,7]. The clustering of pixels in a color image based on the unsupervised classification of millions of pixels compares their color similarities. The clustering methods used in CIQ provide high-quality quantized images, but their high computational complexity is the reason for the long quantization time. Additionally, their results depend on the initial conditions.
At the end of the last century, attempts were made to apply genetic [8] and evolutionary [9] algorithms for color quantization. Since 2005, work on applying swarm-based algorithms to color quantization has taken off. To solve this image preprocessing task, various metaheuristics have been used to mimic the behavior of groups of individuals, such as particles [10], ants [11], bacteria [12], frogs [13], fishes [14], bees [15], fireflies [16], wolves [17], etc. Recently, researchers have started using hybrid methods, that is, methods composed of several simpler algorithms, for color quantization. An example of this approach is the ant algorithm combined with the firefly algorithm [18] and a combination of the particle swarm optimization algorithm and the ant algorithm [19]. The goal of further work in the CIQ area will be methods that generate high-quality quantized images in a short time.
The aforementioned swarm and evolutionary algorithms belong to a large group of stochastic methods used in both optimization and simulation tasks [20]. Swarm intelligence algorithms were developed as a result of observations of nature in which different herds cooperate and exchange information between individuals. This leads to intelligent collective behavior that allows us to solve difficult problems. One of the first such algorithms was proposed in 1995 by J. Kennedy and R. Eberhart, the particle swarm optimization algorithm (PSO) [21]. In the PSO algorithm, each particle represents a candidate solution to a given optimization problem. The change in the position of a particle and, therefore, the change in the potential solution in each iteration, depends on the best known position of a given particle and the best known position of a particle from the entire swarm. One of the main advantages of swarm algorithms with respect to traditional optimization algorithms is that they start their operation from a population of potential solutions, rather than from a single point.
The PSO algorithm was first applied to the task of quantizing color images in 2005 [10]. The proposed PSO-CIQ algorithm combines the PSO algorithm with the K-means clustering algorithm. Before the values of the function are calculated, the particles are randomly selected and their positions are then refined by several iterations of the K-means algorithm.
For many years, PSO modifications have focused mainly on the selection of parameters (e.g., the number of particles in a swarm, acceleration coefficients, inertia weight, etc.), the modification of the neighborhood topology (e.g., grid, circle, star, and random), and the development of hybrid algorithms combining PSO with other metaheuristics (e.g., evolutionary algorithms). However, competition between particles was generally ignored. Among the many modifications of the recently developed PSO algorithm, we should note the version that also uses the evolution of individual differences (IDE-PSO) [22]. The authors of this algorithm introduced an evolutionary approach using individual differences. The algorithm assigns a coefficient called emotional status to each particle to determine individual differences, divides the whole swarm into three groups, and chooses an evolutionary method for each particle depending on its emotional status.
One way to reduce the CIQ time may be to preprocess the image by segmenting the image into superpixels and setting the color palette based on superpixels rather than pixels [23]. An image has fewer superpixels than pixels, so algorithms that operate on superpixels can run faster than pixel-based algorithms. The most popular method for generating superpixels is SLIC (simple linear iterative clustering) [24]. This algorithm uses the K-means method and classifies pixels based on color and their location. The distance between pixel colors is computed in the perceptually uniform CIELab color space. Each superpixel is assigned a mean color derived from the colors of its pixels. The number of superpixels in an image, their size, or their compactness depends on the type of superpixel generator. Of the characteristics listed here, color plays the most important role in the CIQ process, both in the pixel and superpixel versions. Splitting an image into superpixels, if performed quickly, can improve the efficiency of such image processing operations as CIQ or image segmentation. In the work [23], the authors proposed superpixel versions of three classic CIQ algorithms: median cut, K-means, and FCM. This approach allowed multiple decreases in computation time (15-30 times) with a minimal reduction in image quality.
In addition to the shorter execution time of CIQ operations, operation with limited computer resources was achieved. Image quality was evaluated using multiple indices (PSNR, FSIMc, DSCSI, HPSI, and SPSIM). An important parameter of superpixel CIQ algorithms is the number of superpixels into which the image was decomposed. It depends only on the number of quantization levels.
This study investigated the suitability of the IDE-PSO algorithm for CIQ tasks and the possibility of speeding up calculations due to the use of the superpixel approach (SP-IDE-PSO). The remainder of this paper is organized as follows. Section 2 presents the PSO-CIQ algorithm, its modification named IDE-PSO-CIQ, and our proposed superpixel versions of both methods. In Section 3, the experimental results and their discussion are shown. Finally, Section 4 concludes the article.

Materials and Methods
Swarm algorithms are a family of global optimization techniques inspired by the collective behavior of individuals (particles) that form a set of candidates to solve an optimization problem. A swarm of particles moves through the parameter space according to trajectories that depend on the best results of their own and those of their neighbors. Swarm particles perform rather simple tasks without central control. These particles actively seek the minimum of the objective function, work together in a topology-dependent manner, and swarm and memorize the previously obtained good results.

PSO-CIQ [10]
Applying the PSO algorithm to the CIQ task should define the space through which the particles will move. The first such application was carried out by Mahamed Omran in his work [10]. Each particle i-th X i can be described as where K-the number of colors in the color palette of the particle. The components of the particle are three-dimensional color vectors: Therefore, each particle in the swarm represents a different color palette, while the quality of each particle is evaluated on the basis of a quality index such as MSE. The pseudocode of the PSO-CIQ algorithm is shown below (Algorithm 1).
Algorithm 1 PSO-CIQ [10]. 1: Initialize each particle by choosing K colors from the RGB color space of the image and take them as initial pBest values. 2: Calculate the values of the objective function f (.) for each particle and find the initial gBest value. 3: while Stop condition of the algorithm do 4: for For each i-th particle do 5: Apply several iterations of the K-means algorithm with probability pkmeans 6: Update the particle velocity (Equation (3)) and position (Equation (4)) 7: Calculate the value of the objective function f (.) for the particle 8: Update the personal best solution for the particle (Equation (5)) 9: Update the best global solution (Equation (6)) 10:

end for 11: end while
The initialization of the starting positions for each particle in the swarm (Step 1) is performed by randomly selecting K colors from the color space of the original image. Initially, these values are regarded as the personal best positions of the particles (pBest). For all particles, we find the global best solution, which minimizes the chosen objective function f (.), in our case the MSE.
The main loop of the algorithm is then executed (Step 3). The algorithm is run and executed until a stop condition is met. The stop condition in this case is to reach the maximum number of iterations tmax. At the beginning of each iteration (step 5), a random number is determined by a continuous uniform distribution of the interval [0; 1]. If this number has a value less than the value of the pkmeans parameter, then on the color palette represented by the particle in question there are several iterations of the K-means algorithm. This procedure reduces the image quantization error that results from the random start of the particle swarm. It also makes it possible to obtain much better final quantization results, both in terms of minimizing the quality index and improving the visual aspects of the image after quantization. In the next step, the particle velocity and position are updated: In Step 7, the value of the objective function for the i-th particle is determined. However, to determine the value of the objective function, one must first determine a new image from the color palette that the particle in question represents in a given iteration. To do this, calculate the distance between each pixel of the original image and each color in the color palette. Furthermore, the pixels are assigned to the colors of the palette for which the calculated distance has the lowest value. After the mapping of the image pixels to palette colors is performed, the MSE quality index of the image after quantization is determined. The value of this index is the value of the objective function of the particle. In the following steps (8,9), the personal and global best solutions are updated: The global best solution, and therefore the best color palette to date, is the solution represented by that particle for which the value of the objective function takes the lowest value.
After each time they are determined, a mechanism is used to prevent particles from "escaping" from the optimized space. In the case of CIQ problems, this space is the RGB color space with components in the range from 0 to 255. Thus, when a color from the palette represented by one of the particles takes a value outside this range, it is then limited to the limiting value of this range. A similar mechanism is also applied to the velocity of particles. In this case, the acceptable range of particle velocities is assumed to be [−V max ; V max ].
The following values were assumed for the parameters of the PSO-CIQ algorithm: • NP-swarm size; • pkmeans = 0.1-probability of applying the K-means algorithm; • t max = 50-total number of iterations; To strengthen the initial exploration phase and the final phase of exploitation, the following inertia weight coefficient was used, the value of which decreases linearly with successive iterations. Therefore, it was assumed that the initial value of the inertia weight coefficient was w max = 0.72, while the final value of this coefficient decreased to w min = 0.4. When using the K-means algorithm, 10 iterations were assumed.

IDE-PSO-CIQ
In 2017, a new modification of the PSO algorithm was proposed [22], which introduced an evolutionary mechanism into the algorithm that uses individual differences between particles. The IDE-PSO algorithm assigns to each particle a competition factor eX, called an emotional state, to quantify individual differences between particles. In this algorithm, based on the emotional state of each particle, the swarm is divided into three subgroups: weak, normal, and good. Depending on the particle's affiliation to a given subgroup, its behavior changes, and thus its movement in the optimized multidimensional space (Algorithm 2).
Algorithm 2 IDE-PSO-CIQ. 1: Initialize each particle by choosing K colors from the RGB color space of the image. 2: Initialize each particle velocity at random and emotional states. 3: Calculate the objective function for the first generation of particles. 4: while Stop condition of the algorithm do 5: Group the particles with respect to their emotional state eX 6: for For each i-th particle do 7: while Stagnation Stag counter value reached do 8: Update the position of the particle according to the mechanism corresponding to the its emotional state 9: if If the position is within the limits of the search space then 10: Then accept the new position and break the stagnation loop 11: else 12: Reject the new position and increment the value of the stagnation counter 13: end if 14: end while 15: Apply several iterations of the K-means algorithm with probability pkmeans 16: end for 17: Evaluate the fitness of the current swarm 18: Update particles' historical best position and the global best position 19: Update emotional status eXi of particles depending on the change in their change of fitness 20: end while The emotional state eX i of the i-th particle is initialized based on a random number r with a continuous uniform distribution in the interval [−0.1; 0.1] (Equation (7)): On the contrary, the emotional state in each iteration is determined by the value of the objective function of all particles. If the actual value of the objective function is better than the value of the previous iteration, then the emotional state of a particle grows by the value of ∆ + (Equation (8)). Similarly, if the value of the objective function of a particle is less than the value of the previous iteration, its emotional state decreases by the value ∆ − (Equation (9)): where the symbols are defined as follows: • f (.)-the value of the objective function for a given solution; • X t i -the position of the i-th particle in a given iteration; • X t−1 i -the position of the i-th particle in the previous iteration; • gb t -the best global position among all particles in a given iteration; • gw t -the worst global position among all particles in a given iteration.
Furthermore, the closer a particle is to the best global solution, the smaller the value by which its emotional state changes.
In the IDE-PSO algorithm, three emotional states of the particles are defined: weak, normal, and good. Accordingly, the particles in each iteration are dynamically assigned to three subgroups based on the value of their emotional state. To split the particles into three groups, the particles are segregated according to their emotional state eX in such a way that the particle with the worst emotional state is the first in line. Two coefficients, m 1 and m 2 , whose values are in the range [0; 1], are used as limits to assign particles to the three groups of the algorithm. Thus, we have a swarm of NP particles, and the allocation to the three emotional groups is performed as follows, where ind i is the index of the i-th particle in the set of particles grouped by their emotional state: Particles with a poor emotional state tend to move in the wrong direction or are more likely to stop at the local minimum of the optimized space. Therefore, the mechanism of moving weak particles leads the particles to abandon their current position and learn from the best particles in the swarm. The way in which weak particles change their position depends on a random number r with a continuous uniform distribution in the interval [0; 1]. A given particle is iterated relative to each j-th dimension. If the value of the random number r is less than the number 0.5, then the dimension j-th of the particle is set as the dimension j-th of the best global position (Equation (10)): Alternatively, if the value of the random number r is greater than or equal to 0.5, then the j-th dimension of the particle is taken as a random number from the interval belonging to the optimized space: where l and u are the upper and lower limits of the optimized space dimension, respectively, and rand[−1; 1] is a random number in the range [−1; 1]. Particles with a normal emotional state determine their new position in both, based on their own previous position and the best global position among the swarm. The formula to determine the velocity (Equation (12)) and position (Equation (13)) of these particles is the same as for the original version of the PSO algorithm: where the symbols are defined as follows: • V t i -the velocity of the i-th particle in a given iteration; i -the velocity of the i-th particle in the previous iteration; • pBest t−1 i -the best known position of the i-th particle; • gBest t−1 -the best known position from the entire swarm; • c 1 , c 2 -the coefficient of exploration and exploitation; • r 1 , r 2 -random numbers with uniform continuous distribution from the interval [0; 1]; • ω-the coefficient of inertia weight.
In a situation where a particle has a good emotional state, it stays confident because its value of the objective function has improved over the previous few iterations. Therefore, its next position is determined only on the basis of its past, regardless of the positions of other particles in the swarm. The mechanism for updating the velocity and position of these particles is presented as follows: The second term in Equation (14) is added to the previous value of the velocity of the particle to improve its exploration ability.
The IDE-PSO algorithm additionally uses the Stag stagnation parameter. This parameter was introduced to prevent the particles from escaping from the optimized space. If a particle in a given iteration finds itself outside the search area, its newly determined position is abandoned, and a new position is recalculated. Each time a particle is found outside the search area, the counter of the stagnation parameter is incremented. If the value of the counter reaches the value of the Stag parameter, then the particle remains in the same place.

Superpixel Versions of PSO-CIQ and IDE-PSO-CIQ Algorithms
The proposed superpixel CIQ methods, presented in Figure 1, use the original image as input to the superpixel generator (in our case, SLIC) and as input to the color pixel mapping. The generation of the color palette, the most complicated stage of the CIQ method, uses only the superpixel image. This proposed approach should significantly decrease the computation time and shorten the CIQ operation. We used three algorithms to determine color palettes. To complete the operation, the color of each pixel within the original image is mapped to its nearest color from the palette. Introducing superpixels into the CIQ algorithm would decrease computational complexity while producing only a small reduce in image quality. We propose modified superpixel versions of the PSO algorithms: SP-PSO-CIQ and SP-IDE-PSO-CIQ. We compare these SP algorithms with their original pixel versions. We consider the dependence between the number of superpixels generated N SP and the quality of the final results. N SP should not be less than the number of colors k in the palette. To specify N SP , we propose the following empirical formula: where SP_Ratio ∈ {2, 4, 8, 16}. The application of this formula to divide the image into superpixels is illustrated in Figure 2. Using a quantization level of k = 32, four images split into 96, 160, 288, and 544 superpixels were generated from the input image. With these tools, we can produce superpixel images, perform CIQ, and objectively evaluate image quality after quantization, allowing us to evaluate and assess superpixel CIQ methods. The computational complexity of the PSO algorithm is linearly dependent on the size of the swarm and the number of input data. For the PSO-CIQ algorithm applied to digital images, the computational complexity is linearly dependent on the size of the swarm and the number of pixels in the image [10]. A simple analysis of the IDE-PSO-CIQ algorithm shows that its complexity is also O(n). The main factor affecting the computation time is the number of pixels, and therefore reducing this number by introducing superpixels significantly affects the computation time, making it possible to obtain high values of computation rates.

Evaluation of Image Quality
Methods for evaluating image quality mainly refer to images with specific distortions. Comparatively, not much research has been conducted on the evaluation of image quality after CIQ-caused changes [25].
To verify image quality, we used ten different image quality indices, including classic mean squared error (MSE), peak signal-to-noise ratio (PSNR), and mean color error (DE). Each of above indices could be considered equally suitable for application to CIQ image changes. However, in recent research, indices have been proposed that are better correlated with the human perception.
For this work, we also chose the following modern quality indices: the color feature similarity index (FSIM) [26], the sparse feature fidelity index [27], the improved color-imagedifference (ICID) index [28], the directional statistics color similarity index (DSCSI) [29], the Haar wavelet perceptual similarity index (HPSI) [30], and the mean deviation similarity index (MDSIS) [31]. The usability of the latter indices within the CIQ field has already been proven [32]. We also used the superpixel similarity (SPSIM) index proposed for application to superpixel segmentation results [33,34]. Most of these quality indices require maximizing values; only MSE, DE, ICID, and MDSIS are minimized.

Experiments, Results, and Discussion
All research was performed on a desktop computer with a CPU AMD Ryzen Threadripper 3960X at 3.80 GHz and 128 GB of RAM. The algorithms were implemented in the Matlab R2021b.

PSO-CIQ vs. IDE-PSO-CIQ
The paper [10] shows, for four benchmark color images: Lenna, Peppers, Jet, and Mandrill, that the PSO-CIQ algorithm generally achieved better results than the compared SOM and GCMA algorithms. Each benchmark image has a spatial resolution of 512 × 512 pixels- Figure 3. Table 1 of this paper has been supplemented with a column containing the results obtained for the proposed IDE-PSO-CIQ algorithm. In ten of the twelve cases studied, the IDE-PSO-CIQ algorithm achieved lower MSE values than PSO-CIQ.  In order not to be limited to the classic MSE index, we conducted similar studies on the same images for nine other pixel-based (PSNE, DE), patch-based (FSIM, SFF, ICID, DSCSI, HPSI, MDSIS), and superpixel-based (SPSIM) indices. The values of the indices are positively correlated with the image quality. The results of such a multi-index evaluation are included in Table 2. The bold values indicate the best of these for each image in the three palette sizes used. As in the previous experiment, the results of the IDE-PSO-CIQ algorithm dominated the PSO-CIQ results (94 of 108 comparisons in favor of the IDE version).

Superpixel Versions of Both Algorithms
The proposed superpixel versions of the PSO-CIQ and IDE-PSO-CIQ algorithms were tested on 24 test images from the Kodak test images (Figure 4) for 3 palette sizes (16, 64, and 256). The average results obtained for both pixel and superpixel versions at different swarm sizes (5 and 20) are given in Tables 3-5 and the best ones are in bold. The quality of quantization is higher when a larger swarm size is used. From the tables discussed, it can be seen that the transition to superpixels results in a slight loss of quantized image quality. The use of a superpixel approach preserves the advantage of IDE-PSO-CIQ over PSO-CIQ. Generally, the IDE-PSO-CIQ algorithm achieved much better color quantization results in terms of the values of the quality indices. Two indices were chosen to evaluate the loss of quality in the quantized image: HPSI and SPSIM. For both algorithms tested (SP-PSO-CIQ and SP-IDE-PSO-CIQ), the percentage loss of the HPSI index values was in the range of 0.14-4.66% and for SPSIM it was in the range of 0.02-0.74%. Such changes can be considered minor changes in quality.   An important criterion for evaluating superpixel CIQ algorithms is the computation rate, defined as the ratio of the computation time of the tested algorithm in the pixel version to its computation time in the superpixel version. Figure 5 shows the computation rates of the tested superpixel versus pixel methods for small (NP = 5) and large (NP = 20) swarms. For example, for 16 colors with swarm size NP = 5, the average computation times (in seconds) for the pixel versions of the SP-PSO-CIQ and SP-IDE-PSO-CIQ algorithms were 2.91 and 5.68, respectively, and for the superpixel version they were 0.69 and 0.72, respectively. For the small swarm, computation rates ranging from 4.2 to 17.3 were obtained for the SP-PSO-CIQ algorithm and for the SP-IDE-PSO-CIQ algorithm, the computation rates ranged from 7.8 to 26. For the large swarm, computation rates ranged from 43.3 to 62.1 for the SP-PSO-CIQ algorithm, and for the SP-IDE-PSO-CIQ algorithm these computation rates ranged from 15.4 to 40.1. The above average computation rates were obtained on 24 images from the Kodak collection, with a 10-fold quantization performed for each image. For a small swarm, higher computation rates were achieved by the superpixel version of the IDE-PSO algorithm, and for a large swarm, higher computation rates were achieved by the super-pixel version of the PSO algorithm.

Conclusions
In this paper, two particle-swarm-optimization-based algorithms (PSO, IDE-PSO) are considered. Both algorithms have been applied to the important task of digital color image processing, i.e., in color image quantization (CIQ).This is the first time the IDE-PSO algorithm has been used in this field. Additionally, superpixel versions of both algorithms were considered, in which the number of superpixels used for adaptive palette generation depends on the palette size. The use of the superpixel approach is part of the novelty of the article. The experimental results show that the use of the IDE-PSO algorithm for CIQ yields better image quality than the classic PSO version, although over a longer time. The study was conducted on a large set of benchmark natural images. To evaluate the results obtained, ten quality indices (pixel-based, patch-based, and superpixel-based) were used, as well as a computation rate that determines the acceleration of calculations. Regardless of the swarm size, the advantage of the IDE-PSO-CIQ algorithms in both pixel and superpixel implementations is noticeable. The introduction of superpixel versions has made it possible to achieve a significant acceleration of PSO algorithm calculations with a small loss of image quality. The computing rates obtained are in the range of 4.2-62.1 with a quality loss of a few percent at most. In future research, we can improve the results using a different, more efficient superpixel decomposition method instead of the classic SLIC algorithm. This research will be important for all those who want a fast and high-quality color reduction method in computer vision tasks.

Data Availability Statement:
The Kodak image dataset is available at: http://r0k.us/graphics/ kodak/ (accessed on 2 December 2022). Other data is not available.

Conflicts of Interest:
The authors declare no conflict of interest.

Abbreviations
In this manuscript, the following abbreviations are used: