Magnesium Ingot Stacking Segmentation Algorithm for Industrial Robot Based on the Correction of Image Overexposure Area

This paper proposes an adaptive threshold segmentation algorithm for the magnesium ingot stack based on image overexposure area correction (ATSIOAC), which solves the problem of mirror reflection on the surface of magnesium alloy ingots caused by external ambient light and auxiliary light sources. Firstly, considering the brightness and chromaticity information of the mapped image, we divide the exposure probability threshold into weak exposure and strong exposure regions. Secondly, the saturation difference between the magnesium ingot region and the background region is used to obtain a mask for the magnesium ingot region to eliminate interference from the image background. Then, the RGB average of adjacent pixels in the overexposed area is used as a reference to correct the colors of the strongly exposed and weakly exposed areas, respectively. Furthermore, in order to smoothly fuse the two corrected images, pixel weighted average (WA) is applied. Finally, the magnesium ingot sorting experimental device was constructed and the corrected top surface image of the ingot pile was segmented through ATSIOAC. The experimental results show that the overexposed area detection and correction algorithm proposed in this paper can effectively correct the color information in the overexposed area, and when segmenting ingot images, complete segmentation results of the top surface of the ingot pile can be obtained, effectively improving the accuracy of magnesium alloy ingot segmentation. The segmentation algorithm achieves a segmentation accuracy of 94.38%.


Introduction
With its excellent properties such as low density, high specific strength and specific stiffness, and good thermal conductivity, magnesium alloy is widely used in robotics [1,2], image processing [3,4], automotive, aerospace, and communication electronics, and also plays an increasingly important role in realizing the light weight of products [5][6][7]. During the magnesium alloy melting process, the key to achieving magnesium ingot sorting with the magnesium ingot automatic picking equipment is the segmentation of the magnesium ingot image. And the quality of the segmentation results will affect the grasping efficiency of the subsequent magnesium ingot automatic loading equipment. Therefore, the segmentation of magnesium ingots in images has important research value.
The segmentation object in this paper is a magnesium ingot with a trapezoidal crosssection, and the specific dimensions are shown in Figure 1a. The surface of the target magnesium ingot is smooth and local overexposure will occur in the acquired image under a complex external light environment [8]. When the image is segmented, the gradient of partial gray value information of the magnesium ingot changes dramatically, and the segmentation result of the magnesium ingot obtained by traditional methods is incomplete. segmentation result of the magnesium ingot obtained by traditional methods is incomplete. When magnesium ingots are tightly arranged, there are gaps between the top surfaces of the ingots due to their trapezoidal cross-section. The color of the gaps is similar to that of the top surfaces of the ingots. During the process of the magnesium ingot segmentation, these gaps can interfere with the segmentation results, leading to suboptimal image segmentation outcomes. The schematic diagram is shown in Figure 1b Currently, in the industrial field, machine vision [9] technology has been widely applied to image segmentation of metal workpieces to better improve industrial production efficiency [10][11][12]. The high reflectivity of metals is a common problem that affects the effectiveness of segmentation algorithms. Jiang et al. [13] established an experimental platform that is composed of a linear array camera, a linear light source, an encoder, and a rotation system to improve image quality in response to the phenomenon of uneven illumination of images caused by the high reflection characteristics of steel pipes. Furferi et al. [14] designed three different lighting environments for the high reflectivity of small metal parts to obtain images that are more conducive to segmentation processing. Feng et al. [15] proposed an adaptive local tone mapping algorithm based on gradient domain to solve the problem of local oversaturation of highly reflective metal surfaces, reducing color shifts caused by brightness compression, enhancing color perception of images, and compensating for the differences between display devices and natural scenes under visual conditions. Zhou et al. [16] used the BEMD algorithm to process images for high contrast textures caused by objective factors such as lighting in order to filter out noise and suppress surface textures. Qiu et al. [17] designed a dual weighted principal component analysis algorithm based on visual and spatial features to reduce the impact of high reflection on the surface of metal workpieces. Existing studies usually fail to locate overexposed regions accurately, and unrecognized overexposed regions may have new impacts on recognition or segmentation. Some studies focus on the identification of small parts, using filtering methods to repair overexposed areas, which are not suitable for large metal planes. Therefore, this paper proposes an accurate algorithm for detecting exposed regions, and based on this, color correction is performed based on neighborhood information to minimize the impact of overexposure on recognition or segmentation.
On the other hand, the disorderly placement of metal parts also requires high stability and accuracy of the algorithm. Hsu et al. [18] developed a machine vision system capable of automatic classification for different lighting environments. Under back-lighting and front-lighting environments, they used global and local threshold methods to achieve simultaneous segmentation of multiple metal parts. Li et al. [19] proposed a method for detecting and recognizing surface features of metal workpieces. Aiming at the problems of uneven illumination and reflection on the surface of metal workpieces, the Retinex Currently, in the industrial field, machine vision [9] technology has been widely applied to image segmentation of metal workpieces to better improve industrial production efficiency [10][11][12]. The high reflectivity of metals is a common problem that affects the effectiveness of segmentation algorithms. Jiang et al. [13] established an experimental platform that is composed of a linear array camera, a linear light source, an encoder, and a rotation system to improve image quality in response to the phenomenon of uneven illumination of images caused by the high reflection characteristics of steel pipes. Furferi et al. [14] designed three different lighting environments for the high reflectivity of small metal parts to obtain images that are more conducive to segmentation processing. Feng et al. [15] proposed an adaptive local tone mapping algorithm based on gradient domain to solve the problem of local oversaturation of highly reflective metal surfaces, reducing color shifts caused by brightness compression, enhancing color perception of images, and compensating for the differences between display devices and natural scenes under visual conditions. Zhou et al. [16] used the BEMD algorithm to process images for high contrast textures caused by objective factors such as lighting in order to filter out noise and suppress surface textures. Qiu et al. [17] designed a dual weighted principal component analysis algorithm based on visual and spatial features to reduce the impact of high reflection on the surface of metal workpieces. Existing studies usually fail to locate overexposed regions accurately, and unrecognized overexposed regions may have new impacts on recognition or segmentation. Some studies focus on the identification of small parts, using filtering methods to repair overexposed areas, which are not suitable for large metal planes. Therefore, this paper proposes an accurate algorithm for detecting exposed regions, and based on this, color correction is performed based on neighborhood information to minimize the impact of overexposure on recognition or segmentation.
On the other hand, the disorderly placement of metal parts also requires high stability and accuracy of the algorithm. Hsu et al. [18] developed a machine vision system capable of automatic classification for different lighting environments. Under back-lighting and front-lighting environments, they used global and local threshold methods to achieve simultaneous segmentation of multiple metal parts. Li et al. [19] proposed a method for detecting and recognizing surface features of metal workpieces. Aiming at the problems of uneven illumination and reflection on the surface of metal workpieces, the Retinex algorithm was used for image enhancement, the Otus algorithm was used for the segmentation of target features on the surface of metal workpieces, and the vertical projection of a binary graph was used for the single separation of target features. Astain et al. [20] proposed a detection, recognition, and positioning system for reflective metal workpieces, using background subtraction and Viola-Jones classifiers to identify target workpieces. Chen et al. [21] used Rotated MS R-CNN and Mask R-CNN to achieve the segmentation of randomly overlapping screws. Zheng et al. [22] proposed a workpiece capture region recognition method that combines deep learning and support vector machines. The method used the Mask R-CNN neural network to complete the preliminary segmentation of the workpiece capture region and used an SVM classifier with multiple feature fusion to perform secondary fine segmentation of the Mask R-CNN recognition results, achieving the elimination of interference regions, and completing the recognition of the irregular placement of workpieces. The methods based on deep learning have high recognition accuracy, but it is difficult to collect data for training, and the results generated during the scene transformation are unstable. Some research is based on clustering or statistical threshold setting methods. They do not adapt to the complex situation of metal parts in space. Therefore, this paper proposes a method that combines the adaptive threshold segmentation algorithm and the morphological algorithm to segment the corrected image. This method has good robustness and can solve edge recognition errors between metal parts.
Currently, there is little research on large metal ingots such as magnesium ingots. Due to the large surface area of magnesium ingots, which are more susceptible to light effects, research on the segmentation and recognition of small metal workpieces is not applicable to magnesium ingots. In addition, the disorderly placement of metal workpieces may lead to difficulties in extracting complete edges. The industrial-grade 3D sensors are expensive, and when using depth cameras to capture images, the high reflective areas on the magnesium ingot surface cause depth information loss and create holes. Therefore, considering the comprehensive factors, this paper proposes an adaptive threshold magnesium ingot stack segmentation algorithm based on image overexposure region correction, using color cameras to capture images. This algorithm can quickly and robustly segment magnesium ingots in an image. The main contributions of this paper are as follows: Firstly, a color correction algorithm based on overexposure detection is proposed. This algorithm accurately identifies the areas of magnesium ingot overexposure under various lighting conditions and corrects the color information of the target ingot. As a result, the grayscale gradient of the image becomes more uniform, leading to improved image quality. Secondly, based on this color correction algorithm, an adaptive threshold segmentation algorithm for stacking magnesium ingots is designed. This algorithm utilizes threshold segmentation and morphological processing techniques to segment the top surfaces of the corrected images of the ingots. It effectively addresses the issues of segmentation results sticking when magnesium ingots are arranged and distributed and the segmentation challenges caused by overexposure.
The other chapters of this article are described as follows: In the second section, the construction of the experimental light source environment is firstly introduced, then the overexposure area detection algorithm and color correction algorithm are introduced in detail for the experimental object. Finally, an adaptive threshold magnesium ingot stack segmentation algorithm is introduced, which is suitable for the image that the color of the overexposure area is corrected. In the third section, we quantitatively compare our algorithm with other image segmentation algorithms, and design multiple sets of controlled experiments to verify the reliability and accuracy of our algorithm. The fourth section summarizes the main contributions of this article and draws conclusions.

