Image shadow restoration method based on residual mask guidance
Through the Transformer-based residual mask guidance method, the real shadow-free image and the residual mask of the repair result are used for iterative repair, which solves the problems of high mask prompt dependence and edge effects in image shadow repair, and achieves a more natural , precise shadow repair effects, improving image quality and performance of subsequent tasks.
Patent Information
- Application Number
- CN202411073021.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-06
- Publication Date
- 2024-09-03
- Estimated Expiration
- 2044-08-06
AI Technical Summary
The existing technology relies too much on mask prompts in image shadow repair and has obvious shadow edge effects, resulting in unnatural repair effects and affecting image quality and look and feel.
The Transformer-based residual mask guidance method is used to perform iterative repair through the residual mask between the real shadow-free image and the repair result. The residual mask loss function is used to optimize the shadow repair effect and reduce the dependence on the input mask prompts. .
It achieves a more natural and accurate shadow repair effect, improves the performance of subsequent tasks, enhances engineering application capabilities, and reduces edge effects and background differences after shadow repair.
Smart Images

Figure CN118587129A
Abstract
Description
Technical Field
[0001] The present invention relates to the fields of image processing and video processing, and specifically to an image shadow restoration method based on residual mask guidance. Background Art
[0002] Shadows are dark areas formed on scenes or targets due to targets blocking direct illumination of light sources, and can be seen everywhere in nature. Appropriate shadows can provide certain depth information and improve the realism of the scene; but at the same time, inappropriate shadows will not only reduce the visual quality and aesthetics of the image, but also affect the color and texture information of the target, occluding important information and details in the image, thereby increasing the difficulty of visual recognition and reducing the stability and accuracy of subsequent tasks. Due to the different types, intensities, and tones of the light sources that form shadows, and the different materials, sizes, and shapes of the occluding targets, the resulting shadows are also different. Based on this, how to efficiently and accurately remove image shadows has become a very important challenge in the field of image processing.
[0003] Traditional image shadow removal algorithms are usually implemented based on mathematical methods and conventional image processing methods: after modeling and analyzing the background in the image, statistical methods are used to find the shadow area and non-shadow area in the image, and the shadow area is interpolated or textured, and finally the image is smoothed through appropriate post-processing. Although the principle of this method is simple, it has poor processing effect for complex scenes and is prone to image distortion and information loss; in addition, the shadow removal of traditional methods is implemented on the basis of shadow detection, so if the detection result is inaccurate, the repair result will also have irreparable errors.
[0004] In recent years, deep learning has shown an explosive development trend and amazing processing effects, and has also made certain breakthroughs in the field of image shadow removal. Shadow restoration methods based on convolutional neural networks, generative adversarial networks, etc. have emerged. These algorithms do not need to accurately model and analyze the image background like traditional algorithms. Instead, they use deep models with powerful computing power to learn feature information such as texture color of non-shadow areas from large-scale data, and directly achieve efficient and flexible shadow removal. However, there is a certain correlation between shadow areas and non-shadow areas. Properly capturing the relationship between the two can effectively improve the shadow restoration effect of the image. In 2023, Guo et al. proposed a Transformer-based shadow restoration algorithm ShadowFormer, trying to use the excellent performance of Transformer in processing long-range dependencies and global context information to process shadow areas, thereby obtaining a more natural shadow restoration image. However, similar to traditional algorithms, this algorithm is highly dependent on the input mask prompts; in addition, the shadow edge effect of the restoration effect obtained by this algorithm is obvious, and the shadow-restored area is more prominent than the surrounding background, affecting the image quality and perception. Summary of the invention
[0005] In order to overcome the problem of excessive dependence on mask prompts and obvious shadow edge effects in the shadow restoration process, the present invention provides an image shadow restoration method based on residual mask guidance. Transformer can achieve global perception, and iterative restoration of shadow images is achieved based on the residual mask between the real shadow-free image and the restoration result and the designed residual mask loss function, so as to obtain a more natural and accurate shadow restoration effect, which can greatly improve the performance of subsequent tasks and enhance engineering application capabilities.
[0006] To achieve the above purpose, the technical solution proposed in the present invention is: an image shadow restoration method based on residual mask guidance. The method mainly includes the following steps: Step (1) Preliminary shadow removal of the input image. Based on the fact that the Transformer network can perceive global context information without being restricted by the length of the input sequence, the input image is first linearly mapped and the image features are extracted using an L-layer encoder structure. The correlation between the shadow area and the non-shadow area is then learned using the hint information provided by the input mask. The output fusion feature information is then decoded using an L-layer decoder structure and mapped back to the original image feature space, thereby achieving preliminary shadow removal. Step (2) Residual mask extraction. During the training process, there is a real shadow-free image as a control. Therefore, the residual mask at this time can be calculated based on the preliminary shadow removal result and the real shadow-free image. The main implementation idea is to perform background difference on the two images, supplemented by appropriate threshold segmentation algorithm and morphological processing method.
[0007] Step (3) Calculate the residual mask loss. Calculate the residual mask loss value obtained at this time according to the designed mask loss function and compare it with the set threshold.
[0008] Step (4) Iterative update of the repair effect. When the loss value calculated in step (3) is greater than the set threshold, the repaired image obtained in step (1) is used as the shadowed image in the next round of iteration, and the residual mask is used as the guide mask at this time, and steps (1) to (3) are re-executed; when the loss value calculated in step (3) is less than or equal to the set threshold, the repair process is completed, and the output image at this time is the shadow repair result finally obtained by the network.
[0009] Furthermore, in step (1), the distinction between the shadow area and the non-shadow area is achieved by a mask. The mask is a binary image, where the shadow area is 1 and the non-shadow area is 0. Therefore, all the shadow areas of the original input image can be annotated by the binary annotated image.
[0010] Furthermore, in step (2), since the shadow image is usually darker than the true shadow-free image, the two images need to be normalized first, that is, the maximum values of the two images are unified to 1.0 according to the following formula: , Wherein, image is the image to be normalized, and max(·) is the maximum value operation. Then, the background difference between the two images is performed to ensure that the background difference between the two images is minimized, and the background can be directly filtered out through threshold segmentation.
[0011] Compared with the prior art, the beneficial effects and advantages of the present invention are as follows: (1) The present invention uses the Trasnformer network structure to effectively capture the global context information and long-range dependencies in the image, which can better learn the correlation between shadow areas and non-shadow areas. Compared with traditional algorithms, it can handle more complex and changeable shadow situations. (2) The present invention uses the background difference method to obtain the shadow mask. By performing background difference between the real shadow-free image and the shadowed image, and supplemented by appropriate threshold segmentation method and morphological processing method, a relatively accurate shadow mask can be obtained. The algorithm is simple and easy to implement.
[0012] (3) The present invention uses residual masks to achieve iterative restoration of shadow images. Compared with existing deep learning algorithms and other shadow removal algorithms implemented using Transformer, the present invention does not need to input accurate shadow area mask prompts. It only needs to input rough mask information to obtain natural and realistic shadow removal effects, complete and repair effective information obscured by shadows, and improve the performance of subsequent advanced image processing tasks such as target recognition and image segmentation. In addition, the restoration effect obtained by the method of the present invention has weak edge effects and smaller differences from the background, so it has a wider applicability and higher practicality and commercial value. BRIEF DESCRIPTION OF THE DRAWINGS
[0013] Figure 1 It is an overall flow chart of the image shadow restoration method based on residual mask guidance of the present invention; Figure 2 A network structure diagram of the image shadow restoration method based on residual mask guidance of the present invention; Figure 3A The shadow repair effect obtained by the method of the present invention; Figure 3B This is the repair effect of the baseline network ShadowFormer; Figure 3C is the original shadow image; Figure 3D A true shadow-free image. DETAILED DESCRIPTION
[0014] The present invention is described in detail below with reference to the accompanying drawings and embodiments.
[0015] Prepare enough shadow image datasets of different types and sizes, including indoor and outdoor shadow scenes, as well as mask prompts for shadow areas; during the model training phase, you also need to prepare shadow-free images corresponding to the shadow data, and then start building and training the network.
[0016] like Figure 1 As shown, the image shadow restoration method based on residual mask guidance of the present invention mainly includes the following steps: Step (1) Preliminary shadow removal of the input image. The Transformer network consists of an input module, an encoder structure, a decoder structure, and an output module. The input module is used to encode the input image information into a vector and add position encoding information on its basis; the encoder module is obtained by stacking L encoder layers, each sub-layer includes a multi-head self-attention structure, a normalization layer, a feedforward fully connected layer and a residual connection structure, which is used to extract image features, that is, the encoding process; the decoder module is composed of L decoder layers, each sub-layer also consists of a multi-head self-attention structure, a normalization layer, a feedforward fully connected layer and a residual connection, which is used to predict the output result from the feature, that is, the decoding process; the output module is composed of a linear layer and a Softmax layer, which is used to map the output of the decoder to a specified dimension to obtain the target output result.
[0017] The overall structure of the algorithm is shown in the figure Figure 2 As shown in the figure, the network structure of Transformer ensures that it has excellent performance that exceeds other networks in processing long-range dependencies and global context information. Therefore, the Transformer network can be used to achieve preliminary shadow removal of the image. First, the input image is linearly mapped to a certain feature space, and the image features are extracted using the L-layer traditional encoder structure. The encoder structure is the same as the original Transformer encoder module, consisting of a multi-head self-attention structure, a normalization layer, a feedforward fully connected layer, and a residual connection structure; the setting of L can be designed according to actual requirements, and L=6 is set in the experiment of the present invention. Then, the shadow area prompt information provided by the input mask is used to learn the correlation between the shadow area and the non-shadow area in the original image. The idea of calculating the regional correlation is as follows: the shadow mask is a binary image of the same size as the input shadowed image, and the shadowed area is marked as 1, that is, the white foreground area; the shadowless image is marked as 0, that is, the black background area. After the shadowed image and the corresponding shadow mask are respectively sent to the encoder, two feature maps are obtained, denoted as X L and M L , respectively represent the features extracted from the original shadow image and the features extracted from the shadow mask by the L-layer coding layer. By matrix multiplying the two feature maps, we can get a fused feature map that integrates the shadow position information, namely: , X L+1 That is the output fusion feature map, represents the multiplication of corresponding elements of the matrix. L+1The shadow position information and the original image information are taken into account, so the correlation between the shadow area and the non-shadow area can be described. Then the fused feature is input into the L-layer decoder structure to decode the output fused feature information. The decoder structure is the same as the original Transformer decoder module, which is also composed of a multi-head self-attention structure, a normalization layer, a feedforward fully connected layer, and a residual connection structure, which is equivalent to the inverse process of the decoder. Finally, the output of the decoder is mapped back to the original image feature space through linear mapping again, thereby achieving preliminary shadow restoration.
[0018] Step (2) Residual mask extraction. The residual mask at this time is extracted using the real shadow-free image and the preliminary shadow removal result obtained in step (1). Since the real shadow-free image is usually brighter than the shadowed image, the two images are first normalized according to the following formula:
[0019] Among them, image is the image to be normalized, and max(·) is the maximum value operation; thus, the grayscale values of the background parts of the two images are similar, reducing the probability that the extracted mask contains the background part. Then, the background difference of the two images is calculated as follows: dif = gt - image; if gt > image, else 0, Among them, dif is the obtained differential image, gt is the real shadow-free image, and image is the preliminary shadow repair result. The meaning of the above formula is: when the grayscale value of the real shadow-free image is greater than the grayscale value of the preliminary shadow repair result, the grayscale value of the differential image is the difference between the two; otherwise it is 0. This differential processing can avoid the appearance of negative values, and the effect is better than directly using the absolute value. This processing is reasonable because: if the grayscale value of the real shadow-free image is less than the grayscale value of the preliminary shadow repair result, it means that there is a high probability that this is the background area or the area that has been repaired, because the grayscale value of the shadow area that has not been repaired must be less than the shadow-free area; and if the absolute value of the difference between the real shadow-free image and the input shadow image is directly used, unnecessary background items may be introduced in the threshold segmentation result, affecting the final processing effect; therefore, the segmentation processing method shown in the formula is adopted. Then, the differential image is threshold segmented, and the Otsu threshold segmentation method can be used for automatic segmentation threshold calculation. The Otsu threshold method is currently the best method for threshold segmentation of the entire image. It can be applied to threshold segmentation of most scenes. The optimal segmentation threshold is obtained by maximizing the inter-class variance between the foreground target area and the background image area: variance is a measure of the uniformity of grayscale distribution. The larger the inter-class variance, the greater the difference between the two parts. Therefore, according to the grayscale information of the input image, a threshold is selected to divide the image into the foreground area and the background area, and the inter-class variance between the two is calculated respectively. The calculation formula of the variance is: , Among them, m1 and m2 refer to the grayscale mean of the foreground and background respectively, and m G refers to the global image mean, p1 and p2 refer to the probability that a pixel belongs to the foreground class and the background class respectively, σ 2 This is the final calculated variance result. Traversing all pixel values in the range of 0-255, performing threshold segmentation based on the pixel value corresponding to the maximum inter-class variance can obtain the best segmentation result based on the Otsu threshold segmentation method.
[0020] Finally, the segmentation results are processed by appropriate morphological methods, and corrosion, expansion, opening and closing operations are used to remove isolated points and holes without affecting the size of the target area. However, the processing template should not be too large, otherwise the block effect of the processing result will be obvious and a smooth and natural mask cannot be obtained. Thus, the residual mask between the true value shadow-free image and the preliminary shadow restoration result is obtained.
[0021] Step (3) Calculation of residual mask loss. The shadow mask image usually has the shadow area marked as 1 and the shadow-free area marked as 0. The residual mask is obtained based on the true shadow-free image and the restoration result. Therefore, if the restoration result is similar to the true shadow-free image, the residual mask obtained at this time should be approximately a zero image. Based on this, the calculation formula of the residual mask loss function is designed as follows: , Where H and W refer to the height and width of the mask (or input image) respectively; Mask[i, j] refers to the grayscale value of the pixel corresponding to the mask. That is, the residual mask loss is expressed as the average pixel value of the residual mask.
[0022] Step (4) Iterative update of the restoration effect. The better the restoration effect of the output image, the smaller the difference between it and the real shadow-free image, and the residual loss value obtained at this time should also be as small as possible. Therefore, a suitable loss value is selected as the threshold. When the loss value calculated in step (3) is greater than the set threshold, it means that the restoration result obtained at this time is much different from the target result. At this time, the restoration image obtained in step (1) is used as the shadow input image in the next iteration process, and the residual mask is used as the guide mask at this time, and steps (1) to (3) are repeated; and when the loss value calculated in step (3) is less than or equal to the set threshold, it means that the gap between the restoration result obtained at this time and the real shadow-free image is small and can be ignored. The output image obtained at this time is the shadow restoration result finally obtained by the network. At the same time, in order to prevent the network from falling into an infinite loop, the maximum number of iterative restorations needs to be set to ensure that the network can be trained normally.
[0023] The overall process of the method of the present invention is as follows: the input is a shadow image and a corresponding shadow mask, and the two are linearly projected and the feature map is sent to an L-layer encoder structure; then the shadow mask and the feature map of the shadow image are fused, and the fused features are decoded by an L-layer decoder, and projected into the original feature space to obtain a preliminary shadow repair result; then the residual mask at this time is calculated using the real shadow-free image and the repair result, and the residual mask loss is calculated. When the loss value is greater than the threshold, the repair result is used as a new round of image to be repaired, and the residual mask is used as a new input mask to perform a new round of iterative repair; when the loss value is less than the threshold, the iterative process can be terminated to obtain the final shadow repair result.
[0024] The image size sent to the network is set to 320×320 pixels, the batch size is 6, and three A5000 GPUs are used for training, with a total of 300 rounds of training. The learning strategy uses the AdamW optimization algorithm, the weight decay coefficient is 0.02, the momentum parameter is set to (0.9, 0.999), and the initial learning rate is 2×10 -4, and then gradually reduced to 1×10 -6 In order to avoid the situation where the loss value cannot be less than the threshold value during the iteration process and the network falls into an infinite loop, the maximum number of iterations in the experiment is limited to 3. The network training is carried out according to the above parameter settings.
[0025] Experimental results demonstrate the effectiveness of the image shadow restoration method based on residual mask guidance proposed in this paper. Figure 3A The repair result proposed by the present invention is Figure 3B This is the repair result of the baseline network ShadowFormer. Figure 3C is the original shadow image, Figure 3D It is a real shadow-free image. By comparison, it can be found that the restoration result of the present invention is more natural than the original baseline network result, the color difference between the restored shadow area and the background is smaller, and the contour edge effect is also improved. In addition, Table 1 shows the training results of the method of the present invention and the baseline method ShadowFormer in the current training environment. By comparing the training results of the two methods on different data sets in Table 1, it can be seen that the model loss value obtained by training with the method proposed in the present invention is smaller and the PSNR value is higher, indicating that the model effect is better.
[0026] Table 1 Comparison of training results between the proposed method and ShadowFormer
[0027] It should be emphasized that the above are only preferred examples of the present invention, but do not limit the present invention in any form.
[0028] The contents not described in detail in the specification of the present invention belong to the prior art known to the professional and technical personnel in this field.
Claims
1. A residual mask-guided image shadow restoration method, characterized in that: The method comprises the following steps: Step (1) Preliminary shadow removal of input image: The Transformer network is used to perceive global context information. First, a linear mapping is performed on the input image, and the image features are extracted using an L-layer encoder structure. Then, the correlation between the shadow area and the non-shadow area is learned using the hint information provided by the input mask. Then, the L-layer decoder structure is used to decode the output fusion feature information and map it back to the original image feature space, thereby achieving preliminary shadow removal. Step (2) Residual mask extraction: The residual mask is calculated based on the preliminary shadow removal result and the true shadow-free image. The background difference between the two images is performed, and an appropriate threshold segmentation algorithm and morphological processing method are used to finally obtain the residual mask for the current restoration result. Step (3) Calculation of residual mask loss: Calculate the residual mask loss value obtained at this time according to the designed mask loss function and compare it with the set threshold; Step (4) Iterative update of the repair effect: When the loss value calculated in step (3) is greater than the set threshold, the repaired image obtained in step (1) is used as the shadowed image in the next iteration, and the residual mask is used as the guide mask at this time, and steps (1) to (3) are re-executed; when the loss value calculated in step (3) is less than or equal to the set threshold, the repair process is completed, and the output image at this time is the shadow repair result finally obtained by the network.
2. The image shadow restoration method based on residual mask guidance according to claim 1, characterized in that: In the step (1), the distinction between the shadow area and the non-shadow area is achieved through a mask, and the mask is a binary image, where the shadow area is 1 and the non-shadow area is 0. The binary labeled image is used to label all the shadow areas of the original input image.
3. The image shadow restoration method based on residual mask guidance according to claim 1, characterized in that: In step (2), the two images are first normalized, that is, the maximum values of the two images are unified to 1.0 according to the following formula: , Wherein, image is the image to be normalized, and max(·) is the maximum value operation. Then, the background difference between the two images is performed to ensure that the background difference between the two images is minimized, and the background is directly filtered out through threshold segmentation.
Citation Information
Patent Citations
Image blind restoration method based on semantic inconsistency detection
CN114897738A
Image shadow removing method, computer equipment and storage medium
CN117392037A
Image shadow removing method
CN118037593A
Image shadow removal method based on mask refinement
CN118314038A
