Document image processing method, device and electronic equipment
Through the mean fuzzy algorithm and image division operation, the problem of the text in the document image is not beautiful enough is solved, and a clear and beautiful black and white document image is generated, which simplifies the algorithm difficulty and improves processing efficiency.
Patent Information
- Application Number
- CN202110420189.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-04-19
- Publication Date
- 2025-09-09
- Estimated Expiration
- 2041-04-19
AI Technical Summary
In the existing black and white effect processing of document images, the text is not beautiful enough, there are problems such as jagged edges and adhesion, and the shadows caused by uneven lighting affect the processing effect.
The mean blur algorithm is used to process document images. By dividing the original document image and the blurred image, the basic properties of fractions are used to increase the grayscale value gap between the text foreground and the white background, and a black and white document image is generated through linear mapping.
The grayscale value difference between the foreground and background of the text in the document image is widened, which avoids the problems of jagged edges and adhesion, and improves the aesthetics and processing efficiency of the document image.
Smart Images

Figure CN113129245B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of image processing, and in particular to a method, device and electronic equipment for processing document images. Background Art
[0002] Document images are mainly obtained by photographing documents with a camera device. Generally, more emphasis is placed on the clarity of the document image content. In order to better highlight the text content of the document image, it is generally necessary to process the image in black and white.
[0003] In addition, OCR technology can be used to meet the needs of extracting text from document images for later editing. OCR (Optical Character Recognition) technology refers to the process in which an electronic device (such as a scanner or digital camera) examines characters printed on paper, determines their shape by detecting dark and light patterns, and then uses character recognition methods to translate the shape into computer text; that is, for printed characters, an optical method is used to convert the text in a paper document into a black and white dot matrix image file, and recognition software is used to convert the text in the image into text format for further editing and processing by word processing software.
[0004] However, whether it is for the purpose of displaying the text in the document image more clearly or for the purpose of improving the text recognition accuracy of OCR technology, the black and white effect processing of the document image is crucial.
[0005] Existing binarization algorithms for achieving black and white effects in document images mainly include fixed thresholding (poor applicability and ineffective results); local thresholding (block-by-block, with a fixed threshold for each block, resulting in lines at transitions; each block's threshold is calculated, such as the Otsu algorithm); and binary gradient algorithms. Binarization of document images often results in text that is less aesthetically pleasing, often exhibiting issues such as jagged edges and blurring. Furthermore, uneven lighting, such as shadows, complicates the binarization process.
[0006] The existing application number is 202010673612.2, and the name is: A Chinese invention patent application for a method for rapid color image processing based on an intelligent terminal. The invention discloses a method for rapid color image processing based on an intelligent terminal. It belongs to the field of color image processing technology; the specific steps are: designing an interface for the application of the intelligent terminal; selecting and opening a color image through the interface design; observing whether the selected color image needs to be compressed; graying the compressed color image to enhance its graying; successively performing Gaussian filtering, binarization, expansion, and corrosion on the grayed image; displaying the above-mentioned processed image and the processing time on the interface; and finally saving the processed image. The present invention can be quickly used for image processing and analysis. The original image processing takes 13.7 seconds, especially after using image compression, it only takes 1.5 seconds, and the processing efficiency is increased by nearly 10 times; it not only improves the extraction efficiency of the target object, but also greatly reduces the required processing time.
[0007] The above patent discloses a method for obtaining a black and white document image through binarization processing. The text of the processed black and white document image is not beautiful enough, and the edges of the text are accompanied by problems such as jaggedness and adhesion. In addition, the shadow problem caused by uneven lighting on the document image affects the difficulty and effect of the binarization algorithm processing.
[0008] In view of this, the present invention is proposed. Summary of the Invention
[0009] The present invention is used to process the display effect of a document image, so that the image presents a black and white effect, that is, the document text is clearly presented, while the background is presented in white.
[0010] In order to achieve the above-mentioned object of the invention, the present invention provides the following technical solutions:
[0011] A method for processing a document image, comprising:
[0012] Copy the original document image to obtain a document copy image;
[0013] The document copy image is processed by the mean fuzzy algorithm to obtain a fuzzy image;
[0014] A calculation image is obtained by dividing the original document image and the blurred image.
[0015] As an optional embodiment of the present invention, performing an image division operation on the original document image and the blurred image to obtain a calculated image includes:
[0016] Division operations are performed on the grayscale values of pixels at corresponding coordinates of the original document image and the blurred image, and the quotients are rounded. The quotients greater than the maximum set grayscale value Hmax are taken as Hmax, and the maximum set grayscale value Hmax≤255. The quotients less than the minimum set grayscale value Hmin are taken as Hmin, and 0≤Hmin<Hmax.
[0017] As an optional embodiment of the present invention, the calculation formula for performing division operation on the grayscale values of pixels at corresponding coordinates of the original document image and the blurred image is: dst_img_0 = min(src_img / (blur_img+k)*Hmax, Hmax), where src_img / (blur_img+k)*Hmax and Hmax take the smaller value;
[0018] Wherein, src_img is the original image of the document, blur_img is the blurred image, dst_img_0 is the calculated image, and k is the set value.
[0019] As an optional implementation of the present invention, linear mapping is performed on the calculated image to obtain a document image with a black and white effect.
[0020] As an optional embodiment of the present invention, performing linear mapping on the calculated image includes:
[0021] A first grayscale value H1 and a second grayscale value H2 are preset, wherein the first grayscale value H1 is smaller than the second grayscale value H2, the linear mapping is preset as [H3, H4], H3 and H4 are preset grayscale values, and H1≤H3<H2≤H4;
[0022] The pixels in the result image that are greater than the second grayscale value H2 are set to H4, the pixels in the result image that are less than the first grayscale value H1 are set to H3, and the pixels in the result image in the interval [H1, H2] are linearly mapped to [H3, H4].
[0023] As an optional implementation manner of the present invention, the operation image may be subjected to binarization processing using a binarization algorithm to obtain a binary image.
[0024] As an optional embodiment of the present invention, the blur radius of the mean blur algorithm in the blurred image obtained by processing the document copy image by the mean blur algorithm is set to [1 / 200, 1 / 30] of the smallest side of the actual image;
[0025] Optionally, the blur radius is set to [1 / 130, 1 / 30] of the smallest side of the actual image.
[0026] This embodiment also provides a document image processing device including:
[0027] A copy module copies the original document image to obtain a document copy image;
[0028] Mean fuzzy algorithm module, which processes the document copy image through the mean fuzzy algorithm to obtain a fuzzy image;
[0029] The image operation module performs a division operation on the original document image and the blurred image to obtain an operation image.
[0030] The present invention also provides an electronic device, comprising a processor and a memory, wherein the memory is used to store a computer executable program.
[0031] When the computer program is executed by the processor, the processor executes the document image processing method.
[0032] The present invention also provides a computer-readable medium storing a computer-executable program, wherein when the computer-executable program is executed, the document image processing method is implemented.
[0033] Compared with the prior art, the present invention has the following beneficial effects:
[0034] The document image processing method of the present invention is mainly aimed at the image characteristics of the document image itself. The document image is generally simple in color, and users are more concerned about the document content in the document image. Therefore, the basic properties of the fraction are utilized: the numerator remains unchanged, the denominator becomes larger, and the fraction becomes smaller. After a simple image division operation, the pixels with small grayscale values become smaller, and the pixels with large grayscale values become larger, thereby widening the gap in grayscale values between the text foreground part and the white background part of the document image. The document content in the document image can be extracted to obtain a document image with a black and white effect. Compared with the binarization algorithm processing, the algorithm is simpler, less difficult to implement, and the document presentation effect is better.
[0035] The document image processing method of the present invention mainly focuses on extracting the text part in the document image, and needs to increase the grayscale value difference between the text foreground part and the white background part. The local shadows in the image have little effect on the image processing effect. Therefore, the document image processing method of this embodiment is more applicable to document images.
[0036] A document image processing method disclosed herein performs linear mapping on a calculated image to produce a black-and-white document image. This method uses a linear mapping algorithm to darken text and brighten the background to produce a black-and-white document image. This method effectively avoids issues such as unattractive text, jagged edges, and sticking in images produced by binarization algorithms. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] Figure 1 A flowchart of the document image processing method of the present invention;
[0038] Figure 2 Blurred image effect diagram after mean blur processing in the document image processing method of the present invention (blur_img, blur radius 17);
[0039] Figure 3 The image effect after the image division operation in the document image processing method of the present invention (corresponding to dst_img with a blur radius of 17);
[0040] Figure 4 Blurred image effect diagram after mean blur processing in the document image processing method of the present invention (blur_img, blur radius 40);
[0041] Figure 5 The image effect after the image division operation in the document image processing method of the present invention (corresponding to dst_img with a blur radius of 40);
[0042] Figure 6 Blurred image effect diagram after mean blur processing in the document image processing method of the present invention (blur_img, blur radius 120);
[0043] Figure 7 The image effect diagram after the image division operation in the document image processing method of the present invention (corresponding to dst_img with a blur radius of 120). DETAILED DESCRIPTION
[0044] To make the purpose, technical solutions and advantages of the embodiments of the present invention more clear, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them.
[0045] Therefore, the following detailed description of the embodiments of the present invention is not intended to limit the scope of the claimed invention, but merely represents some embodiments of the present invention. All other embodiments derived by persons of ordinary skill in the art based on the embodiments of the present invention without creative effort shall fall within the scope of protection of the present invention.
[0046] It should be noted that, in the absence of conflict, the embodiments of the present invention and the features and technical solutions therein may be combined with each other.
[0047] It should be noted that similar reference numerals and letters denote similar items in the following drawings, and therefore, once an item is defined in one drawing, it does not need to be further defined or explained in subsequent drawings.
[0048] In the description of the present invention, it should be noted that the terms "upper" and "lower" and the like indicate orientations or positional relationships based on the orientations or positional relationships shown in the accompanying drawings, or the orientations or positional relationships in which the inventive product is typically placed when in use, or the orientations or positional relationships commonly understood by those skilled in the art. Such terms are intended solely to facilitate the description of the present invention and simplify the description, and are not intended to indicate or imply that the device or element referred to must have a specific orientation, be constructed, or operate in a specific orientation. Therefore, they should not be construed as limitations on the present invention. Furthermore, the terms "first" and "second" and the like are used solely for distinction and should not be construed as indicating or implying relative importance.
[0049] See also Figure 1 As shown, this embodiment provides a method for processing a document image, including:
[0050] Copy the original document image to obtain a document copy image;
[0051] The document copy image is processed by the mean fuzzy algorithm to obtain a fuzzy image;
[0052] A calculation image is obtained by dividing the original document image and the blurred image.
[0053] The main principle of the document image processing method of this embodiment is similar to target extraction, treating the text as the foreground, dividing the original image and the blurred image, and expanding the quotient value to 256 grayscale values. The background is basically white and the document text is basically black, obtaining a document image with a black and white effect.
[0054] The document image processing method of this embodiment is mainly aimed at the image characteristics of the document image itself. The document image is generally simple in color, and users are more concerned about the document content in the document image. Therefore, the basic properties of the fraction are utilized: the numerator remains unchanged, the denominator becomes larger, and the fraction becomes smaller. Through a simple image division operation, the quotient is expanded to 256 grayscale values. The pixels with small grayscale values are smaller, and the pixels with large grayscale values are larger. Therefore, the grayscale value gap between the text foreground part and the white background part of the document image is widened, and the document content in the document image can be extracted to obtain a document image with a black and white effect. Compared with the binarization algorithm processing, the algorithm is simpler, less difficult to implement, and the document presentation effect is better.
[0055] The document image processing method of this embodiment mainly focuses on extracting the text part in the document image, and needs to increase the grayscale value difference between the text foreground part and the white background part. The local shadows in the image have little effect on the image processing effect. Therefore, the document image processing method of this embodiment is more applicable to document images.
[0056] The document image processing method of this embodiment performs mean filtering and blurring on the document copy image. The large-radius mean blurring can make the grayscale value of low-grayscale text larger and the grayscale value of high-grayscale background smaller. The radius parameter affects the final black edge effect. The larger the radius, the thicker the final text or edge will be, and the smaller the radius, the thinner it will be and may even cause broken strokes. Therefore, it is necessary to combine the appropriate proportions or set the controllable parameters according to the specific project.
[0057] Furthermore, in this embodiment, performing an image division operation on the original document image and the blurred image to obtain a calculated image includes:
[0058] Division operations are performed on the grayscale values of pixels at corresponding coordinates of the original document image and the blurred image, and the quotients are rounded. The quotients greater than the maximum set grayscale value Hmax are taken as Hmax, and the maximum set grayscale value Hmax≤255. The quotients less than the minimum set grayscale value Hmin are taken as Hmin, and 0≤Hmin<Hmax.
[0059] Since the original document image and the blurred image are of the same size, the grayscale values of the pixels at the corresponding coordinates of the original document image and the blurred image are divided separately. Because after the original image is blurred with a large radius, the small grayscale values become larger and the large grayscale values become smaller. When dividing, the pixels with small grayscale values become smaller and the pixels with large grayscale values become larger, thereby widening the grayscale value between the text foreground and the white background of the document image, and obtaining a document image with a black and white effect.
[0060] As an optional implementation scheme of this embodiment, the calculation formula for performing division operations on the grayscale values of pixels at each corresponding coordinate of the original document image and the blurred image is: dst_img_0 = min(src_img / (blur_img+k)*Hmax, Hmax), src_img / (blur_img+k)*Hmax and Hmax take the smaller value; wherein, src_img is the original document image, blur_img is the blurred image, dst_img_0 is the calculated image, and k is the set value.
[0061] In the calculation formula of this embodiment, the dividend blur_img is added with k to prevent the dividend from being 0. The k is a positive integer, which can be 1, 2, 3, etc. In this embodiment, the quotient and Hmax are minimized to ensure that dst_img_0 does not cross the grayscale. Within the 256 grayscales, Hmax can be set to 255.
[0062] Furthermore, in a document image processing method of this embodiment, linear mapping is performed on the calculated image to obtain a black and white document image. This method uses a linear mapping algorithm to darken the text and brighten the background to obtain a black and white document image. This effectively avoids the problems of unsightly text in the image after binarization processing, such as jagged edges and sticking.
[0063] Specifically, the linear mapping of the calculated image in this embodiment includes:
[0064] A first grayscale value H1 and a second grayscale value H2 are preset, wherein the first grayscale value H1 is smaller than the second grayscale value H2, the linear mapping is preset to [H3, H4], H3 and H4 are preset grayscale values, and H1≤H3<H2≤H4.
[0065] The pixels in the result image that are greater than the second grayscale value H2 are set to H4, the pixels in the result image that are less than the first grayscale value H1 are set to H3, and the pixels in the result image in the interval [H1, H2] are linearly mapped to [H3, H4].
[0066] Optionally, H3 of this embodiment corresponds to a black effect and can be set to 0; H4 of this embodiment corresponds to a white effect and can be set to 255. In this embodiment, the first grayscale value H1 is ≥ 200, and the second grayscale value H2 is ≤ 240. In this embodiment, [H1, H2] can optionally be [200, 230].
[0067] This embodiment performs linear mapping on the calculated image, and the resulting image is visually distinct in black and white, with a small amount of grayscale values in between, making the text smoother and more beautiful. This can effectively avoid the problem of the text in the image being less beautiful after being processed by the binarization algorithm, and the problems of jagged edges and adhesion of the text.
[0068] In this embodiment, there may be multiple linear mapping schemes, such as level adjustment, curve adjustment, etc.
[0069] The implementation of color level adjustment is: when the input value is less than the black point value, all are converted to the black value of the output color level; when the input value is greater than the white point, all are converted to the white value of the output color level; when the input value is between the black value and the white value, it is combined with the grayscale coefficient and recalculated proportionally to become a new value.
[0070] The document image processing method of this embodiment uses a binarization algorithm to binarize the computed image to produce a binary image suitable for thermal printing. Image binarization is the process of setting the grayscale values of pixels in an image to 0 or 255, effectively rendering the entire image in distinct black and white. Specifically, the OSTU method can be used for binarization.
[0071] The OSTU method, also known as the maximum inter-class difference method, automatically selects the global threshold T by statistically analyzing the histogram characteristics of the entire image. The algorithm steps are as follows:
[0072] 1) First calculate the histogram of the image, that is, all the pixels of the image are divided into 256 bins from 0 to 255, and the number of pixels falling in each bin is counted;
[0073] 2) Normalize the histogram, that is, divide the number of pixels in each bin by the total number of pixels;
[0074] 3) i represents the classification threshold, that is, a gray level, and iterates from 0;
[0075] 4) Using the normalized histogram, count the proportion w0 of pixels with grayscale values of 0 to i (assuming that pixels with grayscale values in this range are called foreground pixels) in the entire image, and count the average grayscale of foreground pixels u0; count the proportion w1 of pixels with grayscale values of i to 255 (assuming that pixels with grayscale values in this range are called background pixels) in the entire image, and count the average grayscale of background pixels u1;
[0076] 5) Calculate the variance of foreground pixels and background pixels g = w0*w1*(u0-u1)(u0-u1);
[0077] 6) i++; Go to 4) and end the iteration when i reaches 256;
[0078] 7) The i value corresponding to the maximum g is used as the global threshold of the image.
[0079] Other image binarization algorithms such as global threshold, local threshold, dynamic threshold, Niblack, P-quantile, iteration, and entropy methods may also be applicable.
[0080] The document copy image is subjected to mean filtering blurring, and the radius parameter can be set according to the specific situation. The blur radius of the mean blurring algorithm in the blurred image obtained by processing the document copy image by the mean blurring algorithm is set to [1 / 200, 1 / 30] of the smallest side of the actual image.
[0081] Optionally, the blur radius is set to [1 / 130, 1 / 30] of the smallest side of the actual image.
[0082] Mean blurring literally takes the average of the surrounding pixels and assigns that value to the central pixel. This embodiment achieves the goal of making smaller grayscale values larger and larger values smaller by using mean blurring. This way, when performing division, pixels with smaller grayscale values become smaller, while pixels with larger grayscale values become larger. This widens the grayscale gap between the foreground text and the white background of the document image, resulting in a black and white document image.
[0083] As an optional implementation of this embodiment, the copying of the original document image described in this embodiment, before obtaining the copied document image, includes grayscale processing the acquired document image to obtain the original document image. In this embodiment, grayscale processing of the document image to obtain the grayscale image has no impact on the processing of the document content of the document image, reduces the processing difficulty of subsequent algorithms for the image, simplifies the algorithms, and improves the image processing rate.
[0084] In order to ensure that the final text is large enough, the original document image collected is generally large. In order to speed up the generation of background images, the document copy image is compressed to reduce the resolution of the image and the difficulty of the subsequent image processing algorithm, making image processing simpler and faster.
[0085] The compressed image is subjected to mean blurring to obtain a blurred image. Since the blurred image is not the same size as the original document image due to image compression, the blurred image needs to be restored to the same size as the original document image before performing the image division operation.
[0086] This embodiment uses an interpolation magnification algorithm to enlarge the blurred image to the size of the original document.
[0087] Image magnification operations require the establishment of a mapping rule (x′, y′) = T(x, y) between the source image and the target image, so that a correspondence is established between the pixel coordinates of the two images, thereby assigning a value to each pixel of the target image.
[0088] Mapping from a source image to a target image is called forward mapping, but this mapping method can present two problems: several pixels in the source image map to the same location in the target image; and some locations in the target image lack pixels to be assigned. This requires considering how to convert multiple output values into a single output value and how to assign values to pixels in the target image that are not mapped. Backward mapping is often used, which is more efficient than forward mapping. It maps from the target image to the source image, i.e., (x, y) = (x′, y) / T. This avoids the problems of forward mapping, but inevitably results in some pixels having floating-point coordinates when mapped back to the source image, while the image's grayscale values are defined only at integer coordinates. In this case, image interpolation methods must be used to assign values to these pixels.
[0089] Commonly used interpolation algorithms include: nearest neighbor interpolation, bilinear interpolation, bicubic interpolation method and so on.
[0090] The nearest neighbor interpolation algorithm is also called zero-order interpolation, which makes the grayscale value of the transformed pixel equal to the grayscale value of the input pixel closest to it.
[0091] The bilinear interpolation algorithm, also known as bilinear interpolation, calculates the source image coordinates (x*m / m'y*n / n') corresponding to the coordinates of point Im(xy) in the target image after scaling up or down several times. These coordinates are typically floating-point numbers. For point P, this is represented by P(i_x+u_xi_y+u_y), where i_x and i_y represent the integer portion and u_x and u_y represent the fractional portion. The grayscale value of point P is calculated based on the linear relationship between the grayscale values of its four neighboring points. That is, the grayscale value of point P is determined by these four neighboring points. The closer the point is to P, the greater its influence factor, and vice versa. For point (i_x i_y), the larger the u_x value in the x direction, the smaller its influence factor, while the larger the u_y value in the i_y direction, the smaller its influence factor. Therefore, the influence value of point (i_x i_y) is Im(i_x i_y)*(1-u_x)*(1-u_y). Similar results apply to the remaining three points. The gray value calculation formula of point P is: P(x, y)=(1-u_x)*(1-u_y)*Im(i_x i_y)+(1-u_x)*u_y*Im(i_x i_y+1)+u_x*(1-u_y)*Im(i_x+1i_y)+u_x*u_y*Im(i_x+1i_y+1).
[0092] Bicubic interpolation, also known as cubic convolution interpolation, uses the grayscale values of 16 points surrounding P to perform cubic interpolation, achieving an amplification effect closer to that of a high-resolution image. This approach also results in a significant increase in computational complexity. This algorithm requires selecting interpolation basis functions to fit the data. The most commonly used interpolation basis functions are expressed as follows: y(x) = 1-2|x|2+x3|x|<1 4-8|x|+5|x|2-|x|3 1<|x|<2 0|x|>2. Similarly, point P represents the source image coordinates (x*m / m'y*n / n') corresponding to the coordinates of point Im(xy) in the target image after several times of magnification or reduction. These coordinates are typically floating-point numbers. For example, point P is represented by P(i_x+u_x i_y+u_y), where i_x and i_y represent the integer portion, and u_x and u_y represent the decimal portion. The coefficients for the 16 points surrounding P are calculated separately and weighted to obtain the grayscale value of point P. Next, calculate the row and column coefficients for each coordinate point: The distances from point P on the four rows along the X-axis are 1+u_x, u_x, 1-u_x, and 2-u_x, respectively. The distances from point P on the four columns along the Y-axis are 1+u_y, u_y, 1-u_y, and 2-u_y, respectively. Using the interpolation basis function, we obtain the row coefficient for point Im(i_x i_y) as y(1+u_x) and the column coefficient as y(1+u_y). The coefficient for this point is K00 = y(1+u_x)*y(1+u_y). Similar calculations are performed for the remaining points.
[0093] The following describes the specific steps of the document image processing method of this embodiment in conjunction with the effect diagram of the image:
[0094] 1. Collect document images and convert them to grayscale. The resulting image is denoted as src_img
[0095] The hardware device captures and scans the document to form a document image, and the image is grayscaled [0-255]. The resulting image is set as src_img.
[0096] 2. Copy the original image, and the result image is recorded as copy_img
[0097] Make a copy of the image src_img and set the resulting image to copy_img.
[0098] 3. Perform mean filter blur on copy_img. The radius parameter can be set according to the specific situation, such as [1 / 130, 1 / 30] of the smallest side of the actual image. The result is recorded as blur_img
[0099] Apply mean filtering to blur copy_img. A large radius mean blur can make the grayscale value of low-grayscale text larger and the grayscale value of high-grayscale background smaller. The radius parameter affects the final black edge effect. The larger the radius, the thicker the final text or edge will be. The smaller the radius, the thinner it will be and the possibility of broken strokes may occur. Therefore, it is necessary to combine the appropriate ratio or set the controllable parameters according to the specific project. The final result image is defined as blur_img.
[0100] See also Figure 2 、 Figure 4 and Figure 6 As shown in FIG, the effects of blurred images corresponding to blur radius of 17, 40, and 120 are shown respectively.
[0101] 4. Divide src_img by the blurred image, the maximum quotient value does not exceed 1, and multiply it by 255 to expand it to a 256-level grayscale image. The result image is recorded as dst_img_0
[0102] The sizes of src_img and blur_img are the same, and the value of each coordinate is calculated using the following formula: dst_img_0 =
[0103] min(src_img / (blur_img + 1) * 255, 255). Add 1 to the dividend blur_img to prevent it from being zero, and minimize the quotient with 255 to ensure dst_img_0 stays within the 256 grayscale range. Because a large-radius blur on the original image increases grayscale values, smaller values become larger and larger values become smaller. When performing the division, assume the text grayscale value is 5. After the mean calculation, the value increases to 9, resulting in a final value of 128. The background grayscale value is 200, which decreases to 197, resulting in a final value of 255.
[0104] See also Figure 3 、 Figure 5 and Figure 7 As shown in FIG. 1 , the effect diagrams of the calculated images after image division operation is performed on blurred images with blur radius of 17, 40, and 120, respectively.
[0105] 5. Use linear mapping to adjust the resulting image, making the background white and the text foreground black. The resulting image is recorded as dst_imgdst_img_0. Due to the division calculation, the text foreground values are generally too large and not dark enough. Although the background is visually white, many values tend to be close to 255. This requires a similar operation to binarization. Set values greater than 230 to 255 and values less than 200 to 0. The values in the interval [200, 230] are linearly mapped to [0, 255]. The final result is a clear visual distinction between black and white, with a small amount of grayscale in between, making the text smoother and more beautiful. The resulting image shows the final output, locating dst_img.
[0106] The document image processed by the document image processing method of this embodiment has a better document presentation effect, a better text effect, and is less difficult to implement.
[0107] This embodiment also provides a document image processing device, including:
[0108] A copy module copies the original document image to obtain a document copy image;
[0109] Mean fuzzy algorithm module, which processes the document copy image through the mean fuzzy algorithm to obtain a fuzzy image;
[0110] The image operation module performs a division operation on the original document image and the blurred image to obtain an operation image.
[0111] The document image processing device of this embodiment is mainly implemented based on the image characteristics of the document image itself. Document images generally have simple colors, and users are more concerned about the document content in the document image. Therefore, after a simple image division operation, pixels with small grayscale values are made smaller, and pixels with large grayscale values are made larger, thereby widening the grayscale value between the text foreground part and the white background part of the document image, and the document content in the document image can be extracted to obtain a document image with a black and white effect. Compared with the binarization algorithm processing, the algorithm is simpler, less difficult to implement, and has a better document presentation effect.
[0112] The document image processing device of this embodiment mainly focuses on extracting the text part in the document image, and needs to increase the grayscale value difference between the foreground part of the text and the white background part. The local shadows in the image have little effect on the image processing effect. Therefore, the document image processing device of this embodiment is more applicable to document images.
[0113] The document image processing device of this embodiment performs mean filtering and blurring on the document copy image. The mean blurring with a large radius can make the grayscale value of low-grayscale text larger and the grayscale value of high-grayscale background smaller. The radius parameter affects the final black edge effect. The larger the radius, the thicker the final text or edge will be, and the smaller the radius, the thinner it will be and may even cause broken strokes. Therefore, it is necessary to combine the appropriate proportions or set the controllable parameters according to the specific project.
[0114] Furthermore, the image operation module of this embodiment performs a division operation on the original document image and the blurred image to obtain the operation image, including:
[0115] Division operations are performed on the grayscale values of pixels at corresponding coordinates of the original document image and the blurred image, and the quotients are rounded. The quotients greater than the maximum set grayscale value Hmax are taken as Hmax, and the maximum set grayscale value Hmax≤255. The quotients less than the minimum set grayscale value Hmin are taken as Hmin, and 0≤Hmin<Hmax.
[0116] Since the original document image and the blurred image are of the same size, the grayscale values of the pixels at the corresponding coordinates of the original document image and the blurred image are divided separately. Because after the original image is blurred with a large radius, the small grayscale values become larger and the large grayscale values become smaller. When dividing, the pixels with small grayscale values become smaller and the pixels with large grayscale values become larger, thereby widening the grayscale value between the text foreground and the white background of the document image, and obtaining a document image with a black and white effect.
[0117] As an optional implementation scheme of this embodiment, the calculation formula for performing division operations on the grayscale values of pixels at each corresponding coordinate of the original document image and the blurred image is: dst_img_0 = min(src_img / (blur_img+k)*Hmax, Hmax), src_img / (blur_img+k)*Hmax and Hmax take the smaller value; wherein, src_img is the original document image, blur_img is the blurred image, dst_img_0 is the calculated image, and k is the set value.
[0118] In the calculation formula of this embodiment, the dividend blur_img is added with k to prevent the dividend from being 0. The k is a positive integer, which can be 1, 2, 3, etc. In this embodiment, the quotient and Hmax are minimized to ensure that dst_img_0 does not cross the grayscale. Within the 256 grayscales, Hmax can be set to 255.
[0119] Furthermore, the document image processing device of this embodiment also includes a linear mapping module that performs linear mapping on the calculated image to obtain a black and white document image. The linear mapping module of the document image processing device of this embodiment uses a linear mapping algorithm to darken the text and brighten the background to obtain a black and white document image. This effectively avoids issues such as unsightly text in the image after binarization processing, such as jagged edges and sticking.
[0120] Specifically, the linear mapping module of this embodiment performs linear mapping on the calculated image, including:
[0121] A first grayscale value H1 and a second grayscale value H2 are preset, wherein the first grayscale value H1 is smaller than the second grayscale value H2, the linear mapping is preset to [H3, H4], H3 and H4 are preset grayscale values, and H1≤H3<H2≤H4.
[0122] The pixels in the result image that are greater than the second grayscale value H2 are set to H4, the pixels in the result image that are less than the first grayscale value H1 are set to H3, and the pixels in the result image in the interval [H1, H2] are linearly mapped to [H3, H4].
[0123] This embodiment performs linear mapping on the calculated image, and the resulting image is visually distinct in black and white, with a small amount of grayscale values in between, making the text smoother and more beautiful. This can effectively avoid the problem of the text in the image being less beautiful after being processed by the binarization algorithm, and the problems of jagged edges and adhesion of the text.
[0124] In this embodiment, there may be multiple linear mapping schemes, such as level adjustment, curve adjustment, etc.
[0125] The implementation of color level adjustment is: when the input value is less than the black point value, all are converted to the black value of the output color level; when the input value is greater than the white point, all are converted to the white value of the output color level; when the input value is between the black value and the white value, it is combined with the grayscale coefficient and recalculated proportionally to become a new value.
[0126] The document image processing device of this embodiment includes a binarization module. This module uses a binarization algorithm to perform binarization on the computed image, resulting in a binary image suitable for thermal printing. Image binarization is the process of setting the grayscale values of image pixels to 0 or 255, effectively rendering the entire image in distinct black and white. Specifically, the OSTU method can be used for binarization.
[0127] The OSTU method, also known as the maximum inter-class difference method, automatically selects the global threshold T by statistically analyzing the histogram characteristics of the entire image. The algorithm steps are as follows:
[0128] 1) First calculate the histogram of the image, that is, all the pixels of the image are divided into 256 bins from 0 to 255, and the number of pixels falling in each bin is counted;
[0129] 2) Normalize the histogram, that is, divide the number of pixels in each bin by the total number of pixels;
[0130] 3) i represents the classification threshold, that is, a gray level, and iterates from 0;
[0131] 4) Using the normalized histogram, count the proportion w0 of pixels with grayscale values of 0 to i (assuming that pixels with grayscale values in this range are called foreground pixels) in the entire image, and count the average grayscale of foreground pixels u0; count the proportion w1 of pixels with grayscale values of i to 255 (assuming that pixels with grayscale values in this range are called background pixels) in the entire image, and count the average grayscale of background pixels u1;
[0132] 5) Calculate the variance of foreground pixels and background pixels g = w0*w1*(u0-u1)(u0-u1);
[0133] 6) i++; Go to 4) and end the iteration when i reaches 256;
[0134] 7) The i value corresponding to the maximum g is used as the global threshold of the image.
[0135] Other image binarization algorithms such as global threshold, local threshold, dynamic threshold, Niblack, P-quantile, iteration, and entropy methods may also be applicable.
[0136] The mean fuzzy algorithm module of this embodiment performs mean filtering fuzziness on the document copy image. The radius parameter can be set according to the specific situation. The fuzzy radius of the mean fuzzy algorithm in the fuzzy image obtained by processing the document copy image through the mean fuzzy algorithm is set to [1 / 200, 1 / 30] of the smallest side of the actual image.
[0137] Optionally, the blur radius is set to [1 / 130, 1 / 30] of the smallest side of the actual image.
[0138] Mean blurring literally takes the average of the surrounding pixels and assigns that value to the central pixel. This embodiment achieves the goal of making smaller grayscale values larger and larger values smaller by using mean blurring. This way, when performing division, pixels with smaller grayscale values become smaller, while pixels with larger grayscale values become larger. This widens the grayscale gap between the foreground text and the white background of the document image, resulting in a black and white document image.
[0139] As an optional implementation of this embodiment, the document image processing device of this embodiment includes a grayscale processing module. Before copying the original document image to obtain the copied document image, the grayscale processing module performs grayscale processing on the acquired document image to obtain the original document image. In this embodiment, grayscale processing of the document image by the grayscale processing module to obtain the grayscale image has no impact on the processing of the document content of the document image, reduces the processing difficulty of subsequent algorithms for the image, simplifies the algorithms, and improves the image processing rate.
[0140] In order to ensure that the final text is large enough, the collected original document image is generally large. In order to speed up the background image generation speed, the document image processing device of this embodiment includes a compression module. The compression module compresses the document copy image to reduce the resolution of the image and the difficulty of the subsequent image processing algorithm. The image processing is simpler and the processing speed is faster.
[0141] The mean blur algorithm module performs mean blurring on the compressed image to produce a blurred image. Since the compression module performs image compression, the blurred image is not the same size as the original document image. Therefore, the blurred image needs to be restored to the same size as the original document image before performing the image division operation.
[0142] The document image processing device of this embodiment includes an interpolation magnification module, which magnifies the blurred image to the size of the original document image through an interpolation magnification algorithm.
[0143] Image magnification operations require the establishment of a mapping rule (x′, y′) = T(x, y) between the source image and the target image, so that a correspondence is established between the pixel coordinates of the two images, thereby assigning a value to each pixel of the target image.
[0144] Mapping from a source image to a target image is called forward mapping, but this mapping method can present two problems: several pixels in the source image map to the same location in the target image; and some locations in the target image lack pixels to be assigned. This requires considering how to convert multiple output values into a single output value and how to assign values to pixels in the target image that are not mapped. Backward mapping is often used, which is more efficient than forward mapping. It maps from the target image to the source image, i.e., (x, y) = (x′, y) / T. This avoids the problems of forward mapping, but inevitably results in some pixels having floating-point coordinates when mapped back to the source image, while the image's grayscale values are defined only at integer coordinates. In this case, image interpolation methods must be used to assign values to these pixels.
[0145] Commonly used interpolation algorithms include: nearest neighbor interpolation, bilinear interpolation, bicubic interpolation method and so on.
[0146] The nearest neighbor interpolation algorithm is also called zero-order interpolation, which makes the grayscale value of the transformed pixel equal to the grayscale value of the input pixel closest to it.
[0147] The bilinear interpolation algorithm, also known as bilinear interpolation, calculates the source image coordinates (x*m / m'y*n / n') corresponding to the coordinates of point Im(xy) in the target image after scaling up or down several times. These coordinates are typically floating-point numbers. For point P, this is represented by P(i_x+u_xi_y+u_y), where i_x and i_y represent the integer portion and u_x and u_y represent the fractional portion. The grayscale value of point P is calculated based on the linear relationship between the grayscale values of its four neighboring points. That is, the grayscale value of point P is determined by these four neighboring points. The closer the point is to P, the greater its influence factor, and vice versa. For point (i_x i_y), the larger the u_x value in the x direction, the smaller its influence factor, while the larger the u_y value in the i_y direction, the smaller its influence factor. Therefore, the influence value of point (i_x i_y) is Im(i_x i_y)*(1-u_x)*(1-u_y). Similar results apply to the remaining three points. The gray value calculation formula of point P is: P(x, y)=(1-u_x)*(1-u_y)*Im(i_x i_y)+(1-u_x)*u_y*Im(i_x i_y+1)+u_x*(1-u_y)*Im(i_x+1i_y)+u_x*u_y*Im(i_x+1i_y+1).
[0148] Bicubic interpolation, also known as cubic convolution interpolation, uses the grayscale values of 16 points surrounding P to perform cubic interpolation, achieving an amplification effect closer to that of a high-resolution image. This approach also results in a significant increase in computational complexity. This algorithm requires selecting interpolation basis functions to fit the data. The most commonly used interpolation basis functions are expressed as follows: y(x) = 1-2|x|2+x3|x|<1 4-8|x|+5|x|2-|x|3 1<|x|<2 0|x|>2. Similarly, point P represents the source image coordinates (x*m / m'y*n / n') corresponding to the coordinates of point Im(xy) in the target image after several times of magnification or reduction. These coordinates are typically floating-point numbers. For example, point P is represented by P(i_x+u_x i_y+u_y), where i_x and i_y represent the integer portion, and u_x and u_y represent the decimal portion. The coefficients for the 16 points surrounding P are calculated separately and weighted to obtain the grayscale value of point P. Next, calculate the row and column coefficients for each coordinate point: The distances from point P on the four rows along the X-axis are 1+u_x, u_x, 1-u_x, and 2-u_x, respectively. The distances from point P on the four columns along the Y-axis are 1+u_y, u_y, 1-u_y, and 2-u_y, respectively. Using the interpolation basis function, we obtain the row coefficient for point Im(i_x i_y) as y(1+u_x) and the column coefficient as y(1+u_y). The coefficient for this point is K00 = y(1+u_x)*y(1+u_y). Similar calculations are performed for the remaining points.
[0149] The following describes the specific steps of the document image processing device of this embodiment in conjunction with the effect diagram of the image:
[0150] 1. Collect document images and convert them to grayscale. The resulting image is denoted as src_img
[0151] The hardware device captures and scans the document to form a document image, and the image is grayscaled [0-255]. The resulting image is set as src_img.
[0152] 2. Copy the original image, and the result image is recorded as copy_img
[0153] Make a copy of the image src_img and set the resulting image to copy_img.
[0154] 3. Perform mean filter blur on copy_img. The radius parameter can be set according to the specific situation, such as [1 / 130, 1 / 30] of the smallest side of the actual image. The result is recorded as blur_img
[0155] Apply mean filtering to blur copy_img. A large radius mean blur can make the grayscale value of low-grayscale text larger and the grayscale value of high-grayscale background smaller. The radius parameter affects the final black edge effect. The larger the radius, the thicker the final text or edge will be. The smaller the radius, the thinner it will be and the possibility of broken strokes may occur. Therefore, it is necessary to combine the appropriate ratio or set the controllable parameters according to the specific project. The final result image is defined as blur_img.
[0156] See also Figure 2 、 Figure 4 and Figure 6 As shown in FIG, the effects of blurred images corresponding to blur radius of 17, 40, and 120 are shown respectively.
[0157] 4. Divide src_img by the blurred image, the maximum quotient value does not exceed 1, and multiply it by 255 to expand it to a 256-level grayscale image. The result image is recorded as dst_img_0
[0158] src_img and blur_img are the same size, and the value of each coordinate is calculated using the following formula: dst_img_0 = min(src_img / (blur_img + 1) * 255, 255). The dividend, blur_img, is added by 1 to prevent the dividend from being 0, and the quotient is minimized by 255 to ensure that dst_img_0 does not exceed the grayscale of 256. Because a large-radius blur on the original image increases grayscale values, small values become larger and large values become smaller. When performing the division, assuming the text grayscale value is 5, the average value increases to 9, resulting in a final value of 128. The background grayscale value is 200, which decreases to 197, resulting in a final value of 255.
[0159] 5. Use linear mapping to adjust the resulting image, making the background white and the text foreground black. The resulting image is recorded as dst_imgdst_img_0. Due to the division calculation, the text foreground values are generally too large and not dark enough. While the background is visually white, many values tend to be close to 255. This requires a similar binarization operation. Set values greater than 230 to 255 and values less than 200 to 0. The values in the interval [200, 230] are linearly mapped to [0, 255]. The final result is a clear visual distinction between black and white, with a small amount of grayscale in between, making the text smoother and more beautiful. The resulting image shows the final output, locating dst_img.
[0160] See also Figure 3 、 Figure 5 and Figure 7 As shown in FIG. 1 , the effect diagrams of the calculated images after image division operation is performed on blurred images with blur radius of 17, 40, and 120, respectively.
[0161] The document image processed by the document image processing device of this embodiment has a better document presentation effect, a better text effect, and is less difficult to implement.
[0162] This embodiment also provides an electronic device, including a processor and a memory, wherein the memory is used to store a computer executable program. When the computer program is executed by the processor, the processor executes the document image processing method.
[0163] This embodiment also provides a computer-readable medium storing a computer-executable program, wherein when the computer-executable program is executed, the document image processing method is implemented.
[0164] Through the above description of the implementation mode, it is easy for those skilled in the art to understand that the present invention can be implemented by hardware capable of executing a specific computer program, such as the system of the present invention, and the electronic processing unit, server, client, mobile phone, control unit, processor, etc. contained in the system. The present invention can also be implemented by computer software that executes the method of the present invention, such as control software executed by a microprocessor, an electronic control unit, a client, a server, etc. However, it should be noted that the computer software that executes the method of the present invention is not limited to being executed by one or a specific hardware entity, and it can also be implemented in a distributed manner by unspecified specific hardware. For computer software, the software product can be stored in a computer-readable storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.), or it can be distributed and stored on a network, as long as it enables an electronic device to execute the method according to the present invention.
[0165] The above embodiments are only used to illustrate the present invention and are not intended to limit the technical solutions described in the present invention. Although this specification has described the present invention in detail with reference to the above embodiments, the present invention is not limited to the above specific implementation methods. Therefore, any modification or equivalent replacement of the present invention; and all technical solutions and improvements thereof that do not depart from the spirit and scope of the invention are included in the scope of the claims of the present invention.
Claims
1. A method for processing black and white effects of a document image, characterized in that: include: Copy the original document image to obtain a document copy image; The document copy image is processed by the mean fuzzy algorithm to obtain a fuzzy image; Performing a division operation on the original document image and the blurred image to obtain a calculated image; The performing an image division operation on the original document image and the blurred image to obtain the calculated image comprises: Performing a division operation on the grayscale values of pixels at corresponding coordinates of the original document image and the blurred image, rounding the quotients, taking Hmax as the integer, and taking Hmin as the integer. The quotients greater than the maximum set grayscale value Hmax are considered to be Hmax, where the maximum set grayscale value Hmax is ≤ 255, and taking Hmin as the integer, where 0 is ≤ Hmin < Hmax. The calculation formula for dividing the grayscale values of the pixels at the corresponding coordinates of the original document image and the blurred image is: dst_img_0 = min(src_img / (blur_img + k) * Hmax, Hmax), which means that the smaller value of src_img / (blur_img + k) * Hmax or Hmax is taken; Wherein, src_img is the original document image, blur_img is the blurred image, dst_img_0 is the calculated image, and k is the set value; Perform linear mapping on the operation image to obtain a document image with black and white effect; The linear mapping of the operation image comprises: A first grayscale value H1 and a second grayscale value H2 are preset, wherein the first grayscale value H1 is smaller than the second grayscale value H2, the linear mapping is preset as [H3, H4], H3 and H4 are preset grayscale values, and H1≤H3<H2≤H4; The pixels in the result image that are greater than the second grayscale value H2 are set to H4, the pixels in the result image that are less than the first grayscale value H1 are set to H3, and the pixels in the result image in the interval [H1, H2] are linearly mapped to [H3, H4].
2. The method for processing black and white effects of a document image according to claim 1, characterized in that: The operation image can be processed into a binary image by a binarization algorithm.
3. The method for processing black and white effects of a document image according to claim 1, characterized in that: The document copy image is processed by the mean blur algorithm to obtain a blurred image, and the blur radius of the mean blur algorithm is set to [1 / 200, 1 / 30] of the smallest side of the actual image.
4. The method for processing black and white effects of a document image according to claim 3, characterized in that: The blur radius is set to [1 / 130, 1 / 30] of the smallest side of the actual image.
5. A device for processing black and white effects of document images, characterized in that: include: A copy module copies the original document image to obtain a document copy image; Mean fuzzy algorithm module, which processes the document copy image through the mean fuzzy algorithm to obtain a fuzzy image; An image operation module performs a division operation on the original document image and the blurred image to obtain an operation image; The performing an image division operation on the original document image and the blurred image to obtain the calculated image comprises: Performing a division operation on the grayscale values of pixels at corresponding coordinates of the original document image and the blurred image, rounding the quotients, taking Hmax as the integer, and taking Hmin as the integer. The quotients greater than the maximum set grayscale value Hmax are considered to be Hmax, where the maximum set grayscale value Hmax is ≤ 255, and taking Hmin as the integer, where 0 is ≤ Hmin < Hmax. The calculation formula for dividing the grayscale values of the pixels at the corresponding coordinates of the original document image and the blurred image is: dst_img_0 = min(src_img / (blur_img + k) * Hmax, Hmax), which means that the smaller value of src_img / (blur_img + k) * Hmax or Hmax is taken; Wherein, src_img is the original document image, blur_img is the blurred image, dst_img_0 is the calculated image, and k is the set value; Perform linear mapping on the operation image to obtain a document image with black and white effect; The linear mapping of the calculated image includes: presetting a first grayscale value H1 and a second grayscale value H2, wherein the first grayscale value H1 is less than the second grayscale value H2, and the linear mapping is preset as [H3, H4], wherein H3 and H4 are preset grayscale values, and H1≤H3<H2≤H4; The pixels in the result image that are greater than the second grayscale value H2 are set to H4, the pixels in the result image that are less than the first grayscale value H1 are set to H3, and the pixels in the result image in the interval [H1, H2] are linearly mapped to [H3, H4].
6. An electronic device comprising a processor and a memory, wherein the memory is used to store a computer executable program, wherein: When the computer program is executed by the processor, the processor executes a method for processing black and white effects of a document image according to any one of claims 1 to 4.
7. A computer-readable medium storing a computer-executable program, characterized in that: When the computer executable program is executed by a processor, a method for processing black and white effects of a document image according to any one of claims 1 to 4 is implemented.
Citation Information
Patent Citations
Color image quick processing method based on intelligent terminal
CN111815545A