Materials and Methods
This paper proposes an algorithm for detecting and correcting overexposed regions, and designs an adaptive threshold magnesium ingot stack segmentation algorithm based on image overexposed region correction, which can effectively solve segmentation failures caused by object overexposure. The algorithm flowchart is shown in Figure 2. This paper proposes an algorithm for detecting and correcting overexposed regions, and designs an adaptive threshold magnesium ingot stack segmentation algorithm based on image overexposed region correction, which can effectively solve segmentation failures caused by object overexposure. The algorithm flowchart is shown in Figure 2.

Light Source Environment Construction
In order to meet the high-quality requirements for the original image of the image recognition, in visual inspection systems, it is often necessary to design appropriate lighting methods with the help of professional visual light sources to illuminate the target object to be detected. Building a suitable light source environment can enhance the distinction between the target object and the background in the captured image, form an imaging effect conducive to image processing, obtain more information about the target object, and reduce the difficulty of image-processing algorithms [23,24].
In natural environments, differences in time, weather, and the indoor environment can cause uneven brightness distribution in the target area in the captured image, directly affecting the segmentation results of the target object [25]. According to the structural characteristics of the target object and the equipment, this paper has built a light source environment composed of four strip light sources, as shown in Figure 3. The models are KM-BRD120030-W-24 and KM-BRD80030-W-24 from KOMA Vision.

Light Source Environment Construction
In order to meet the high-quality requirements for the original image of the image recognition, in visual inspection systems, it is often necessary to design appropriate lighting methods with the help of professional visual light sources to illuminate the target object to be detected. Building a suitable light source environment can enhance the distinction between the target object and the background in the captured image, form an imaging effect conducive to image processing, obtain more information about the target object, and reduce the difficulty of image-processing algorithms [23,24].
In natural environments, differences in time, weather, and the indoor environment can cause uneven brightness distribution in the target area in the captured image, directly affecting the segmentation results of the target object [25]. According to the structural characteristics of the target object and the equipment, this paper has built a light source environment composed of four strip light sources, as shown in Figure 3. The models are KM-BRD120030-W-24 and KM-BRD80030-W-24 from KOMA Vision.  According to the arrangement of magnesium ingots and the frame structure of the equipment, two sizes and lengths of strip light sources, 800 mm and 1200 mm, are selected and placed symmetrically. They are surrounded by a high angle above the target magnesium ingot for illumination. In order to ensure a clean background for the captured image According to the arrangement of magnesium ingots and the frame structure of the equipment, two sizes and lengths of strip light sources, 800 mm and 1200 mm, are selected and placed symmetrically. They are surrounded by a high angle above the target magnesium ingot for illumination. In order to ensure a clean background for the captured image and to better highlight the target object, the target magnesium ingot is placed on a solid color background. The constructed light source environment has a simple structure and is convenient for debugging, which can more effectively save space without affecting the movement of the mechanical arm above the magnesium ingot. The surrounding lighting scheme can effectively prevent the impact of shadows generated by the movement of the robot arm on the captured image, and avoid uneven brightness caused by shadows generated on the captured image. Figure 4 is a schematic diagram of the installation positions of the experimental camera and the movable mechanical arm. According to the arrangement of magnesium ingots and the frame structur equipment, two sizes and lengths of strip light sources, 800 mm and 1200 mm, are and placed symmetrically. They are surrounded by a high angle above the target sium ingot for illumination. In order to ensure a clean background for the capture and to better highlight the target object, the target magnesium ingot is placed on color background. The constructed light source environment has a simple struct is convenient for debugging, which can more effectively save space without affec movement of the mechanical arm above the magnesium ingot. The surrounding scheme can effectively prevent the impact of shadows generated by the moveme robot arm on the captured image, and avoid uneven brightness caused by shado erated on the captured image. Figure 4 is a schematic diagram of the installation p of the experimental camera and the movable mechanical arm.  Because the magnesium ingot is located at the middle of the light source environment and the surface of the metal magnesium ingot is smooth, the parts of the magnesium ingot that are close to the light source may experience varying degrees of overexposure. However, the overexposure positions in the image are relatively concentrated, located at both ends of the magnesium ingot and at a few positions on the edge of the magnesium ingot, as shown in Figure 5.
Because the magnesium ingot is located at the middle of the light source environment and the surface of the metal magnesium ingot is smooth, the parts of the magnesium ingot that are close to the light source may experience varying degrees of overexposure. However, the overexposure positions in the image are relatively concentrated, located at both ends of the magnesium ingot and at a few positions on the edge of the magnesium ingot, as shown in Figure 5.

