# Grayscale Iterative Star Spot Extraction Algorithm Based on Image Entropy
## 4 Image Preprocessing Based on Grayscale Iteration  
Accurately detecting star spots in star images is the foundation for star trackers to extract star spot centroids, solve star attitudes, and extract targets. Due to the interference of stray light, the overall grayscale of star images captured by star trackers increases, resulting in poor background uniformity, leading to low star spot extraction rates and a high number of false star spots. To address this issue, this paper proposes a grayscale iterative image preprocessing algorithm that effectively filters out background noise and enhances the local contrast of the targets. When the star tracker acquires a star image containing stray light, let the grayscale values of each pixel in the star image be denoted as \(f_{n}(x,y)\). The average grayscale value \(Avg\) of the star image is calculated using the following equation:  
\[Avg=\frac{\sum_{1}^{N}f_{n}(x,y)}{N} \tag{7}\]  
where \(f_{n}(x,y)\) represents the grayscale value of each pixel in the star image, and \(N\) is the number of pixels in the star image with non-zero grayscale values. Subtracting the average grayscale value \(Avg\) from the grayscale value of all pixels yields a new image denoted as \(f_{n}^{*}(x,y)\), with the equation as follows:  
\[f_{n}^{*}(x,y)=f_{n}(x,y)-Avg \tag{8}\]  
After the aforementioned steps, image regions with grayscale values less than \(Avg\) are eliminated from the stray light background. To maintain the reliability of star spot information as much as possible, the entire star image undergoes grayscale stretching, with the calculation equation as follows:  
\[f_{n}^{i}(x,y)=f_{n}^{*}(x,y)\times\frac{2^{j}-1}{Max} \tag{9}\]  
where \(f_{n}^{i}(x,y)\) represents the grayscale value of the star image after the \(i\)-th iteration, \(Max\) is the maximum grayscale value in the image, and \(j\) is the pixel bit width. Grayscale iteration can effectively remove background stray light, and its removal extent and the accuracy of star spot extraction are related to the number of iterations of this algorithm. Before the iteration starts, due to the interference of background stray light, it is almost impossible to extract connected regions or only a few connected regions can be extracted. As the number of iterations increases, the background is gradually removed, and the number of connected regions that can be extracted also increases. However, during this process, a large number of false star spots may appear. Therefore, it is stipulated that when the number of extracted connected regions reaches the maximum value, the iteration continues, and the noise will be gradually removed, causing the number of connected regions to decrease gradually. However, when the number of extracted connected regions is equal to the number of star spots that can be retained, if the iteration continues, it will gradually remove the dim and fainter star spots, leading to the loss of star spots. Therefore, when the following conditions are met, the iteration should be stopped:  
\[ \begin{cases}N_{i}\leq N_{max}\\ N_{min}\leq N_{i}\leq N_{i-1}\end{cases} i=0,1,\ldots \tag{10}\]  
where \(N_{i}\) represents the number of connected regions extracted after the \(i\)-th iteration; specifically, \(N_{0}\) is the number of connected regions when the star image has not undergone grayscale iteration. To ensure that the extracted star spot information is sufficient for star image recognition, the stopping condition of the iteration needs to satisfy \(N_{i}\) greater than \(N_{min}\) to prevent extracting only a small number of star spots for two consecutive iterations, while there are still other star spots that can be extracted in the star image, leading to errors and omissions in the star spot information. Based on prior information, star spots typically occupy only 3 \(\times\) 3 pixels on the focal plane. Therefore, when the number of pixels in a connected region exceeds this threshold, the connected region should be excluded.