Image background removal methods, apparatus, equipment and storage media
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-22
- Publication Date
- 2026-08-14
AI Technical Summary
[0004]本发明的主要目的在于提供了一种图像底色去除方法、装置、设备及存储介质,旨在解决现有技术通过二值化的方式去除图像底色导致文字边缘出现锯齿,不能达到很好的视觉效果的技术问题
[0088]本发明将待处理图像转变为目标灰度图像;根据所述目标灰度图像确定灰度值分布信息;根据所述灰度值分布信息确定背景灰度阈值和文字灰度阈值;根据所述背景灰度阈值和所述文字灰度阈值对所述待处理图像进行灰度变换,获得去除底色后的目标图像。由于本发明是根据待处理图像的灰度值分布信息确定背景灰度阈值和文字灰度阈值;根据背景灰度阈值和文字灰度阈值对待处理图像进行灰度变换,获得去除底色后的目标图像。相对于现有的通过二值化去除图像底色的方式,本发明上述方式能够避免处理后的目标图像的文字边缘存在明显的锯齿。
Smart Images

Figure CN116152076B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image processing technology, and in particular to a method, apparatus, device, and storage medium for removing background color from an image. Background Technology
[0002] Document photos taken with cameras often contain shadows, gray backgrounds, and pixel noise introduced by insufficient lighting. Scanned document images may also have a background color due to paper quality and other factors. The preview and printing results of such document images are not ideal. Therefore, image processing techniques are needed to remove the background color and convert the image to black text on a white background for better visual quality. However, existing background color removal functions use binarization methods, resulting in document images with noticeable jagged edges on the text, failing to achieve satisfactory visual effects.
[0003] The above content is only used to help understand the technical solution of the present invention and does not represent an admission that the above content is prior art. Summary of the Invention
[0004] The main objective of this invention is to provide an image background removal method, apparatus, device, and storage medium, which aims to solve the technical problem that the existing technology of removing image background through binarization results in jagged edges on text, failing to achieve a good visual effect.
[0005] To achieve the above objectives, the present invention provides a method for removing background color from an image, the method comprising the following steps:
[0006] Convert the image to be processed into the target grayscale image;
[0007] Determine the grayscale value distribution information based on the target grayscale image;
[0008] The background grayscale threshold and text grayscale threshold are determined based on the grayscale value distribution information.
[0009] The image to be processed is subjected to grayscale transformation based on the background grayscale threshold and the text grayscale threshold to obtain the target image after removing the background color.
[0010] Optionally, the step of determining the background grayscale threshold and the text grayscale threshold based on the grayscale value distribution information includes:
[0011] The background grayscale value and the text grayscale value are determined based on the grayscale value distribution information.
[0012] The background grayscale threshold is determined based on the background grayscale value and the first non-negative preset value;
[0013] The text grayscale threshold is determined based on the text grayscale value and the second non-negative preset value.
[0014] Optionally, the step of determining the background grayscale value and the text grayscale value based on the grayscale value distribution information includes:
[0015] The background grayscale value of the image to be processed is determined based on the grayscale value distribution information;
[0016] When the background grayscale value is greater than the preset background grayscale threshold, the minimum grayscale value of the image to be processed is obtained according to the grayscale value distribution information.
[0017] Use the minimum grayscale value as the text grayscale value;
[0018] The background grayscale value is determined based on the grayscale value distribution information.
[0019] Optionally, the step of determining the background grayscale value based on the grayscale value distribution information includes:
[0020] The target grayscale image is subjected to median filtering based on the grayscale value distribution information to obtain the filtered target image.
[0021] Determine the maximum grayscale value of the target filtered image;
[0022] The maximum grayscale value is used as the background grayscale value.
[0023] Optionally, the step of performing grayscale transformation on the image to be processed based on the background grayscale threshold and the text grayscale threshold to obtain the target image after removing the background color includes:
[0024] When the background grayscale value is greater than the preset background grayscale threshold, pixels in the image to be processed whose grayscale value is less than the text grayscale threshold are filled with black.
[0025] Fill white pixels in the image to be processed that have a grayscale value greater than the background grayscale threshold.
[0026] Pixels in the image to be processed whose grayscale values are greater than or equal to the text grayscale threshold and less than or equal to the background grayscale threshold are subjected to linear grayscale stretching to obtain the target image after removing the background color.
[0027] Optionally, before the step of converting the image to be processed into the target grayscale image, the method further includes:
[0028] Acquire the image to be processed and determine whether the image to be processed has a single background color;
[0029] When the image to be processed is not the single background color, the image to be processed is divided into multiple squares, and each square is used as the image to be processed for grayscale transformation to obtain the target square after removing the background color.
[0030] Construct the target image after removing the background color based on the target squares.
[0031] Optionally, after the steps of acquiring the image to be processed and determining whether the image to be processed has a single background color, the method further includes:
[0032] Obtain the background color distribution information of the image to be processed;
[0033] The image to be processed is segmented according to the background color distribution information to obtain segmented images, and the segmented images are used as the images to be processed.
[0034] Optionally, the step of determining the text grayscale threshold based on the text grayscale value and the second non-negative preset value includes:
[0035] When the background grayscale value is greater than the preset background grayscale threshold, the text grayscale value and the second non-negative preset value are added together to obtain the addition result;
[0036] The summation result is used as the grayscale threshold for the text.
[0037] Optionally, the step of determining the background grayscale threshold based on the background grayscale value and the first non-negative preset value includes:
[0038] When the background grayscale value is greater than the preset background grayscale threshold, the first non-negative preset value is subtracted from the background grayscale value to obtain the subtraction result;
[0039] The result of the subtraction is used as the background grayscale threshold.
[0040] Optionally, before the step of determining the background grayscale threshold based on the background grayscale value and the first non-negative preset value, the method further includes:
[0041] The range of non-negative preset values is determined based on the background grayscale value and the text grayscale value;
[0042] The first non-negative preset value and the second non-negative preset value are determined based on the range of values.
[0043] Optionally, after the step of determining the background grayscale value of the image to be processed based on the grayscale value distribution information, the method further includes:
[0044] When the background grayscale value is less than or equal to a preset background grayscale threshold, the maximum grayscale value of the image to be processed is obtained according to the grayscale value distribution information.
[0045] Use the maximum grayscale value as the text grayscale value;
[0046] The background grayscale value is determined based on the grayscale value distribution information.
[0047] Optionally, the step of converting the image to be processed into a target grayscale image includes:
[0048] Obtain the RGB values of each pixel in the image to be processed;
[0049] The target grayscale value of each pixel in the image to be processed is determined based on the RGB values.
[0050] The image to be processed is converted into a target grayscale image based on the target grayscale value.
[0051] Furthermore, to achieve the above objectives, the present invention also provides an image background color removal apparatus, the apparatus comprising:
[0052] The image transformation module is used to transform the image to be processed into a target grayscale image;
[0053] A grayscale value distribution determination module is used to determine grayscale value distribution information based on the target grayscale image;
[0054] A grayscale threshold determination module is used to determine the background grayscale threshold and the text grayscale threshold based on the grayscale value distribution information.
[0055] The grayscale transformation module is used to perform grayscale transformation on the image to be processed according to the background grayscale threshold and the text grayscale threshold to obtain the target image after removing the background color.
[0056] Optionally, the grayscale threshold determination module is further configured to determine the background grayscale value and the text grayscale value based on the grayscale value distribution information;
[0057] The background grayscale threshold is determined based on the background grayscale value and the first non-negative preset value;
[0058] The text grayscale threshold is determined based on the text grayscale value and the second non-negative preset value.
[0059] Optionally, the grayscale threshold determination module is further configured to determine the background grayscale value of the image to be processed based on the grayscale value distribution information;
[0060] When the background grayscale value is greater than the preset background grayscale threshold, the minimum grayscale value of the image to be processed is obtained according to the grayscale value distribution information.
[0061] Use the minimum grayscale value as the text grayscale value;
[0062] The background grayscale value is determined based on the grayscale value distribution information.
[0063] Optionally, the grayscale threshold determination module is further configured to perform median filtering on the target grayscale image based on the grayscale value distribution information to obtain the filtered target image;
[0064] Determine the maximum grayscale value of the target filtered image;
[0065] The maximum grayscale value is used as the background grayscale value.
[0066] Optionally, the grayscale conversion module is further configured to fill pixels in the image to be processed whose grayscale values are less than the text grayscale threshold with black when the grayscale value of the background color is greater than the preset grayscale threshold.
[0067] Fill white pixels in the image to be processed that have a grayscale value greater than the background grayscale threshold.
[0068] Pixels in the image to be processed whose grayscale values are greater than or equal to the text grayscale threshold and less than or equal to the background grayscale threshold are subjected to linear grayscale stretching to obtain the target image after removing the background color.
[0069] Optionally, the image transformation module is further configured to acquire the image to be processed and determine whether the image to be processed has a single background color;
[0070] When the image to be processed is not the single background color, the image to be processed is divided into multiple squares, and each square is used as the image to be processed for grayscale transformation to obtain the target square after removing the background color.
[0071] Construct the target image after removing the background color based on the target squares.
[0072] Optionally, the image transformation module is further configured to obtain background color distribution information of the image to be processed;
[0073] The image to be processed is segmented according to the background color distribution information to obtain segmented images, and the segmented images are used as the images to be processed.
[0074] Optionally, the grayscale threshold determination module is further configured to add the text grayscale value and the second non-negative preset value to obtain the addition result when the background grayscale value is greater than the preset background grayscale threshold.
[0075] The summation result is used as the grayscale threshold for the text.
[0076] Optionally, the grayscale threshold determination module is further configured to subtract a first non-negative preset value from the background grayscale value when the background grayscale value is greater than a preset background grayscale threshold, and obtain a subtraction result.
[0077] The result of the subtraction is used as the background grayscale threshold.
[0078] Optionally, the grayscale threshold determination module is further configured to determine the range of non-negative preset values based on the background grayscale value and the text grayscale value;
[0079] The first non-negative preset value and the second non-negative preset value are determined based on the range of values.
[0080] Optionally, the grayscale threshold determination module is further configured to obtain the maximum grayscale value of the image to be processed based on the grayscale value distribution information when the background grayscale value is less than or equal to a preset background grayscale threshold.
[0081] Use the maximum grayscale value as the text grayscale value;
[0082] The background grayscale value is determined based on the grayscale value distribution information.
[0083] Optionally, the image transformation module is further configured to obtain the RGB values of each pixel in the image to be processed;
[0084] The target grayscale value of each pixel in the image to be processed is determined based on the RGB values.
[0085] The image to be processed is converted into a target grayscale image based on the target grayscale value.
[0086] Furthermore, to achieve the above objectives, the present invention also proposes an image background color removal device, the device comprising: a memory, a processor, and an image background color removal program stored in the memory and executable on the processor, the image background color removal program being configured to implement the steps of the image background color removal method as described above.
[0087] In addition, to achieve the above objectives, the present invention also proposes a storage medium storing an image background removal program, wherein the image background removal program, when executed by a processor, implements the steps of the image background removal method as described above.
[0088] This invention transforms the image to be processed into a target grayscale image; determines grayscale value distribution information based on the target grayscale image; determines background grayscale thresholds and text grayscale thresholds based on the grayscale value distribution information; and performs grayscale transformation on the image to be processed based on the background grayscale thresholds and the text grayscale thresholds to obtain the target image after removing the background color. Because this invention determines the background grayscale thresholds and text grayscale thresholds based on the grayscale value distribution information of the image to be processed, and then performs grayscale transformation on the image to be processed based on these thresholds to obtain the target image after removing the background color, compared to existing methods that remove the background color through binarization, the above method of this invention can avoid obvious jagged edges on the text edges of the processed target image. Attached Figure Description
[0089] Figure 1 This is a schematic diagram of the structure of the image background removal device in the hardware operating environment involved in the embodiments of the present invention;
[0090] Figure 2 This is a flowchart illustrating the first embodiment of the image background color removal method of the present invention;
[0091] Figure 3 This is a flowchart illustrating the second embodiment of the image background color removal method of the present invention;
[0092] Figure 4 This is a partial flowchart of the third embodiment of the image background color removal method of the present invention;
[0093] Figure 5 This is a structural block diagram of the first embodiment of the image background removal device of the present invention.
[0094] The realization of the objective, functional features and advantages of the present invention will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation
[0095] It should be understood that the specific embodiments described herein are for illustrative purposes only and are not intended to limit the scope of the invention.
[0096] Reference Figure 1 , Figure 1 This is a schematic diagram of the image background removal device structure in the hardware operating environment involved in the embodiments of the present invention.
[0097] like Figure 1 As shown, the image background removal device may include: a processor 1001, such as a central processing unit (CPU), a communication bus 1002, a user interface 1003, a network interface 1004, and a memory 1005. The communication bus 1002 is used to enable communication between these components. The user interface 1003 may include a display screen or an input unit such as a keyboard; optionally, the user interface 1003 may also include a standard wired interface or a wireless interface. The network interface 1004 may optionally include a standard wired interface or a wireless interface (such as a Wireless-Fidelity (Wi-Fi) interface). The memory 1005 may be high-speed random access memory (RAM) or stable non-volatile memory (NVM), such as a disk drive. The memory 1005 may also optionally be a storage device independent of the aforementioned processor 1001.
[0098] Those skilled in the art will understand that Figure 1The structure shown does not constitute a limitation on the image background removal device and may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0099] like Figure 1 As shown, the memory 1005, which serves as a storage medium, may include an operating system, a network communication module, a user interface module, and an image background removal program.
[0100] exist Figure 1 In the image background removal device shown, the network interface 1004 is mainly used for data communication with the network server; the user interface 1003 is mainly used for data interaction with the user; the processor 1001 and the memory 1005 in the image background removal device of the present invention can be set in the image background removal device, and the image background removal device calls the image background removal program stored in the memory 1005 through the processor 1001 and executes the image background removal method provided in the embodiment of the present invention.
[0101] Based on the aforementioned image background removal device, this invention provides an image background removal method, referring to... Figure 2 , Figure 2 This is a flowchart illustrating the first embodiment of the image background color removal method of the present invention.
[0102] In this embodiment, the image background removal method includes the following steps:
[0103] Step S10: Convert the image to be processed into the target grayscale image.
[0104] It should be noted that the executing entity in this embodiment can be a computing service device with data processing, network communication, and program execution functions, such as a mobile phone, tablet computer, or personal computer, or an electronic device or image background removal device capable of performing the above functions. The following description uses the image background removal device as an example to illustrate this embodiment and the subsequent embodiments.
[0105] It should be noted that the image to be processed can be an image whose background color needs to be removed. The target grayscale image can be a grayscale image obtained by performing grayscale transformation on the image to be processed.
[0106] Furthermore, to illustrate how to convert the image to be processed into a target grayscale image, step S10 may include: obtaining the RGB values of each pixel in the image to be processed; determining the target grayscale value of each pixel in the image to be processed based on the RGB values; and converting the image to be processed into a target grayscale image based on the target grayscale value.
[0107] It should be noted that the target grayscale value of each pixel in the image to be processed can be determined according to the following formula:
[0108] Gray = R * 0.3 + G * 0.59 + B * 0.11
[0109] Where Gray is the target grayscale value, R is the R value in RGB values, that is, the red channel color value in the pixel, G is the G value in RGB values, that is, the green channel color value in the pixel, and B is the B value in RGB values, that is, the blue channel color value in the pixel.
[0110] It should be understood that the target grayscale value of each pixel in the image to be processed can be calculated using the above formula. Then, by transforming each pixel in the image to be processed into the target grayscale value, the target grayscale image corresponding to the image to be processed can be obtained.
[0111] In practical implementation, the image to be processed can also be converted into a target grayscale image using the averaging method. That is, the average value of the three colors red, green and blue is taken as the target grayscale value, and the formula is as follows: Gray = (R + G + B) / 3, where Gray is the target grayscale value, R is the R value in the RGB values, that is, the red channel color value in the pixel, G is the G value in the RGB values, that is, the green channel color value in the pixel, and B is the B value in the RGB values, that is, the blue channel color value in the pixel.
[0112] Step S20: Determine the grayscale value distribution information based on the target grayscale image.
[0113] It should be noted that the grayscale value distribution information can be the grayscale value of each pixel in the target grayscale image and the distribution of grayscale values. This can include information such as the maximum and minimum grayscale values in the target grayscale image, as well as the grayscale values of the image background and text. For example, if the grayscale value at the edge of the target grayscale image is greater than the grayscale value of the text in the middle, then the background grayscale value of the target grayscale image can be considered greater than the text grayscale value. The target grayscale image can be a light-colored background image, such as a white image with black text, where the background grayscale value is greater than the text grayscale value.
[0114] Step S30: Determine the background grayscale threshold and text grayscale threshold based on the grayscale value distribution information.
[0115] It should be noted that, to avoid jagged edges on the text edges in the target image after background removal, this embodiment sets background grayscale thresholds and text grayscale thresholds. These thresholds serve as boundaries to distinguish the background and text portions of the image. Specifically, when the background grayscale value is greater than the text grayscale value, a background grayscale threshold is set, filling pixels with grayscale values greater than this threshold with white, thus treating them as the image background. Conversely, a text grayscale threshold is set, filling pixels with grayscale values less than this threshold with black, thus treating them as the text portion of the image. This is to prevent jagged edges on the text portion after background removal.
[0116] Step S40: Perform grayscale transformation on the image to be processed according to the background grayscale threshold and the text grayscale threshold to obtain the target image after removing the background color.
[0117] It should be noted that the target image can be the image after grayscale transformation of the image to be processed to remove the background color. Grayscale transformation of the image to be processed based on the background grayscale threshold and the text grayscale threshold can be performed as follows: If the background grayscale threshold is greater than the text grayscale threshold, pixels in the image to be processed with grayscale values greater than the background grayscale threshold are filled with white, and pixels in the image to be processed with grayscale values less than the text grayscale threshold are filled with black. Pixels in the image to be processed with grayscale values between the background grayscale threshold and the text grayscale threshold are linearly stretched. This yields the target image with white paper and black text after background color removal. If the background grayscale threshold is less than the text grayscale threshold, pixels in the image to be processed with grayscale values less than the background grayscale threshold are filled with black, and pixels in the image to be processed with grayscale values greater than the text grayscale threshold are filled with white. Pixels in the image to be processed with grayscale values between the background grayscale threshold and the text grayscale threshold are linearly stretched. This yields the target image with black paper and white text after background color removal.
[0118] Furthermore, to specifically illustrate how to perform grayscale transformation on the image to be processed based on the background grayscale threshold and the text grayscale threshold, step S40 may include: when the background grayscale value is greater than a preset background grayscale threshold, filling pixels in the image to be processed with grayscale values less than the text grayscale threshold with black; filling pixels in the image to be processed with grayscale values greater than the background grayscale threshold with white; and performing grayscale linear stretching on pixels in the image to be processed with grayscale values greater than or equal to the text grayscale threshold and less than or equal to the background grayscale threshold to obtain the target image after removing the background color.
[0119] It should be noted that the background grayscale value can be the grayscale value of the background of the target grayscale image. When the grayscale value of the background of the target grayscale image is greater than the preset background grayscale threshold, it can be determined that the grayscale value of the background part in the target grayscale image is greater than the grayscale value of the text part. In this case, the background of the image to be processed can be filled with white, and the text part can be filled with black. Conversely, if the grayscale value of the background of the target grayscale image is less than the preset background grayscale threshold, it can be determined that the grayscale value of the background part in the target grayscale image is less than the grayscale value of the text part. In this case, the background of the image to be processed can be filled with black, and the text part can be filled with white. The preset background grayscale threshold can be a pre-set threshold. The maximum and minimum grayscale values in the target grayscale image can be obtained based on the grayscale value distribution information, and the average of the maximum and minimum grayscale values can be used as the preset background grayscale threshold.
[0120] In specific implementation, when the background grayscale value is greater than a preset background grayscale threshold, pixels in the image to be processed with grayscale values less than the text grayscale threshold are filled with black; pixels in the image to be processed with grayscale values greater than the background grayscale threshold are filled with white. When the grayscale value in the image to be processed is between the background grayscale threshold and the text grayscale threshold, no processing is required, or linear grayscale stretching can be performed. Linear grayscale stretching can be performed as follows: for example, if the background grayscale threshold is 200 and the text grayscale threshold is 50, the grayscale value range requiring linear grayscale stretching is 50-200. Since the image's grayscale value range is 0-255, pixels with grayscale values between 50-200 need to be linearly stretched proportionally between 0-255. When the actual grayscale value of a pixel is 50, the grayscale value after grayscale transformation should be 0; when the actual grayscale value of a pixel is 200, the grayscale value after grayscale transformation should be 255. To better understand the linear relationship between stretching and grayscale, we can use a linear equation in two variables, assuming the equation is y = ax + b. We know that when x = 50, y = 0; and when x = 200, y = 255. Substituting this into the equation, we find that the equation corresponding to linear grayscale stretching is y = 1.7x - 85. Here, x represents the grayscale value in the target grayscale image, and y represents the grayscale value in the target image after grayscale transformation.
[0121] This embodiment transforms the image to be processed into a target grayscale image; determines grayscale value distribution information based on the target grayscale image; determines background grayscale thresholds and text grayscale thresholds based on the grayscale value distribution information; and performs grayscale transformation on the image to be processed based on the background grayscale thresholds and the text grayscale thresholds to obtain the target image after removing the background color. Because this embodiment determines the background grayscale thresholds and text grayscale thresholds based on the grayscale value distribution information of the image to be processed, and then performs grayscale transformation on the image to be processed based on these thresholds to obtain the target image after removing the background color, compared to existing methods that remove the background color through binarization, the above method in this embodiment can avoid obvious jagged edges on the text edges of the processed target image.
[0122] refer to Figure 3 , Figure 3 This is a flowchart illustrating the second embodiment of the image background color removal method of the present invention.
[0123] Based on the first embodiment described above, in this embodiment, step S30 includes:
[0124] Step S301: Determine the background grayscale value and text grayscale value based on the grayscale value distribution information.
[0125] It should be noted that the image to be processed may be an image of black text on white paper, or an image of black paper with white text, where the gray value of the background is greater than the gray value of the text. Therefore, before determining the gray values of the background and the text, it is necessary to first determine whether the gray value of the background is greater than the gray value of the text. When the gray value of the background is greater than the gray value of the text, the maximum and minimum gray values in the gray value distribution information are obtained. The maximum gray value can be used as the background gray value, and the minimum gray value can be used as the text gray value.
[0126] Furthermore, to specifically illustrate how to determine the background grayscale value and the text grayscale value, step S301 may include: determining the background grayscale value of the image to be processed based on the grayscale value distribution information; when the background grayscale value is greater than a preset background grayscale threshold, obtaining the minimum grayscale value of the image to be processed based on the grayscale value distribution information; using the minimum grayscale value as the text grayscale value; and determining the background grayscale value based on the grayscale value distribution information. The step of determining the background grayscale value based on the grayscale value distribution information includes: performing median filtering on the target grayscale image based on the grayscale value distribution information to obtain a filtered target image; determining the maximum grayscale value of the target filtered image; and using the maximum grayscale value as the background grayscale value.
[0127] It should be noted that median filtering is a non-linear smoothing technique that sets the gray value of each pixel to the median of the gray values of all pixels within a certain neighborhood window of that pixel.
[0128] In specific implementation, when the background grayscale value is greater than a preset background grayscale threshold, the image to be processed can be transformed into a target image with black text on white paper. Therefore, the minimum grayscale value of the image to be processed is obtained according to the grayscale value distribution information; the minimum grayscale value is used as the text grayscale value; in order to improve the visual effect of the target image, median filtering is performed on the target grayscale image according to the grayscale value distribution information to obtain a filtered target image; the maximum grayscale value of the filtered target image is determined; the maximum grayscale value is used as the background grayscale value.
[0129] In practice, the maximum and minimum grayscale values of the target grayscale image can be obtained, and the average value of the maximum and minimum grayscale values can be calculated. Based on the average value, the target image can be divided into a background part and a text part. Specifically, by combining the grayscale value distribution information, it can be determined whether the grayscale values of the pixels distributed in the image edge part are large grayscale values. When the grayscale values distributed in the image edge part are large grayscale values, the pixels corresponding to the grayscale values greater than the average value are determined as the background part, and the pixels corresponding to the grayscale values less than the average value are determined as the text part.
[0130] Step S302: Determine the background grayscale threshold based on the background grayscale value and the first non-negative preset value.
[0131] It should be noted that the first non-negative preset value can be a pre-set value.
[0132] Furthermore, in order to improve the visual effect of the target image, before step S302, the method further includes: determining the range of non-negative preset values based on the background grayscale value and the text grayscale value; and determining a first non-negative preset value and a second non-negative preset value based on the range of values.
[0133] It should be noted that determining the range of the non-negative preset value based on the background grayscale value and the text grayscale value can be achieved by subtracting the background grayscale value and the text grayscale value, obtaining the subtraction result, and then determining the range of the non-negative preset value based on the subtraction result. This range can be 5%-10% of the subtraction result. Specifically, it can be adjusted according to the visual effect of different images to be processed or target images. The non-negative preset value can include a first non-negative preset value and a second non-negative preset value. The first and second non-negative preset values are used to determine the text grayscale threshold and the background grayscale threshold based on the text grayscale value and the background grayscale value.
[0134] Further, step S302 may include: when the background grayscale value is greater than a preset background grayscale threshold, subtracting a first non-negative preset value from the background grayscale value to obtain a subtraction result; and using the subtraction result as the background grayscale threshold.
[0135] It should be noted that the background grayscale value is the maximum grayscale value of the target filtered image. Therefore, when performing grayscale transformation on the image to be processed, a first non-negative preset value needs to be subtracted from the background grayscale value to obtain the subtraction result; this subtraction result is used as the background grayscale threshold. Then, pixels in the target grayscale image with grayscale values greater than the background grayscale threshold are filled with white to obtain a target image with a white background.
[0136] Step S303: Determine the text grayscale threshold based on the text grayscale value and the second non-negative preset value.
[0137] It should be noted that the second non-negative preset value can be a value determined according to the range of the non-negative preset value, and can be adjusted accordingly based on the visual effect of the target image after grayscale transformation.
[0138] Further, step S303 may include: when the background grayscale value is greater than a preset background grayscale threshold, adding the text grayscale value and a second non-negative preset value to obtain an addition result; and using the addition result as the text grayscale threshold.
[0139] It should be noted that the grayscale value of the text is the minimum grayscale value of the target grayscale. Therefore, when performing grayscale transformation on the image to be processed, a second non-negative preset value needs to be added to the grayscale value of the text to obtain the sum result; the sum result is used as the grayscale threshold of the text. Then, pixels in the target grayscale image with grayscale values less than the grayscale threshold of the text are filled with black to obtain the target image of black text.
[0140] Furthermore, to avoid jagged edges on text after removing the background color from the image, after the step of determining the background gray value of the image to be processed based on the gray value distribution information, the method further includes: when the background gray value is less than or equal to a preset background gray value threshold, obtaining the maximum gray value of the image to be processed based on the gray value distribution information; using the maximum gray value as the text gray value; and determining the background gray value based on the gray value distribution information.
[0141] It should be noted that when the background grayscale value is less than or equal to the preset background grayscale threshold, it indicates that the grayscale value of the background portion in the target grayscale image is less than the grayscale value of the text portion. In this case, removing the background color from the image to be processed transforms the image to be processed into a target image with a black background and white text. At this point, the maximum grayscale value is used as the text grayscale value, and the minimum grayscale value in the target filtered image is used as the background grayscale value. The preset grayscale threshold is obtained by subtracting the preset grayscale value from the text grayscale value, and the preset grayscale threshold is obtained by adding the preset grayscale value to the background grayscale value. Pixels in the target grayscale image with grayscale values less than the background grayscale threshold are filled with white, and pixels with grayscale values greater than the text grayscale threshold are filled with black. Pixels with grayscale values between the text grayscale threshold and the background grayscale threshold undergo linear grayscale stretching. The specific steps of linear stretching can be referred to the above-described linear stretching steps, and this embodiment does not impose any limitations on them.
[0142] This embodiment determines the background grayscale value and text grayscale value based on the grayscale value distribution information; it determines the background grayscale threshold based on the background grayscale value and a first non-negative preset value; and it determines the text grayscale threshold based on the text grayscale value and a second non-negative preset value. This embodiment determines the background grayscale value and text grayscale value based on the grayscale value distribution information, and then determines the background grayscale threshold and text grayscale threshold, ensuring that the target image obtained during grayscale transformation is clearly distinguishable between black and white, without jagged edges, thus improving the visual effect.
[0143] refer to Figure 4 , Figure 4 This is a partial flowchart of the third embodiment of the image background color removal method of the present invention.
[0144] Based on the above embodiments, in this embodiment, before step S10, the method further includes:
[0145] Step S001: Obtain the image to be processed and determine whether the image to be processed has a single background color.
[0146] It should be noted that the "single background color" can be determined by the similarity of color values in the background portion of the image to be processed. Specifically, this can be achieved by obtaining the RGB color values of the background portion of the image to be processed, inputting these RGB color values into a preset color value prediction model, and obtaining the output result of the preset color value prediction model. When the preset color value prediction model has only one output result, the image to be processed is determined to have a single background color. When the preset color value prediction model has multiple output results, the image to be processed is determined not to have a single background color. The preset color value prediction model can be a prediction model obtained by pre-training a neural network, which can predict whether an image has a single background color. When it outputs one result, the image is determined to have a single background color; when it outputs multiple results, the image is determined to have multiple background colors.
[0147] Furthermore, in order to improve the visual effect of the target image after grayscale transformation, after step S001, the following steps may be included: obtaining the background color distribution information of the image to be processed; segmenting the image to be processed according to the background color distribution information to obtain segmented images, and using the segmented images as the images to be processed.
[0148] It should be noted that the background color distribution information may include the number of background colors, the distribution area information, and the corresponding RGB color values. When the number of background colors in the background color distribution information is 1, the image to be processed does not need to be segmented, and grayscale transformation can be performed directly to obtain the target image. When the number of background colors in the background color distribution information is not 1, the image to be processed can be segmented according to the background color distribution area information. For example, according to the background color distribution area information, if area A in the image to be processed is a red background and area B is a blue background, then the image to be processed is segmented into two images: the image corresponding to area A and the image corresponding to area B. Steps S10-S40 in the above embodiment are performed on the images corresponding to these two areas respectively to obtain their respective target images. The respective target images are then stitched together to obtain the target image.
[0149] Step S002: When the image to be processed is not the single background color, the image to be processed is divided into multiple squares, and each square is used as the image to be processed for grayscale transformation to obtain the target square after removing the background color.
[0150] It should be noted that dividing the image to be processed into multiple squares can be done proportionally. For example, dividing the image to be processed into 4 squares can mean dividing the image to be processed into 4 squares of the same size. Each square is used as the image to be processed, and then the steps S10-S40 described above are performed to obtain the target square after removing the background color.
[0151] Step S003: Construct the target image after removing the background color based on the target square.
[0152] It should be noted that constructing the target image after removing the background color based on the target squares can be achieved by stitching the target squares together according to the order of segmentation or the connection relationship between the squares to form the target image.
[0153] This embodiment acquires an image to be processed and determines whether the image has a single background color. If the image does not have a single background color, the image is divided into multiple squares, and each square is used as the image to be processed for grayscale transformation to obtain a target square after removing the background color. The target image after removing the background color is then constructed based on the target squares. This embodiment determines whether the image to be processed has a single background color. If the image does not have a single background color, the image is divided into multiple squares, and each square is used as the image to be processed for grayscale transformation to obtain a target square after removing the background color. The target image after removing the background color is then constructed based on the target squares. Through this method, even when the background color of the image to be processed is not uniform, the target image can still achieve a good visual effect.
[0154] Reference Figure 5 , Figure 5 This is a structural block diagram of the first embodiment of the image background removal device of the present invention.
[0155] like Figure 5 As shown, the image background color removal device proposed in this embodiment of the invention includes:
[0156] Image conversion module 10 is used to convert the image to be processed into a target grayscale image;
[0157] The grayscale value distribution determination module 20 is used to determine grayscale value distribution information based on the target grayscale image;
[0158] Grayscale threshold determination module 30 is used to determine the background grayscale threshold and the text grayscale threshold based on the grayscale value distribution information;
[0159] The grayscale conversion module 40 is used to perform grayscale conversion on the image to be processed according to the background grayscale threshold and the text grayscale threshold to obtain the target image after removing the background color.
[0160] This embodiment transforms the image to be processed into a target grayscale image; determines grayscale value distribution information based on the target grayscale image; determines background grayscale thresholds and text grayscale thresholds based on the grayscale value distribution information; and performs grayscale transformation on the image to be processed based on the background grayscale thresholds and the text grayscale thresholds to obtain the target image after removing the background color. Because this embodiment determines the background grayscale thresholds and text grayscale thresholds based on the grayscale value distribution information of the image to be processed, and then performs grayscale transformation on the image to be processed based on these thresholds to obtain the target image after removing the background color, compared to existing methods that remove the background color through binarization, the above method in this embodiment can avoid obvious jagged edges on the text edges of the processed target image.
[0161] It should be noted that the workflow described above is merely illustrative and does not limit the scope of protection of this invention. In practical applications, those skilled in the art can select some or all of the workflow to achieve the purpose of this embodiment according to actual needs, and no restrictions are imposed here.
[0162] In addition, for technical details not described in detail in this embodiment, please refer to the parameter operation method provided in any embodiment of the present invention, which will not be repeated here.
[0163] Based on the first embodiment of the image background removal device of the present invention, a second embodiment of the image background removal device of the present invention is proposed.
[0164] In this embodiment, the grayscale threshold determination module 30 is further configured to determine the background grayscale value and the text grayscale value based on the grayscale value distribution information; determine the background grayscale threshold based on the background grayscale value and the first non-negative preset value; and determine the text grayscale threshold based on the text grayscale value and the second non-negative preset value.
[0165] Furthermore, the grayscale threshold determination module 30 is also used to determine the background grayscale value of the image to be processed based on the grayscale value distribution information; when the background grayscale value is greater than the preset background grayscale threshold, to obtain the minimum grayscale value of the image to be processed based on the grayscale value distribution information; to use the minimum grayscale value as the text grayscale value; and to determine the background grayscale value based on the grayscale value distribution information.
[0166] Furthermore, the grayscale threshold determination module 30 is also used to perform median filtering on the target grayscale image according to the grayscale value distribution information to obtain a filtered target image; determine the maximum grayscale value of the target filtered image; and use the maximum grayscale value as the background grayscale value.
[0167] Furthermore, the grayscale transformation module 40 is also used to fill pixels in the image to be processed with grayscale values less than the text grayscale threshold with black when the background grayscale value is greater than the preset background grayscale threshold; fill pixels in the image to be processed with grayscale values greater than the background grayscale threshold with white; and perform grayscale linear stretching on pixels in the image to be processed with grayscale values greater than or equal to the text grayscale threshold and less than or equal to the background grayscale threshold to obtain the target image after removing the background color.
[0168] Furthermore, the image transformation module 10 is also used to acquire the image to be processed, determine whether the image to be processed is a single background color; when the image to be processed is not a single background color, the image to be processed is divided into multiple squares, and each square is used as the image to be processed for grayscale transformation to obtain the target square after removing the background color; and the target image after removing the background color is constructed based on the target square.
[0169] Furthermore, the image transformation module 10 is also used to obtain the background color distribution information of the image to be processed; to segment the image to be processed according to the background color distribution information to obtain segmented images, and to use the segmented images as the images to be processed.
[0170] Furthermore, the grayscale threshold determination module 30 is also used to add the text grayscale value and the second non-negative preset value when the background grayscale value is greater than the preset background grayscale threshold, and obtain the addition result; and use the addition result as the text grayscale threshold.
[0171] Furthermore, the grayscale threshold determination module 30 is also used to subtract a first non-negative preset value from the background grayscale value when the background grayscale value is greater than the preset background grayscale threshold, and obtain a subtraction result; and use the subtraction result as the background grayscale threshold.
[0172] Furthermore, the grayscale threshold determination module 30 is also used to determine the range of non-negative preset values based on the background grayscale value and the text grayscale value; and to determine a first non-negative preset value and a second non-negative preset value based on the range of values.
[0173] Furthermore, the grayscale threshold determination module 30 is also used to obtain the maximum grayscale value of the image to be processed based on the grayscale value distribution information when the background grayscale value is less than or equal to a preset background grayscale threshold; use the maximum grayscale value as the text grayscale value; and determine the background grayscale value based on the grayscale value distribution information.
[0174] Furthermore, the image transformation module 10 is also used to acquire the RGB values of each pixel of the image to be processed; determine the target grayscale value of each pixel of the image to be processed based on the RGB values; and transform the image to be processed into a target grayscale image based on the target grayscale value.
[0175] Other embodiments or specific implementations of the image background removal device of the present invention can be found in the above-described method embodiments, and will not be repeated here.
[0176] Furthermore, this embodiment of the invention also proposes a storage medium storing an image background removal program, which, when executed by a processor, implements the steps of the image background removal method described above.
[0177] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or system that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or system. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or system that includes that element.
[0178] The sequence numbers of the above embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0179] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as read-only memory / random access memory, magnetic disk, optical disk) and includes several instructions to cause a terminal device (which may be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in the various embodiments of the present invention.
[0180] The above are merely preferred embodiments of the present invention and do not limit the scope of the patent. Any equivalent structural or procedural transformations made based on the description and drawings of the present invention, or direct or indirect applications in other related technical fields, are similarly included within the scope of patent protection of the present invention.
Claims
1. A method for removing background color from an image, characterized in that, The image background removal method includes the following steps: Convert the image to be processed into the target grayscale image; Determine the grayscale value distribution information based on the target grayscale image; The background grayscale threshold and text grayscale threshold are determined based on the grayscale value distribution information. The image to be processed is subjected to grayscale transformation based on the background grayscale threshold and the text grayscale threshold to obtain the target image after removing the background color. The step of determining the background grayscale threshold and the text grayscale threshold based on the grayscale value distribution information includes: The background grayscale value and the text grayscale value are determined based on the grayscale value distribution information. The background grayscale threshold is determined based on the background grayscale value and the first non-negative preset value; The text grayscale threshold is determined based on the text grayscale value and the second non-negative preset value.
2. The image background color removal method as described in claim 1, characterized in that, The step of determining the background grayscale value and the text grayscale value based on the grayscale value distribution information includes: The background grayscale value of the image to be processed is determined based on the grayscale value distribution information; When the background grayscale value is greater than the preset background grayscale threshold, the minimum grayscale value of the image to be processed is obtained according to the grayscale value distribution information. Use the minimum grayscale value as the text grayscale value; The background grayscale value is determined based on the grayscale value distribution information.
3. The image background color removal method as described in claim 2, characterized in that, The step of determining the background grayscale value based on the grayscale value distribution information includes: The target grayscale image is subjected to median filtering based on the grayscale value distribution information to obtain the filtered target image. Determine the maximum grayscale value of the target filtered image; The maximum grayscale value is used as the background grayscale value.
4. The image background color removal method according to any one of claims 1-3, characterized in that, The step of performing grayscale transformation on the image to be processed based on the background grayscale threshold and the text grayscale threshold to obtain the target image after removing the background color includes: When the background grayscale value is greater than the preset background grayscale threshold, pixels in the image to be processed whose grayscale value is less than the text grayscale threshold are filled with black. Fill white pixels in the image to be processed that have a grayscale value greater than the background grayscale threshold. Pixels in the image to be processed whose grayscale values are greater than or equal to the text grayscale threshold and less than or equal to the background grayscale threshold are subjected to linear grayscale stretching to obtain the target image after removing the background color.
5. The image background color removal method according to any one of claims 1-3, characterized in that, Before the step of converting the image to be processed into the target grayscale image, the method further includes: Acquire the image to be processed and determine whether the image to be processed has a single background color; When the image to be processed is not the single background color, the image to be processed is divided into multiple squares, and each square is used as the image to be processed for grayscale transformation to obtain the target square after removing the background color. Construct the target image after removing the background color based on the target squares.
6. The image background color removal method as described in claim 5, characterized in that, After the steps of acquiring the image to be processed and determining whether the image to be processed has a single background color, the method further includes: Obtain the background color distribution information of the image to be processed; The image to be processed is segmented according to the background color distribution information to obtain segmented images, and the segmented images are used as the images to be processed.
7. The image background color removal method as described in claim 2, characterized in that, The step of determining the text grayscale threshold based on the text grayscale value and the second non-negative preset value includes: When the background grayscale value is greater than the preset background grayscale threshold, the text grayscale value and the second non-negative preset value are added together to obtain the addition result; The summation result is used as the grayscale threshold for the text.
8. The image background color removal method as described in claim 3, characterized in that, The step of determining the background grayscale threshold based on the background grayscale value and the first non-negative preset value includes: When the background grayscale value is greater than the preset background grayscale threshold, the first non-negative preset value is subtracted from the background grayscale value to obtain the subtraction result; The result of the subtraction is used as the background grayscale threshold.
9. The image background color removal method as described in claim 1, characterized in that, Before the step of determining the background grayscale threshold based on the background grayscale value and the first non-negative preset value, the method further includes: The range of non-negative preset values is determined based on the background grayscale value and the text grayscale value; The first non-negative preset value and the second non-negative preset value are determined based on the range of values.
10. The image background color removal method as described in claim 2, characterized in that, After the step of determining the background grayscale value of the image to be processed based on the grayscale value distribution information, the method further includes: When the background grayscale value is less than or equal to a preset background grayscale threshold, the maximum grayscale value of the image to be processed is obtained according to the grayscale value distribution information. Use the maximum grayscale value as the text grayscale value; The background grayscale value is determined based on the grayscale value distribution information.
11. The image background color removal method according to any one of claims 1-3, characterized in that, The step of converting the image to be processed into a target grayscale image includes: Obtain the RGB values of each pixel in the image to be processed; The target grayscale value of each pixel in the image to be processed is determined based on the RGB values. The image to be processed is converted into a target grayscale image based on the target grayscale value.
12. An image background color removal device, characterized in that, The image background removal device includes: The image transformation module is used to transform the image to be processed into a target grayscale image; A grayscale value distribution determination module is used to determine grayscale value distribution information based on the target grayscale image; A grayscale threshold determination module is used to determine the background grayscale threshold and the text grayscale threshold based on the grayscale value distribution information. The grayscale transformation module is used to perform grayscale transformation on the image to be processed according to the background grayscale threshold and the text grayscale threshold to obtain the target image after removing the background color. The grayscale threshold determination module is further configured to determine the background grayscale value and the text grayscale value based on the grayscale value distribution information; determine the background grayscale threshold based on the background grayscale value and the first non-negative preset value; and determine the text grayscale threshold based on the text grayscale value and the second non-negative preset value.
13. The image background color removal apparatus as described in claim 12, characterized in that, The grayscale threshold determination module is further configured to determine the background grayscale value of the image to be processed based on the grayscale value distribution information; when the background grayscale value is greater than the preset background grayscale threshold, obtain the minimum grayscale value of the image to be processed based on the grayscale value distribution information; use the minimum grayscale value as the text grayscale value; and determine the background grayscale value based on the grayscale value distribution information.
14. The image background color removal apparatus as described in claim 13, characterized in that, The grayscale threshold determination module is further configured to perform median filtering on the target grayscale image based on the grayscale value distribution information to obtain a filtered target image; determine the maximum grayscale value of the target filtered image; and use the maximum grayscale value as the background grayscale value.
15. The image background color removal apparatus as described in claim 12, characterized in that, The grayscale transformation module is further configured to, when the grayscale value of the background color is greater than a preset grayscale threshold, fill pixels in the image to be processed with grayscale values less than the text grayscale threshold with black; fill pixels in the image to be processed with grayscale values greater than the background grayscale threshold with white; and perform grayscale linear stretching on pixels in the image to be processed with grayscale values greater than or equal to the text grayscale threshold and less than or equal to the background grayscale threshold to obtain the target image after removing the background color.
16. The image background color removal apparatus as described in claim 12, characterized in that, The image transformation module is also used to acquire the image to be processed and determine whether the image to be processed has a single background color. When the image to be processed is not a single background color, the image to be processed is divided into multiple squares, and each square is used as the image to be processed for grayscale transformation to obtain the target square after removing the background color; the target image after removing the background color is constructed based on the target square.
17. An image background color removal device, characterized in that, The device includes: a memory, a processor, and an image background removal program stored in the memory and executable on the processor, the image background removal program being configured to implement the steps of the image background removal method as described in any one of claims 1 to 11.
18. A storage medium, characterized in that, The storage medium stores an image background removal program, which, when executed by a processor, implements the steps of the image background removal method as described in any one of claims 1 to 11.
Citation Information
Patent Citations
Character extraction method, image processing equipment and computer readable storage medium
CN111695550A
Method for image binarization
US6351566B1