Image Processing Method and Apparatus
By dynamically determining the threshold value of each pixel point of the image, according to its pixel value and background color type, the problem of poor image binarization processing in the prior art is solved, and a clearer binary image effect is achieved.
Patent Information
- Application Number
- CN202211473505.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-11-21
- Publication Date
- 2025-07-25
- Estimated Expiration
- 2042-11-21
AI Technical Summary
In the prior art, the image binary processing effect is poor, especially in document scanning application scenarios, which may lead to unclear handwriting and blurred handwriting problems. The reason is that the actual situation of each pixel point and the image background situation are not taken into account using fixed thresholds.
By acquiring the grayscale image of the image, the type of background color is determined, and the threshold of adaptability is dynamically determined for each pixel point based on its pixel value, background color type and average value of surrounding pixel points, and then the target value is determined to form a binary image.
Improve the image binary processing effect, ensure that the text or foreground pixels are clearly visible, avoid the large-scale disappearance of text, and achieve a clearer binary image.
Smart Images

Figure CN118071610B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of terminal technologies, and in particular, to an image processing method and apparatus. Background Art
[0002] In the field of image processing, there is an image processing method of performing binarization processing on an image. Image binarization is to set the grayscale value of the pixel points on the image to 0 or 255, so that the image presents an effect including only black and white.
[0003] In the current prior art, during the process of performing binarization processing on an image, usually a fixed threshold is set, and then the grayscale values of each pixel point in the image are compared with this fixed threshold. For example, the grayscale values of the pixel points with grayscale values less than the threshold are set to 0, and the grayscale values of the pixel points with grayscale values greater than the threshold are set to 255.
[0004] However, this implementation method of comparing with a fixed threshold and setting grayscale values will result in poor processing effects of image binarization. Summary of the Invention
[0005] Embodiments of this application provide an image processing method and apparatus, which are applied to the field of terminal technologies.
[0006] In a first aspect, embodiments of this application propose an image processing method. The method includes:
[0007] Obtain a first image to be processed, and determine the grayscale image corresponding to the first image;
[0008] According to the grayscale image, determine the type of the background color of the first image;
[0009] For any first pixel point in the grayscale image, obtain a first threshold corresponding to the first pixel point according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points in a first region, where the first region is a region formed by the pixel points surrounding the first pixel point in the grayscale image, and determine a target value corresponding to the first pixel point according to the first threshold;
[0010] According to the target values corresponding to the respective pixel points in the grayscale image, obtain a binary image corresponding to the first image.
[0011] In this implementation, by first processing the first image into a grayscale image, then based on the grayscale image, determining the type of the background color of the first image, and for each pixel point in the grayscale image, according to the pixel value of the pixel point itself, the type of the background color, and the average value of the pixel values of the pixel points surrounding each pixel point, determining the first threshold corresponding to each pixel point respectively, so that it can be ensured that according to the own situation, the surrounding situation of each pixel point, and the background color situation of the first image, the first threshold adaptive to each pixel point is dynamically determined. Then, according to the first threshold corresponding to each pixel point respectively, determining the target value corresponding to each pixel point, where the target value is one of the two pixel value selections in the binary image, and then according to the target value corresponding to each pixel point respectively, the binary image corresponding to the first image can be determined. At the same time, because the determination of the target value for each pixel point is dynamically determined, the processing effect of image binarization can be effectively improved.
[0012] In a possible design, the determining the type of the background color of the first image according to the grayscale image includes:
[0013] Performing edge detection processing on the grayscale image to obtain an edge image corresponding to the grayscale image;
[0014] Performing dilation filtering processing on the edge image to obtain a first image;
[0015] Performing erosion filtering processing on the edge image to obtain a second image;
[0016] Determining the type of the background color of the first image according to the first image, the second image, and the grayscale image.
[0017] In this implementation, first perform edge detection on the grayscale image, then perform dilation filtering and top-down filtering on the edge image respectively, so that the edge part can be expanded and shrunk to a certain extent, and then according to the first image, the second image, and the grayscale image, the type of the background color of the first image can be effectively determined. This implementation method can simply and effectively determine the type of the background color without dividing foreground pixels and background pixels, so that on the basis of effectively realizing the type of the background color of the image, energy conservation can be further realized.
[0018] In a possible design, the pixel value of the edge pixel points in the edge image is a first value, and the pixel value of the non-edge pixel points in the edge image is a second value;
[0019] The determining the type of the background color of the first image according to the first image, the second image, and the grayscale image includes:
[0020] In the first image, a second pixel point with a pixel value of the first value is obtained, and in the second image, a third pixel point with a pixel value of the first value is obtained;
[0021] In the grayscale image, a fourth pixel point at the same position as the second pixel point is obtained, and a fifth pixel point at the same position as the third pixel point is obtained;
[0022] Based on the fourth pixel point and the fifth pixel point, the type of the background color of the first image is determined.
[0023] In this implementation, by determining the white part in the first image after dilation filtering, and by determining the white part in the second image after erosion filtering, and then determining the pixel points corresponding to these two white parts in the grayscale image, since the background area included in the white part in dilation filtering is more than the background area included in the white part in erosion filtering, therefore, based on the fourth pixel point and the fifth pixel point introduced above, the type of the background color of the first image can be determined simply and accurately.
[0024] In a possible design, the determining the type of the background color of the first image according to the fourth pixel point and the fifth pixel point includes:
[0025] Determine a first average value of the pixel values of the fourth pixel points, and determine a second average value of the pixel values of the fifth pixel points;
[0026] If the first average value is greater than the second average value, determine that the type of the background color of the first image is the first type; or, if the first average value is less than or equal to the second average value, determine that the type of the background color of the first image is the second type;
[0027] Wherein, the gray value corresponding to the background color of the first type is greater than a preset threshold value, and the gray value corresponding to the background color of the second type is less than or equal to the preset threshold value.
[0028] In this implementation, by comparing the average value of the pixel values of the fourth pixel points and the average value of the pixel values of the fourth pixel points, the type of the background color of the first image can be effectively determined, so the determination of the type of the background color can be simply realized.
[0029] In a possible design, the obtaining the first threshold value corresponding to the pixel point according to the pixel value of the first pixel point, the type of the background color, and the average pixel value of the pixel points in the first region includes:
[0030] Determine the type value corresponding to the type of the background color, where the type value corresponding to the first type is t, the type value corresponding to the second type is -t, and t is an integer greater than or equal to 1;
[0031] Determine a first parameter according to the pixel value of the first pixel point, the type value, and the pixel average value;
[0032] Determine the sum of the pixel average value and the first parameter as the first threshold corresponding to the first pixel point.
[0033] In a possible design, the determining the first parameter according to the pixel value of the first pixel point, the type value, and the pixel average value includes:
[0034] Determine a second parameter according to the pixel value of the first pixel point and the pixel average value, where when the pixel value of the first pixel point is greater than the pixel average value, the second parameter is an integer greater than 0, when the pixel value of the first pixel point is less than the pixel average value, the second parameter is an integer less than 0, and when the pixel value of the first pixel point is equal to the pixel average value, the second parameter is equal to 0;
[0035] Determine the first parameter according to the product of the second parameter, the type value, and the pixel average value.
[0036] In this implementation manner, through the above-described processing process and the positive and negative relationships of the corresponding parameters, the binarization effect of corresponding a dark background to black and a light text to white can be achieved. Correspondingly, the binarization effect of corresponding a light background to white and a dark text to black can be achieved to ensure that the content in the binary image after binarization processing is clearly visible and there will be no situation where the text or the foreground pixels disappear in a large area. And in this application, for each pixel point, according to the pixel value of the pixel point itself, the pixel values around the pixel point, and the type of the background color of the first image, the first threshold corresponding to the pixel point is determined. Therefore, the adaptive threshold can be effectively determined dynamically for each pixel point to improve the processing effect of the finally obtained binary image.
[0037] In a possible design, before obtaining the first threshold corresponding to the first pixel point according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points in the first region, the method further includes:
[0038] For any first pixel point in the grayscale image, determine the first abscissa value and the first ordinate value of the first pixel point;
[0039] In the grayscale image, obtain a sixth pixel point whose abscissa value is less than or equal to the first abscissa value and whose ordinate value is less than or equal to the first ordinate value;
[0040] Determine the sum of the pixel values of each of the sixth pixel points as the accumulated value corresponding to the first pixel point;
[0041] Determine an accumulated image according to the accumulated values of the pixel points in the grayscale image, where the pixel value of each pixel point in the accumulated image is the accumulated value.
[0042] In a possible design, the abscissa value of the pixel point on the left edge of the first region is x l and the abscissa value of the pixel point on the right edge of the first region is x h and the ordinate value of the pixel point on the upper edge of the first region is y l and the ordinate value of the pixel point on the lower edge of the first region is y h ;
[0043] Before obtaining the first threshold corresponding to the first pixel point according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points within the first region, the method further includes:
[0044] In the accumulated image, obtain a first positioning pixel point whose abscissa value is x h and whose ordinate value is y h ;
[0045] In the accumulated image, obtain a second positioning pixel point whose abscissa value is x l minus 1 and whose ordinate value is y l minus 1;
[0046] In the accumulated image, obtain a third positioning pixel point whose abscissa value is x l minus 1 and whose ordinate value is y h ;
[0047] In the accumulated image, obtain a fourth positioning pixel point whose abscissa value is x h and whose ordinate value is y l minus 1;
[0048] Determine the pixel average value of the pixel points within the first region according to the pixel value of the first positioning pixel point, the pixel value of the second positioning pixel point, the pixel value of the third positioning pixel point, and the pixel value of the fourth positioning pixel point.
[0049] In this implementation manner, by pre-determining the accumulated image, when determining the pixel average value of the pixel points in the first region later, the pixel average value can be obtained only based on the accumulated values of the four positioning points, without having to traverse each pixel point in the first region. Therefore, the computational amount can be effectively reduced and the overhead can be saved.
[0050] In a possible design, the determining the target value corresponding to the first pixel point according to the first threshold includes:
[0051] If the pixel value of the first pixel point is less than the first threshold, determining the target value corresponding to the pixel point as a third value;
[0052] If the pixel value of the pixel point is greater than or equal to the first threshold, determining the target value corresponding to the pixel point as a fourth value.
[0053] In a second aspect, an embodiment of the present application provides an image processing apparatus, and the apparatus includes:
[0054] An acquisition module, configured to acquire a first image to be processed and determine a grayscale image corresponding to the first image;
[0055] A determination module, configured to determine the type of the background color of the first image according to the grayscale image;
[0056] A processing module, configured to, for any first pixel point in the grayscale image, obtain a first threshold corresponding to the first pixel point according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points in a first region, and determine a target value corresponding to the first pixel point according to the first threshold, where the first region is a region formed by the pixel points surrounding the first pixel point in the grayscale image;
[0057] The processing module is further configured to obtain a binary image corresponding to the first image according to the target values corresponding to the respective pixel points in the grayscale image.
[0058] In a possible design, the determination module is specifically configured to:
[0059] Perform edge detection processing on the grayscale image to obtain an edge image corresponding to the grayscale image;
[0060] Perform dilation filtering processing on the edge image to obtain a first image;
[0061] Perform erosion filtering processing on the edge image to obtain a second image;
[0062] Determine the type of the background color of the first image according to the first image, the second image, and the grayscale image.
[0063] In a possible design, the pixel value of the edge pixel points in the edge image is a first value, and the pixel value of the non-edge pixel points in the edge image is a second value;
[0064] The determining module is specifically configured to:
[0065] In the first image, obtain a second pixel point with the pixel value being the first value, and in the second image, obtain a third pixel point with the pixel value being the first value;
[0066] In the grayscale image, obtain a fourth pixel point having the same position as the second pixel point, and obtain a fifth pixel point having the same position as the third pixel point;
[0067] Determine the type of the background color of the first image according to the fourth pixel point and the fifth pixel point.
[0068] In a possible design, the determining module is specifically configured to:
[0069] Determine a first average value of the pixel values of the fourth pixel points, and determine a second average value of the pixel values of the fifth pixel points;
[0070] If the first average value is greater than the second average value, determine that the type of the background color of the first image is a first type; or, if the first average value is less than or equal to the second average value, determine that the type of the background color of the first image is a second type;
[0071] Wherein, the gray value corresponding to the background color of the first type is greater than a preset threshold, and the gray value corresponding to the background color of the second type is less than or equal to the preset threshold.
[0072] In a possible design, the processing module is specifically configured to:
[0073] Determine a type value corresponding to the type of the background color, wherein the type value corresponding to the first type is t, the type value corresponding to the second type is -t, and t is an integer greater than or equal to 1;
[0074] Determine a first parameter according to the pixel value of the first pixel point, the type value, and the pixel average value;
[0075] Determine the sum of the pixel average value and the first parameter as the first threshold corresponding to the first pixel point.
[0076] In a possible design, the processing module is specifically configured to:
[0077] Determine a second parameter according to the pixel value of the first pixel point and the pixel average value, where when the pixel value of the first pixel point is greater than the pixel average value, the second parameter is an integer greater than 0; when the pixel value of the first pixel point is less than the pixel average value, the second parameter is an integer less than 0; when the pixel value of the first pixel point is equal to the pixel average value, the second parameter is equal to 0;
[0078] Determine the first parameter according to the product of the second parameter, the type value, and the pixel average value.
[0079] In a possible design, the processing module is further configured to:
[0080] Before obtaining the first threshold corresponding to the first pixel point according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points in the first region, for any first pixel point in the grayscale image, determine the first abscissa value and the first ordinate value of the first pixel point;
[0081] In the grayscale image, obtain a sixth pixel point whose abscissa value is less than or equal to the first abscissa value and whose ordinate value is less than or equal to the first ordinate value;
[0082] Determine the sum of the pixel values of each of the sixth pixel points as the cumulative value corresponding to the first pixel point;
[0083] Determine a cumulative image according to the cumulative values of the pixel points in the grayscale image, where the pixel value of each pixel point in the cumulative image is the cumulative value.
[0084] In a possible design, the abscissa value of the pixel point on the left edge of the first region is x l , and the abscissa value of the pixel point on the right edge of the first region is x h , the ordinate value of the pixel point on the upper edge of the first region is y l , and the ordinate value of the pixel point on the lower edge of the first region is y h ;
[0085] The processing module is further configured to:
[0086] Before obtaining the first threshold corresponding to the first pixel point according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points in the first region, in the cumulative image, obtain a first positioning pixel point whose abscissa value is x h , and whose ordinate value is y h ;
[0087] In the cumulative image, obtain a second positioning pixel point where the abscissa value is x l minus 1, and the ordinate value is y l minus 1;
[0088] In the cumulative image, obtain a third positioning pixel point where the abscissa value is x l minus 1, and the ordinate value is y h ;
[0089] In the cumulative image, obtain a fourth positioning pixel point where the abscissa value is x h , and the ordinate value is y l minus 1;
[0090] Determine the pixel average value of the pixel points in the first area according to the pixel value of the first positioning pixel point, the pixel value of the second positioning pixel point, the pixel value of the third positioning pixel point, and the pixel value of the fourth positioning pixel point.
[0091] In a possible design, the processing module is further configured to:
[0092] If the pixel value of the first pixel point is less than the first threshold, determine that the target value corresponding to the pixel point is the third value;
[0093] If the pixel value of the pixel point is greater than or equal to the first threshold, determine that the target value corresponding to the pixel point is the fourth value.
[0094] In a third aspect, an embodiment of the present application provides a terminal device, which may also be referred to as a terminal, a user equipment (UE), a mobile station (MS), a mobile terminal (MT), etc. The terminal device may be a mobile phone, a smart TV, a wearable device, a tablet computer (Pad), a computer with wireless transceiver function, a virtual reality (VR) terminal device, an augmented reality (AR) terminal device, a wireless terminal in industrial control, a wireless terminal in self-driving, a wireless terminal in remote medical surgery, a wireless terminal in a smart grid, a wireless terminal in transportation safety, a wireless terminal in a smart city, a wireless terminal in a smart home, and so on.
[0095] The terminal device includes: a processor and a memory; the memory stores computer-executable instructions; the processor executes the computer-executable instructions stored in the memory, so that the terminal device executes the method as described in the first aspect.
[0096] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, the method as described in the first aspect is implemented.
[0097] In a fifth aspect, an embodiment of the present application provides a computer program product including a computer program. When the computer program is run, a computer is caused to execute the method as described in the first aspect.
[0098] In a sixth aspect, an embodiment of the present application provides a chip including a processor, and the processor is used to call a computer program in a memory to execute the method as described in the first aspect.
[0099] It should be understood that the technical solutions of the second to fifth aspects of the present application correspond to the technical solution of the first aspect of the present application, and the beneficial effects obtained by each aspect and the corresponding feasible implementation manners are similar and will not be elaborated herein. Description of the Drawings
[0100] Figure 1 It is a schematic diagram of the effect of image binarization provided by an embodiment of the present application;
[0101] Figure 2 It is a schematic diagram of document scanning processing in the related art provided by an embodiment of the present application;
[0102] Figure 3 It is a flowchart of the image processing method provided by an embodiment of the present application;
[0103] Figure 4 It is the process of the image processing method provided by an embodiment of the present application Figure 2 ;
[0104] Figure 5 It is the process of the image processing method provided by an embodiment of the present application Figure 3 ;
[0105] Figure 6 It is a schematic diagram of the implementation of determining the accumulated value of pixel points provided by an embodiment of the present application;
[0106] Figure 7 It is the process of the image processing method provided by an embodiment of the present application Figure 4 ;
[0107] Figure 8 It is a schematic diagram of the implementation of determining the pixel average value provided by an embodiment of the present application;
[0108] Figure 9 is the flowchart of the image processing method provided by the embodiment of the present application Figure 5 ;
[0109] Figure 10 is the schematic diagram of document scanning processing provided by the embodiment of the present application Figure 1 ;
[0110] Figure 11 is the schematic diagram of document scanning processing provided by the embodiment of the present application Figure 2 ;
[0111] Figure 12 is the schematic diagram of the processing process of the image processing method provided by the embodiment of the present application;
[0112] Figure 13 is the schematic diagram of the structure of the image processing device provided by the embodiment of the present application;
[0113] Figure 14 is the schematic diagram of the hardware structure of the terminal device provided by the embodiment of the present application. Detailed implementation manners
[0114] For the convenience of clearly describing the technical solutions of the embodiments of the present application, in the embodiments of the present application, words such as "exemplary" or "for example" are used to represent examples, illustrations or explanations. Any embodiment or design solution described as "exemplary" or "for example" in the present application should not be construed as being more preferred or having more advantages than other embodiments or design solutions. Exactly speaking, the use of words such as "exemplary" or "for example" is intended to present relevant concepts in a specific manner.
[0115] In the embodiments of the present application, "at least one" means one or more, and "a plurality" means two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships can exist. For example, A and / or B can represent: A exists alone, A and B exist simultaneously, and B exists alone, where A and B can be singular or plural. The character " / " generally represents an "or" relationship between the associated objects before and after. "At least one (item)" or similar expressions thereof refer to any combination of these items, including any combination of single item (item) or plural items (items). For example, at least one (item) of a, b, or c can represent: a, b, c, a - b, a - c, b - c, or a - b - c, where a, b, and c can be single or multiple.
[0116] It should be noted that the "at..." in the embodiment of the present application can be the instant when a certain situation occurs, or a period of time after a certain situation occurs, and the embodiment of the present application does not specifically limit this. In addition, the display interface provided in the embodiment of the present application is only an example, and the display interface can also include more or less content.
[0117] In order to better understand the technical solution of the present application, the relevant technologies involved in the present application are further introduced in detail below.
[0118] Image binarization is an important image processing method, wherein image binarization indicates setting the grayscale value of a pixel on an image to 0 or 255 so that the image appears to include only black and white.
[0119] For example, you can combine Figure 1 To understand, Figure 1 A schematic diagram of the effect of image binarization provided in an embodiment of the present application.
[0120] like Figure 1 As shown, assuming that there is Figure 1 The image shown in 101 is referenced Figure 1 It can be determined that there are different levels of grayscale effects in the image 101. Then, for example, the image 101 can be binarized to obtain Figure 1 By comparing the image 101 and the image 102, it can be determined that the image 102 only includes black and white.
[0121] Among them, image binarization processing plays an important role in many application scenarios. For example, in the application scenario of document scanning, the user can take an image of the content to be scanned, and then perform further image processing on the taken image to obtain the scanned content.
[0122] In the process of image processing for the captured image, for example, after document correction, the user can select different filter effects, such as color enhancement, grayscale, black and white, etc., to obtain the desired document scanning effect. For example, if the user chooses to add a black and white filter to the scanned image, it is actually a binarization process for the scanned image.
[0123] It is understandable that in the actual implementation process, the application scenario of image binarization is not limited to the document scanning scenario introduced above. For example, it can also be applied to image processing scenarios after taking pictures, etc. Therefore, the specific processing scenario of image binarization is not limited in this application, and it can be selected and set according to actual needs. The image processing method introduced in this application can be used in any application scenario that requires image binarization processing.
[0124] The following is an explanation of the processing method of image binarization in the current related technology. In the process of binarization processing for an image, the current prior art usually sets a fixed threshold, and then compares the grayscale value of each pixel in the image with the fixed threshold, for example, the grayscale value of the pixel whose grayscale value is less than the threshold is set to 0, and the grayscale value of the pixel whose grayscale value is greater than the threshold is set to 255. The pixel whose grayscale value is set to 0 presents a black effect, and the pixel whose grayscale value is set to 255 presents a white effect.
[0125] However, this implementation method of comparing with a fixed threshold and setting the grayscale value will result in poor image binarization processing effect. For example, in the application scenario of document scanning introduced above, it may cause problems such as unclear handwriting and blurred handwriting. Figure 2 To understand, Figure 2 A schematic diagram of document scanning processing in the related technology provided in an embodiment of the present application.
[0126] like Figure 2 As shown, for example, the user took a photo Figure 2 The image shown in 201 in the figure is scanned and binarized to obtain the effect shown in image 202. Figure 2 It can be confirmed that in the effect shown in image 202, the handwriting is unclear and the text content is not clear.
[0127] In response to the technical problems introduced above, the present application proposes the following technical concept: by comparing with a fixed threshold to determine the grayscale value of each pixel, and then realizing the implementation method of image binarization. The reason why the binarization processing effect is not good is, on the one hand, because it uses a fixed threshold to measure each pixel, and does not consider the actual situation of each pixel; on the other hand, because it uses a fixed threshold to measure the entire image, and does not consider the actual situation of the image. Therefore, the present application proposes a technical solution, which is to consider the background color of the image itself, as well as the situation of each pixel itself, and adaptively determine the threshold to realize the binarization processing of the image, thereby improving the image effect of the processed binary image.
[0128] The image processing method provided by this application is introduced below in conjunction with specific embodiments. It should be noted that the execution subject of each embodiment of this application can be a server, processor, chip, etc., which has a data processing function. In the actual implementation process, the specific execution subject can be selected and set according to actual needs, and this embodiment does not impose any special restrictions on this. Any device with a data processing function can be used as the execution subject of each embodiment of this application.
[0129] First, the following will be introduced in conjunction with Figure 3 as follows. Figure 3 This is a flowchart of the image processing method provided by the embodiment of the present application.
[0130] As Figure 3 shown, the method includes:
[0131] S301. Obtain a first image to be processed and determine the grayscale image corresponding to the first image.
[0132] In this embodiment, the first image is an image that needs to be binarized. For example, the first image can be a document image collected during document scanning introduced above, or it can also be an arbitrarily captured image. In the actual implementation process, the specific acquisition method and specific image content of the first image can be selected and set according to actual needs. It can be understood that any image that needs to be binarized can be the first image in this embodiment.
[0133] After obtaining the first image to be processed, the first image may be a color image or a grayscale image. For the convenience of subsequent processing, in this embodiment, the first image needs to be first grayscaled to determine the grayscale image corresponding to the first image.
[0134] In a possible implementation manner, for example, for each pixel point in the first image, the grayscale value corresponding to each pixel point can be determined according to the r-channel value, g-channel value, and b-channel value corresponding to each pixel point. The r-channel corresponds to the red channel, the g-channel corresponds to the green channel, and the b-channel corresponds to the blue channel.
[0135] Among them, the grayscale value of the pixel point can satisfy, for example, the following formula (1):
[0136] I gray = 0.3I r + 0.59I g + 0.11I b Formula (1)
[0137] Among them, I r is the r-channel value of the pixel point, I g is the g-channel value of the pixel point, I b is the b-channel value of the pixel point, and I gray is the grayscale value of the pixel point.
[0138] The implementation method of determining the gray value of a pixel point in the above formula (1) is actually an implementation method of performing weighted processing on the three channel values. The 0.3, 0.59, and 0.11 in the above formula (1) are actually the weight coefficients corresponding to the three channel values respectively. In the actual implementation process, the weight coefficients corresponding to each channel value can be selected and set according to actual needs, and are not limited to the implementation method introduced in the above formula (1).
[0139] Alternatively, when determining the gray value corresponding to a pixel point, it can also be to calculate the average value of the three channel values of the pixel point and use the obtained average value as the gray value of the pixel point. Or the specific implementation method of image grayscale conversion can also select any possible method according to actual needs, as long as it is ensured that each pixel point in the image after grayscale conversion corresponds to only one pixel value, and this pixel value represents the gray scale of the pixel point.
[0140] The value range of the gray value is usually 0 to 255. When the gray value is 0, the pixel point appears black; when the gray value is 255, the pixel point appears white; when the pixel point is a value between 0 and 255, the pixel point appears gray in different degrees.
[0141] After performing grayscale conversion on the first image to obtain the grayscale-converted image, for example, the grayscale-converted image can be directly determined as the grayscale image. Or in another possible implementation method, the grayscale-converted image can be further normalized to obtain the grayscale image.
[0142] Among them, the normalization process is to limit the pixel value of each pixel point in the grayscale-converted image within the range of 0 to 1. For example, the pixel value of each pixel point in the image after grayscale conversion can be divided by 255 to obtain the normalized pixel value of each pixel point, so as to obtain the grayscale image.
[0143] For example, it can be expressed as I = I gray / 255, where I represents the normalized pixel value, and the range of I is between 0 and 1. After normalization, the pixel value of each pixel point can be limited within the range of 0 to 1 to obtain the grayscale image. The pixel value of each pixel point in the grayscale image is within the range of 0 to 1, which is convenient for subsequent processing. It can be understood that the grayscale image obtained by processing in this way has the pixel value of each pixel point in the range of 0 to 1, and the pixel value of each pixel point actually represents the gray scale of the pixel point.
[0144] The above describes the implementation method of obtaining the gray value of each pixel point in the first image respectively, so as to determine the gray image. Alternatively, in an optional implementation method, the first image can also be directly input into a plug-in, model or algorithm with gray-scale processing function to obtain the gray image corresponding to the first image. This embodiment does not limit the specific implementation method of determining the gray image corresponding to the first image.
[0145] S302. Determine the type of the background color of the first image according to the gray image.
[0146] After determining the gray image corresponding to the first image, in this embodiment, the type of the background color of the first image can be determined according to the gray image. The types of the background color in this embodiment can include, for example, a first type and a second type.
[0147] Among them, the gray value corresponding to the background color of the first type is greater than a preset threshold, and the gray value corresponding to the background color of the second type is less than or equal to the preset threshold.
[0148] Here, the concept of the image background is first explained. An image can usually be divided into a foreground and a background. For example, in the scene of document recognition, the foreground of the image can be understood as the part where the text content in the image is located, and the background of the image can be understood as the part where the non-text content in the image is located. Another example is that the part of the image where the contour can be extracted can be determined as the foreground part, and the part outside the extracted contour can be determined as the background part. In the actual implementation process, the specific determination method of the image background can be selected and set according to actual needs, and this embodiment does not limit this.
[0149] It should also be noted that the image background may have only one color. For example, in the scene of text recognition, a solid color background is selected except for the text part. Or the image background may also have multiple colors. For example, in the scene of text recognition, there may be some patterns and designs in the background part except for the text part. And the background color in this embodiment can be understood as the average color of each pixel point of the image background, or the main color of the background in the image. The main color is, for example, the color with the largest number of pixels of a certain color in the background. As long as the background color can reflect the overall color situation presented by the image background.
[0150] Therefore, the gray value corresponding to the background color introduced above can be understood as the gray value corresponding to the average value of the color values of each pixel point of the image background, or can also be understood as the gray value corresponding to the color value of the main color of the image background, which can be determined according to the actual situation.
[0151] It can be understood that the background color of the image can be diverse, such as blue, white, black, etc. When the gray value corresponding to the background color of the image is greater than the preset threshold, it can be considered that the tone of the background of the image is relatively bright, that is, the background color of the image is light. Therefore, the first type of background color can be understood as the light type of background color. And when the gray value corresponding to the background color of the image is less than or equal to the preset threshold, it can be considered that the tone of the background of the image is relatively dark, that is, the background color of the image is dark. Therefore, the first type of background color can be understood as the dark type of background color.
[0152] For example, if the type of the background color of the first image is the first type, it can be understood that the first image is an image with a light background. And when the type of the background color of the first image is the second type, it can be understood that the first image is an image with a dark background.
[0153] And the preset threshold introduced above is actually the gray value used to divide dark and light. For example, the preset threshold can be determined to be 128, or it can also be any value selected between 0 and 255, which depends on how to define light and dark in the actual implementation process. The specific implementation method of the preset threshold in this embodiment is not limited.
[0154] S303. For any first pixel point in the grayscale image, obtain the first threshold corresponding to the first pixel point according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points in the first area, and determine the target value corresponding to the first pixel point according to the first threshold.
[0155] In this embodiment, for each pixel point in the grayscale image, the threshold corresponding to each pixel point can be determined respectively according to the actual situation of each pixel point. Here, the first pixel point in the grayscale image is taken as an example for illustration. The first pixel point can be any pixel point in the grayscale image. The processing methods of the pixel points in the grayscale image are similar. Therefore, the processing methods for the remaining pixel points will not be elaborated.
[0156] In a possible implementation manner, the first threshold corresponding to the first pixel point can be determined according to the pixel value of the first pixel point in the grayscale image, the type of the background color of the first image determined above, and the pixel average value of the pixel points in the first area corresponding to the first pixel point in the grayscale image.
[0157] Among them, the pixel value of the first pixel point in the grayscale image is actually the gray value of the first pixel point. For example, if the grayscale image is obtained through the grayscale processing and normalization processing introduced above, then the pixel value of the first pixel point is actually the value ranging from 0 to 1 used to represent the gray value of the first pixel point.
[0158] Moreover, the first region is the region formed by the pixel points surrounding the first pixel point in the grayscale image. Among them, the pixel points surrounding the first pixel point are actually the pixel points located around the first pixel point. As for the specific selection method, the number of selected pixel points, etc. of the pixel points surrounding the first pixel point, they can all be selected and set according to actual needs. For example, the region formed by s×s pixel points centered on the first pixel point can be determined as the first region.
[0159] Then it can be understood that multiple pixel points can be included in the first region, and the pixel points included in the first region are actually the pixel points around the first pixel point. In this embodiment, after determining the first region, the pixel average value of the pixel points in the first region can be determined, that is, the average value of the pixel values of the pixel points in the first region, so as to determine the average pixel value situation of the pixel points around the first pixel point.
[0160] Among them, for the first pixel point, the first threshold corresponding to the first pixel point can be adaptively determined according to the pixel value of the first pixel point, the type of the background color of the first image, and the pixel average value of the pixel points in the first region. Then the first threshold is determined considering the pixel value situation of the first pixel point itself, whether the background color of the first image is dark or light, and the average value situation of the pixel values of the pixel points around the first pixel point, so as to ensure that the determined first threshold is adaptively determined according to the own situation of the first pixel point, effectively improving the flexibility and adaptability of the determined first threshold.
[0161] After determining the first threshold for the first pixel point, the target value corresponding to the first pixel point can be determined according to the first threshold. Since it is necessary to determine a binary image in this embodiment, and there are only two pixel values in the binary image, assuming they are 0 and 255 respectively, then the target value is 0 or 255. For another example, if the two pixel values included in the binary image are 0 and 1 respectively, then the target value can also be 0 or 1. Therefore, it can be understood that the possible values of the target value in this embodiment are the two pixel values in the binary image, and its specific value can be selected and set according to actual needs.
[0162] In a possible implementation, the pixel value of the first pixel point can be compared with the first threshold value corresponding to the first pixel point. If the pixel value of the first pixel point is less than the first threshold value, for example, it can be determined that the target value corresponding to the first pixel point is the smaller of the two pixel values included in the binary image. That is to say, the first pixel point appears black in the binary image. Or, if the pixel value of the first pixel point is greater than or equal to the first threshold value, for example, it can be determined that the target value corresponding to the first pixel point is the larger of the two pixel values included in the binary image. That is to say, the first pixel point appears white in the binary image.
[0163] S304. Obtain the binary image corresponding to the first image according to the target values respectively corresponding to the respective pixel points in the grayscale image.
[0164] In addition to determining the target values respectively corresponding to the respective pixel points in the grayscale image according to the above - determined method, the binary image corresponding to the first image can be determined according to the target values respectively corresponding to the respective pixel points, where the pixel values of the respective pixel points in the binary image are actually the above - determined target values.
[0165] The image processing method provided by the embodiments of the present application includes: obtaining a first image to be processed and determining the grayscale image corresponding to the first image. According to the grayscale image, determining the type of the background color of the first image. For any first pixel point in the grayscale image, obtaining the first threshold value corresponding to the first pixel point according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points in the first region, and determining the target value corresponding to the first pixel point according to the first threshold value, where the first region is the region composed of the pixel points surrounding the first pixel point in the grayscale image. Obtain the binary image corresponding to the first image according to the target values respectively corresponding to the respective pixel points in the grayscale image. By first processing the first image into a grayscale image, then based on the grayscale image, determining the type of the background color of the first image, and for each pixel point in the grayscale image, determining the first threshold value corresponding to each pixel point according to the pixel value of the pixel point itself, the type of the background color, and the average value of the pixel values of the pixel points around each pixel point, it can be ensured that the first threshold value adaptive to each pixel point is dynamically determined according to the own situation, surrounding situation of each pixel point, and the background color situation of the first image. Then, according to the first threshold value respectively corresponding to each pixel point, determine the target value respectively corresponding to each pixel point, where the target value is one of the two pixel value selections in the binary image, and then according to the target values respectively corresponding to each pixel point, the binary image corresponding to the first image can be determined. At the same time, because the determination of the target value for each pixel point is dynamically determined, the processing effect of image binarization can be effectively improved.
[0166] Based on the above introduction, the following further details the implementation of determining the type of the background color of the first image in the image processing method provided by this application. Figure 4 This further details the implementation of determining the type of the background color of the first image in the image processing method provided by this application. Figure 4 This is the flowchart of the image processing method provided by the embodiments of this application. Figure 2 .
[0167] As Figure 4 shown, the method includes:
[0168] S401. Perform edge detection processing on the grayscale image to obtain an edge image corresponding to the grayscale image.
[0169] In this embodiment, edge detection processing can be performed on the grayscale image, and the purpose of edge detection is to identify points with obvious brightness changes in the image.
[0170] In a possible implementation, for example, the canny edge detection algorithm can be used for edge detection processing to obtain an edge image corresponding to the grayscale image. Among them, canny edge detection can include the following steps: image denoising, calculating image gradients, non-maximum suppression, double-threshold screening, and marking.
[0171] Among them, during the image denoising process, a Gaussian kernel can be used for filtering. The standard deviation of the Gaussian kernel is set according to the actual size of the processed image. For example, the standard deviation of the Gaussian kernel can be selected as 1 and the kernel size as 5. In the actual implementation process, it can be selected and set according to the actual situation.
[0172] And, during the process of calculating image gradients, for example, the Sobel operator can be used to calculate image gradients.
[0173] And, during the non-maximum suppression process, for example, the gradient intensity of the current pixel point can be compared with the gradient intensities of adjacent pixel points along the positive and negative gradient directions. If its value is the largest, the pixel point is retained as an edge point.
[0174] And, during the double-threshold screening process, points with pixel values higher than the high threshold can be regarded as strong edge points, and points with pixel values lower than the low threshold can be regarded as non-edge points. Points with pixel values between the low threshold and the high threshold are weak edge points; if there are strong edge points in the 8-neighborhood of a weak edge point, it is determined as a strong edge point, otherwise it is a non-edge point. For example, the low threshold can be set to 0 and the high threshold to 0.3. In the actual implementation process, it can be selected and set according to actual requirements.
[0175] And, during the marking process, the values of non-edge points can be marked as 0, and the values of strong edge points can be marked as 255, so as to output the edge image.
[0176] The above introduction is just one possible implementation of edge detection processing. In the actual implementation process, the specific implementation of edge detection processing can be selected and set according to actual needs, as long as non-edge points and strong edge points can be marked from the grayscale image.
[0177] Therefore, the edge image in this embodiment may include edge pixels and non-edge pixels. Among them, the edge pixels are the strong edge points introduced above, and the non-edge pixels are the non-edge points introduced above. The pixel value of the edge pixels in the edge image is the first value, and the first value can be, for example, 255 introduced above. And the pixel value of the non-edge pixels in the edge image is the second value, and the second value can be, for example, 0 introduced above.
[0178] S402. Perform dilation filtering on the edge image to obtain a first image.
[0179] After obtaining the edge image, dilation filtering can be performed on the edge image. Among them, dilation filtering is a maximum value filtering. In the specific processing process, each pixel point in the edge image can be traversed, and then the center point of the structuring element coincides with the currently traversed pixel point. On this basis, the maximum value within the area covered by the structuring element is used to replace the pixel value of the currently traversed pixel point.
[0180] In one possible implementation, in the scenario of document scanning, the principle for selecting the window size of the structuring element is to be as large as possible than the average number of pixels between the strokes of the text elements in the image. Since the algorithm complexity should be as low as possible, the window size should not be too large. Therefore, for example, the structuring element can be selected as a 5×5 window, or it can also be adaptively set according to actual needs.
[0181] After performing the above-introduced dilation filtering on the edge image, the first image can be obtained.
[0182] S403. Perform erosion filtering on the edge image to obtain a second image.
[0183] After obtaining the edge image, erosion filtering can be performed on the edge image. Among them, erosion filtering is a minimum value filtering. In the specific processing process, each pixel point in the edge image can be traversed, and then the center point of the structuring element coincides with the currently traversed pixel point. On this basis, the minimum value within the area covered by the structuring element is used to replace the pixel value of the currently traversed pixel point.
[0184] In one possible implementation, in the scenario of document scanning, the structuring element can also be selected as a 5×5 window for the structuring element, or it can also be adaptively set according to actual needs.
[0185] After performing the erosion filtering process on the edge image as described above, the second image can be obtained.
[0186] S404. In the first image, obtain a second pixel point with a pixel value of the first value, and in the second image, obtain a third pixel point with a pixel value of the first value.
[0187] Based on the above description, it can be determined that in the edge image, the pixel points with a pixel value of the first value are edge pixel points, and the pixel points with a pixel value of the second value are non-edge pixel points. For example, the first value can be 255, then the edge pixel points appear white, and the second value can be 0 as described above, then the non-edge pixel points appear black.
[0188] And it can be further understood that in the scenario of document scanning, the edge pixel points in the edge image actually correspond to the pixel points where the text part is located, and the non-edge pixel points in the edge image actually correspond to the pixel points corresponding to the non-text part.
[0189] And the dilation filtering described above actually expands the pixel points with a pixel value of the first value in the edge image. Correspondingly, the pixel points with a pixel value of the first value in the first image actually correspond to most of the area of the text and a part of the background area around the text.
[0190] And the erosion filtering described above actually shrinks the pixel points with a pixel value of the first value in the edge image. Correspondingly, the pixel points with a pixel value of the first value in the second image actually correspond to a part of the area of the text and rarely include the background area around the text.
[0191] In this embodiment, a second pixel point with a pixel value of the first value can be obtained in the first image, and a third pixel point with a pixel value of the first value can be obtained in the second image.
[0192] S405. In the grayscale image, obtain a fourth pixel point with the same position as the second pixel point, and obtain a fifth pixel point with the same position as the third pixel point.
[0193] It can be understood that the first image and the second image in this embodiment are both processed based on the edge image, and the edge image is processed based on the grayscale image. Therefore, the image sizes of the first image, the second image, the edge image, and the grayscale image are the same, and the pixel points in these images can also correspond to each other according to the position relationship.
[0194] Therefore, in a possible implementation, a fourth pixel point having the same position as the second pixel point can be obtained in the grayscale image. Based on the above introduction, it can be determined that the second pixel point is the pixel point corresponding to most of the text area and a part of the background area around the text in the first image. Similarly, in the grayscale image, the fourth pixel point is the pixel point corresponding to most of the text area and a part of the background area around the text.
[0195] In addition, a fifth pixel point having the same position as the third pixel point can also be obtained in the grayscale image. Based on the above introduction, it can be determined that the third pixel point is the pixel point where a part of the text area in the second image is located, and rarely includes the pixel points where the background area around the text is located. Similarly, in the grayscale image, the fifth pixel point is the pixel point where a part of the text area in the grayscale image is located, and rarely includes the pixel points where the background area around the text is located.
[0196] After determining the fourth pixel point and the fifth pixel point in the grayscale image, by comparing the fourth pixel point and the fifth pixel point, it can be determined that the number of pixel points of the background area included in the multiple fourth pixel points is greater than the number of pixel points of the background area included in the multiple fifth pixel points.
[0197] S406. Determine a first average value of the pixel values of each fourth pixel point, and determine a second average value of the pixel values of each fifth pixel point.
[0198] After that, the average value of the pixel values of each fourth pixel point in the grayscale image can be determined, for example, it can be expressed as the first average value m1. In addition, the average value of the pixel values of each fifth pixel point in the grayscale image can also be determined, for example, it can be expressed as the second average value m2.
[0199] Because the number of pixel points of the background area included in the fourth pixel point is relatively large, while the number of pixel points of the background area included in the fifth pixel point is relatively small, and the amount of the area including the background area determines the size of the average value. Therefore, by comparing the sizes of the first average value and the second average value determined above, the type of the background color of the first image can be distinguished.
[0200] It can be understood that if the background color of the first image is light, since the grayscale value corresponding to the light color is relatively large, the larger the area of the background area included, the larger the corresponding average value. On the contrary, the smaller the area of the background area included, the smaller the corresponding average value.
[0201] In addition, if the background color of the first image is dark, since the grayscale value corresponding to the dark color is relatively small, the larger the area of the background area included, the smaller the corresponding average value. On the contrary, the smaller the area of the background area included, the smaller the corresponding average value.
[0202] S407. If the first average value is greater than the second average value, determine that the type of the background color of the first image is the first type; or, if the first average value is less than or equal to the second average value, determine that the type of the background color of the first image is the second type.
[0203] Based on the processing logic introduced above, the magnitudes of the first average value and the second average value can be compared to determine the type of the background color of the first image.
[0204] In a possible implementation, if the first average value is greater than the second average value, since the number of pixel points in the background area included in the fourth pixel point is more than that in the background area included in the fifth pixel point, the current situation is that the larger the area of the included background area, the larger the corresponding average value. Therefore, it can be determined that the background color of the first image is light color, that is, the type of the background color of the first image is the first type.
[0205] Or, if the first average value is less than or equal to the second average value, since the number of pixel points in the background area included in the fourth pixel point is more than that in the background area included in the fifth pixel point, the current situation is that the larger the area of the included background area, the smaller the corresponding average value. Therefore, it can be determined that the background color of the first image is dark color, that is, the type of the background color of the first image is the second type.
[0206] Among them, the gray value corresponding to the background color of the first type is greater than the preset threshold, and the gray value corresponding to the background color of the second type is less than or equal to the preset threshold. It can be understood that the preset threshold in this embodiment is used to define dark color and light color.
[0207] The above introduces the analysis process in the application scenario of document scanning. In other application scenarios, the edge pixel points actually correspond to the pixel points where the object with a contour is located, and the non-edge pixel points actually correspond to the pixel points corresponding to the large area without a contour, that is, the pixel points of the background part. Its understanding is similar to the content introduced above and will not be elaborated here.
[0208] Based on the above introduction, it can be determined that in the image processing method provided in the embodiments of the present application, by performing edge detection on the grayscale image to obtain an edge image, edge pixels and non-edge pixels can be distinguished in the edge image. Then, dilation filtering and erosion filtering are respectively performed on the edge image to obtain a first image and a second image. Then, by determining a second pixel point with a first value in the first image and a third pixel point with a first value in the second image, since the dilation filtering and erosion filtering are performed, it can be ensured that the background area corresponding to the second pixel point is larger than the background area corresponding to the third pixel point. Then, a fourth pixel point corresponding to the second pixel point is determined in the grayscale image, and a fifth pixel point corresponding to the third pixel point is determined. Then, by analyzing and comparing the pixel value mean of the fourth pixel point and the fourth pixel point, the type of the background color of the first image is determined. Based on the above-described processing process, the type of the background color of the first image can be accurately and effectively determined. And this implementation method can effectively identify the type of the background color of the first image without separating the background and foreground of the first image. Therefore, in addition to accurately identifying the type of the background color of the first image, it can also effectively reduce the complexity and cost of determining the background color of the first image.
[0209] Based on the introduction of the above embodiments, it can be determined that when determining the corresponding first threshold for each pixel point, it needs to be determined according to the pixel average value of the pixel points in the first region corresponding to the pixel point. Next, a possible implementation method for determining the pixel average value will be introduced in combination with specific embodiments. In a possible implementation method, for example, the corresponding cumulative image can be first determined according to the grayscale image, and then the pixel average value is determined based on the cumulative image.
[0210] Next, first in combination with Figure 5 and Figure 6 the implementation method for determining the cumulative image will be introduced. Figure 5 is the flow of the image processing method provided in the embodiments of the present application Figure 3 , Figure 6 is the schematic diagram of the implementation for determining the cumulative value of the pixel point provided in the embodiments of the present application.
[0211] As Figure 5 shown, the method includes:
[0212] S501. For any first pixel point in the grayscale image, determine the first abscissa value and the first ordinate value of the first pixel point.
[0213] In this embodiment, each pixel point in the grayscale image is also processed, and the processing method for each pixel point is similar. Therefore, any first pixel point in the grayscale image will be taken as an example for illustration below.
[0214] For the first pixel point, the first abscissa value and the first ordinate value of the first pixel point can be determined. It can be understood that there are many pixel points in the grayscale image, and each pixel point can correspond to its respective abscissa value x and ordinate value y. Among them, the abscissa value x indicates that the pixel point is the x-th pixel point in the row direction, and the ordinate value y indicates that the pixel point is the y-th pixel point in the column direction. The first abscissa value and the first ordinate value of the first pixel point can be determined according to the actual situation.
[0215] S502. In the grayscale image, obtain a sixth pixel point whose abscissa value is less than or equal to the first abscissa value and whose ordinate value is less than or equal to the first ordinate value.
[0216] After determining the first abscissa value and the first ordinate value of the first pixel point, in one possible implementation, a sixth pixel point whose abscissa value is less than or equal to the first abscissa value and whose ordinate value is less than or equal to the first ordinate value can be obtained.
[0217] For example, it can be understood in combination with Figure 6 As shown in Figure 6 , Figure 6 The left side in Figure 6 shows the grayscale image. In this grayscale image, there are a total of 8×8, that is, 64 pixel points. And assume that
[0218] the pixel point 601 in Figure 6 is the first pixel point. Assuming that the abscissa value and the ordinate value of the pixel point are numbered starting from 0, then the first abscissa value of the first pixel point in the current example is 1, and the first ordinate value of the first pixel point is also 1.
[0219] Referring to Figure 6 It can be understood that the sixth pixel point actually includes the pixel points in the row and column where the first pixel point is located and in the upper left corner of the first pixel point, and the sixth pixel point also includes the first pixel point.
[0220] S503. Determine the sum of the pixel values of each sixth pixel point as the accumulated value corresponding to the first pixel point.
[0221] After determining the corresponding sixth pixel points for the first pixel point, the sum of the pixel values of each sixth pixel point in the grayscale image can be determined, and the sum of the pixel values of each sixth pixel point is determined as the accumulated value corresponding to the first pixel point. For example, in Figure 6 's example, for the first pixel point 601, the determined accumulated value is 1.7.
[0222] Among them, the implementation method of determining the accumulated value corresponding to each pixel point, for example, can also be understood with reference to the following formula two:
[0223]
[0224] Among them, I(i,j) is the grayscale value of the pixel point with abscissa i and ordinate y in the grayscale image, and the value range of I(i,j) can be 0 to 1, I int (x,y) is the accumulated value of the pixel point with abscissa x and ordinate y in the grayscale image.
[0225] S504. Determine an accumulated image according to the accumulated values of the pixel points in the grayscale image, and the pixel value of each pixel point in the accumulated image is the accumulated value.
[0226] In this embodiment, the corresponding accumulated value can be determined for each pixel point in the grayscale image. After that, the accumulated image can be determined according to the accumulated values of the pixel points in the grayscale image. The pixel value of each pixel point in the accumulated image is the accumulated value of each pixel point.
[0227] For example, with reference to Figure 6 , for Figure 6 each pixel point in the grayscale image on the left, determine its corresponding accumulated value, and then use the accumulated value as the pixel value of the pixel point, and the accumulated image shown on the right in Figure 6 can be obtained.
[0228] After determining the above-mentioned accumulated image, based on the above-mentioned accumulated image, the pixel average value of the pixel points in the first area corresponding to the first pixel point can be simply and conveniently determined. The implementation method of determining the pixel average value corresponding to the first area will be introduced below in combination with Figure 7 to the figure. Figure 7 is the flowchart of the image processing method provided by the embodiment of the present application Figure 4 , Figure 8 is the schematic diagram of the implementation of determining the pixel average value provided by the embodiment of the present application.
[0229] As Figure 7As shown, the method includes:
[0230] S701. In the cumulative image, obtain a first positioning pixel point with an abscissa value of x h and an ordinate value of y h .
[0231] In this embodiment, the first region is a region composed of pixel points surrounding the first pixel point in the grayscale image. In a possible implementation manner, the first region can be a region with s×s pixel points surrounding the first pixel point in the grayscale image. That is to say, the first region can be a square region, where s is an integer greater than 1, and the specific value of s can be selected according to actual needs.
[0232] Alternatively, the first region can also be a region with m×n pixel points surrounding the first pixel point in the grayscale image. That is to say, the first region can be a rectangular region, where both m and n are integers greater than 1, and the specific values of m and n can be selected according to actual needs.
[0233] Alternatively, the first region can also be a polygonal region, a quasi-circular region, a region with an irregular shape, etc. surrounding the first pixel point in the grayscale image. This embodiment does not limit the specific implementation manner of the first region, as long as the first region is a region in the grayscale image and is composed of pixel points surrounding the first pixel point.
[0234] Taking the first region as an example of a region composed of s×s pixel points surrounding the first pixel point in the grayscale image for illustration, it can be understood that the first region has a left edge, a right edge, an upper edge, and a lower edge. In this embodiment, the abscissa value of the pixel points on the left edge of the first region is represented as x l , the abscissa value of the pixel points on the right edge of the first region is represented as x h , the ordinate value of the pixel points on the upper edge of the first region is y l , and the ordinate value of the pixel points on the lower edge of the first region is y h .
[0235] In a possible implementation manner, for the first pixel point with an abscissa value of x and an ordinate value of y, the four coordinate values introduced above can be represented as follows:
[0236] Among them, w is the width of the grayscale image, and h is the height of the grayscale image, and their unit is the number of pixel points.
[0237] For example, it can be understood in combination with Figure 8 . As shown in Figure 8 , assuming that currently there isFigure 8 The accumulated image shown. Since the sizes of the accumulated image and the grayscale image are the same, the first region in the grayscale image can be understood with the help of the accumulated image. Assume that the current first pixel point is Figure 8 the pixel point 80 shown in Figure 8 , and assume that currently, for the first pixel point 80, the region formed by the 3×3 pixels surrounding the first pixel point is determined as the first region. Then, referring to
[0238] and, in Figure 8 the example of l the abscissa value x of the pixel point on the left edge of the first region 805 h is equal to 1, and the abscissa value x of the pixel point on the right edge of the first region 805 l is equal to 3, and the ordinate value y of the pixel point on the upper edge of the first region 805 h is equal to 1, and the ordinate value y of the pixel point on the lower edge of the first region 805
[0239] Based on the above-introduced calculation methods of x l , x h , y l , y h , it can be determined that no matter where the first pixel point is located in the grayscale image, such as at the edge position or non-edge position, etc., the corresponding x l , x h , y l , y h can be determined. Then, based on these four coordinates, the first region surrounding the first pixel point can be determined.
[0240] The above-introduced is a possible implementation method for determining the first region. In the actual implementation process, the specific implementation method of the first region can be selected and set according to actual needs. And correspondingly, the specific determination methods of x l , x h , y l , y h can also be adaptively determined according to the selection method of the first region, as long as these four coordinates respectively represent the meanings introduced above.
[0241] Based on the coordinate values of the edge pixels of the first region introduced above, in this embodiment, in the accumulated image, the first positioning pixel point with the abscissa value of x h and the ordinate value of y h can be obtained. Correspondingly, in Figure 8In the example, that is, obtaining the pixel at the coordinate value (3, 3), then the first positioning pixel is Figure 8 the pixel 801 in
[0242] S702. In the cumulative image, obtain the second positioning pixel whose abscissa value is x l minus 1 and whose ordinate value is y l minus 1.
[0243] Moreover, in this embodiment, the second positioning pixel whose abscissa value is x l minus 1 and whose ordinate value is y l minus 1 can also be obtained in the cumulative image. Corresponding to the example in Figure 8 that is, obtaining the pixel at the coordinate value (0, 0), then the second positioning pixel is Figure 8 the pixel 802 in
[0244] S703. In the cumulative image, obtain the third positioning pixel whose abscissa value is x l minus 1 and whose ordinate value is y h minus 1.
[0245] Moreover, in this embodiment, the third positioning pixel whose abscissa value is x l minus 1 and whose ordinate value is y h minus 1 can also be obtained in the cumulative image. Corresponding to the example in Figure 8 that is, obtaining the pixel at the coordinate value (0, 3), then the third positioning pixel is Figure 8 the pixel 803 in
[0246] S704. In the cumulative image, obtain the fourth positioning pixel whose abscissa value is x h and whose ordinate value is y l minus 1.
[0247] Moreover, in this embodiment, the fourth positioning pixel whose abscissa value is x h and whose ordinate value is y l minus 1 can also be obtained in the cumulative image. Corresponding to the example in Figure 8 that is, obtaining the pixel at the coordinate value (3, 0), then the fourth positioning pixel is Figure 8 the pixel 804 in
[0248] S705. Determine the pixel average value of the pixels in the first region according to the pixel values of the first positioning pixel, the second positioning pixel, the third positioning pixel, and the fourth positioning pixel.
[0249] After determining the first positioning pixel, the second positioning pixel, the third positioning pixel, and the fourth positioning pixel introduced above, the pixel average value of the pixels in the first region can be determined according to the pixel values of the first positioning pixel, the second positioning pixel, the third positioning pixel, and the fourth positioning pixel in the cumulative image.
[0250] In a possible implementation, the pixel average value of the pixels in the first region can satisfy, for example, the following formula three:
[0251]
[0252] Where, I int (x h , y h ) represents the cumulative value of the first positioning pixel, and I int (x l - 1, y l - 1) represents the cumulative value of the second positioning pixel, and I int (x l - 1, y h ) represents the cumulative value of the third positioning pixel, and I int (x h , y l - 1) represents the cumulative value of the fourth positioning pixel.
[0253] Based on the above introduction, it can be determined that the cumulative value of a pixel is actually the sum of the pixel values of the pixels (including the current pixel) located in the upper left of the pixel. Therefore, based on the calculation of the cumulative values of the four positioning pixels in the numerator of the above formula two, the sum of the pixel values of each pixel in the first region is actually obtained.
[0254] And the denominator part of formula two actually represents the number of pixels in the first region corresponding to the first pixel. Therefore, based on the calculation of the above formula two, the pixel average value m of the pixels in the first region can be obtained.
[0255] In the embodiments of the present application, an accumulation image is obtained by pre-computing the accumulation value of each pixel point in the grayscale image. Then, when determining the pixel average value of the pixel points within the first region for the first region, the accumulation values of four positioning pixel points can be directly obtained from the accumulation image, and the pixel average value can be determined based on the accumulation values of these four positioning pixel points, thus simply and effectively realizing the determination of the pixel average value. Since in the present application, the pixel average value of the corresponding first region needs to be determined for each pixel point, the implementation method of pre-determining an accumulation image and determining the pixel average value based on the accumulation image in this embodiment, compared with the implementation method of calculating the average value according to the pixel values of each pixel point within the first region corresponding to the pixel point to determine the pixel average value, does not require traversing all pixel points within the first region, but only needs to accumulate the accumulation values of four positioning pixel points in the accumulation image. Therefore, the calculation amount and system overhead can be effectively saved.
[0256] Based on the above-described embodiments, the following further Figure 9 details the implementation method for determining the first threshold corresponding to the first pixel point in the image processing method provided by the present application. Figure 9 is the flowchart of the image processing method provided by the embodiments of the present application Figure 5 .
[0257] As Figure 9 shown, the method includes:
[0258] S901. Determine the type value corresponding to the type of the background color, where the type value corresponding to the first type is t, and the type value corresponding to the second type is -t, and t is an integer greater than or equal to 1.
[0259] In this embodiment, the type of the background color may include the first type and the second type. To facilitate numerical calculation according to the type of the background color to determine the first threshold, the type value corresponding to the type of the background color can be determined in this embodiment.
[0260] In a possible implementation manner, the type value corresponding to the first type is t (for example, it can be 1), and the type value corresponding to the second type is -t (for example, it can be -1), where t is an integer greater than or equal to 1, and the specific value of t can be selected and set according to actual requirements.
[0261] S902. Determine a second parameter according to the pixel value and the pixel average value of the first pixel point, where when the pixel value of the first pixel point is greater than the pixel average value, the second parameter is an integer greater than 0; when the pixel value of the first pixel point is less than the pixel average value, the second parameter is an integer less than 0; and when the pixel value of the first pixel point is equal to the pixel average value, the second parameter is equal to 0.
[0262] Further, in this embodiment, the second parameter may also be determined according to the pixel value of the first pixel and the pixel average value of the first region corresponding to the first pixel.
[0263] In a possible implementation, the second parameter may be determined according to the magnitude relationship between the pixel value of the first pixel and the corresponding pixel average value. For example, when the pixel value of the first pixel is greater than the pixel average value, the second parameter may be determined as an integer greater than 0, that is, the second parameter is a positive number. Or, when the pixel value of the first pixel is less than or equal to the pixel average value, the second parameter may be determined as an integer less than 0, that is, the second parameter is a negative number. The specific value of the second parameter may also be selected and set according to actual requirements, as long as it conforms to the positive and negative value situations described above.
[0264] S903. Determine the first parameter according to the product of the second parameter, the type value, and the pixel average value.
[0265] After determining the second parameter and the type value described above, the first parameter may be determined according to the values of the second parameter, the type value, and the pixel average value.
[0266] In an alternative implementation, the second parameter may also be multiplied by a corresponding coefficient to amplify the second parameter, and then the first parameter may be determined according to the product of the amplified second parameter, the type value, and the pixel average value.
[0267] S904. Determine the sum of the pixel average value and the first parameter as the first threshold corresponding to the first pixel.
[0268] After that, the sum of the pixel average value and the first parameter is determined as the first threshold corresponding to the first pixel. For example, the above-described content may be understood with reference to the following formula four:
[0269] T = m + c × k × m × [sign(I(x, y) - m) × e |I(x,y)-m| -1] Formula Four
[0270] Wherein, c is the type value, k is the proportional parameter, m is the pixel average value, sign() is the sign function. When the input value of the sign function is less than 0, the sign function outputs -1. When the input value of the sign function is greater than 0, the sign function outputs 1. When the input value of the sign function is equal to 0, the sign function outputs 0. Therefore, sign(I(x, y) - m) corresponds to the second parameter described above, and T is the first threshold.
[0271] Further, sign(I(x, y) - m) × e in the above formula |I(x,y)-m|This term is used to amplify the difference between the pixel value of the first pixel and the pixel average value, so as to obtain a threshold value that is conducive to highlighting foreground pixels in the case of low contrast. Then this term can also be understood as the amplified second parameter introduced above.
[0272] And the whole term c×k×m×[sign(I(x,y)-m)×e |I(x,y)-m| -1] corresponds to the first parameter introduced above. After summing this term and the pixel average value m, the first threshold value T can be obtained.
[0273] The determination logic of the first threshold value is introduced below based on the above formula three:
[0274] Based on the above introduction, it can be determined that the first threshold value T is obtained by adding a term to the pixel average value m, that is, increasing or decreasing the pixel average value m on the basis of the pixel average value m to obtain the first threshold value T. And whether to increase the pixel average value m to obtain the first threshold value T or decrease the pixel average value m to obtain the first threshold value T depends on the sign of the first parameter (that is, the term after the plus sign).
[0275] The sign of the first parameter is affected by two aspects. One is the sign of the type value c, and the other is the sign of the second parameter, that is, sign(I(x,y)-m).
[0276] And referring to the introduction of the above formula four, it can be determined that e |I(x,y)-m| This term is almost less than 1 and must be greater than 0, so that [sign(I(x,y)-m)×e |I(x,y)-m| -1] this term is almost negative. Therefore, what actually affects the sign of the first parameter is mainly the type value c. Then it can be understood in the following two cases:
[0277] Case 1: The type of the background color of the first image is the first type, and the corresponding type value c is a positive number.
[0278] In this case, because the type value c is a positive number, the first threshold value T is less than the pixel average value m, that is, we reduce it on the basis of the pixel average value m to obtain the first threshold value T corresponding to the first pixel.
[0279] It can be understood that the type of the background color of the first image is the first type, that is to say, the background color of the first image is light. Usually, it is an image with a light background and dark text. In this case, if the threshold is too high, it is easy for the gray value of the pixel point to be less than the threshold, and then the pixel point is binarized to black, which will cause some text to stick together. Therefore, in this scenario, reducing the threshold is beneficial to reducing the adhesion of dark text. If the contrast of the text is low, that is, I(x,y)-m is small, then the threshold is adjusted downward less to retain the text information.
[0280] Case 2: The type of the background color of the first image is the second type, and the corresponding type value c is negative.
[0281] In this case, since the type value c is negative, the first threshold T is greater than the pixel average value m. That is to say, we add some on the basis of the pixel average value m to obtain the first threshold T corresponding to the first pixel point.
[0282] It can be understood that the type of the background color of the first image is the second type, that is to say, the background color of the first image is dark. Usually, it is an image with a dark background and light text. On the contrary to the above introduction, in this case, if the threshold is too low, it is easy for the gray value of the pixel point to be greater than the threshold, and then the pixel point is binarized to white, which will cause some text to stick together. Therefore, in this scenario, raising the threshold is beneficial to reducing the adhesion of light text. If the contrast of the text is low, that is, I(x,y)-m is small, then the threshold is adjusted upward less to retain the text information.
[0283] Based on the above analysis, it can be determined that by determining the first threshold corresponding to each pixel point according to the implementation method introduced above, and comparing the pixel value of each pixel point with its corresponding first threshold, the target value corresponding to the pixel point with a pixel value less than the first threshold is set to the third value. For example, the third value is 0, that is to say, the binarization effect of this type of pixel point is set to black. And, the target value corresponding to the pixel point with a pixel value greater than or equal to the first threshold is set to the fourth value. For example, the fourth value is 255, that is to say, the binarization effect of this type of pixel point is set to white. And based on the above introduction, it can be determined that for images with a light background and images with a dark background, there are different binarization effects. Specifically, for an image with a light background and dark text, it will be processed into a binarization effect with a white background and black text, and for an image with a dark background and light text, it will be processed into a binarization effect with a black background and white text, so as to ensure that there are different binarization processing effects for images with different types of background colors, and referring to the above Figure 2It can be determined from the introduction that for an image with a dark background and light-colored text, if it is still processed into a white background and black text, the text may become unclear and difficult to recognize, or even all the text shown in Figure 2 disappears. Therefore, in this embodiment, the specific background color of the image, the pixel value of each pixel itself, and the pixel values of the pixels around each pixel can be effectively combined to dynamically determine the corresponding first threshold for each pixel, and then a binary image can be determined based on the first threshold, thereby effectively improving the performance of the determined binary image.
[0284] For example, for the binary processing described above, the binary image obtained based on the image processing method in this application can be understood with reference to Figure 2 and Figure 10 is shown in Figure 10 which is a schematic diagram of document scanning processing provided by an embodiment of this application. Figure 1
[0285] As Figure 10 shown, for example, the user takes a picture of the image shown in Figure 10 1001. After image scanning and binary processing of the image 1001, the effect shown in the image 1002 is obtained. It can be determined with reference to Figure 10 that the binary image obtained by binary processing of the image with a dark background and light-colored text in this application shows the processing effect of a black background and white text, so the recognizability and clarity of the text can be effectively guaranteed.
[0286] In addition, the processing effect diagram for a light background can also be understood in combination with Figure 11 and Figure 11 is a schematic diagram of document scanning processing provided by an embodiment of this application. Figure 2
[0287] As Figure 11 shown, 1101 shows the binary processing effect in the current related technology, and 1102 shows the binary processing effect obtained based on the technical solution of this application. It can be determined with reference to Figure 11 that the binary processing effect obtained by the technical solution of this application is significantly clearer.
[0288] Based on the above-described embodiments, the image processing method provided by this application will be described as a whole below in combination with Figure 12 and Figure 12 is a schematic diagram of the processing process of the image processing method provided by an embodiment of this application.
[0289] As Figure 12 shown, the method includes:
[0290] First, preprocess the first image. The image preprocessing, for example, can include the grayscale processing and normalization processing introduced above, so as to obtain the grayscale image corresponding to the first image.
[0291] After that, on the one hand, processing can be performed based on the grayscale image to obtain the cumulative image corresponding to the grayscale image; on the other hand, the type of the background color of the first image can be determined according to the grayscale image.
[0292] After that, according to the grayscale image, the cumulative image, and the type of the background color of the first image, the respective first threshold corresponding to each pixel point in the grayscale image can be determined, and then adaptive local binarization can be performed based on the first threshold corresponding to each pixel point, so as to output the binary image corresponding to the grayscale image.
[0293] The specific implementation manners of the above-introduced respective processes can refer to the introduction of the above embodiments, and will not be elaborated here.
[0294] In summary, the image processing method provided in this application can determine the type of the background color of the first image, and determine the threshold corresponding to each pixel point based on the type of the background color of the first image, so as to avoid the situation where all the characters disappear. And in this application, the first threshold corresponding to each pixel point will be dynamically and adaptively determined for each pixel point, and then binarization processing will be performed based on the first threshold corresponding to each pixel point. No matter what the contrast of the image is, in this embodiment, the first threshold is determined for each pixel point respectively. Therefore, the binarization effect of images with different contrasts can be effectively improved. At the same time, in this embodiment, the calculation amount and power consumption can be saved by determining the cumulative image and other methods, and when calculating the threshold in this application, based on the simple parameter calculation introduced above, the first threshold corresponding to each pixel point can be determined, without calculating the local variance. Compared with the implementation manner of calculating the local variance, the implementation manner of calculating the first threshold in this application can effectively reduce the calculation amount. Therefore, the technical solution of this application can be effectively deployed and used on lightweight devices such as mobile terminals to meet the deployment requirements of mobile terminals with low latency and low power consumption.
[0295] The image processing method of the embodiment of this application has been described above. Next, the device for executing the above image processing method provided by the embodiment of this application will be described. Those skilled in the art can understand that the method and the device can be combined and cited with each other, and the relevant device provided by the embodiment of this application can execute the steps in the above image processing method.
[0296] Figure 13 It is a schematic structural diagram of the image processing device provided by the embodiment of this application. As Figure 13As shown, the device 130 includes: an acquisition module 1301, a determination module 1302, and a processing module 1303.
[0297] The acquisition module 1301 is configured to acquire a first image to be processed and determine a grayscale image corresponding to the first image;
[0298] The determination module 1302 is configured to determine a type of the background color of the first image according to the grayscale image;
[0299] The processing module 1303 is configured to, for any first pixel point in the grayscale image, obtain a first threshold corresponding to the first pixel point according to a pixel value of the first pixel point, the type of the background color, and a pixel average value of pixel points in a first region, and determine a target value corresponding to the first pixel point according to the first threshold, where the first region is a region formed by pixel points surrounding the first pixel point in the grayscale image;
[0300] The processing module 1303 is further configured to obtain a binary image corresponding to the first image according to target values respectively corresponding to each pixel point in the grayscale image.
[0301] In a possible design, the determination module 1302 is specifically configured to:
[0302] Perform edge detection processing on the grayscale image to obtain an edge image corresponding to the grayscale image;
[0303] Perform dilation filtering processing on the edge image to obtain a first image;
[0304] Perform erosion filtering processing on the edge image to obtain a second image;
[0305] Determine the type of the background color of the first image according to the first image, the second image, and the grayscale image.
[0306] In a possible design, a pixel value of an edge pixel point in the edge image is a first value, and a pixel value of a non-edge pixel point in the edge image is a second value;
[0307] The determination module 1302 is specifically configured to:
[0308] In the first image, acquire a second pixel point with a pixel value of the first value, and in the second image, acquire a third pixel point with a pixel value of the first value;
[0309] In the grayscale image, acquire a fourth pixel point having the same position as the second pixel point, and acquire a fifth pixel point having the same position as the third pixel point;
[0310] Determine the type of the background color of the first image according to the fourth pixel point and the fifth pixel point.
[0311] In a possible design, the determining module 1302 is specifically configured to:
[0312] Determine a first average value of the pixel values of the fourth pixel points, and determine a second average value of the pixel values of the fifth pixel points;
[0313] If the first average value is greater than the second average value, determine that the type of the background color of the first image is the first type; or, if the first average value is less than or equal to the second average value, determine that the type of the background color of the first image is the second type;
[0314] Wherein, the gray value corresponding to the background color of the first type is greater than a preset threshold, and the gray value corresponding to the background color of the second type is less than or equal to the preset threshold.
[0315] In a possible design, the processing module 1303 is specifically configured to:
[0316] Determine a type value corresponding to the type of the background color, wherein the type value corresponding to the first type is t, the type value corresponding to the second type is -t, and t is an integer greater than or equal to 1;
[0317] Determine a first parameter according to the pixel value of the first pixel point, the type value, and the pixel average value;
[0318] Determine the sum of the pixel average value and the first parameter as the first threshold corresponding to the first pixel point.
[0319] In a possible design, the processing module 1303 is specifically configured to:
[0320] Determine a second parameter according to the pixel value of the first pixel point and the pixel average value, wherein when the pixel value of the first pixel point is greater than the pixel average value, the second parameter is an integer greater than 0, when the pixel value of the first pixel point is less than the pixel average value, the second parameter is an integer less than 0, and when the pixel value of the first pixel point is equal to the pixel average value, the second parameter is equal to 0;
[0321] Determine the first parameter according to the product of the second parameter, the type value, and the pixel average value.
[0322] In a possible design, the processing module 1303 is further configured to:
[0323] Before obtaining the first threshold corresponding to the first pixel point according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points within the first region, for any first pixel point in the grayscale image, determine the first abscissa value and the first ordinate value of the first pixel point;
[0324] In the grayscale image, obtain a sixth pixel point whose abscissa value is less than or equal to the first abscissa value and whose ordinate value is less than or equal to the first ordinate value;
[0325] Determine the sum of the pixel values of each of the sixth pixel points as the cumulative value corresponding to the first pixel point;
[0326] According to the cumulative values of the pixel points in the grayscale image, determine a cumulative image, where the pixel value of each pixel point in the cumulative image is the cumulative value.
[0327] In a possible design, the abscissa value of the pixel point on the left edge of the first region is x l , and the abscissa value of the pixel point on the right edge of the first region is x h , the ordinate value of the pixel point on the upper edge of the first region is y l , and the ordinate value of the pixel point on the lower edge of the first region is y h ;
[0328] The processing module 1303 is further configured to:
[0329] Before obtaining the first threshold corresponding to the first pixel point according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points within the first region, in the cumulative image, obtain a first positioning pixel point whose abscissa value is x h , and whose ordinate value is y h ;
[0330] In the cumulative image, obtain a second positioning pixel point whose abscissa value is x l minus 1, and whose ordinate value is y l minus 1;
[0331] In the cumulative image, obtain a third positioning pixel point whose abscissa value is x l minus 1, and whose ordinate value is y h ;
[0332] In the cumulative image, obtain a fourth positioning pixel point whose abscissa value is x h , and whose ordinate value is y l minus 1;
[0333] Determine the pixel average value of the pixel points within the first region based on the pixel values of the first positioning pixel point, the second positioning pixel point, the third positioning pixel point, and the fourth positioning pixel point.
[0334] In a possible design, the processing module 1303 is further configured to:
[0335] If the pixel value of the first pixel point is less than the first threshold, determine that the target value corresponding to the pixel point is the third value;
[0336] If the pixel value of the pixel point is greater than or equal to the first threshold, determine that the target value corresponding to the pixel point is the fourth value.
[0337] The device provided in this embodiment can be used to execute the technical solutions of the above method embodiment. The implementation principle and technical effects are similar, and will not be elaborated here.
[0338] The image processing method provided in the embodiments of the present application can be applied to an electronic device with communication functions. The electronic device includes a terminal device. The specific device form of the terminal device and the like can refer to the above relevant description and will not be elaborated here.
[0339] The embodiments of the present application provide a terminal device, which can be referred to Figure 14 for understanding, Figure 14 and is a schematic diagram of the hardware structure of the terminal device provided in the embodiments of the present application.
[0340] As Figure 14 shown, the terminal device 140 includes: a processor 1401 and a memory 1402; the memory 1402 stores computer execution instructions; the processor 1401 executes the computer execution instructions stored in the memory 1402, so that the terminal device 140 executes the above method.
[0341] When the memory 1402 is independently provided, the terminal device further includes a bus 1403 for connecting the memory 1402 and the processor 1401.
[0342] The embodiments of the present application provide a chip. The chip includes a processor, and the processor is used to call a computer program in the memory to execute the technical solutions in the above embodiments. The implementation principle and technical effects are similar to those of the above relevant embodiments and will not be elaborated here.
[0343] The embodiments of the present application also provide a computer-readable storage medium. The computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the above-mentioned method is implemented. The method described in the above embodiments can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. If implemented in software, the functions can be stored on a computer-readable medium or transmitted on a computer-readable medium as one or more instructions or codes. The computer-readable medium can include a computer storage medium and a communication medium, and can also include any medium that can transfer a computer program from one place to another. The storage medium can be any target medium accessible by a computer.
[0344] In a possible implementation, the computer-readable medium may include RAM, ROM, a compact disc read-only memory (CD-ROM), or other optical disc storage, a magnetic disk storage, or other magnetic storage device, or any other medium targeted to carry the required program code in the form of instructions or data structures and accessible by a computer. Moreover, any connection is properly termed a computer-readable medium. For example, if software is transmitted from a website, server, or other remote source using coaxial cable, fiber optic cable, twisted pair, Digital Subscriber Line (DSL), or wireless technologies such as infrared, radio, and microwave, then the coaxial cable, fiber optic cable, twisted pair, DSL, or wireless technologies such as infrared, radio, and microwave are included in the definition of the medium. As used herein, disk and optical disc include optical discs, laser discs, optical discs, Digital Versatile Discs (DVDs), floppy disks, and Blu-ray discs, where disks typically reproduce data magnetically, while optical discs reproduce data optically using lasers. The above combinations should also be included within the scope of the computer-readable medium.
[0345] The embodiments of the present application provide a computer program product. The computer program product includes a computer program. When the computer program is run, the computer is caused to execute the above-mentioned method.
[0346] The embodiments of the present application are described with reference to the flowcharts and / or block diagrams of methods, apparatuses (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or block in the flowcharts and / or block diagrams, and the combination of processes and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to the processing unit of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable devices to generate a machine, such that the instructions executed by the processing unit of the computer or other programmable data processing devices generate for implementing in the process Figure 1means for the functions specified in one or more processes and / or blocks Figure 1 in one or more blocks.
[0347] In the above specific embodiments, the objectives, technical solutions and beneficial effects of the present invention have been further described in detail. It should be understood that the above are only specific embodiments of the present invention and are not used to limit the protection scope of the present invention. Any modifications, equivalent replacements, improvements, etc. made on the basis of the technical solutions of the present invention shall be included in the protection scope of the present invention.
Claims
1. An image processing method, characterized in that, Including: Obtain a first image to be processed and determine the grayscale image corresponding to the first image; Determine the type of the background color of the first image according to the grayscale image; wherein, the type of the background color includes a first type and a second type, the grayscale value corresponding to the background color of the first type is greater than a preset threshold, and the grayscale value corresponding to the background color of the second type is less than or equal to the preset threshold; For any first pixel point in the grayscale image, obtain a first threshold corresponding to the first pixel point according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points in a first region, and determine a target value corresponding to the first pixel point according to the first threshold, where the first region is a region formed by the pixel points surrounding the first pixel point in the grayscale image; Obtain a binary image corresponding to the first image according to the target values corresponding to the respective pixel points in the grayscale image.
2. The method according to claim 1, wherein The determining the type of the background color of the first image according to the grayscale image includes: Perform edge detection processing on the grayscale image to obtain an edge image corresponding to the grayscale image; Perform dilation filtering processing on the edge image to obtain a first image; Perform erosion filtering processing on the edge image to obtain a second image; Determine the type of the background color of the first image according to the first image, the second image, and the grayscale image.
3. The method according to claim 2, wherein The pixel value of the edge pixel point in the edge image is a first value, and the pixel value of the non-edge pixel point in the edge image is a second value; The determining the type of the background color of the first image according to the first image, the second image, and the grayscale image includes: In the first image, obtain a second pixel point with a pixel value of the first value, and in the second image, obtain a third pixel point with a pixel value of the first value; In the grayscale image, obtain a fourth pixel point with the same position as the second pixel point, and obtain a fifth pixel point with the same position as the third pixel point; Determine the type of the background color of the first image according to the fourth pixel point and the fifth pixel point.
4. The method according to claim 3, wherein The determining the type of the background color of the first image according to the fourth pixel point and the fifth pixel point includes: Determine a first average value of the pixel values of the respective fourth pixel points, and determine a second average value of the pixel values of the respective fifth pixel points; If the first average value is greater than the second average value, determine that the type of the background color of the first image is the first type; or, if the first average value is less than or equal to the second average value, determine that the type of the background color of the first image is the second type.
5. The method according to any one of claims 1 to 4, characterized in that, The obtaining the first threshold corresponding to the pixel point according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points in the first region includes: Determine a type value corresponding to the type of the background color, where the type value corresponding to the first type is t, the type value corresponding to the second type is -t, and t is an integer greater than or equal to 1; Determine a first parameter according to the pixel value of the first pixel, the type value, and the pixel average value; Determine the sum of the pixel average value and the first parameter as the first threshold corresponding to the first pixel.
6. The method according to claim 5, wherein The determining the first parameter according to the pixel value of the first pixel, the type value, and the pixel average value includes: Determine a second parameter according to the pixel value of the first pixel and the pixel average value, where when the pixel value of the first pixel is greater than the pixel average value, the second parameter is an integer greater than 0, when the pixel value of the first pixel is less than the pixel average value, the second parameter is an integer less than 0, and when the pixel value of the first pixel is equal to the pixel average value, the second parameter is equal to 0; Determine the first parameter according to the product of the second parameter, the type value, and the pixel average value.
7. The method according to claim 6, wherein Before obtaining the first threshold corresponding to the first pixel according to the pixel value of the first pixel, the type of the background color, and the pixel average value of the pixels in the first region, the method further includes: For any first pixel in the grayscale image, determine the first abscissa value and the first ordinate value of the first pixel; In the grayscale image, obtain a sixth pixel whose abscissa value is less than or equal to the first abscissa value and whose ordinate value is less than or equal to the first ordinate value; Determine the sum of the pixel values of each of the sixth pixels as the accumulation value corresponding to the first pixel; Determine an accumulation image according to the accumulation values of the pixels in the grayscale image, where the pixel value of each pixel in the accumulation image is the accumulation value.
8. The method according to claim 7, wherein The abscissa value of the pixel points on the left edge of the first region is , and the abscissa value of the pixel points on the right edge of the first region is , the ordinate value of the pixel points on the upper edge of the first region is , and the ordinate value of the pixel points on the lower edge of the first region is ; Before obtaining the first threshold corresponding to the first pixel according to the pixel value of the first pixel, the type of the background color, and the pixel average value of the pixels in the first region, the method further includes: In the cumulative image, obtain a first positioning pixel point whose abscissa value is , and whose ordinate value is . In the accumulated image, obtain a second positioning pixel point whose abscissa value is minus 1 and whose ordinate value is minus 1; In the cumulative image, obtain a third positioning pixel point whose abscissa value is minus 1 and whose ordinate value is . In the accumulated image, obtain a fourth positioning pixel point whose abscissa value is , and whose ordinate value is minus 1; Determine the pixel average value of the pixels in the first region according to the pixel value of the first positioning pixel, the pixel value of the second positioning pixel, the pixel value of the third positioning pixel, and the pixel value of the fourth positioning pixel.
9. The method according to claim 8, wherein The determining the target value corresponding to the first pixel according to the first threshold includes: If the pixel value of the first pixel is less than the first threshold, determine that the target value corresponding to the pixel is a third value; If the pixel value of the pixel is greater than or equal to the first threshold, determine that the target value corresponding to the pixel is a fourth value.
10. An image processing apparatus, characterized in that, Includes: An acquisition module, configured to acquire a first image to be processed and determine the grayscale image corresponding to the first image; A determination module, configured to determine the type of the background color of the first image according to the grayscale image; where the type of the background color includes a first type and a second type, the grayscale value corresponding to the background color of the first type is greater than a preset threshold, and the grayscale value corresponding to the background color of the second type is less than or equal to the preset threshold; A processing module, configured to obtain a first threshold corresponding to any first pixel point in the grayscale image according to the pixel value of the first pixel point, the type of the background color, and the pixel average value of the pixel points in a first region, where the first region is a region formed by the pixel points surrounding the first pixel point in the grayscale image, and determine a target value corresponding to the first pixel point according to the first threshold. The processing module is further configured to obtain a binary image corresponding to the first image according to the target values respectively corresponding to the pixel points in the grayscale image.
11. A terminal device, characterized in that, Comprising: A processor and a memory; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory, so that the terminal device executes the method according to any one of claims 1-9.
12. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by the processor, the method according to any one of claims 1-9 is implemented.
13. A computer program product, characterized in that, Including a computer program, when the computer program is run, the computer is made to execute the method according to any one of claims 1-9.
Citation Information
Patent Citations
Image binarization method and device, computer storage medium
CN108961260A
Apparatus and method for image processing, recording media, and program
JP2004199622A