Scale character automatic recognition method and system based on multi-mask fusion and DBSCAN clustering
By employing multi-mask fusion and DBSCAN clustering, the problems of image preprocessing distortion, poor illumination adaptability, and interference from invalid regions in ruler character recognition are solved, achieving high-precision and secure automatic ruler character recognition, which is suitable for industrial measurement and precision mapping scenarios.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- ZHENGZHOU UNIV
- Filing Date
- 2026-04-23
- Publication Date
- 2026-07-03
AI Technical Summary
Existing ruler character recognition technology suffers from problems such as image preprocessing distortion, poor adaptability to lighting and background, severe interference from invalid areas, insufficient robustness of character feature extraction, reliance on cloud computing for OCR recognition and poor universality, and lack of standardized output of recognition results.
The method employs multi-mask fusion and DBSCAN clustering, including adaptive background correction, multi-mask feature extraction, adaptive filtering of invalid regions, accurate clustering and local offline OCR inference, and other techniques such as adaptive top-hat transformation, edge detection, MSER algorithm and DBSCAN clustering to achieve high-precision automatic recognition of ruler characters.
It achieves high-precision character recognition in complex lighting conditions, reduces the false negative rate, improves character region positioning accuracy and processing efficiency, ensures data security, and adapts to the data interface requirements of industrial automation systems.
Smart Images

