Method for removing watermark from portable document format, computer equipment and storage medium
Through optical character recognition and clustering analysis technology, the watermark area in PDF documents is determined and removed, and dynamically filled according to the RGB value of surrounding pixel points is solved, and the watermark removal and image repair effect in the prior art is achieved, and efficient and accurate watermark removal and image repair are achieved.
Patent Information
- Application Number
- CN202411799275.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-09
- Publication Date
- 2025-02-28
- Estimated Expiration
- 2044-12-09
AI Technical Summary
The prior art cannot generalize the removal of different types of watermarks, especially poor compatibility in text, colors and regions, and lacks efficient methods for watermark area detection and image repair.
The character bounding box in the document is obtained through optical character recognition technology and the watermark area is determined based on specific conditions. Then, the watermark pixel points are identified using clustering analysis techniques and dynamically filled based on the RGB values of the surrounding non-watermark pixel points to repair the image.
It realizes general removal and image repair of various watermarks, improves the efficiency of watermark area detection, can accurately locate the watermark area, and maintains picture quality during the repair process.
Smart Images

Figure 00000000_0000_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of watermark removal, and in particular relates to a portable document format (PDF) document watermark removal method, computer equipment and storage medium. Background Art
[0002] Although there are mature technologies for removing watermarks, there is no universal solution to remove various watermarks, including different texts, different colors, and different areas.
[0003] The current technical solutions all use preset values to determine the watermark text, watermark color, watermark color fill value, and watermark area. In terms of watermark area detection, the more advanced technology is to train a watermark area detection model through a model, but for watermarks in different industries, it takes a lot of cost to train a watermark area detection model; in terms of watermark text, watermark color, and watermark fill value, they are still implemented through preset values.
[0004] In summary, none of the existing technologies is a universal watermark removal method, and they cannot be well compatible with different texts and colors. In other words, the existing technologies may perform well on the data they process, but fail on data sets that they do not process. There is no efficient solution to detect the watermark area in terms of watermark detection. In addition, in terms of image restoration, the watermark area is filled with red, green and blue (RGB) preset values, which will erase useful fonts in the image, or the watermark color is roughly replaced with white or preset RGB values, resulting in poor image restoration effects. Summary of the invention
[0005] In view of the above problems, the present invention provides a PDF document watermark removal method, a computer device and a storage medium.
[0006] The portable file format document watermark removal method provided by the present invention comprises the following steps:
[0007] S1, obtaining each character boundary box and text in the portable document format document according to optical character recognition technology, and then determining which character boundary boxes belong to the watermark area according to relevant information of the character boundary boxes;
[0008] S2, performing cluster analysis on the pixels in each watermark area according to the red, green and blue values to obtain the pixels in each watermark;
[0009] S3, watermark removal and image restoration.
[0010] further,
[0011] In step S1,
[0012] If the width of the character boundary box is greater than 50% of the average width of the character boundary box, or the height of the character boundary box is greater than 50% of the average height of the character boundary box, then the character boundary box is deemed to belong to the watermark area.
[0013] further,
[0014] In step S1,
[0015] If the character in the character boundary box appears more than once, and the slopes of the two long sides of the character boundary box are between 30 degrees and 90 degrees, the character boundary box is determined to belong to the watermark area.
[0016] further,
[0017] In step S1,
[0018] If the characters in the character boundary box hit the keywords in the keyword list, it is determined that the character boundary box belongs to the watermark area.
[0019] further,
[0020] In step S1,
[0021] If the character boundary box CC appears at the same position on different document pages of the portable document format document, it is considered that the character boundary box CC belongs to the watermark area.
[0022] further,
[0023] In step S1,
[0024] Extend the two long sides of each character boundary box that is determined to belong to the watermark area so that the extended lines of the two long sides intersect with the edge of the portable file format document to obtain four intersection points, and finally count the largest area formed by the four intersection points as the watermark area.
[0025] further,
[0026] In step S2,
[0027] A clustering algorithm is used to set the number of categories, i.e., clusters, to 3, one for background color, one for watermark color, and one for normal text color. The values of the red, green, and blue channels are regarded as a three-dimensional coordinate (X, Y, Z), and the pixels clustered into the second largest category are the pixels in the watermark.
[0028] further,
[0029] Traverse all the pixels of the portable document format document, and when traversing to the pixel point m, first determine whether the pixel point m is a pixel point in the watermark, if it is not a pixel point in the watermark, directly put it into the pixel list A, and also put it into the list B; if the pixel point m is a pixel point in the watermark, obtain the last 100 pixel points from the list B as the target pixel points, if the elements in the list B are less than 100 pixel points, then take all the pixel points in the list B as the target pixel points, calculate the frequency of occurrence of the red, green and blue values of the target pixel points, replace the red, green and blue values of the pixel point m with the red, green and blue values of the target pixel point with the highest frequency, then put the pixel point m into the pixel list A, and finally update the pixel points in the pixel list A to the entire portable document format document.
[0030] The present invention also provides a computer device, which includes a memory, a first processor and a first computer program stored in the memory and executable on the first processor. When the first computer program is executed by the first processor, the above-mentioned portable file format document watermark removal method is implemented.
[0031] The present invention also provides a computer-readable storage medium, which is used to store a second computer program. The second computer program can be executed by at least one second processor to enable the at least one second processor to execute the above-mentioned portable file format document watermark removal method.
[0032] The PDF document watermark removal method provided by the present invention can be used for processing any watermark, successfully removes the watermark and repairs the image; the present invention can improve the efficiency of watermark area detection, does not need to train the watermark detection model in advance, and can also accurately locate the watermark area; compared with the original method that can only fill the watermark area with a preset value, the present invention can dynamically obtain the RGB value that should be filled in the watermark area according to the RGB values of non-watermark pixel points around the watermark, and can better perform image repair.
[0033] Other features and advantages of the present invention will be described in the following description, and partly become apparent from the description, or understood by practicing the present invention. The purpose and other advantages of the present invention can be realized and obtained by the structures pointed out in the description, claims and drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0035] Figure 1 A flow chart of a method for removing watermarks from a PDF document according to an embodiment of the present invention is shown;
[0036] Figure 2 An example of a watermark area obtained in the method for removing watermark from a PDF document according to an embodiment of the present invention is shown. DETAILED DESCRIPTION
[0037] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0038] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by technicians in the technical field of the present application; the terms used in the specification of the application herein are only for the purpose of describing specific embodiments and are not intended to limit the present application; the terms "including" and "having" in the specification and claims of the present application and the above-mentioned drawings and any variations thereof are intended to cover non-exclusive inclusions. The terms "first", "second", "third", etc. in the specification and claims of the present application or the above-mentioned drawings are used to distinguish different objects, not to describe a specific order or a primary and secondary relationship. "Multiple" appearing in this application refers to more than two (including two).
[0039] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0040] Figure 1 This is a flowchart of the PDF document watermark removal method provided by the present invention. Figure 1 , the PDF document watermark removal method includes the following steps.
[0041] S1. Obtain the bounding box and text of each character in a portable document format document according to optical character recognition technology, and then determine which character bounding boxes belong to the watermark area based on the relevant information of the character bounding boxes.
[0042] In this step, the bounding box of the characters in the PDF document (which can also be regarded as a PDF image) can be obtained through optical character recognition (OCR) technology such as PaddleOCR, and then the watermark area can be obtained. Among them, the PaddleOCR optical character recognition technology will recognize the rectangular area where the characters in the PDF document are located and give the coordinates of the four corners of the rectangular area. Connecting the coordinates of these four corners can obtain the character bounding box of the character.
[0043] The watermarks on the PDF image are divided into the following situations: 1. The watermark text is very large and is embedded in the image in a way that is inclined or horizontal relative to the bottom edge of the PDF image; 2. The watermark text is small-sized characters and is distributed throughout the image; 3. The watermark is in the lower left corner or lower right corner of the image. The present invention is compatible with these three situations through the following discrimination: If the width of the character bounding box is greater than 50% of the average width of the character bounding boxes, or the height of the character bounding box is greater than 50% of the average height of the character bounding boxes, then it is determined that this character bounding box belongs to the watermark area; if the characters in the character bounding box appear more than once, and the slopes of the two long sides of the character bounding box are between 30 degrees and 90 degrees, it is determined that this character bounding box belongs to the watermark area; if the characters in the character bounding box hit the keywords in the keyword list, such as "Dali Auto Repair" and "Auto Repair Treasure" in the auto repair field, then it is determined that this character bounding box belongs to the watermark area; if a certain character bounding box CC appears at the same position on more than one different page of the PDF document, then it is considered that this character bounding box CC belongs to the watermark area. Among them, the keyword list is composed of the key technical terms in the relevant technical field determined by technical personnel, such as the auto repair field. Among them, the average width and height of the character bounding boxes refer to the average width and height of all the character bounding boxes on the same PDF image. For example, if there are N character bounding boxes, then the average width of the character bounding boxes = the sum of the widths of N character bounding boxes / N; the average height of the character bounding boxes = the sum of the heights of N character bounding boxes / N.
[0044] Considering that the OCR technology may miss some character bounding boxes in the watermark area, therefore, this step further includes: extending the two long sides of each character bounding box determined to be in the watermark area so that the extension lines of these two long sides intersect with the edges of the PDF document image, obtaining 4 intersection points, and finally counting the largest area formed by these 4 intersection points as the watermark area. Figure 2 Shows an example of the watermark area obtained in the PDF document watermark removal method provided by the present invention. Figure 2For example, based on OCR technology, we can determine that the solid-line frame area is a watermark, and the text in it is: internal information. Then a part of the watermark area will be missed, and the text in it is: prohibited from dissemination. Then we can obtain the slopes of the two long sides of the solid-line frame by calculation, and extend the two long sides of the solid-line frame according to the slope until the extension lines of the two long sides intersect with the boundary of the PDF image. The entire area formed is the watermark area, and then only the watermark area is processed to remove the watermark. That is, if the pixel point is not in the watermark area, it is considered that the pixel point is a non-watermark area pixel point, and no processing is done. Otherwise, the watermark is removed.
[0045] S2. Perform cluster analysis on the pixels in each watermark area according to the RGB values to obtain the pixels in each watermark.
[0046] There are three elements in the watermark area cut out from the PDF document, namely: background, font, and watermark. Correspondingly, this step uses a clustering algorithm such as the K-means clustering algorithm, and sets the number of categories, i.e., clusters, to 3, i.e., K=3, one for background color, one for watermark color, and one for normal text color. Among them, the values of the RGB three channels are regarded as a three-dimensional coordinate (X, Y, Z), and the pixels clustered into the second largest category are the pixels in the watermark, that is, when clustering the pixel data, each pixel is assigned to a cluster, the number of pixels in each cluster is calculated, the clustering results are sorted by the number of pixels, the cluster of the second largest category is selected, and the pixels in this cluster are regarded as the pixels corresponding to the watermark.
[0047] S3, watermark removal and image restoration.
[0048] This step replaces the RGB value of the pixel in the watermark with the RGB value of the surrounding pixel, rather than directly replacing the RGB value of the pixel in the watermark with the preset value, so that the quality of the image restoration can be guaranteed. Since the RGB value of the watermark pixel has been obtained through step S2, this step specifically includes: traversing all the pixel points of the PDF image, when traversing to the pixel point m, first determine whether the pixel point m is a pixel point in the watermark, if it is not a pixel point in the watermark, directly put it into the pixel list A, and also put it into the list B, where the list B is specially used to store non-watermark pixels; if the pixel point m is a pixel point in the watermark, then get the last 100 pixel points from the list B as the target pixel points, if the elements in B are less than 100 pixel points, then get all the pixel points in B as the target pixel points, calculate the frequency of occurrence of these target pixel RGB values, replace the RGB value of the pixel point m with the RGB value of the target pixel point with the highest frequency, then put the pixel point m into the pixel list A, and finally update the pixel points in the list A to the entire image, and complete the removal of the watermark and the restoration of the image. Considering that the pixel traversal starts from the upper left corner of the PDF image, it is almost impossible for the watermark to cover the first point in the upper left corner. Therefore, usually there will be no value in list B. If such a situation really occurs, since the present invention is aimed at PDF documents and the background color is white, a (255, 255, 255) RGB value is initially stored in the default list B.
[0049] The present invention also provides a computer device, which includes a memory, a first processor and a first computer program stored in the memory and executable on the first processor. When the first computer program is executed by the first processor, the above-mentioned portable file format document watermark removal method is implemented.
[0050] The present invention also provides a computer-readable storage medium, which is used to store a second computer program. The second computer program can be executed by at least one second processor to enable the at least one second processor to execute the above-mentioned portable file format document watermark removal method.
[0051] The PDF document watermark removal method provided by the present invention can be used for processing any watermark, successfully removes the watermark and repairs the image; the present invention can improve the efficiency of watermark area detection, does not need to train the watermark detection model in advance, and can also accurately locate the watermark area; compared with the original method that can only fill the watermark area with a preset value, the present invention can dynamically obtain the RGB value that should be filled in the watermark area according to the RGB values of non-watermark pixel points around the watermark, and can better perform image repair.
[0052] Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent substitutions for some of the technical features therein; and these modifications or substitutions do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for removing watermarks from portable file formats, characterized in that: The following steps are involved: S1, obtaining each character boundary box and text in the portable document format document according to optical character recognition technology, and then determining which character boundary boxes belong to the watermark area according to relevant information of the character boundary boxes; S2, performing cluster analysis on the pixels in each watermark area according to the red, green and blue values to obtain the pixels in each watermark; S3, watermark removal and image restoration, In step S1, If the width of the character boundary box is greater than 50% of the average width of the character boundary box, or the height of the character boundary box is greater than 50% of the average height of the character boundary box, then the character boundary box is determined to belong to the watermark area, wherein the average width and height of the character boundary box refer to the average width and height of all character boundary boxes on the same portable document format document; In step S2, Using a clustering algorithm, the number of categories, i.e., clusters, is set to 3, one category represents the background color, one category represents the watermark color, and one category represents the normal text color, wherein the values of the red, green, and blue channels are regarded as a three-dimensional coordinate (X, Y, Z), and the pixels clustered into the second largest category are the pixels in the watermark, that is, when clustering the pixel data, each of the pixels is assigned to one of the clusters, the number of pixels in each cluster is calculated, the clustering results are sorted by the number of pixels, the cluster of the second largest category is selected, and the pixels in the cluster of the second largest category are regarded as the pixels corresponding to the watermark; In step S3, The red, green and blue values of the pixels in the watermark are replaced with the red, green and blue values of the surrounding pixels.
2. The portable document format document watermark removal method according to claim 1, characterized in that: In step S1, If the character in the character boundary box appears more than once, and the slopes of the two long sides of the character boundary box are between 30 degrees and 90 degrees, the character boundary box is determined to belong to the watermark area.
3. The method for removing watermarks from portable document format documents according to claim 1, characterized in that: In step S1, If the characters in the character boundary box hit the keywords in the keyword list, it is determined that the character boundary box belongs to the watermark area.
4. The portable document format document watermark removal method according to claim 1, characterized in that: In step S1, If the character boundary box appears at the same position on different document pages of the portable document format document, it is considered that the character boundary box belongs to the watermark area.
5. The method for removing watermarks from portable document format documents according to claim 1, characterized in that: In step S1, Extend the two long sides of each character boundary box that is determined to belong to the watermark area so that the extended lines of the two long sides intersect with the edge of the portable file format document to obtain four intersection points, and finally count the largest area formed by the four intersection points as the watermark area.
6. The method for removing watermarks from portable document formats according to any one of claims 1 to 5, characterized in that: Traverse all the pixels of the portable document format document, and when traversing to the pixel point m, first determine whether the pixel point m is a pixel point in the watermark, if it is not a pixel point in the watermark, directly put it into the pixel list A, and also put it into the list B; if the pixel point m is a pixel point in the watermark, obtain the last 100 pixel points from the list B as the target pixel points, if the elements in the list B are less than 100 pixel points, then take all the pixel points in the list B as the target pixel points, calculate the frequency of occurrence of the red, green and blue values of the target pixel points, replace the red, green and blue values of the pixel point m with the red, green and blue values of the target pixel point with the highest frequency, then put the pixel point m into the pixel list A, and finally update the pixel points in the pixel list A to the entire portable document format document.
7. A computer device, characterized in that: The invention comprises a memory, a first processor and a first computer program stored in the memory and executable on the first processor, wherein the first computer program, when executed by the first processor, implements the portable file format document watermark removal method described in any one of claims 1 to 6.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium is used to store a second computer program, and the second computer program can be executed by at least one second processor, so that the at least one second processor executes the portable document format document watermark removal method described in any one of claims 1-6.
Citation Information
Patent Citations
Watermark method capable of resisting printing scanning attack and based on character refinement
CN103761700A
Three-dimensional mesh model watermarking method using segmentation and apparatus thereof
CN107067361A