Overexposure Area Detection Based on Lab Color Space
We generally believe that the overexposed area in an image is white [26], and the grayscale value of this part in a grayscale image is 255. Using the grayscale threshold segmentation method, the part of the image with a grayscale value greater than 254 is defined as an overexposed area. Figure 6b shows the grayscale threshold segmentation result. As shown in Figure 6b, the segmented result is discrete and the region is not complete enough to handle the transition from the overexposed region to the adjacent region well. In this paper, we use Lab color space to map the brightness and chrominance information in an image to determine whether there is a possibility of overexposure of pixels in the image, obtaining strong and weak exposure areas. Compared to traditional segmentation methods using single color information, this method can better handle the transition from overexposed regions to adjacent regions.

Overexposure Area Detection Based on Lab Color Space
We generally believe that the overexposed area in an image is white [26], and the grayscale value of this part in a grayscale image is 255. Using the grayscale threshold segmentation method, the part of the image with a grayscale value greater than 254 is defined as an overexposed area. Figure 6b shows the grayscale threshold segmentation result. As shown in Figure 6b, the segmented result is discrete and the region is not complete enough to handle the transition from the overexposed region to the adjacent region well. and the surface of the metal magnesium ingot is smooth, the parts of the magnesium ingot that are close to the light source may experience varying degrees of overexposure. However, the overexposure positions in the image are relatively concentrated, located at both ends of the magnesium ingot and at a few positions on the edge of the magnesium ingot, as shown in Figure 5.

Overexposure Area Detection Based on Lab Color Space
We generally believe that the overexposed area in an image is white [26], and the grayscale value of this part in a grayscale image is 255. Using the grayscale threshold segmentation method, the part of the image with a grayscale value greater than 254 is defined as an overexposed area. Figure 6b shows the grayscale threshold segmentation result. As shown in Figure 6b, the segmented result is discrete and the region is not complete enough to handle the transition from the overexposed region to the adjacent region well. In this paper, we use Lab color space to map the brightness and chrominance information in an image to determine whether there is a possibility of overexposure of pixels in the image, obtaining strong and weak exposure areas. Compared to traditional segmentation methods using single color information, this method can better handle the transition from overexposed regions to adjacent regions. In this paper, we use Lab color space to map the brightness and chrominance information in an image to determine whether there is a possibility of overexposure of pixels in the image, obtaining strong and weak exposure areas. Compared to traditional segmentation methods using single color information, this method can better handle the transition from overexposed regions to adjacent regions.

Lab Color Space Conversion
Lab color space is a uniform color space model for visual perception, independent of device differences. It is a digital method for describing human visual perception based on physiological characteristics [27], where L represents the pixel brightness of the image, with a value range of [0, 100], and a and b represent the position of the pixel color in the color space, with a value range of [−127, 127]. This space is a three-dimensional rectangular coordinate system, and the color space model can be referenced in [27]. The three components are perpendicular to each other and correspond to x, y, and z in the coordinate axis based on the non-linear compressed XYZ color space coordinates.
Before converting an image from RGB color space to Lab color space, it is necessary to normalize the r, g, and b values of the image. The conversion relationship is shown in Equations (1) and (2): In addition, the conversion between RGB color space and Lab color space is an indirect process, which requires mapping the RGB color space to the XYZ color space. The conversion relationship is shown in Equation (3).
Finally, the conversion of the XYZ color space to the Lab color space, and the conversion relationship is shown in Equation (4): In Equation (4), X n , Y n , and Z n are related to the row vector elements of the transformation matrix A. The default values are 0.9504, 1.0, and 1.089.

Overexposure Region Segmentation
The strong exposure area in the image has strong brightness, and just performing color correction on the strong exposure area will lead to uneven color transition of the magnesium ingot. Therefore, this paper divides the overexposed area in the image into a strong exposure area and weak exposure area, wherein the strong exposure area is the area that still has the possibility of overexposure in the weak exposure area.
We map the brightness and chroma information of the image I in the Lab color space and perform normalization processing. We use the brightness channels L and color channels C in the Lab color space to define the weak exposure region S in the image, which makes C = a b T . The weak exposure probability value M i of the pixel i is defined as: where L T is the brightness boundary value of the overexposed area, C T is the chromaticity boundary value of the overexposed area, and δ controls the growth rate of M i when L i is larger or C i 2 . is smaller. γ i is a non-linear weight, as shown in Equation (7). It indicates that the greater the brightness, the higher the weight of the pixel in the overexposed area.
The strong exposure area H is the area where there is still a possibility of overexposure in S. The strong exposure probability value P i of the pixel i is defined as: where g is the normalization factor to make max i P i = 1.
The critical value of weak exposure probability d m is 0.5. When M i ≥ d m , it is indicated that there is a possibility of overexposure of the pixel, that is, overexposure occurs. After binarizing all overexposed pixels in the image, its result is shown in Figure 7a. The critical value of strong exposure probability d p is also 0.5. When P i ≥ d p , it is considered that the region where the pixel is located is the area where there is still a possibility of overexposure in S, that is, the strong exposure region. The result is shown in Figure 7b: nels in the Lab color space to define the weak exposure region in the image, which makes = [ ] . The weak exposure probability value of the pixel is defined as: where is the brightness boundary value of the overexposed area, is the chromaticity boundary value of the overexposed area, and controls the growth rate of when is larger or ‖ ‖ . is smaller.
is a non-linear weight, as shown in Equation (7). It indicates that the greater the brightness, the higher the weight of the pixel in the overexposed area.

