An image text main color extraction method, device, equipment and medium
By combining PaddleOCR and Kmeans clustering algorithms, the color of color-changing fonts in product images can be accurately extracted, solving the problems of background interference and edge noise, and improving the processing efficiency and accuracy of visual verification for e-commerce and brands.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- XIAMEN ZIXUN INFORMATION TECHNOLOGY CO LTD
- Filing Date
- 2026-01-30
- Publication Date
- 2026-06-05
AI Technical Summary
Existing technologies struggle to accurately extract color information from color-changing fonts in product images. Background interference and edge noise contribute to inaccurate color estimation results, impacting the efficiency and accuracy of e-commerce product information management and brand visual verification.
By combining PaddleOCR for precise text region localization, edge extraction and filling algorithms are used to generate a clean text target region mask. The color with the highest proportion in the mask region is extracted by Kmeans clustering algorithm and used as the main color of the font.
It achieves highly accurate estimation of color-changing font colors, has strong anti-interference capabilities, high computational efficiency, and is suitable for product image processing in complex scenarios. It supports real-time product information labeling and brand visual control on e-commerce platforms.
Smart Images

Figure CN122157273A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image processing technology, and in particular to a method, apparatus, device, and medium for extracting the primary color of text in an image. Background Technology
[0002] In e-commerce product display, product information retrieval, and brand visual control scenarios, the color information of color-changing fonts (such as gradient fonts, fonts with light and shadow effects, and multi-color spliced fonts) in product images is crucial product attribute data. However, current mainstream font color estimation methods still have significant shortcomings and cannot meet the high-precision requirements for color estimation of color-changing fonts in product images.
[0003] First, traditional methods mostly sample the color of the entire product image directly without accurately locating the text area. This makes them susceptible to interference from irrelevant areas such as the product background and decorative patterns, leading to significant deviations in color estimation. Second, for color-changing fonts, their edges often exhibit unwanted colors due to factors like light and shadow reflection and image compression distortion. Traditional methods lack effective filtering mechanisms for these edge colors, and direct sampling can confuse the font's true color. Furthermore, traditional color extraction methods struggle to accurately capture the dominant core color in color-changing fonts, often misclassifying secondary colors as the primary font color. This fails to provide reliable color data support for downstream processes such as product information labeling and similar product matching, severely impacting the efficiency and accuracy of subsequent workflows. Summary of the Invention
[0004] The technical problem this invention aims to solve is to provide a method, apparatus, device, and medium for extracting the primary color of text in images. This addresses issues such as background interference, edge noise, and inaccurate primary color extraction in traditional methods. By combining PaddleOCR for precise text region localization, edge extraction and filling algorithms are used to obtain a clean text target region mask. Then, K-means clustering algorithm is used to extract the color with the highest proportion within the mask region as the primary color of the font. This achieves efficient and accurate estimation of the color of color-changing fonts in product images, providing reliable color data for e-commerce product information management, brand visual verification, and product retrieval matching scenarios, thereby improving the processing efficiency and quality of related business processes.
[0005] In a first aspect, the present invention provides a method for extracting the primary color of text in an image, comprising the following steps: S1. Preprocess the input product image containing text regions to obtain a preprocessed grayscale image; S2. Use a text detection model to detect text regions in the preprocessed grayscale image and obtain the bounding box coordinates of at least one text region. S3. Based on the bounding box coordinates, crop out the corresponding text region grayscale image and text region color image from the preprocessed grayscale image and the original color image, respectively. S4. Perform edge detection and connected component filling on the grayscale image of the text region to generate a preliminary text mask; S5. Extract the core region from the preliminary text mask based on the distance transform algorithm, and calculate the average color of the core region in the color image of the text region; then, based on the similarity between the pixel color and the average color, perform layered screening from the inside to the outside on the preliminary text mask to obtain the purified text mask. S6. Perform color clustering analysis on the foreground pixels corresponding to the purified text mask in the color image of the text region, and determine the average color of the color cluster with the largest proportion as the main color of the text region.
[0006] Further, the preprocessing in S1 specifically involves: converting the product image into a grayscale image; and applying a Gaussian filter to smooth and denoise the grayscale image.
[0007] Furthermore, S4 specifically includes: The grayscale image of the text region is processed using the Canny edge detection algorithm to obtain the text outline edge image; Extract the outer contour from the text outline edge image; Seed points are selected inside each outer contour, and a preliminary text mask is generated using a region filling algorithm; The region filling algorithm is a 4-connected or 8-connected seed filling algorithm; the selection of seed points inside each contour specifically involves: calculating the minimum bounding rectangle of each contour and using the geometric center point of the rectangle as the seed point.
[0008] Furthermore, the extraction of the core region from the preliminary text mask based on the distance transformation algorithm in S5 specifically involves: calculating the distance transformation map of the preliminary text mask, and determining the pixel region whose distance transformation value is greater than a preset distance threshold as the core region; The step S5, which involves performing layered filtering of the preliminary text mask from the inside out based on the similarity between pixel colors and the average color, specifically includes: Obtain the coordinates of all foreground pixels in the preliminary text mask, and obtain the distance transformation value corresponding to each foreground pixel based on the distance transformation map of the preliminary text mask; The foreground pixels are sorted in descending order of the distance transformation values; Following the sorted order, the normalized Euclidean distance between the RGB color and the average color of each foreground pixel in the color image of the text region is calculated sequentially. Pixels whose normalized Euclidean distance is less than or equal to a preset similarity threshold are retained in the purified text mask, and the remaining pixels are discarded. The color clustering analysis in S6 uses the K-means clustering algorithm, where the number of clusters K in the K-means clustering algorithm is 3 to 5.
[0009] Secondly, the present invention provides an image text primary color extraction device, comprising: The preprocessing module preprocesses the input product image containing text regions to obtain a preprocessed grayscale image; The text detection module uses a text detection model to detect text regions in the preprocessed grayscale image and obtains the bounding box coordinates of at least one text region. The cropping module, based on the bounding box coordinates, crops out the corresponding grayscale image and color image of the text region from the preprocessed grayscale image and the original color image, respectively. The mask generation module performs edge detection and connected region filling on the grayscale image of the text region to generate a preliminary text mask. The mask purification module extracts the core region from the preliminary text mask based on the distance transform algorithm and calculates the average color of the core region in the color image of the text region; then, based on the similarity between the pixel color and the average color, it performs layer-by-layer filtering on the preliminary text mask from the inside out to obtain the purified text mask. The primary color extraction module performs color clustering analysis on the foreground pixels corresponding to the purified text mask in the color image of the text region, and determines the average color of the color cluster with the largest proportion as the primary color of the text region.
[0010] Furthermore, the preprocessing in the preprocessing module specifically involves: converting the product image into a grayscale image; and applying a Gaussian filter to smooth and denoise the grayscale image.
[0011] Furthermore, the mask generation module specifically comprises: The grayscale image of the text region is processed using the Canny edge detection algorithm to obtain the text outline edge image; Extract the outer contour from the text outline edge image; Seed points are selected inside each outer contour, and a preliminary text mask is generated using a region filling algorithm; The region filling algorithm is a 4-connected or 8-connected seed filling algorithm; the selection of seed points inside each contour specifically involves: calculating the minimum bounding rectangle of each contour and using the geometric center point of the rectangle as the seed point.
[0012] Furthermore, the extraction of the core region from the preliminary text mask based on the distance transformation algorithm in the mask purification module specifically involves: calculating the distance transformation map of the preliminary text mask, and determining the pixel region with a distance transformation value greater than a preset distance threshold as the core region; The mask purification module performs layered filtering of the preliminary text mask from the inside out based on the similarity between pixel color and the average color, specifically including: Obtain the coordinates of all foreground pixels in the preliminary text mask, and obtain the distance transformation value corresponding to each foreground pixel based on the distance transformation map of the preliminary text mask; The foreground pixels are sorted in descending order of the distance transformation values; Following the sorted order, the normalized Euclidean distance between the RGB color and the average color of each foreground pixel in the color image of the text region is calculated sequentially. Pixels whose normalized Euclidean distance is less than or equal to a preset similarity threshold are retained in the purified text mask, and the remaining pixels are discarded. The color clustering analysis in the main color extraction module uses the K-means clustering algorithm, and the number of clusters K in the K-means clustering algorithm is 3 to 5.
[0013] Thirdly, the present invention provides an electronic device including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the method described in the first aspect.
[0014] Fourthly, the present invention provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method described in the first aspect.
[0015] One or more technical solutions provided by this invention have at least the following technical effects or advantages: (a) High precision By using PaddleOCR, text regions can be accurately located, effectively isolating color interference from irrelevant areas such as product backgrounds and decorative patterns. Combined with a layered filling strategy from the inside out, it can accurately filter out noise pixels at the edges of text, ensuring that the extracted color pixels are all the true colors of the font. Based on the Kmeans clustering algorithm, the most prevalent color is selected to avoid misjudging secondary noise as the main color. The color estimation accuracy can reach over 95%, which is significantly better than traditional methods.
[0016] (ii) Strong anti-interference capability This invention employs a comprehensive "localization-filtering-purification-clustering" design, possessing multiple anti-interference capabilities: image preprocessing and PaddleOCR text detection resist complex background interference; Canny edge extraction accurately captures text outlines; inside-out filling filters edge noise interference; and K-means clustering further eliminates the influence of minor residual noise. This method is applicable to color-changing fonts in product images under various complex scenarios (such as gradient fonts, light and shadow effect fonts, and multi-color spliced fonts), and can stably output accurate color estimation results in product images under different lighting conditions and image resolutions.
[0017] (III) High efficiency and practicality The PaddleOCR text detection, Canny edge extraction, and K-means clustering algorithms employed in this invention all utilize lightweight computational logic, resulting in high computational efficiency. Color estimation for color-changing fonts in a single 1024×1024 resolution product image can be completed within 2 seconds. It supports rapid processing of batch product images, meeting the real-time requirements of e-commerce platforms for labeling massive amounts of product information. Furthermore, the algorithm parameters can be adaptively adjusted according to actual scenarios, demonstrating strong versatility and wide applicability in e-commerce, brand visual management, product retrieval, and other fields, possessing extremely high practical value.
[0018] The above description is merely an overview of the technical solution of the present invention. In order to better understand the technical means of the present invention and to implement it in accordance with the contents of the specification, and in order to make the above and other objects, features and advantages of the present invention more apparent and understandable, specific embodiments of the present invention are described below. Attached Figure Description
[0019] The present invention will be further described below with reference to the accompanying drawings and embodiments.
[0020] Figure 1 This is a flowchart of the method in Embodiment 1 of the present invention; Figure 2 This is a schematic diagram of the device in Embodiment 2 of the present invention. Detailed Implementation
[0021] The overall concept of the technical solution in this application is as follows: This invention achieves accurate color estimation for color-changing fonts in product images through a four-step core process: "text region localization - edge extraction and target region determination - edge noise filtering - main color extraction." It combines the synergistic effects of PaddleOCR, edge extraction algorithms, filling algorithms, and K-means clustering algorithms. Each step is closely linked and progressively advanced, effectively eliminating background and edge noise interference to ensure the accuracy and reliability of the color estimation results. The specific steps are as follows: (I) Terminology Explanation 1. PaddleOCR: An optical character recognition tool based on the PaddlePaddle deep learning framework. It has high-precision text detection and recognition capabilities, can quickly locate the bounding box region of text in an image, and is suitable for text region extraction in various complex backgrounds.
[0022] 2. Edge Extraction Algorithm: This algorithm is used to detect regions with abrupt changes in grayscale values (i.e., edges) in an image. This invention adopts the Canny edge detection algorithm, which accurately extracts the contour edges of text through steps such as Gaussian filtering for noise reduction, calculation of gradient strength and direction, non-maximum suppression, and double threshold detection and connection.
[0023] 3. Text region mask: refers to a binary image mask that retains only the text target area and sets the rest of the area as the background. The text area is marked as the foreground (e.g., pixel value of 255), and the background area is marked as the background (e.g., pixel value of 0). It is the core area carrier for subsequent color extraction.
[0024] 4. K-means clustering algorithm: an unsupervised clustering algorithm based on distance metric. By pre-setting the number of clusters, it groups samples that are close in distance in the dataset into one class. This invention is used to cluster color pixels in text regions and select the color cluster with the largest proportion as the main color of the font.
[0025] (II) PaddleOCR Text Target Region Localization After preprocessing the input product images, PaddleOCR is used for text detection to accurately locate the target area of the color-changing text in the product image. The specific steps are as follows: 1. Image preprocessing: The original product image is converted to grayscale and then filtered by Gaussian. Grayscale conversion converts the color product image into a grayscale image, reducing the amount of data for subsequent processing. Gaussian filtering uses a 3×3 Gaussian convolution kernel to smooth the grayscale image, removing slight noise from the image and preventing noise from interfering with the text detection results.
[0026] 2. Text Region Detection: The preprocessed image is input into the text detection model of PaddleOCR. The model outputs the bounding box coordinates (x1, y1, x2, y2) of the text region through steps such as feature extraction, multi-scale fusion, text box prediction and post-processing (such as NMS non-maximum suppression). Here, (x1, y1) is the coordinate of the upper left corner of the bounding box and (x2, y2) is the coordinate of the lower right corner of the bounding box. Each bounding box corresponds to a complete text region (single character or continuous text block).
[0027] 3. Text region cropping: Based on the coordinates of the detected bounding box, the corresponding text region image is cropped from the preprocessed image to obtain a local image containing only the text and a small amount of surrounding background, thus reducing the scope of subsequent processing and minimizing background interference.
[0028] (III) Edge extraction and text target region determination Based on the cropped text region image, an edge extraction algorithm is used to extract the text outline, and then a filling algorithm is used to determine the complete text target region. The specific steps are as follows: 1. Text outline edge extraction: The Canny edge detection algorithm is used to extract edges from the cropped text region image. A reasonable dual threshold is set (low threshold T1 and high threshold T2, where T1 ranges from 50 to 80 and T2 ranges from 150 to 200, which can be adaptively adjusted according to the clarity of the product image). The algorithm calculates the gradient change of image pixels, retains strong edges with gradient values greater than T2 and weak edges with gradient values between T1 and T2 that are connected to strong edges, and removes isolated weak edges, finally obtaining a clear outline edge image of the text.
[0029] 2. Text region filling: For the extracted text outline edge image, a seed filling algorithm (such as the four-connected region filling algorithm) is used for internal filling. Any pixel point inside the text outline is selected as a seed point. All pixels connected to the seed point and meeting the filling conditions (i.e., belonging to the internal region of the text outline) are filled with the foreground color to obtain the initial text region mask. At this time, the mask region has basically covered the text target area, but there may still be a small number of noise pixels at the edge.
[0030] (iv) Fill the filter edge with impurities from the inside out. To eliminate noise interference at the edges of text (such as color gradient noise caused by lighting and shadows, and jagged edge noise caused by image compression), a layered filling strategy from the inside out is applied to the initially obtained text area mask to further purify the target text area. The specific steps are as follows: 1. Determine the core area inside the mask: First, perform distance transformation calculation on the initial text area mask. The distance transformation value represents the distance from each foreground pixel to the nearest background pixel in the mask. Select the pixel area with a distance transformation value greater than the preset threshold (the threshold value ranges from 3 to 5 pixels and is adaptively adjusted according to the text size) as the core area inside the text. This area is far away from the edge, has no noise interference, and the color is the true color of the font.
[0031] 2. Layered Fill from the Inside Out: Starting from the core area, a layered diffusion fill method is used to gradually expand the fill range towards the text edges. During the filling process, only pixels with a color similarity to the core area higher than a preset threshold (the similarity threshold ranges from 0.9 to 0.95, calculated based on Euclidean distance in the RGB color space) are retained. Edge noise pixels with a similarity lower than the threshold are marked as background and removed. This layered fill strategy effectively filters edge noise, resulting in a clean text target area mask.
[0032] (v) The K-means algorithm extracts the color with the highest percentage. Based on the purified text target region mask, the RGB color values of all foreground pixels within the mask are extracted. The color with the highest proportion is then selected using the K-means clustering algorithm and used as the main color for the color-changing font in the product image. The specific steps are as follows: 1. Color pixel sampling: Traverse the clean text region mask, extract the RGB color values of all foreground pixels, and form a color pixel dataset, where each data sample is a (R,G,B) triplet (the value range is 0-255).
[0033] 2. K-means clustering parameter settings: Based on the color complexity of the color-changing font, preset the number of clusters K (K ranges from 3 to 5 and can be adjusted according to the actual application scenario), and set the clustering iteration termination condition (such as the number of iterations reaching 100 or the change in cluster centers being less than 1e-6) to ensure the stability of the clustering results.
[0034] 3. Color Clustering and Proportion Statistics: The color pixel dataset is input into the K-means clustering algorithm. The algorithm initializes cluster centers, calculates the Euclidean distance from each sample to the cluster center, updates the cluster centers, and repeats the iteration until the termination condition is met, ultimately obtaining K color clusters. The number of pixels contained in each color cluster is counted, and the pixel proportion of each color cluster is calculated (number of pixels in a single color cluster / total number of foreground pixels).
[0035] 4. Primary Color Determination: Select the color cluster with the largest pixel proportion, calculate the average RGB value of all pixels in the color cluster, and use it as the primary color of the color-changing font in the product image; at the same time, it can output the proportion of each color cluster and the corresponding color value, providing complete data for scenarios that require detailed color information.
[0036] Example 1 like Figure 1 As shown, this embodiment provides a method for extracting the primary color of text in an image, including the following steps: S1. Preprocess the input product image containing text regions to obtain a preprocessed grayscale image; S2. Use a text detection model to detect text regions in the preprocessed grayscale image and obtain the bounding box coordinates of at least one text region. S3. Based on the bounding box coordinates, crop out the corresponding text region grayscale image and text region color image from the preprocessed grayscale image and the original color image, respectively; the text region grayscale image and the text region color image shall be at least one; S4. Perform edge detection and connected component filling on the grayscale image of the text region to generate a preliminary text mask; S5. Extract the core region from the preliminary text mask based on the distance transform algorithm, and calculate the average color of the core region in the color image of the text region; then, based on the similarity between the pixel color and the average color, perform layered screening from the inside to the outside on the preliminary text mask to obtain the purified text mask. S6. Perform color clustering analysis on the foreground pixels corresponding to the purified text mask in the color image of the text region, and determine the average color of the color cluster with the largest proportion as the main color of the text region.
[0037] In this embodiment, preferably, the preprocessing in S1 specifically involves: converting the product image into a grayscale image; and using a Gaussian filter to perform smoothing and noise reduction processing on the grayscale image.
[0038] In this embodiment, preferably, S4 specifically comprises: The grayscale image of the text region is processed using the Canny edge detection algorithm to obtain the text outline edge image; Extract the outer contour from the text outline edge image; Seed points are selected inside each outer contour, and a preliminary text mask is generated using a region filling algorithm; The region filling algorithm is a 4-connected or 8-connected seed filling algorithm; the selection of seed points inside each contour specifically involves: calculating the minimum bounding rectangle of each contour and using the geometric center point of the rectangle as the seed point.
[0039] In this embodiment, preferably, the extraction of the core region from the preliminary text mask based on the distance transformation algorithm in S5 specifically involves: calculating the distance transformation map of the preliminary text mask, and determining the pixel region whose distance transformation value is greater than a preset distance threshold as the core region; The step S5, which involves performing layered filtering of the preliminary text mask from the inside out based on the similarity between pixel colors and the average color, specifically includes: Obtain the coordinates of all foreground pixels in the preliminary text mask, and obtain the distance transformation value corresponding to each foreground pixel based on the distance transformation map of the preliminary text mask; The foreground pixels are sorted in descending order of the distance transformation values; Following the sorted order, the normalized Euclidean distance between the RGB color and the average color of each foreground pixel in the color image of the text region is calculated sequentially. Pixels whose normalized Euclidean distance is less than or equal to a preset similarity threshold are retained in the purified text mask, and the remaining pixels are discarded. The color clustering analysis in S6 uses the K-means clustering algorithm, where the number of clusters K in the K-means clustering algorithm is 3 to 5.
[0040] Based on the same inventive concept, this application also provides an apparatus corresponding to the method in Embodiment 1, as detailed in Embodiment 2.
[0041] Example 2 like Figure 2 As shown, this embodiment provides an image text primary color extraction device, including: Secondly, the present invention provides an image text primary color extraction device, comprising: The preprocessing module preprocesses the input product image containing text regions to obtain a preprocessed grayscale image; The text detection module uses a text detection model to detect text regions in the preprocessed grayscale image and obtains the bounding box coordinates of at least one text region. The cropping module, based on the bounding box coordinates, crops out the corresponding grayscale image and color image of the text region from the preprocessed grayscale image and the original color image, respectively. The mask generation module performs edge detection and connected region filling on the grayscale image of the text region to generate a preliminary text mask. The mask purification module extracts the core region from the preliminary text mask based on the distance transform algorithm and calculates the average color of the core region in the color image of the text region; then, based on the similarity between the pixel color and the average color, it performs layer-by-layer filtering on the preliminary text mask from the inside out to obtain the purified text mask. The primary color extraction module performs color clustering analysis on the foreground pixels corresponding to the purified text mask in the color image of the text region, and determines the average color of the color cluster with the largest proportion as the primary color of the text region.
[0042] In this embodiment, preferably, the preprocessing in the preprocessing module specifically involves: converting the product image into a grayscale image; and applying a Gaussian filter to smooth and denoise the grayscale image.
[0043] In this embodiment, preferably, the mask generation module specifically comprises: The grayscale image of the text region is processed using the Canny edge detection algorithm to obtain the text outline edge image; Extract the outer contour from the text outline edge image; Seed points are selected inside each outer contour, and a preliminary text mask is generated using a region filling algorithm; The region filling algorithm is a 4-connected or 8-connected seed filling algorithm; the selection of seed points inside each contour specifically involves: calculating the minimum bounding rectangle of each contour and using the geometric center point of the rectangle as the seed point.
[0044] In this embodiment, preferably, the extraction of the core region from the preliminary text mask based on the distance transformation algorithm in the mask purification module specifically involves: calculating the distance transformation map of the preliminary text mask, and determining the pixel region with a distance transformation value greater than a preset distance threshold as the core region; The mask purification module performs layered filtering of the preliminary text mask from the inside out based on the similarity between pixel color and the average color, specifically including: Obtain the coordinates of all foreground pixels in the preliminary text mask, and obtain the distance transformation value corresponding to each foreground pixel based on the distance transformation map of the preliminary text mask; The foreground pixels are sorted in descending order of the distance transformation values; Following the sorted order, the normalized Euclidean distance between the RGB color and the average color of each foreground pixel in the color image of the text region is calculated sequentially. Pixels whose normalized Euclidean distance is less than or equal to a preset similarity threshold are retained in the purified text mask, and the remaining pixels are discarded. The color clustering analysis in the main color extraction module uses the K-means clustering algorithm, and the number of clusters K in the K-means clustering algorithm is 3 to 5.
[0045] Since the apparatus described in Embodiment 2 of the present invention is an apparatus used to implement the method of Embodiment 1 of the present invention, those skilled in the art can understand the specific structure and variations of the apparatus based on the method described in Embodiment 1 of the present invention, and therefore will not be described again here. All apparatuses used in the method of Embodiment 1 of the present invention fall within the scope of protection of the present invention.
[0046] Based on the same inventive concept, this application provides an electronic device embodiment corresponding to Embodiment 1, as detailed in Embodiment 3.
[0047] Example 3 This embodiment provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it can implement any of the implementation methods in Embodiment 1.
[0048] Since the electronic device described in this embodiment is the device used to implement the method in Embodiment 1 of this application, those skilled in the art can understand the specific implementation method and various variations of the electronic device in this embodiment based on the method described in Embodiment 1 of this application. Therefore, how the electronic device implements the method in the embodiment of this application will not be described in detail here. Any device used by those skilled in the art to implement the method in the embodiment of this application falls within the scope of protection of this application.
[0049] Based on the same inventive concept, this application provides a storage medium corresponding to Embodiment 1, as detailed in Embodiment 4.
[0050] Example 4 This embodiment provides a computer-readable storage medium storing a computer program thereon. When the computer program is executed by a processor, it can implement any of the implementation methods in Embodiment 1.
[0051] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0052] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0053] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0054] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0055] While specific embodiments of the present invention have been described above, those skilled in the art should understand that the specific embodiments described are merely illustrative and not intended to limit the scope of the invention. Equivalent modifications and variations made by those skilled in the art in accordance with the spirit of the invention should be covered within the scope of protection of the claims of the present invention.
Claims
1. A method for extracting the primary color of text in an image, characterized in that: Includes the following steps: S1. Preprocess the input product image containing text regions to obtain a preprocessed grayscale image; S2. Use a text detection model to detect text regions in the preprocessed grayscale image and obtain the bounding box coordinates of at least one text region. S3. Based on the bounding box coordinates, crop out the corresponding text region grayscale image and text region color image from the preprocessed grayscale image and the original color image, respectively. S4. Perform edge detection and connected component filling on the grayscale image of the text region to generate a preliminary text mask; S5. Extract the core region from the initial text mask based on the distance transform algorithm, and calculate the average color of the core region in the text region color image; Then, based on the similarity between the pixel color and the average color, the initial text mask is subjected to layered screening from the inside out to obtain a purified text mask. S6. Perform color clustering analysis on the foreground pixels corresponding to the purified text mask in the color image of the text region, and determine the average color of the color cluster with the largest proportion as the main color of the text region.
2. The method for extracting the primary color of text in an image according to claim 1, characterized in that, The preprocessing in S1 specifically involves: converting the product image into a grayscale image; and applying a Gaussian filter to smooth and denoise the grayscale image.
3. The method for extracting the primary color of text in an image according to claim 1, characterized in that, Specifically, S4 is: The grayscale image of the text region is processed using the Canny edge detection algorithm to obtain the text outline edge image; Extract the outer contour from the text outline edge image; Seed points are selected inside each outer contour, and a preliminary text mask is generated using a region filling algorithm; The region filling algorithm is a 4-connected or 8-connected seed filling algorithm; the selection of seed points inside each contour specifically involves: calculating the minimum bounding rectangle of each contour and using the geometric center point of the rectangle as the seed point.
4. The method for extracting the primary color of text in an image according to claim 1, characterized in that, The extraction of the core region from the preliminary text mask based on the distance transformation algorithm in S5 specifically involves: calculating the distance transformation map of the preliminary text mask, and determining the pixel region with a distance transformation value greater than a preset distance threshold as the core region; The step S5, which involves performing layered filtering of the preliminary text mask from the inside out based on the similarity between pixel colors and the average color, specifically includes: Obtain the coordinates of all foreground pixels in the preliminary text mask, and obtain the distance transformation value corresponding to each foreground pixel based on the distance transformation map of the preliminary text mask; The foreground pixels are sorted in descending order of the distance transformation values; Following the sorted order, the normalized Euclidean distance between the RGB color and the average color of each foreground pixel in the color image of the text region is calculated sequentially. Pixels whose normalized Euclidean distance is less than or equal to a preset similarity threshold are retained in the purified text mask, and the remaining pixels are discarded. The color clustering analysis in S6 uses the K-means clustering algorithm, where the number of clusters K in the K-means clustering algorithm is 3 to 5.
5. A device for extracting the primary color of text in an image, characterized in that, include: The preprocessing module preprocesses the input product image containing text regions to obtain a preprocessed grayscale image; The text detection module uses a text detection model to detect text regions in the preprocessed grayscale image and obtains the bounding box coordinates of at least one text region. The cropping module, based on the bounding box coordinates, crops out the corresponding grayscale image and color image of the text region from the preprocessed grayscale image and the original color image, respectively. The mask generation module performs edge detection and connected region filling on the grayscale image of the text region to generate a preliminary text mask. The mask purification module extracts the core region from the initial text mask based on the distance transform algorithm and calculates the average color of the core region in the color image of the text region. Then, based on the similarity between the pixel color and the average color, the initial text mask is subjected to layered screening from the inside out to obtain a purified text mask. The primary color extraction module performs color clustering analysis on the foreground pixels corresponding to the purified text mask in the color image of the text region, and determines the average color of the color cluster with the largest proportion as the primary color of the text region.
6. The image text primary color extraction device according to claim 5, characterized in that, The preprocessing in the preprocessing module specifically involves: converting the product image into a grayscale image; and applying a Gaussian filter to smooth and denoise the grayscale image.
7. The image text primary color extraction device according to claim 5, characterized in that, The mask generation module specifically comprises: The grayscale image of the text region is processed using the Canny edge detection algorithm to obtain the text outline edge image; Extract the outer contour from the text outline edge image; Seed points are selected inside each outer contour, and a preliminary text mask is generated using a region filling algorithm; The region filling algorithm is a 4-connected or 8-connected seed filling algorithm; the selection of seed points inside each contour specifically involves: calculating the minimum bounding rectangle of each contour and using the geometric center point of the rectangle as the seed point.
8. The image text primary color extraction device according to claim 5, characterized in that, The extraction of the core region from the preliminary text mask in the mask purification module based on the distance transformation algorithm specifically involves: calculating the distance transformation map of the preliminary text mask, and determining the pixel region with a distance transformation value greater than a preset distance threshold as the core region; The mask purification module performs layered filtering of the preliminary text mask from the inside out based on the similarity between pixel color and the average color, specifically including: Obtain the coordinates of all foreground pixels in the preliminary text mask, and obtain the distance transformation value corresponding to each foreground pixel based on the distance transformation map of the preliminary text mask; The foreground pixels are sorted in descending order of the distance transformation values; Following the sorted order, the normalized Euclidean distance between the RGB color and the average color of each foreground pixel in the color image of the text region is calculated sequentially. Pixels whose normalized Euclidean distance is less than or equal to a preset similarity threshold are retained in the purified text mask, and the remaining pixels are discarded. The color clustering analysis in the main color extraction module uses the K-means clustering algorithm, and the number of clusters K in the K-means clustering algorithm is 3 to 5.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the method as described in any one of claims 1 to 4.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1 to 4.