Figure CN122336766A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of data processing technology, and particularly to the field of image data processing technology. Specifically, it relates to a method and system for automatic recognition of ruler characters based on multi-mask fusion and DBSCAN clustering. Background Technology
[0002] As a core metrological tool in industrial measurement, experimental testing, and precision mapping, the rapid and accurate recognition of scale numbers and units is a crucial aspect of automated measurement systems. However, existing scale character recognition technologies generally suffer from the following technical shortcomings:
[0003] Image preprocessing is prone to distortion: Traditional methods directly perform global contrast enhancement on the original image, which can easily lead to blurred edges of ruler characters and pixel distortion, reducing recognition accuracy.
[0004] Poor light and background adaptability: Scale images collected in industrial sites often have problems such as uneven lighting, gradual changes in background grayscale, and local reflections, which make it impossible to achieve adaptive background subtraction, resulting in confusion between characters and background boundaries and failure of subsequent feature extraction.
[0005] Severe interference from invalid regions: The ruler image contains a large number of low-contrast background regions outside the ruler (such as blank areas and blurred backgrounds), which cannot be effectively filtered out, resulting in mislocation of character regions and low inference efficiency;
[0006] Character feature extraction lacks robustness: Single threshold segmentation or contrast filtering cannot fully preserve the character features of the scale under complex lighting conditions, which can easily lead to missed detection of effective characters and cannot adapt to the changing imaging conditions in industrial settings.
[0007] Low accuracy of character region positioning: Region positioning methods mostly rely on fixed threshold segmentation or preset shape matching, which cannot adapt to the irregular distribution of ruler characters and make it difficult to distinguish effective character regions from noise.
[0008] OCR recognition relies on the cloud and has poor versatility: Most mainstream OCR models use cloud interface calls, which poses a risk of data privacy leakage. Furthermore, they are not optimized for low-contrast scales, small characters, and complex industrial scenarios, and their recognition accuracy cannot meet industrial needs.
[0009] The recognition results lack standardized output: There is a lack of a unified result format packaging, which cannot be directly adapted to the data docking needs of automated systems. Summary of the Invention
[0010] In view of this, the present invention provides an integrated automatic ruler character recognition scheme that integrates background adaptive correction, multi-mask fusion feature extraction, invalid region adaptive filtering, character region accurate clustering and local offline OCR inference. This automatic recognition method has strong anti-interference ability, no distortion, high precision, and is adaptable to complex lighting scenarios.
[0011] On one hand, embodiments of the present invention disclose an automatic ruler character recognition method based on multi-mask fusion and DBSCAN clustering, including the following steps:
[0012] S1. Perform grayscale processing on the original image of the ruler to be recognized to obtain a single-channel grayscale image;
[0013] S2. Perform adaptive top-hat transform and open background estimation on the single-channel grayscale image to eliminate the interference of uneven illumination and gradual change in background grayscale, and obtain the grayscale image after background correction.
[0014] S3. Perform edge detection on the background-corrected grayscale image to generate an edge auxiliary mask; use the MSER algorithm to detect stable extreme value regions and generate an MSER feature mask.
[0015] The edge-assisted mask and the MSER feature mask are fused to obtain the fused feature mask;
[0016] S4. Perform connected component analysis and feature filtering based on the fused feature mask, and filter and retain effective regions as effective character candidate regions;
[0017] S5. Input the coordinate features of the effective character candidate region into the DBSCAN density clustering algorithm for analysis, extract the core cluster and determine the boundary coordinates of the effective character region, and crop the original image according to the boundary coordinates to obtain the screenshot of the effective region of the ruler character.
[0018] S6. Perform OCR inference and recognition on the effective area screenshot of the cropped ruler characters;
[0019] S7. Output standardized recognition results.
[0020] Furthermore, in some embodiments, the automatic ruler character recognition method based on multi-mask fusion and DBSCAN clustering is disclosed, and step S1 specifically includes:
[0021] The original image of the scale to be identified is obtained, and the three-channel RGB color image is converted to grayscale using a weighted average method to convert it into a single-channel grayscale image.
[0022] Some embodiments disclose an automatic ruler character recognition method based on multi-mask fusion and DBSCAN clustering, wherein step S2 specifically includes:
[0023] S201. Adaptive Calculation of Structural Element Kernel Size: Based on the size of the input image, the size of the morphological structural element kernel is adaptively determined. The kernel width and kernel height are taken in proportion to 1 / 80 to 1 / 50 of the shorter side of the image to ensure that the kernel size is adapted to the character scale.
[0024] S202, Opening operation background estimation: Using a defined morphological structuring element, an opening operation is performed on a single-channel grayscale image to obtain an estimated background illumination distribution map of the image;
[0025] S203, Division Normalization Background Subtraction: Perform pixel-by-pixel division between the original grayscale image and the estimated background image to complete the normalization process, eliminate the interference of uneven lighting and gray background gradation, and obtain a grayscale image with uniform background correction.
[0026] S204, Top-hat Transform Enhancement: Performs a top-hat transformation on the background-corrected image to further highlight the bright ruler character area, suppress low-brightness background interference, and enhance the contrast between the characters and the background.
[0027] In some embodiments of the automatic ruler character recognition method based on multi-mask fusion and DBSCAN clustering, in step S3, the edge auxiliary mask uses the Canny edge detection algorithm to extract character edge features, and generates a binary mask after closing operation filling and morphological dilation.
[0028] In some embodiments of the automatic ruler character recognition method based on multi-mask fusion and DBSCAN clustering, in step S3, when generating the MSER feature mask, a preset area and rate of change threshold are used to filter background areas and noise areas that do not conform to the ruler character scale, and stable extreme value areas are extracted to generate a binary mask.
[0029] In some embodiments of the automatic ruler character recognition method based on multi-mask fusion and DBSCAN clustering, step S3 uses pixel-wise AND operation of edge-assisted mask and MSER feature mask to achieve mask fusion. After fusion, morphological closing and opening operations are performed on the mask to eliminate holes and isolated noise.
[0030] Some embodiments disclose an automatic ruler character recognition method based on multi-mask fusion and DBSCAN clustering, in which step S4 specifically includes:
[0031] S401. Perform connected component analysis based on the fusion feature mask, and extract the feature parameters of all candidate connected regions, including region area, aspect ratio of the bounding rectangle, and standard deviation of region gray level.
[0032] S402. Set filtering rules to remove invalid connected regions that do not conform to the characteristics of the ruler characters, and retain high-contrast valid character candidate regions that meet the conditions, thus completing the precise filtering of invalid regions.
[0033] Some embodiments disclose an automatic ruler character recognition method based on multi-mask fusion and DBSCAN clustering, wherein step S5 specifically includes:
[0034] S501. Set the neighborhood radius and minimum number of samples for the DBSCAN algorithm, and perform clustering of effective candidate regions to distinguish between effective character clusters and isolated noise points.
[0035] S502. Clustering result determination: If no valid clusters exist, the image is determined to have no valid ruler characters, the process is terminated and a no-character prompt is output; if valid clusters exist, the total number of clusters, the number of regions contained in each cluster, and the number of noise points are counted.
[0036] S503, Core Cluster Extraction: Select the largest cluster with the most regions and the densest cluster with the highest sample density in the neighborhood from the effective clusters, and merge the pixel coordinate range of the two core clusters.
[0037] S504. Boundary Coordinate Determination and Cropping: Based on the merged core cluster pixel coordinates, calculate its minimum bounding rectangle, and extract the coordinates of the upper left and lower right corners of the rectangle as the boundary coordinates of the valid area of the ruler character; crop the original image according to the boundary coordinates to obtain a screenshot of the valid area containing only the ruler character.
[0038] The automatic ruler character recognition method based on multi-mask fusion and DBSCAN clustering disclosed in some embodiments includes step S6 as follows:
[0039] S601. Input the cropped ruler character effective area screenshot into the locally deployed OCR model to perform end-to-end character recognition reasoning and extract the key information of the ruler.
[0040] S602. The recognition results are structured and organized, and a standardized markdown format file containing ruler numbers, units, and character area position information is output to complete the automatic recognition process.
[0041] On the other hand, some embodiments disclose an automatic ruler character recognition system based on multi-mask fusion and DBSCAN clustering, used to implement the automatic ruler character recognition method based on multi-mask fusion and DBSCAN clustering disclosed in the embodiments of the present invention, including:
[0042] The preprocessing module is configured to perform grayscale processing on the image of the ruler to be recognized to obtain a single-channel grayscale image;
[0043] The background correction module is configured to perform adaptive top-hat transformation and open background estimation on a single-channel grayscale image to eliminate uneven illumination and background grayscale gradient interference, thereby obtaining a grayscale image with background correction.
[0044] The mask generation module is configured to perform edge detection on the background-corrected grayscale image to generate an edge-assisted mask; use the MSER algorithm to detect stable extremum regions to generate an MSER feature mask; and fuse the edge-assisted mask and the MSER feature mask to obtain a fused feature mask.
[0045] The region filtering module is configured to perform connected component analysis and feature filtering based on a fused feature mask, and filter and retain valid regions as valid character candidate regions.
[0046] The clustering and localization module is configured to input the coordinate features of the effective character candidate region into the DBSCAN density clustering algorithm for analysis, extract the core cluster and determine the boundary coordinates of the effective character region, and crop the original image according to the boundary coordinates to obtain the screenshot of the effective region of the ruler character.
[0047] The OCR reasoning and recognition module is configured to perform OCR reasoning and recognition on the effective area screenshot of the cropped ruler characters.
[0048] The output module is configured to generate and output recognition results in a standardized markdown format.
[0049] The automatic ruler character recognition method and system based on multi-mask fusion and DBSCAN clustering disclosed in this invention have at least the following beneficial technical effects:
[0050] (1) Adaptive background correction, distortion-free preprocessing: Adaptive kernel top-hat transform and division normalization background subtraction technology can accurately adapt to scale images of different sizes, completely solve the pain points of uneven lighting and gradual changes in background grayscale in industrial sites, without the need for global image enhancement, and avoid character edge blurring and pixel distortion problems from the root.
[0051] (2) Multi-mask fusion feature extraction significantly improves robustness: Combining the boundary features of the edge-assisted mask with the regional stability features of the MSER mask, dual-mask fusion can completely preserve the ruler character features under complex lighting, low contrast, and local reflection scenes, effectively improving the completeness of character region extraction by more than 40% and significantly reducing the false negative rate.
[0052] (3) Precise region filtering, significantly improving computational efficiency: By filtering through connected component features, invalid regions are precisely removed. Combined with double mask pre-filtering, background interference regions outside the scale can be 100% removed, greatly reducing the computational load of subsequent clustering analysis and improving processing efficiency.
[0053] (4) Adaptive character positioning with significantly improved accuracy: DBSCAN unsupervised density clustering is adopted, which does not require a preset number of clusters. It can adaptively adapt to the irregular horizontal / vertical distribution of ruler characters, effectively distinguish effective character clusters from isolated noise points, and improve the character region positioning accuracy by more than 35% compared with traditional methods.
[0054] (5) Offline secure reasoning with strong scene adaptability: Reasoning is achieved based on the DeepSeek-OCR model deployed locally offline, without the need for cloud interface calls, completely avoiding the risk of privacy leakage of industrial measurement data. At the same time, it is adapted and optimized for small characters on the scale and low contrast scenarios, and the recognition accuracy in complex industrial scenarios reaches more than 99.2%.
[0055] (6) Full-process automation and extremely versatile: The entire process from image input to result output is automated without human intervention. The final output is a standardized markdown format result, which can be directly connected to the data interface of industrial automation measurement, precision testing and other systems, and is suitable for automatic recognition scenarios of various industrial rulers, experimental rulers and surveying rulers. Attached Figure Description
[0056] Figure 1 , one The flowchart of the ruler character automatic recognition method based on multi-mask fusion and DBSCAN clustering disclosed in these embodiments;
[0057] Figure 2 Example 1: Schematic diagram of grayscale conversion and adaptive top-hat transformation background correction;
[0058] Figure 3 Example 1: Schematic diagram of edge-assisted mask and MSER mask generation and fusion;
[0059] Figure 4 Example 1: Schematic diagram of DBSCAN clustering results;
[0060] Figure 5 Example 1: Schematic diagram of character region boundary positioning and clipping; wherein, the upper diagram is a schematic diagram of character region boundary positioning, and the lower diagram is a schematic diagram of clipping.
[0061] Figure 6 Example 1: Schematic diagram of input image and final recognition result output; where the upper figure is a schematic diagram of input image and the lower figure is a schematic diagram of final recognition result output. Detailed Implementation
[0062] The term "embodiment" used herein, as an example, is not necessarily to be construed as superior to or better than other embodiments. Performance testing in these embodiments of the invention, unless otherwise specified, employs conventional testing methods in the art. It should be understood that the terminology used in these embodiments is merely for describing particular implementations and is not intended to limit the scope of the disclosure of these embodiments.
[0063] Unless otherwise stated, the technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which the embodiments of this invention pertain; other experimental methods and technical means not specifically noted in the embodiments of this invention refer to experimental methods and technical means commonly used by one of ordinary skill in the art.
[0064] The terms “basic” and “approximately” as used herein are used to describe small fluctuations. For example, they can mean less than or equal to ±5%, such as less than or equal to ±2%, such as less than or equal to ±1%, such as less than or equal to ±0.5%, such as less than or equal to ±0.2%, such as less than or equal to ±0.1%, such as less than or equal to ±0.05%. Numerical data presented or expressed in range format herein are used for convenience and brevity only, and should therefore be interpreted flexibly to include not only the explicitly listed values that define the range, but also all independent values or subranges contained within that range. For example, a numerical range of “1–5%” should be interpreted to include not only the explicitly listed values from 1% to 5%, but also the independent values and subranges within the indicated range. Thus, this numerical range includes independent values such as 2%, 3.5%, and 4%, and subranges such as 1%–3%, 2%–4%, and 3%–5%, etc. This principle also applies to ranges that list only one value. Furthermore, this interpretation applies regardless of the width of the range or the characteristics described.
[0065] In this document, including in the claims, conjunctions such as "comprising," "including," "with," "having," "containing," "involving," and "accommodating" are understood to be open-ended, meaning "including but not limited to." Only the conjunctions "consisting of" and "composed of" are closed conjunctions.
[0066] To better illustrate the content of this invention, numerous specific details are provided in the following detailed embodiments. Those skilled in the art should understand that the invention can be practiced even without certain specific details. In the embodiments, some methods, means, instruments, and devices well-known to those skilled in the art are not described in detail, in order to highlight the main points of the invention.
[0067] Without conflict, the technical features disclosed in the embodiments of the present invention can be combined arbitrarily, and the resulting technical solution belongs to the content disclosed in the embodiments of the present invention.
[0068] In some implementations, such as Figure 1 As shown, the automatic ruler character recognition method based on multi-mask fusion and DBSCAN clustering includes the following steps:
[0069] S1. Perform grayscale processing on the original image of the ruler to be recognized to obtain a single-channel grayscale image;
[0070] In some embodiments, the specific steps include: acquiring the original image of the scale to be identified, performing grayscale processing on the three-channel RGB color image using a weighted average method, converting it into a single-channel grayscale image, eliminating color information interference, and preserving the image grayscale gradient and pixel distribution characteristics, thus laying the foundation for subsequent morphological processing and feature extraction.
[0071] S2. Perform adaptive top-hat transform and open background estimation on the single-channel grayscale image to eliminate the interference of uneven illumination and gradual change in background grayscale, and obtain the grayscale image after background correction.
[0072] Typically, for grayscale images, adaptive top-hat transform and opening operation background estimation are performed to eliminate uneven illumination and background grayscale gradation problems, specifically including:
[0073] S201. Adaptive Calculation of Structural Element Kernel Size: Based on the size of the input image, the size of the morphological structural element kernel is adaptively determined. The kernel width and kernel height are taken in proportion to 1 / 80 to 1 / 50 of the short side of the image to ensure that the kernel size is adapted to the character scale and avoid overcorrection.
[0074] S202, Opening operation background estimation: Using a defined morphological structuring element, an opening operation is performed on a single-channel grayscale image to obtain an estimated background illumination distribution map of the image;
[0075] S203, Division Normalization Background Subtraction: Perform pixel-by-pixel division between the original grayscale image and the estimated background image to complete the normalization process, eliminate the interference of uneven lighting and gray background gradation, and obtain a grayscale image with uniform background correction.
[0076] S204, Top-hat Transform Enhancement: Performs a top-hat transformation on the background-corrected image to further highlight the bright ruler character area, suppress low-brightness background interference, and enhance the contrast between the characters and the background.
[0077] S3. Perform edge detection on the background-corrected grayscale image to generate an edge auxiliary mask; use the MSER algorithm to detect stable extreme value regions to generate an MSER feature mask; fuse the edge auxiliary mask and the MSER feature mask to obtain a fused feature mask.
[0078] Typically, edge-assisted masks are generated by extracting character edge features using the Canny edge detection algorithm, followed by closing operations, filling, and morphological dilation to produce a binary mask. In some embodiments, the generation of the edge-assisted mask includes: using the Canny edge detection algorithm, adaptively calculating high and low thresholds, extracting the edge gradient features of characters in the image, performing closing operations, filling, and morphological dilation on the edge regions, and generating a binary edge-assisted mask that covers the complete character outline to lock the boundary range of the character.
[0079] Typically, when generating an MSER feature mask, preset thresholds for region area and rate of change are used to filter out background and noise regions that do not conform to the scale of the character on the ruler, and stable extremum regions are extracted to generate a binary mask. In some embodiments, generating an MSER feature mask includes: using the maximum stable extremum region algorithm to detect character regions in the background-corrected grayscale image, extracting connected regions in the image that are grayscale stable and conform to the scale of the character on the ruler, filtering out background regions that are too large and noise regions that are too small, and generating a binary MSER feature mask to lock the characters and core effective regions.
[0080] Typically, mask fusion is achieved by performing a pixel-wise AND operation on an edge-assisted mask and an MSER feature mask. After fusion, morphological closing and opening operations are performed on the mask to eliminate holes and isolated noise. In some embodiments, the fusion process of the edge-assisted mask and the MSER feature mask specifically includes: performing a pixel-wise AND operation on the edge-assisted mask and the MSER feature mask to complete the dual-mask fusion and obtain a fused mask; performing morphological closing and opening operations on the fused mask to eliminate holes and isolated noise within the mask, resulting in a binarized fused feature mask that retains only the ruler character candidate region.
[0081] S4. Perform connected component analysis and feature filtering based on the fused feature mask, and retain valid regions as valid character candidate regions. Typically, based on the fused feature mask, perform connected component analysis, extract feature parameters of all candidate connected regions, including region area, aspect ratio of the bounding rectangle, and standard deviation of region gray level. Set filtering rules, remove invalid connected regions that do not meet the character features of the scale, and retain high-contrast valid character candidate regions that meet the conditions, thus completing the accurate filtering of invalid regions. Among them, connected regions that simultaneously meet the region area threshold, the aspect ratio threshold of the bounding rectangle, and the standard deviation threshold of region gray level are determined to be valid character candidate regions.
[0082] S5. Input the coordinate features of the effective character candidate region into the DBSCAN clustering algorithm for analysis, extract the core cluster and determine the boundary coordinates of the effective character region, and crop the original image according to the boundary coordinates to obtain the screenshot of the effective region of the ruler character.
[0083] Typically, DBSCAN clustering is an unsupervised density clustering method, requiring no preset number of clusters and adaptively distinguishing between valid character clusters and isolated noise points. The core clusters are the largest and densest clusters in the clustering results, and the boundary of the valid character region is the diagonal coordinates of the smallest bounding rectangle after merging the core clusters. In some embodiments, the center coordinates of the filtered valid character candidate regions are used as feature vectors and input into the DBSCAN density clustering algorithm for unsupervised clustering analysis, specifically including:
[0084] S501. Set the neighborhood radius and minimum number of samples for the DBSCAN algorithm, and perform clustering of effective candidate regions to distinguish between effective character clusters and isolated noise points.
[0085] S502. Clustering result determination: If no valid clusters exist, the image is determined to have no valid ruler characters, the process is terminated and a no-character prompt is output; if valid clusters exist, the total number of clusters, the number of regions contained in each cluster, and the number of noise points are counted.
[0086] S503, Core Cluster Extraction: Select the largest cluster with the most regions and the densest cluster with the highest sample density in the neighborhood from the effective clusters, and merge the pixel coordinate range of the two core clusters.
[0087] S504. Boundary Coordinate Determination and Cropping: Based on the merged core cluster pixel coordinates, calculate its minimum bounding rectangle, and extract the coordinates of the upper left and lower right corners of the rectangle as the boundary coordinates of the valid area of the ruler character; crop the original image according to the boundary coordinates to obtain a screenshot of the valid area containing only the ruler character.
[0088] S6. Perform OCR inference and recognition on the effective area screenshot of the cropped ruler characters;
[0089] S7. Output standardized recognition results.
[0090] In some embodiments, the cropped valid area of the ruler characters is input into the locally deployed DeepSeek-OCR model for end-to-end character recognition and reasoning to extract key information such as the ruler's scale numbers, units of measurement, and symbols. The recognition results are then structured and output as a standardized markdown file containing the ruler's numbers, units, and character area location information, thus completing the entire automatic recognition process.
[0091] In some embodiments, the ruler character automatic recognition system based on multi-mask fusion and DBSCAN clustering is used to implement the ruler character automatic recognition method based on multi-mask fusion and DBSCAN clustering, including:
[0092] The preprocessing module is configured to perform grayscale processing on the image of the ruler to be recognized to obtain a single-channel grayscale image;
[0093] The background correction module is configured to perform adaptive top-hat transformation and open background estimation on a single-channel grayscale image to eliminate uneven illumination and background grayscale gradient interference, thereby obtaining a grayscale image with background correction.
[0094] The mask generation module is configured to perform edge detection on the background-corrected grayscale image to generate an edge-assisted mask; use the MSER algorithm to detect stable extremum regions to generate an MSER feature mask; and fuse the edge-assisted mask and the MSER feature mask to obtain a fused feature mask.
[0095] The region filtering module is configured to perform connected component analysis and feature filtering based on a fused feature mask, and filter and retain valid regions as valid character candidate regions.
[0096] The clustering and localization module is configured to input the coordinate features of the effective character candidate region into the DBSCAN density clustering algorithm for analysis, extract the core cluster and determine the boundary coordinates of the effective character region, and crop the original image according to the boundary coordinates to obtain the screenshot of the effective region of the ruler character.
[0097] The OCR reasoning and recognition module is configured to perform OCR reasoning and recognition on the effective area screenshot of the cropped ruler characters.
[0098] The output module is configured to generate and output recognition results in a standardized markdown format.
[0099] The technical details are further illustrated below with reference to the embodiments.
[0100] Example 1
[0101] The automatic character recognition method for rulers based on multi-mask fusion and DBSCAN clustering provided in Embodiment 1 is used to recognize the original RGB image of an industrial surveying ruler. The specific steps include:
[0102] S1: Image grayscale conversion
[0103] Acquire raw RGB JPEG images of electron microscope images including the scale bar, with an image size of 1920×1280 pixels; perform grayscale conversion using a weighted average method, with the grayscale conversion formula as follows:
[0104]
[0105] In the formula, R, G, and B are the pixel values of the red, green, and blue channels of the original image, respectively. The above formula is used to convert the values into a single-channel grayscale image, thus eliminating color information interference.
[0106] S2: Adaptive Top Hat Transform and Background Subtraction
[0107] (1) Adaptive kernel size calculation: The short side of the input image is 1280 pixels. The kernel size of the morphological structural element is determined to be 25×25 pixels according to the ratio of 1 / 50 of the short side. An elliptical structural element is generated to adapt to the scale features of the ruler character.
[0108] (2) Opening operation background estimation: Using the above 25×25 elliptical structuring element, morphological opening operation is performed on the grayscale image to obtain the background illumination distribution estimation map of the image, and restore the gradient background and uneven illumination distribution of the image;
[0109] (3) Division normalization background subtraction: Perform pixel-by-pixel division operation between the original grayscale image and the background estimation image to complete the illumination normalization, eliminate the interference of background grayscale gradient and local illumination unevenness, and obtain a corrected image with uniform background.
[0110] (4) Top-hat transformation enhancement: Using the same 25×25 elliptical structuring element, a top-hat transformation is performed on the background-corrected image to further highlight the bright ruler scale characters, suppress low-brightness background interference, and enhance the contrast between the characters and the background. The results are as follows: Figure 2 As shown.
[0111] S3: Edge-assisted mask generation
[0112] For the background-corrected grayscale image, the Canny edge detection algorithm is used to adaptively calculate high and low thresholds. The high threshold is three times the average grayscale gradient of the image, and the low threshold is half of the high threshold. The edge gradient features of the ruler characters are extracted. The detected edge regions are filled with edge holes by performing a 3×3 structuring element closing operation, and then a 3×3 structuring element morphological dilation is performed to expand the edge coverage area and generate a binarized edge auxiliary mask to completely cover the contour boundary of the ruler characters.
[0113] S4: MSER Feature Mask Generation
[0114] For the background-corrected grayscale image, the MSER algorithm is used for maximum stable extremum region detection. The parameters are set as follows: minimum region area is 20 pixels, maximum region area does not exceed 0.3 of the original pixel size of the image, maximum change rate is 0.2, and minimum stable duration is 5. Stable connected regions that conform to the scale of the ruler characters are extracted, and background regions with excessively large areas and noise regions with excessively small areas are filtered out to generate a binarized MSER feature mask and lock the core effective region of the ruler characters.
[0115] S5: Multi-mask fusion processing
[0116] The edge-assisted mask generated in step S3 and the MSER feature mask generated in step S4 are subjected to a pixel-by-pixel AND operation to complete the fusion of the two masks and obtain an initial fused mask. A 5×5 structuring element closing operation is then performed on the initial fused mask to fill the character holes within the mask, followed by a 3×3 structuring element opening operation to eliminate isolated noise pixels. Finally, a binarized fused feature mask retaining only the candidate region of the ruler character is obtained. The result is as follows: Figure 3 As shown.
[0117] S6: Effective Region Detection and Screening
[0118] Based on the fusion feature mask, 8-neighborhood connected component analysis is used to extract feature parameters of all candidate connected regions, including region area, aspect ratio of the bounding rectangle, and standard deviation of region gray level; filtering rules are set:
[0119] (1) The area is between 50 and 8000 pixels;
[0120] (2) The aspect ratio of the circumscribed rectangle is between 0.1 and 10;
[0121] (3) The standard deviation of gray level in the region is greater than 8;
[0122] Regions that simultaneously meet the above three conditions are determined to be valid character candidate regions, and the remaining invalid regions are eliminated; after screening in this embodiment 1, a total of 92 valid character candidate regions are retained.
[0123] S7: DBSCAN Cluster Analysis and Character Region Cropping
[0124] The center coordinates of the 92 selected valid character candidate regions are used as two-dimensional feature vectors and input into the DBSCAN density clustering algorithm. The EPS value of DBSCAN is adapted to the image size, and the minimum number of samples is min_samples=2.
[0125] After performing clustering, one effective cluster and 11 isolated noise regions were obtained. The largest cluster containing 8 effective regions and the densest cluster with a sample density of 0.93 were selected, and the pixel coordinate ranges of the two core clusters, the largest cluster and the densest cluster, were merged.
[0126] Calculate the minimum bounding rectangle of the merged core cluster to obtain the coordinates of the top left corner (x1, y1) = (82, 1133) and the bottom right corner (x1, y1) = (329, 1203) of the effective character region. Based on these boundary coordinates, crop the original image to obtain a screenshot of the effective region containing only the ruler character. The DBSCAN clustering results are as follows: Figure 4 As shown, the character region boundary is located in the cropping result as follows. Figure 5 As shown.
[0127] S8: Local OCR inference recognition and standardized result output. The input image and final recognition result of Example 1 are as follows... Figure 6 As shown.
[0128] The cropped valid area of the ruler characters is input into the locally deployed DeepSeek-OCR model for end-to-end inference and recognition, extracting the scale numbers and unit information: 2 1 / nm "optical ruler"; the recognition results are then structured and output as a standardized markdown file, as follows:
[0129] Ruler recognition results
[0130]
[0131] The ruler character automatic recognition method and system disclosed in this invention based on multi-mask fusion and DBSCAN clustering can be adapted to complex lighting and low-contrast industrial scenarios. It retains character features without distortion, has high character positioning and recognition accuracy, and runs automatically offline throughout the entire process, ensuring data security. It is suitable for various ruler automatic recognition scenarios such as industrial measurement, experimental testing, and precision mapping.
[0132] The technical solutions and technical details disclosed in the embodiments of this invention are merely illustrative of the inventive concept of this invention and do not constitute a limitation on the technical solutions of the embodiments of this invention. Any conventional changes, substitutions, or combinations made to the technical details disclosed in the embodiments of this invention have the same inventive concept as this invention and are within the protection scope of the claims of this invention.
Claims
1. A scale character automatic recognition method based on multi-mask fusion and DBSCAN clustering, characterized in that, Including the following steps: S1. Perform grayscale processing on the original image of the ruler to be recognized to obtain a single-channel grayscale image; S2. Perform adaptive top-hat transform and open background estimation on the single-channel grayscale image to eliminate the interference of uneven illumination and gradual change in background grayscale, and obtain the grayscale image after background correction. S3. Perform edge detection on the background-corrected grayscale image to generate an edge auxiliary mask; use the MSER algorithm to detect stable extreme value regions and generate an MSER feature mask. The edge-assisted mask and the MSER feature mask are fused together to obtain the fused feature mask. S4. Perform connected component analysis and feature filtering based on the fused feature mask, and filter and retain effective regions as effective character candidate regions; S5. Input the coordinate features of the effective character candidate region into the DBSCAN density clustering algorithm for analysis, extract the core cluster and determine the boundary coordinates of the effective character region, and crop the original image according to the boundary coordinates to obtain the screenshot of the effective region of the ruler character. S6. Perform OCR inference and recognition on the effective area screenshot of the cropped ruler characters; S7. Output standardized recognition results.
2. The scale character automatic recognition method based on multi-mask fusion and DBSCAN clustering of claim 1, wherein, Step S1 specifically includes: The original image of the scale to be identified is obtained, and the three-channel RGB color image is converted to grayscale using a weighted average method to convert it into a single-channel grayscale image. 3.The scale character automatic recognition method based on multi-mask fusion and DBSCAN clustering of claim 1, characterized in that, Step S2 specifically includes: S201. Adaptive Calculation of Structural Element Kernel Size: Based on the size of the input image, the size of the morphological structural element kernel is adaptively determined. The kernel width and kernel height are taken in proportion to 1 / 80 to 1 / 50 of the shorter side of the image to ensure that the kernel size is adapted to the character scale. S202, Opening operation background estimation: Using a defined morphological structuring element, an opening operation is performed on a single-channel grayscale image to obtain an estimated background illumination distribution map of the image; S203, Division Normalization Background Subtraction: Perform pixel-by-pixel division between the original grayscale image and the estimated background image to complete the normalization process, eliminate the interference of uneven lighting and gray background gradation, and obtain a grayscale image with uniform background correction. S204, Top-hat Transform Enhancement: Performs a top-hat transformation on the background-corrected image to further highlight the bright ruler character area, suppress low-brightness background interference, and enhance the contrast between the characters and the background.
4. The scale character automatic recognition method based on multi-mask fusion and DBSCAN clustering of claim 1, wherein, In step S3, the edge-assisted mask generation includes: extracting character edge features using the Canny edge detection algorithm, and generating a binarized mask after closing operation filling and morphological dilation.
5. The method of claim 1, wherein the method is characterized by, In step S3, when generating the MSER feature mask, a preset threshold for the area and rate of change is used to filter out background and noise areas that do not conform to the scale of the character on the ruler, and stable extreme value areas are extracted to generate a binarized mask.
6. The automatic ruler character recognition method based on multi-mask fusion and DBSCAN clustering according to claim 1, characterized in that, In step S3, the edge-assisted mask and the MSER feature mask are used to perform pixel-by-pixel AND operation to achieve mask fusion. After fusion, morphological closing and opening operations are performed on the mask to eliminate holes and isolated noise.
7. The method of claim 1, wherein the method is characterized by, Step S4 specifically includes: S401. Perform connected component analysis based on the fusion feature mask, and extract the feature parameters of all candidate connected regions, including region area, aspect ratio of the bounding rectangle, and standard deviation of region gray level. S402. Set filtering rules to remove invalid connected regions that do not meet the characteristics of the scale characters, and retain high-contrast valid character candidate regions that meet the conditions to complete the accurate filtering of invalid regions; among them, connected regions that simultaneously meet the region area threshold, the bounding rectangle aspect ratio threshold, and the region grayscale standard deviation threshold are determined to be valid character candidate regions.
8. The method of claim 1, wherein the method is characterized by, Step S5 specifically includes: S501. Set the neighborhood radius and minimum number of samples for the DBSCAN algorithm, and perform clustering of effective candidate regions to distinguish between effective character clusters and isolated noise points. S502. Clustering result determination: If no valid clusters exist, the image is determined to have no valid ruler characters, the process is terminated and a no-character prompt is output; if valid clusters exist, the total number of clusters, the number of regions contained in each cluster, and the number of noise points are counted. S503, Core Cluster Extraction: Select the largest cluster with the most regions and the densest cluster with the highest sample density in the neighborhood from the effective clusters, and merge the pixel coordinate range of the two core clusters. S504. Boundary Coordinate Determination and Cropping: Based on the merged core cluster pixel coordinates, calculate its minimum bounding rectangle, and extract the coordinates of the upper left and lower right corners of the rectangle as the boundary coordinates of the valid area of the ruler character; crop the original image according to the boundary coordinates to obtain a screenshot of the valid area containing only the ruler character.
9. The method of claim 1, wherein the method is characterized by, Step S6 specifically includes: S601. Input the cropped ruler character effective area screenshot into the locally deployed OCR model to perform end-to-end character recognition reasoning and extract the key information of the ruler. S602. The recognition results are structured and organized, and a standardized markdown format file containing ruler numbers, units, and character area position information is output to complete the automatic recognition process.
10. A scale character automatic recognition system based on multi-mask fusion and DBSCAN clustering, for implementing the method of any one of claims 1-9, characterized in that, include: The preprocessing module is configured to perform grayscale processing on the image of the ruler to be recognized to obtain a single-channel grayscale image; The background correction module is configured to perform adaptive top-hat transformation and open background estimation on a single-channel grayscale image to eliminate uneven illumination and background grayscale gradient interference, thereby obtaining a grayscale image with background correction. The mask generation module is configured to perform edge detection on the background-corrected grayscale image to generate an edge-assisted mask; and to use the MSER algorithm to detect stable extremum regions to generate an MSER feature mask. The edge-assisted mask and the MSER feature mask are fused to obtain the fused feature mask; The region filtering module is configured to perform connected component analysis and feature filtering based on a fused feature mask, and filter and retain valid regions as valid character candidate regions. The clustering and localization module is configured to input the coordinate features of the effective character candidate region into the DBSCAN density clustering algorithm for analysis, extract the core cluster and determine the boundary coordinates of the effective character region, and crop the original image according to the boundary coordinates to obtain the screenshot of the effective region of the ruler character. The OCR reasoning and recognition module is configured to perform OCR reasoning and recognition on the effective area screenshot of the cropped ruler characters. The output module is configured to generate and output recognition results in a standardized markdown format.