100
, 100 The strong exposure area is the area where there is still a possibility of overexposure in . The strong exposure probability value of the pixel is defined as: where g is the normalization factor to make The critical value of weak exposure probability is 0.5. When ≥ , it is indicated that there is a possibility of overexposure of the pixel, that is, overexposure occurs. After binarizing all overexposed pixels in the image, its result is shown in Figure 7a. The critical value of strong exposure probability is also 0.5. When ≥ , it is considered that the region where the pixel is located is the area where there is still a possibility of overexposure in , that is, the strong exposure region. The result is shown in Figure 7b: In order to obtain the optimal parameters of , this paper selects five sets of values for comparison. When = 1 , it takes separately = 1/50 , = 1/40 , = 1/30 , = 1/20, and = 1/10. The results are shown in Figure 8. They show that as decreases, is less affected by the brightness and chromaticity of the exposed area, and the proportion of wooden frames in the background becomes larger, which will affect the selection of correction colors; but with the 's increase, gradually approaches the maximum value of 1, at which point is infinite, which will cause segmentation failure of the region. Therefore, it is comprehensively considered to select = 1/30. In order to obtain the optimal parameters of δ, this paper selects five sets of values for comparison. When γ i = 1, it takes separately δ = 1/50, δ = 1/40, δ = 1/30, δ = 1/20, and δ = 1/10. The results are shown in Figure 8. They show that as δ decreases, M i is less affected by the brightness and chromaticity of the exposed area, and the proportion of wooden frames in the background becomes larger, which will affect the selection of correction colors; but with the δ's increase, M i gradually approaches the maximum value of 1, at which point P i is infinite, which will cause segmentation failure of the H region. Therefore, it is comprehensively considered to select δ = 1/30. γ i is a gamma function that serves as a non-linear weight to adjust the brightness information in the image to the impact of M i . The index k is used as a variable in the weight, and its value is selected based on the optimal value obtained through multiple experiments for the object in this paper. We take k = 0.1, k = 0.2, k = 0.3, k = 0.4, k = 0.5, k = 0.7, k = 0.9, k = 1, k = 1.2, and k = 1.4 to compare the results. The comparison results are shown in Figure 9. When k increases, the impact of brightness information on M i becomes more severe, and the gradual disappearance of the brightness changes in the overexposed area. When calculating the H region, the infinite approximation of M i to 1 will result in the fact that the H region segmentation will fail. At this moment, P i is infinitely close to 0. The value of k is too small, resulting in incomplete segmentation of the S region. Therefore, it is comprehensively considered to select k = 0.2. is a gamma function that serves as a non-linear weight to adjust the brightness information in the image to the impact of . The index is used as a variable in the weight, and its value is selected based on the optimal value obtained through multiple experiments for the object in this paper. We take = 0.1, = 0.2, = 0.3, = 0.4, = 0.5, = 0.7, = 0.9, = 1, = 1.2, and = 1.4 to compare the results. The comparison results are shown in Figure 9. When increases, the impact of brightness information on becomes more severe, and the gradual disappearance of the brightness changes in the overexposed area. When calculating the region, the infinite approximation of to 1 will result in the fact that the region segmentation will fail. At this moment, is infinitely close to 0. The value of is too small, resulting in incomplete segmentation of the region. Therefore, it is comprehensively considered to select = 0.2. The Pseudocode of the overexposure detection algorithm is shown in Algorithm 1.  is a gamma function that serves as a non-linear weight to adjust the brightness information in the image to the impact of . The index is used as a variable in the weight, and its value is selected based on the optimal value obtained through multiple experiments for the object in this paper. We take = 0.1, = 0.2, = 0.3, = 0.4, = 0.5, = 0.7, = 0.9, = 1, = 1.2, and = 1.4 to compare the results. The comparison results are shown in Figure 9. When increases, the impact of brightness information on becomes more severe, and the gradual disappearance of the brightness changes in the overexposed area. When calculating the region, the infinite approximation of to 1 will result in the fact that the region segmentation will fail. At this moment, is infinitely close to 0. The value of is too small, resulting in incomplete segmentation of the region. Therefore, it is comprehensively considered to select = 0.2. The Pseudocode of the overexposure detection algorithm is shown in Algorithm 1.
The Pseudocode of the overexposure detection algorithm is shown in Algorithm 1. Compared to the segmentation by single threshold, the proposed method can segment more complete regions, including most of the regions in the image where brightness and chroma differ. In addition, the proposed method has a better ability to handle transition regions and obtain more complete segmentation results.

Overexposure Area Color Correction
Since the overexposed regions in the image are mainly concentrated at both ends and edge positions of the magnesium ingot, the region in the magnesium ingot whose color information is normal is used as a reference to determine the correction color, filling this color into the segmented overexposed region. Finally, the correction image of strong exposure and the correction image of weak exposure are fused according to the pixel WA [28].

Acquisition of Magnesium Ingot Region Mask
Due to the shape and surface physical characteristics of magnesium ingots, which are affected by the surrounding lighting and lighting methods, the overexposed areas in the image are mainly concentrated at both ends of the magnesium ingot. Therefore, to correct the color of the overexposed areas, it is necessary to obtain the appropriate color in the normal color area of the magnesium ingot. First, it is important to eliminate the background effect in order to better obtain the appropriate color of the magnesium ingot.
The main objects in the collected images have significant color differences, and the HSV color space is used to segment the image background. After color space conversion of the image, the three channels of the HSV color space are separated and represented as binary images. Histograms of the three channels are drawn, as shown in Figure 10. In Figure 10a-c, the horizontal bar in the middle is a magnesium ingot, while the remaining objects are a wooden pallet and solid colored ground backgrounds. The threshold segmentation of images utilizes the "bimodality" characteristic of image histograms. The binary image histograms in Figure 10d,e have obvious "bimodality", while the binary image histograms in Figure 10f do not have this characteristic. So, it does not meet the conditions for the threshold segmentation of images. In Figure 10a, the magnesium ingot region is not significantly distinguished from other background objects in The threshold segmentation result obtained from the S channel binary image, namely the magnesium ingot region mask, , is applied as a mask to the original image to remove the background object and retain only the magnesium ingot. The result is shown in Figure 11c.

