A method of cropping or erasing content according to the line in which the picture characters are located
Patent Information
- Application Number
- CN202410230443.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-02-29
- Publication Date
- 2026-10-27
- Estimated Expiration
- 2044-02-29
AI Technical Summary
[0003]本发明为解决目前裁剪图片需要人工采用截图软件处理,存在费时费力的问题,具体为一种根据图片字符所在行裁剪或抹除内容的方法
本发明所述的一种根据图片字符所在行裁剪或抹除内容的方法,该方法可以计算识别内容所在行,计算行在图片内的坐标,根据计算所得的坐标裁剪图片,可指定方向,即所在行向上裁剪或者向下裁剪,也可以根据坐标判断是否需要抹除内容而不是裁剪。相比现有对图片裁剪或抹除内容的方法能够实现根据字符的识别进行坐标计算从而进行裁剪或抹除。
Smart Images

Figure CN118155217B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image character recognition, specifically a method for cropping or erasing content based on the row where the image characters are located. Background Technology
[0002] In exam grading, it's necessary to review scanned and uploaded student answer sheets. These sheets contain sensitive information such as student names, so it's crucial to remove student names, exam numbers, and signatures from the images during grading. Current technology mostly relies on manual processing using screenshot software for image cropping, which is time-consuming, labor-intensive, and prone to errors. Therefore, developing a method to crop or erase content based on the line where the text in an image is located would have significant practical application value. Summary of the Invention
[0003] This invention addresses the problem that current image cropping requires manual processing using screenshot software, which is time-consuming and labor-intensive. Specifically, it provides a method for cropping or erasing content based on the row where the characters in an image are located.
[0004] This invention is implemented using the following technical solution: a method for cropping or erasing content based on the line where characters in an image are located, comprising the following steps: reading an image file, converting the image into a BufferedImage object, binarizing the BufferedImage object to change the pixel values in the image to 0 or 1, and then segmenting the binarized image into multiple connected regions; for each connected region, using the Tesseract character recognition algorithm to recognize characters and obtain the text content and character coordinate information of that region; traversing all connected regions and saving the text content and character coordinate information of each region into a result set; returning the result set to complete the recognition and coordinate acquisition of each character in the image; matching according to the required character, matching line by line from the recognized text content, calculating the line coordinates of the matched required character, and then cropping or erasing according to the requirements.
[0005] This invention primarily targets image character recognition, calculating the row where the character is located, calculating the row coordinates, and cropping or erasing the content of the image based on the coordinates.
[0006] Firstly, the image characters are recognized using Tesseract technology. By utilizing the font model, Tesseract can identify all the characters in the image and the coordinates of each character. However, it cannot determine the number of rows or the row coordinates. If cropping or erasing is performed based on individual characters, there will be too many identical characters in the same image. The main solution of this invention is to calculate the number of rows based on the character content and then determine the row coordinates.
[0007] The characters refer to glyphs or symbols, including letters, numbers, operators, punctuation marks, and other symbols, as well as some functional symbols; the images refer to exam papers obtained through scanning (usually as separate images one by one), where all the characters in a certain area constitute the text content of that area, and all the characters on each image constitute the text content of that image (image text).
[0008] Furthermore, during image binarization, the pixel values in the image are changed to 0 or 1. Each pixel in the image is scanned sequentially from left to right, forming a horizontal scan line. For each pixel, the position of its intersection with the current horizontal scan line is determined. Based on the position and direction of the intersection, it is determined whether the pixel intersects with the scan line. If the pixel intersects with the scan line, it is filled with a predetermined color (usually black or white, corresponding to 0 or 1 after binarization). The ordinate of the current pixel is recorded and set as the position of the next scan line. At the start of the next scan, the scan line is moved to the ordinate position recorded in the previous step, instead of continuing from the bottom of the previous line. The purpose of this is to continue processing the regions connected to the previous pixel, ensuring that the pixels in the connected regions are correctly filled. The above steps are repeated until all pixels have been processed.
[0009] Furthermore, after converting the image into a BufferedImage object, the BufferedImage object is used to determine its format. This is then compared with existing image formats to check if the format is correct. If incorrect, the current image format is modified.
[0010] If a character is misrecognized, the system will return the recognition result to remind the user that the character in that line was misrecognized. If the line is not used, the error will be ignored. If the line is used for cropping, the cropping can be performed according to the recognition result.
[0011] Furthermore, when calculating the line coordinates of the matched characters, the image text is first split according to " / n" to determine the characters in each line. Then, based on the coordinates of each character, all characters are put into a List. The line text is processed by toCharArray. To calculate the line coordinates, the character with the index of text length-1 is taken from the list, and the line coordinate is (0, the y-coordinate of the character).
[0012] The image text mentioned here refers to the collection of all characters on the entire image; the image text is divided into many lines according to the arrangement of characters, and each line constitutes a line of text.
[0013] Furthermore, the cropping includes upward cropping and downward cropping; the erasing includes upward erasing and downward erasing. This method uses the top-left corner of the image as the origin. Based on the coordinates of the obtained characters (x, y, width, height), for upward cropping, it is (0, y, width, height-y), meaning that a new image is cropped from the starting coordinate (0, y) according to the image width and height-y. Since the cropping is based on lines, x is 0 by default, and the same applies below. The downward cropping algorithm is (0, 0, width, y), meaning that a new image is cropped from the starting coordinate (0, 0) according to the image width and height-y. Upward erasure (0, 0, width, y) means that the image is erased upward from the starting coordinate (0, 0), with the erased content having a width of width and a height of y. Downward erasure (0, y, width, height-y) means that the image is erased downward from the starting coordinate (0, y), with the erased content having a width of width and a height of height-y.
[0014] Advantages compared to existing technologies: This invention discloses a method for cropping or erasing content based on the row where the image characters are located. This method calculates and identifies the row where the content is located, calculates the row's coordinates within the image, and crops the image based on the calculated coordinates. The direction can be specified, i.e., cropping upwards or downwards from the row. Alternatively, the method can determine whether to erase content instead of cropping based on the coordinates. Compared to existing methods for cropping or erasing image content, this method achieves cropping or erasing by calculating coordinates based on character recognition. Attached Figure Description
[0015] Figure 1 A flowchart of the method described in this invention.
[0016] Figure 2 The original image before cropping or erasing.
[0017] Figure 3 The image after being cropped upwards.
[0018] Figure 4 The image after being erased downwards. Detailed Implementation
[0019] A method for cropping or erasing content based on the row where characters are located in an image, compared to existing methods for cropping or erasing image content, can perform coordinate calculations based on character recognition and then perform cropping or erasing. The following are the technical features of this invention: 1. Obtaining the coordinates of each character in the image: Read the image file and convert it into a BufferedImage object. Binarize the BufferedImage object, changing the pixel values to 0 or 1. Then, segment the binarized image into multiple connected regions. For each connected region, use the Tesseract character recognition algorithm to identify characters, obtaining the text content and character coordinate information of that region. Traverse all connected regions, saving the text content and character coordinate information of each region to the result set. Return the result set, completing the recognition and coordinate acquisition of each character in the image.
[0020] 2. Cropping and erasing algorithm: Based on the obtained character coordinates (x, y, width, height), the upward cropping algorithm is (0, y, width, height-y). Since the cropping is based on the line, x is 0 by default. The downward cropping algorithm is (0, 0, width, y), the upward erasing algorithm is (0, 0, width, y), and the downward erasing algorithm is (0, y, width, height-y).
[0021] 3. Error handling: If the image format is incorrect, the image will be converted to the correct format based on the error message. We have encountered an issue where an image exception occurred after manually converting a JPG image to PNG; we have addressed this exception. If an error is detected, the system will return the detection result to remind the user that the character in the line was not detected. If no line break is needed, the error will be ignored. If a line break is used for cropping, the cropping will be performed according to the detection result.
[0022] When examiners are grading the papers, the examinee's identity information needs to be cropped out, leaving only the answer sheet to avoid interference with the grading process (to prevent cheating). When the exam papers are released in the future, the previously cropped papers need to be restored to their original state, and the part containing the examinee's information needs to be combined with the answer sheet. Therefore, the content below the binding line needs to be erased, and the examinee's information above the binding line needs to be retained. The image with the content below the binding line erased can be combined with the image remaining after cropping above the binding line to form a complete exam paper containing the examinee's identity information and the graded answer sheet.
[0023] This invention employs Tesseract technology for recognition. It utilizes a font model in conjunction with Tesseract to identify character content within an image. Characters are segmented into a list, and the row number is determined by the list index. The coordinates of the first and last characters of each row are used to derive the coordinates of that row. For upward cropping: the height of the image to be cropped is determined based on the row's y-axis coordinates, and the width is the image width. The image is then cropped according to the coordinates and the set width and height (0, y, width, height-y), where (0, y) represents cropping a new image starting from the initial coordinates (0, y) with the image width (width) and height (height-y). Figure 3 As shown.
[0024] Cropping downwards: This requires determining the height (y) of the image to be cropped based on the y-axis coordinates. The width is the image width. Then, according to the coordinates and the set width and height (0, 0, width, y), a new image is cropped and saved starting from the image's starting coordinates (0, 0) with the image width (width) and image height (y).
[0025] Upward erasure: The Y-axis coordinate is set as the starting point for erasure, with parameters (0, 0, width, y). This means that the erasure starts from the starting coordinate (0, 0) of the image and moves upward. The width of the erased content is width, and the height is y.
[0026] Downward erasure: This requires calculating the width of the image to be cropped by subtracting the Y-axis coordinate from the image height. The Y-axis coordinate should be set as the starting point for erasure, with parameters (0, y, width, height-y). This means erasing downwards from the image's starting coordinate (0, y), with the width of the erased content being `width` and the height being `height-y`. For example... Figure 4 As shown.
[0027] When announcing the exam papers, you can Figure 3 and Figure 4 When pieced together, you get a complete, graded exam paper.
[0028] Recognition Errors: Character recognition errors are determined by comparing the recognition result with the preprocessing result (referring to the previously processed image). If they are different, it is considered a recognition error. The error is returned to the front end to display, indicating to the user which line and which character was recognized incorrectly. The image should be cropped based on the incorrect character. The incorrect recognition result can be copied for cropping. If the incorrect character is not needed, it can be ignored.
Claims
1. A method for cropping or erasing content based on the line where the characters in an image are located, characterized in that: The process includes the following steps: reading the image file, converting the image into a BufferedImage object, binarizing the BufferedImage object to change the pixel values in the image to 0 or 1, and then segmenting the binarized image into multiple connected regions; for each connected region, using the Tesseract character recognition algorithm to recognize characters and obtain the text content and character coordinate information of that region; and traversing all connected regions, saving the text content and character coordinate information of each region into a result set. Return the result set, completing the recognition and coordinate acquisition of each character in the image; perform matching according to the required characters, matching line by line from the recognized text content, and calculating the line coordinates of the matched required characters. When calculating the line coordinates of the matched required characters, first split the image text according to "\n" to determine the characters in each line, and then put all the characters into a List according to the coordinates of each character. Process the line text using toCharArray, and calculate the line coordinates by taking the character with the index of text length-1 in the list, and the line coordinate is (0, the y-coordinate of the character); then perform cropping or erasure as required.
2. The method for cropping or erasing content based on the line where the image characters are located, as described in claim 1, is characterized in that: When binarizing an image, the pixel values in the image are changed to 0 or 1. Each pixel in the image is scanned from left to right to form a horizontal scan line. For each pixel, the position of its intersection with the current horizontal scan line is determined. Based on the position and direction of the intersection point, determine whether the pixel intersects with the scan line; If a pixel intersects with a scan line, fill the pixel with a predetermined color; record the ordinate of the current pixel and set this ordinate as the position of the next scan line; at the start of the next scan, move the scan line to the ordinate position recorded in the previous step, and repeat the above steps until all pixels have been processed.
3. The method for cropping or erasing content based on the line where the image characters are located, as described in claim 1, is characterized in that: After converting the image to a BufferedImage object, the system determines the image's format by comparing it with existing image formats. If the format is incorrect, the system modifies the current image format.
4. The method for cropping or erasing content based on the line where the image characters are located, as described in claim 1, is characterized in that: When matching characters according to requirements, if a character is misrecognized, the recognition result is returned to remind the user that the character in that line was misrecognized. If the line is not used, it is ignored. If the line is used for cropping or erasing, the cropping or erasing is performed according to the recognition result.
5. The method for cropping or erasing content based on the line where the image characters are located, as described in claim 4, is characterized in that: The cropping includes upward cropping and downward cropping; the erasing includes upward erasing and downward erasing.
6. The method for cropping or erasing content based on the line where the image characters are located, as described in claim 5, is characterized in that: This method uses the top-left corner of the image as the origin. Based on the obtained character coordinates (x, y, width, height), for upward cropping, it is (0, y, width, height-y), meaning that a new image is cropped from the starting coordinate (0, y) according to the image width and height-y. Since the cropping is based on lines, x is 0 by default, and the same applies below. The downward cropping algorithm is (0, 0, width, y), meaning that a new image is cropped from the starting coordinate (0, 0) according to the image width and height-y. Upward erasure (0, 0, width, y) means that the image is erased upward from the starting coordinate (0, 0), with the erased content having a width of width and a height of y. Downward erasure (0, y, width, height-y) means that the image is erased downward from the starting coordinate (0, y), with the erased content having a width of width and a height of height-y.
Citation Information
Patent Citations
Automatic generation method of handwritten sample set based on answer library
CN110378310A
Picture batch processing method and device and storage medium
CN115797174A