Determining the Correction Color and Filling of Overexposed Areas
The color correction of an image is a process of inferring and filling to the color and texture information of the region to be corrected based on known region information. The color of the overexposed area of the magnesium ingot region differs from the normal region of the middle portion, and there is a significant color transition from both ends to the middle. A secondary filling method is used for color correction of this portion. Different correction colors are used to fill strong and weak exposure areas, respectively. Compared to single large area filling, processing for areas with different overexposure levels can achieve a uniform color transition effect.
The reference area for color correction is obtained from the exposure probability values of the underexposed pixels in the magnesium ingot. Taking color correction in the weakly exposed areas as an example, the algorithm is detailed as follows: A. Calculating the threshold of color reference area.
Using the idea of mask superposition, the original image is overlaid with the nonmagnesium ingot region mask, , to obtain the non-magnesium ingot region . According to Equation (6), the weak exposure probability value, ( ∈ ) of pixel in is calculated. The result of Equation (9) is the threshold , which is as the left critical threshold for dividing the color reference region. Right critical threshold is from Section 2.2.2, that is = 0.5.
B. Obtaining color reference areas.
We superposition the original image with to obtain the magnesium ingot region . If the pixel within satisfies: ≤ ≤ , ∈ , it is indicated that the region where the pixel is located can provide a color reference for the overexposed region, and it is recorded as the reference region . If the pixel satisfies: ≥ , ∈ , it is considered that the region where the pixel is located is overexposed, and it is recorded as the region to be corrected . The binarized image obtained using two critical thresholds is shown in Figure 12. The threshold segmentation of images utilizes the "bimodality" characteristic of image histograms. The binary image histograms in Figure 10d,e have obvious "bimodality", while the binary image histograms in Figure 10f do not have this characteristic. So, it does not meet the conditions for the threshold segmentation of images. In Figure 10a, the magnesium ingot region is not significantly distinguished from other background objects in terms of hue values. If threshold segmentation is performed using the lowest point value of the bimodal depression in the histogram of the H channel binary image in Figure 10d, it will result in incomplete segmentation of the magnesium ingot region and the image background. Figure 11a shows the threshold segmentation result of an H channel binary image. The magnesium ingot region is confused with a solid color background, which is detrimental to the subsequent determination of the correction color. Using pixel saturation information to perform threshold segmentation on the S channel binary image, the resulting segmentation result is shown in Figure 11b, which better preserves the magnesium ingot region in the original image.
The threshold segmentation result obtained from the S channel binary image, namely the magnesium ingot region mask, mask, is applied as a mask to the original image to remove the background object and retain only the magnesium ingot. The result is shown in Figure 11c.

Determining the Correction Color and Filling of Overexposed Areas
The color correction of an image is a process of inferring and filling to the color and texture information of the region to be corrected based on known region information. The color of the overexposed area of the magnesium ingot region differs from the normal region of the middle portion, and there is a significant color transition from both ends to the middle. A secondary filling method is used for color correction of this portion. Different correction colors are used to fill strong and weak exposure areas, respectively. Compared to single large area filling, processing for areas with different overexposure levels can achieve a uniform color transition effect.
The reference area for color correction is obtained from the exposure probability values of the underexposed pixels in the magnesium ingot. Taking color correction in the weakly exposed areas as an example, the algorithm is detailed as follows: A. Calculating the threshold of color reference area.
Using the idea of mask superposition, the original image is overlaid with the nonmagnesium ingot region mask, mask, to obtain the non-magnesium ingot region N. According to Equation (6), the weak exposure probability value, M j (j ∈ N) of pixel j in N is calculated. The result of Equation (9) is the threshold T 1 , which is as the left critical threshold for dividing the color reference region. Right critical threshold T 2 is d m from Section 2.2.2, that is T 2 = 0.5.
B. Obtaining color reference areas.
We superposition the original image with mask to obtain the magnesium ingot region Q. If the pixel l within Q satisfies: T 1 ≤ M l ≤ T 2 , l ∈ Q, it is indicated that the region where the pixel is located can provide a color reference for the overexposed region, and it is recorded as the reference region C. If the pixel l satisfies: M l ≥ T 2 , l ∈ Q, it is considered that the region where the pixel is located is overexposed, and it is recorded as the region to be corrected R. The binarized image obtained using two critical thresholds is shown in Figure 12. C. Calculating the correction color of the area to be corrected.
The filling color of the region to be corrected is calculated using the RGB value of the pixel in the reference region , and the calculation equation is shown in Equation (10).
where , ̅ , and is the , , and values of the filling color of the pixels in , represents the number of pixels in , and , , and is the , , and values of pixels within .

D. Filling color.
is filled with the color that was obtained from Equation (10), and the result is shown in Figure 13b. The portion of the magnesium ingot area that serves as a color reference. The white area is the area providing color reference. The white area is C, and based on this area, a color reference is provided for R. C. Calculating the correction color of the area to be corrected.
The filling color of the region to be corrected R is calculated using the RGB value of the pixel in the reference region C, and the calculation equation is shown in Equation (10).
where R, G, and B is the r, g, and b values of the filling color of the pixels in R, n represents the number of pixels in C, and R l , G l , and B l is the r, g, and b values of pixels within C.
D. Filling color.
R is filled with the color that was obtained from Equation (10), and the result is shown in Figure 13b. where , ̅ , and is the , , and values of the filling color of the pixels in , represents the number of pixels in , and , , and is the , , and values of pixels within .

D. Filling color.
is filled with the color that was obtained from Equation (10), and the result is shown in Figure 13b.  The Pseudocode of the color correction algorithm is shown in Algorithm 2.

Algorithm 2:
Color correction algorithm (taking color correction for weak exposure as an example).
Input: image I, S, H Output: R, G, B 1: convert I to HSV color space 2: s_image ← the S channel image of image I 3: mask ← the result of threshold segmentation for s_image, represents the magnesium ingot area, and mask represents non-magnesium ingot region.

4:
N ← the result of applying mask to image I 5: Q ← the result of applying mask to image I 6: for j ∈ I do 7: calculate the weak exposure probability value, M j (j ∈ N) of pixel j in N 8: T 1 is the Maximum value of M j 9: calculate the weak exposure probability value, M l (l ∈ Q) of pixel l in Q 10: T 2 is d m , critical value of weak exposure probability 11: if 12: T 1 ≤ M l ≤ T 2 , l ∈ Q 13: pixels l can provide a color reference for the overexposed region 14: C is the reference region 15: As can be seen from the Figure 13b, the color of the overexposed area has been improved. But after color filling, the filling edge appears jagged, and the color transition is uneven. A median filter is selected to filtering to blur its outline and interior and making the overall image color uniform, which achieves a smooth transition between high and low frequencies and protects edge information effectively. The result of median filter processing is shown in Figure 13c. The color correction algorithm for strong exposure areas is the same, and the results are shown in Figure 14.
As can be seen from the Figure 13b, the color of the overexposed area has been improved. But after color filling, the filling edge appears jagged, and the color transition is uneven. A median filter is selected to filtering to blur its outline and interior and making the overall image color uniform, which achieves a smooth transition between high and low frequencies and protects edge information effectively. The result of median filter processing is shown in Figure 13c. The color correction algorithm for strong exposure areas is the same, and the results are shown in Figure 14.

Image Fusion
The image that is corrected by single exposure intensity has a certain degree of uneven color transition. From Section 2.2.2, we obtained the results of color correction for strong and weak exposure areas. According the rule of WA, the results are fused, as shown in Equation (11).
where dst represents an effect image after image fusion; α and β is image fusion weight, affecting the proportion of the image to be fused in the fusion result; src M and src P represents the result image after color correction for weak and strong exposure; and γ represents a scalar value attached to the sum of weights, and when γ becomes larger, the overall brightness value of the image becomes larger. For the object of this paper, α = 0.5, β = 0.4, and γ = 0, and the image fusion result is shown in Figure 15.

Image Fusion
The image that is corrected by single exposure intensity has a certain degree of uneven color transition. From Section 2.2.2, we obtained the results of color correction for strong and weak exposure areas. According the rule of WA, the results are fused, as shown in Equation (11).
where represents an effect image after image fusion; and is image fusion weight, affecting the proportion of the image to be fused in the fusion result; and represents the result image after color correction for weak and strong exposure; and represents a scalar value attached to the sum of weights, and when becomes larger, the overall brightness value of the image becomes larger. For the object of this paper, = 0.5, = 0.4, and = 0, and the image fusion result is shown in Figure 15. Compared to the original image, the fusion result shows that the overexposure phenomenon on the magnesium ingot region is reduced and the color transition is uniform. From Figure 16, the number of pixels in the grayscale range 230-255 in Figure  16b is significantly less than that in Figure 16a, especially when the grayscale value is 255, which indicates that the overexposure area detection and correction algorithm proposed Compared to the original image, the fusion result shows that the overexposure phenomenon on the magnesium ingot region is reduced and the color transition is uniform. From Figure 16, the number of pixels in the grayscale range 230-255 in Figure 16b is significantly less than that in Figure 16a, especially when the grayscale value is 255, which indicates that the overexposure area detection and correction algorithm proposed in this paper can effectively solve the overexposure phenomenon in the magnesium ingot region of the image.

Segmentation Algorithm Based on the Correction of the Image Overexposure Area
Compared to the original image, the brightness is more uniform and color information is more complete for the corrected image, which is conducive to the segmentation of magnesium ingot regions. In this paper, an adaptive threshold segmentation algorithm [29] is used to segment the magnesium ingot regions in the image. This method can adapt to complex and variable lighting environments compared to traditional threshold segmentation methods.
The result of color correction in Section 2.3.3 is superimposed with obtained from Section 2.3.1. The results are shown in Figure 17b. It only retains the magnesium ingot region that narrows the segmentation range and reduces the computational complexity of the algorithm. Figure 17c is the result of grayscale conversion. The adaptive threshold segmentation function is run with select appropriate parameters and the results are shown in Figure 17d. The obtained result is processed by image morphology, and the result of the final segmentation is shown in Figure 17e.

Results and Discussion
To verify the image segmentation effect of the algorithm in this paper, experiments were conducted on an automatic magnesium ingot sorting device, as shown in Figure 18. Since there is overlap in the grayscale value range between the wooden frame area and the magnesium ingot area, the recognition of magnesium ingots cannot be achieved solely

Segmentation Algorithm Based on the Correction of the Image Overexposure Area
Compared to the original image, the brightness is more uniform and color information is more complete for the corrected image, which is conducive to the segmentation of magnesium ingot regions. In this paper, an adaptive threshold segmentation algorithm [29] is used to segment the magnesium ingot regions in the image. This method can adapt to complex and variable lighting environments compared to traditional threshold segmentation methods.
The result of color correction in Section 2.3.3 is superimposed with mask obtained from Section 2.3.1. The results are shown in Figure 17b. It only retains the magnesium ingot region that narrows the segmentation range and reduces the computational complexity of the algorithm. Figure 17c is the result of grayscale conversion. The adaptive threshold segmentation function is run with select appropriate parameters and the results are shown in Figure 17d. The obtained result is processed by image morphology, and the result of the final segmentation is shown in Figure 17e.

Segmentation Algorithm Based on the Correction of the Image Overexposure Area
Compared to the original image, the brightness is more uniform and color information is more complete for the corrected image, which is conducive to the segmentation of magnesium ingot regions. In this paper, an adaptive threshold segmentation algorithm [29] is used to segment the magnesium ingot regions in the image. This method can adapt to complex and variable lighting environments compared to traditional threshold segmentation methods.
The result of color correction in Section 2.3.3 is superimposed with obtained from Section 2.3.1. The results are shown in Figure 17b. It only retains the magnesium ingot region that narrows the segmentation range and reduces the computational complexity of the algorithm. Figure 17c is the result of grayscale conversion. The adaptive threshold segmentation function is run with select appropriate parameters and the results are shown in Figure 17d. The obtained result is processed by image morphology, and the result of the final segmentation is shown in Figure 17e.

Results and Discussion
To verify the image segmentation effect of the algorithm in this paper, experiments were conducted on an automatic magnesium ingot sorting device, as shown in Figure 18. Since there is overlap in the grayscale value range between the wooden frame area and

Results and Discussion
To verify the image segmentation effect of the algorithm in this paper, experiments were conducted on an automatic magnesium ingot sorting device, as shown in Figure 18. Since there is overlap in the grayscale value range between the wooden frame area and the magnesium ingot area, the recognition of magnesium ingots cannot be achieved solely through the grayscale information of the images. Therefore, a color camera is chosen for image collection. The camera used in the experiment is Hikvision's MV-CA060-10GC color camera, as shown in Figure 19a. The lens model used is Hikvision's MVL-HF0628M-6MP wide-angle lens, as shown in Figure 19b. Its focal length is 6 mm and the image resolution is 3072 × 2048. The experiments shown in this paper mainly validate the algorithm from four aspects: (  (1) Verifying the impact of illumination on the algorithm when the magnesium ingot is at different positions.
Due to the method of lighting on all sides, the distance between magnesium ingots at different locations and the light source is different, and magnesium ingots close to the light source are significantly affected by light, making overexposure more likely to occur. By controlling the number, placement position, and placement posture of magnesium ingots, the experiment was designed. And the segmentation results of magnesium ingots are shown in Figure 20.  (1) Verifying the impact of illumination on the algorithm when the magnesium ingot is at different positions.
Due to the method of lighting on all sides, the distance between magnesium ingots at different locations and the light source is different, and magnesium ingots close to the light source are significantly affected by light, making overexposure more likely to occur. By controlling the number, placement position, and placement posture of magnesium ingots, the experiment was designed. And the segmentation results of magnesium ingots are shown in Figure 20. (1) Verifying the impact of illumination on the algorithm when the magnesium ingot is at different positions.
Due to the method of lighting on all sides, the distance between magnesium ingots at different locations and the light source is different, and magnesium ingots close to the light source are significantly affected by light, making overexposure more likely to occur. By controlling the number, placement position, and placement posture of magnesium ingots, the experiment was designed. And the segmentation results of magnesium ingots are shown in Figure 20. In Figure 20, several images are selected with distinct positional characteristics and processed using the algorithm in this paper. The second row images are the segmentation results of images. In Figure  20a-f, when the magnesium ingot is located at different positions, the degree of overexposure on the surface will change. For the same magnesium ingot, both ends of the magnesium ingot are closer to the light source, so the positions of both ends of the magnesium ingot are significantly overexposed. When multiple magnesium ingots are arranged, the magnesium ingots located at the edge of the wooden tray are more prone to overexposure. From the results of image segmentation, whether it is changing the number of magnesium ingots or changing the placement position of magnesium ingots, the algorithm in this paper can achieve good segmentation results.
(2) Verifying the influence of light intensity on the algorithm. The intensity of the light source will directly affect the area of the overexposed area on the surface of the magnesium ingot. In order to verify the adaptability of the algorithm in this paper to different overexposed areas and to be able to adapt to the interference of light sources with different intensities, a control experiment is designed for different illumination. The brightness of the light source of the strip light is adjusted through a dimmer controller. And the segmentation results of the magnesium ingot are shown in Figure 21. In Figure 20, several images are selected with distinct positional characteristics and processed using the algorithm in this paper. The second row images are the segmentation results of images. In Figure 20a-f, when the magnesium ingot is located at different positions, the degree of overexposure on the surface will change. For the same magnesium ingot, both ends of the magnesium ingot are closer to the light source, so the positions of both ends of the magnesium ingot are significantly overexposed. When multiple magnesium ingots are arranged, the magnesium ingots located at the edge of the wooden tray are more prone to overexposure. From the results of image segmentation, whether it is changing the number of magnesium ingots or changing the placement position of magnesium ingots, the algorithm in this paper can achieve good segmentation results.
(2) Verifying the influence of light intensity on the algorithm. The intensity of the light source will directly affect the area of the overexposed area on the surface of the magnesium ingot. In order to verify the adaptability of the algorithm in this paper to different overexposed areas and to be able to adapt to the interference of light sources with different intensities, a control experiment is designed for different illumination. The brightness of the light source of the strip light is adjusted through a dimmer controller. And the segmentation results of the magnesium ingot are shown in Figure 21.
As can be seen from the input image in Figure 21, as the brightness of the light source increases, the overexposure area of the magnesium ingot region gradually increases, and the degree of overexposure becomes more severe. From the segmentation results of the magnesium ingot image, the algorithm in this paper is basically not affected by changes in light source brightness, and has good reliability. It can maintain a higher rate of image segmentation under different light source brightness.  As can be seen from the input image in Figure 21, as the brightness of the light source increases, the overexposure area of the magnesium ingot region gradually increases, and the degree of overexposure becomes more severe. From the segmentation results of the magnesium ingot image, the algorithm in this paper is basically not affected by changes in light source brightness, and has good reliability. It can maintain a higher rate of image segmentation under different light source brightness.
(3) Verifying the effectiveness of the algorithm under actual operating conditions. In an industrial production environment, magnesium ingots are arranged in a vertical cross shape and in multiple layers. Due to the limit of experimental conditions, the number of magnesium ingots is insufficient. Take a double-layer arrangement of magnesium ingots as an example, and the specific arrangement is shown in Figure 22. In addition, during the actual sorting process, magnesium ingots are identified layer by layer, and each layer of magnesium ingots is covered, enabling one-time access to the location information of the entire layer of magnesium ingots. The experiment arranges magnesium ingots according to actual industrial production, and designs a comparison of brightness values of light sources. (3) Verifying the effectiveness of the algorithm under actual operating conditions. In an industrial production environment, magnesium ingots are arranged in a vertical cross shape and in multiple layers. Due to the limit of experimental conditions, the number of magnesium ingots is insufficient. Take a double-layer arrangement of magnesium ingots as an example, and the specific arrangement is shown in Figure 22. In addition, during the actual sorting process, magnesium ingots are identified layer by layer, and each layer of magnesium ingots is covered, enabling one-time access to the location information of the entire layer of magnesium ingots. The experiment arranges magnesium ingots according to actual industrial production, and designs a comparison of brightness values of light sources.
In Figure 23, it can be seen from the image in the first row that the lower magnesium ingots leaked out in the middle of the magnesium ingot gap. Due to the fixed position of the strip light source, as the number of layers of magnesium ingots increases, the distance from the light source becomes closer and closer, and the overexposure situation on the surface of the magnesium ingot changes. In addition, a comparison of light source brightness value is designed. From the results of image segmentation, the algorithm in this paper can still have the good accuracy of segmentation under the actual working conditions, even if the brightness of the light source changes. In Figure 23, it can be seen from the image in the first row that the lower magnesium ingots leaked out in the middle of the magnesium ingot gap. Due to the fixed position o the strip light source, as the number of layers of magnesium ingots increases, the distanc from the light source becomes closer and closer, and the overexposure situation on th surface of the magnesium ingot changes. In addition, a comparison of light sourc brightness value is designed. From the results of image segmentation, the algorithm in this paper can still have the good accuracy of segmentation under the actual working conditions, even if the brightness of the light source changes.   In Figure 23, it can be seen from the image in the first row that the lower magnesium ingots leaked out in the middle of the magnesium ingot gap. Due to the fixed position of the strip light source, as the number of layers of magnesium ingots increases, the distance from the light source becomes closer and closer, and the overexposure situation on the surface of the magnesium ingot changes. In addition, a comparison of light source brightness value is designed. From the results of image segmentation, the algorithm in this paper can still have the good accuracy of segmentation under the actual working conditions, even if the brightness of the light source changes. (4) Comparing with other algorithms to verify the reliability of this algorithm. This experiment selects four magnesium ingot images with different postures and compares the segmentation results of the four algorithms. They are the Otsu threshold [30], Triangle threshold [31], Adaptive threshold, and proposed method. The segmentation results of the experiment are shown in Figure 24. (4) Comparing with other algorithms to verify the reliability of this algorithm. This experiment selects four magnesium ingot images with different postures and compares the segmentation results of the four algorithms. They are the Otsu threshold [30], Triangle threshold [31], Adaptive threshold, and proposed method. The segmentation results of the experiment are shown in Figure 24.
From Figure 24 the four segmentation methods can achieve good segmentation results for a single magnesium ingot and when magnesium ingots are separated from each other. When the ingots are closely aligned, in Figure 24(b3,c3,b4,c4), there is an adhesion phenomenon in the segmentation results, which makes it impossible to distinguish between magnesium ingots and magnesium ingot gaps. Therefore, the Otsu threshold and Triangle threshold are not suitable for magnesium ingot segmentation. In Figure 24(d3,d4), mag-nesium ingots and magnesium ingot gaps are distinguished to some extent, but there are varying degrees of missing phenomena in the segmentation results. The reason is that after processing the image using the Adaptive threshold, the pixels in the initial segmentation result do not have connectivity, resulting in the failure of subsequent morphological processing. The initial segmentation result of Figure 24(a4) is shown in Figure 25. Compared with the Adaptive threshold, the segmentation result of the proposed method is more complete. The proposed method can cope with the changing posture of magnesium ingots with greater accuracy and robustness.  From Figure 24 the four segmentation methods can achieve good segmentation results for a single magnesium ingot and when magnesium ingots are separated from each other. When the ingots are closely aligned, in Figure 24(b3,c3,b4,c4), there is an adhesion phenomenon in the segmentation results, which makes it impossible to distinguish between magnesium ingots and magnesium ingot gaps. Therefore, the Otsu threshold and Triangle threshold are not suitable for magnesium ingot segmentation. In Figure  24(d3,d4), magnesium ingots and magnesium ingot gaps are distinguished to some extent, but there are varying degrees of missing phenomena in the segmentation results. The reason is that after processing the image using the Adaptive threshold, the pixels in the initial segmentation result do not have connectivity, resulting in the failure of subsequent morphological processing. The initial segmentation result of Figure 24(a4) is shown in We collected a total of 250 magnesium ingot images, including various light source brightness, various magnesium ingot postures, and magnesium ingot arrangement methods. When each magnesium ingot in the image is recognized independently, the image is considered as a fully recognized image. The calculation for the segmentation accuracy in this paper is the percentage of fully recognized images out of the total number of captured images. Comparative experiments were conducted between the three threshold segmentation algorithms and ours, and their segmentation accuracies are shown in Table 1. Because the experimental image contains multiple magnesium ingot arrangements, the Otsu threshold and Triangle threshold cannot accurately distinguish between magnesium ingots and magnesium ingot gaps, and their segmentation accuracies are unsatisfactory. Compared with the Adaptive threshold, the segmentation accuracy of the proposed method has significantly improved. We collected a total of 250 magnesium ingot images, including various light source brightness, various magnesium ingot postures, and magnesium ingot arrangement methods. When each magnesium ingot in the image is recognized independently, the image is considered as a fully recognized image. The calculation for the segmentation accuracy in this paper is the percentage of fully recognized images out of the total number of captured images. Comparative experiments were conducted between the three threshold segmentation algorithms and ours, and their segmentation accuracies are shown in Table 3. Because the experimental image contains multiple magnesium ingot arrangements, the Otsu threshold and Triangle threshold cannot accurately distinguish between magnesium ingots and magnesium ingot gaps, and their segmentation accuracies are unsatisfactory. Compared with the Adaptive threshold, the segmentation accuracy of the proposed method has significantly improved. From the collected images, a total of 225 images were selected. They were divided into two groups of experiments according to different the postures and light source brightness of magnesium ingot images, comparing the recognition rates of the four methods.
In Figure 26, the Otsu threshold and Triangle threshold have unideal recognition rates in experiments and cannot meet normal industrial needs. Due to changes in the brightness of the light source, the overexposure on the surface of the magnesium ingot will change, increasing the difficulty of segmentation, and significantly reducing the recognition rate. In experiments with different locations of magnesium ingots as variables, the recognition rates of the Adaptive threshold and the proposed method in this paper are both relatively high, reaching 90.91% and 95.96%, respectively; however, in experiments with different light source brightness as variables, the recognition rate of the Adaptive threshold is only 88.71%, while the recognition rate of the proposed method can reach  From the collected images, a total of 225 images were selected. They were divided into two groups of experiments according to different the postures and light source brightness of magnesium ingot images, comparing the recognition rates of the four methods.
In Figure 26, the Otsu threshold and Triangle threshold have unideal recognition rates in experiments and cannot meet normal industrial needs. Due to changes in the brightness of the light source, the overexposure on the surface of the magnesium ingot will change, increasing the difficulty of segmentation, and significantly reducing the recognition rate. In experiments with different locations of magnesium ingots as variables, the recognition rates of the Adaptive threshold and the proposed method in this paper are both relatively high, reaching 90.91% and 95.96%, respectively; however, in experiments with different light source brightness as variables, the recognition rate of the Adaptive threshold is only 88.71%, while the recognition rate of the proposed method can reach 96.77%, which proves that the proposed method has strong anti-interference ability in the face of changes in ambient luminance, and the overall recognition rate is high.
In addition, during the magnesium alloy smelting process, the basic production requirement is to deliver two magnesium ingots per minute. The real-time requirement for the identification process is not high, and our current identification efficiency can meet the production requirements. In addition, during the magnesium alloy smelting process, the basic production requirement is to deliver two magnesium ingots per minute. The real-time requirement for the identification process is not high, and our current identification efficiency can meet the production requirements.

Conclusions
This algorithm can be applied to the recognition of magnesium ingots with high reflectivity on the surface, solving the segmentation difficulties caused by overexposure of magnesium ingots due to changes in lighting. This paper proposes an algorithm for detecting and correcting overexposed regions for high reflection of magnesium ingots, and designs an adaptive threshold magnesium ingot stack segmentation algorithm based on image overexposed region correction. This algorithm identifies overexposed areas in an image through the mapping relationship between image brightness and chrominance information, and corrects them based on their neighborhood color information. The corrected image effectively improves segmentation accuracy, and this algorithm can effectively improve the antienvironmental interference ability of image segmentation processing.
In the experiment presented in this paper, we achieved satisfactory segmentation results for the magnesium ingots with our current shape and size under the current lighting environment. However, in order to obtain even better experimental results, we plan to further explore the segmentation of magnesium ingots with different shapes and sizes under more complex lighting conditions.

Conclusions
This algorithm can be applied to the recognition of magnesium ingots with high reflectivity on the surface, solving the segmentation difficulties caused by overexposure of magnesium ingots due to changes in lighting. This paper proposes an algorithm for detecting and correcting overexposed regions for high reflection of magnesium ingots, and designs an adaptive threshold magnesium ingot stack segmentation algorithm based on image overexposed region correction. This algorithm identifies overexposed areas in an image through the mapping relationship between image brightness and chrominance information, and corrects them based on their neighborhood color information. The corrected image effectively improves segmentation accuracy, and this algorithm can effectively improve the antienvironmental interference ability of image segmentation processing.
In the experiment presented in this paper, we achieved satisfactory segmentation results for the magnesium ingots with our current shape and size under the current lighting environment. However, in order to obtain even better experimental results, we plan to further explore the segmentation of magnesium ingots with different shapes and sizes under more complex lighting conditions.