Thermal printing image processing method, device, equipment and medium
By generating a threshold distribution map and a heat accumulation risk map, combined with an error diffusion dithering algorithm and pixel optimization processing, the blurring problem caused by heat accumulation in dark areas of thermal printers is solved, achieving high-quality image conversion and clarity improvement.
Patent Information
- Application Number
- CN202511286006.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-10
- Publication Date
- 2025-10-21
- Estimated Expiration
- 2045-09-10
AI Technical Summary
Thermal printers suffer from image blur and distortion due to heat accumulation when printing dark areas. Existing technologies make it difficult to effectively alleviate the heat accumulation effect while improving image detail and layering.
By generating a threshold distribution map and a heat accumulation risk map, combining the error diffusion dithering algorithm and pixel optimization processing, the processing parameters are adaptively adjusted to optimize the quality of the binarized image and reduce the heat accumulation risk in dark areas.
It improves the clarity and reliability of thermal printing images, reduces blur and distortion caused by overheating of the print head, and improves the overall print quality.
Smart Images

Figure CN120823401A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of thermal printing, and in particular to a thermal printing image processing method, device, equipment and medium. Background Art
[0002] Thermal printing technology is a printing method that uses heat energy to act on special media to develop colors. Due to its advantages such as simple structure, fast printing and no need for ink cartridges, it has been widely used in the fields of commercial bills, logistics labels and portable printing. However, a thermal printer is essentially a binary output device, and its printing unit can only perform operations such as printing black dots or leaving white dots. Therefore, converting the original grayscale image with rich tonal levels into a binary image is a necessary prerequisite for printing. The traditional method uses a simple fixed threshold method for conversion. Although the processing speed is fast, it will cause obvious contour artifacts in the grayscale transition area of the image and lead to the loss of a large amount of image detail information. The quality of the printed product is usually difficult to meet the use requirements.
[0003] To improve the image quality degradation caused by the fixed threshold method, existing technologies calculate the resulting quantization error while binarizing the current pixel and distribute this error to neighboring, unprocessed pixels according to preset weights. This method maintains the conservation of local grayscale energy in the image from a macroscopic perspective, effectively simulating a continuous grayscale transition effect. Compared to the fixed threshold method, it significantly improves the detail and layering of printed images.
[0004] However, while the above method improves image expressiveness, it also introduces new problems. Specifically, when processing dark areas or large dark areas of an image, the algorithm will inevitably generate a high-density set of continuous black pixels in order to maintain local grayscale energy. When the printer performs a print task, this high-density print instruction causes the heating unit of the print head to continue working for a long time and at a high frequency, which in turn causes heat to accumulate rapidly on the print head and spread to the surrounding area, which is the heat accumulation effect. This effect causes the actual printed black dots to expand in size and adhere to each other due to overheating, eventually forming blurred black patches on the paper, seriously damaging the image's outline clarity. Summary of the Invention
[0005] The present application provides a thermal printing image processing method, device, equipment and medium, which improves printing clarity.
[0006] In a first aspect of the present application, a thermal printing image processing method is provided, the method comprising: obtaining an original image to be printed and converting the original image into a grayscale image; calculating the gradient difference value of the grayscale image in each preset pixel area, and generating a threshold distribution map based on the gradient difference value of each preset pixel area; generating a heat accumulation risk map based on the weighted grayscale value of the grayscale image in each preset neighborhood window, wherein the preset neighborhood window is a window centered on a first pixel point, and the first pixel point is any one of a plurality of pixel points included in the grayscale image; performing error diffusion on the grayscale image according to the threshold distribution map A dithering algorithm is provided to generate a preliminary binarized image; image features of the preliminary binarized image are extracted, and the original image is classified according to texture information of the original image and the image features to obtain the image type of the original image; according to the image type, a target processing parameter set corresponding to the image type is called from a plurality of preset processing parameter sets; pixel processing is performed on the preliminary binarized image according to the target processing parameter set and the heat accumulation risk map to obtain an optimized binarized image; the optimized binarized image is resampled to generate final printing image data adapted to the target printer resolution, and the final printing image data is output.
[0007] By employing the above technical solution, the original image is first converted into a grayscale image. Then, by calculating the gradient difference values of the grayscale image within each preset pixel region, a threshold distribution map is generated for use in the subsequent error diffusion dithering algorithm to improve the quality of the binarized image. Furthermore, based on the weighted grayscale values of the grayscale image within each preset neighborhood window, a heat accumulation risk map is generated to assess the heat distribution risk during the printing process. After generating a preliminary binarized image, the method extracts image features and classifies the image using the texture information of the original image to determine the image type. Based on the image type, the corresponding target processing parameter set is called from a preset processing parameter set. The pixel processing of the preliminary binarized image is performed using the heat accumulation risk map to obtain an optimized binarized image. Finally, the method resamples the optimized binarized image to generate and output final print image data adapted to the target printer resolution. Through this series of processes, the method can adaptively adjust processing parameters based on the characteristics of different images, optimize the quality of the binarized image, and simultaneously consider the heat distribution risk during the printing process, improving printing reliability. Ultimately, the method generates high-quality, highly adaptable print image data to meet diverse printing requirements. This method improves print quality and clarity by adjusting pixel distribution density in dark areas (high-risk areas for heat) and reducing the generation of consecutive black dots, thereby reducing the cumulative effect of heat.
[0008] Optionally, the calculating of the gradient difference value of the grayscale image in each preset pixel area and generating a threshold distribution map based on the gradient difference value of each preset pixel area specifically includes: dividing the grayscale image into multiple preset pixel areas of equal size; for each preset pixel area, calculating the grayscale difference between a target pixel point in the preset pixel area and an adjacent pixel point corresponding to the target pixel point, to obtain multiple grayscale difference values in the preset pixel area, where the target pixel point is any pixel point in the preset pixel area; determining a weight coefficient of the corresponding grayscale difference value according to the position of the target pixel point in the preset pixel area; multiplying each grayscale difference value by a corresponding weight coefficient and summing and averaging them to obtain the gradient difference value of the preset pixel area; determining a grayscale threshold value corresponding to the gradient difference value according to a preset threshold distribution function, where the preset threshold distribution function includes a correspondence between gradient difference values and grayscale thresholds; constructing a threshold distribution matrix according to the grayscale thresholds of each preset pixel area, where the elements of the threshold distribution matrix are the grayscale thresholds; and performing interpolation and smoothing processing on the threshold distribution matrix to obtain a threshold distribution map with the same resolution as the original image.
[0009] By adopting the above technical solution, the grayscale image is first divided into multiple preset pixel areas of equal size. For each area, the grayscale difference between the target pixel point and the adjacent pixel points in the area is calculated, and the weight coefficient of the grayscale difference is determined according to the position of the target pixel point. The weighted grayscale differences are summed and averaged to obtain the gradient difference value of the preset pixel area. Then, according to the preset threshold distribution function, the grayscale threshold corresponding to the gradient difference value is determined, and a threshold distribution matrix is constructed. Finally, the threshold distribution matrix is interpolated and smoothed to obtain a threshold distribution map with the same resolution as the original image. The threshold distribution map generated by this method can adaptively adjust the binarization threshold according to the gradient change characteristics of the local area of the image, thereby improving the quality of the binarized image. At the same time, the interpolation and smoothing process can eliminate mutations and discontinuities in the threshold distribution map, making the threshold distribution smoother and more natural, and reducing artifacts and noise in the binarized image.
[0010] Optionally, the heat accumulation risk map is generated based on the weighted grayscale values of the grayscale image in each preset neighborhood window, specifically including: calculating the Euclidean distance between each second pixel point in the preset neighborhood window and the first pixel point as the center of the window, and based on the Euclidean distance, generating a Gaussian weight coefficient for the first pixel point and the second pixel point by a preset Gaussian function, where the second pixel point is any pixel point in the preset neighborhood window except the first pixel point; multiplying the grayscale values of the first pixel point and the second pixel point by the Gaussian weight coefficient, and accumulating all the product results to obtain a weighted grayscale value; normalizing the weighted grayscale value to obtain the heat accumulation value of the grayscale image in each of the preset neighborhood windows, and generating a heat accumulation risk map based on the heat accumulation value.
[0011] By adopting the above technical solution, for each pixel in the grayscale image, a preset neighborhood window is determined with it as the center, the Euclidean distance between each pixel in the window and the center pixel is calculated, and the corresponding Gaussian weight coefficient is generated by a preset Gaussian function. The grayscale value of the pixel is multiplied by the Gaussian weight coefficient, and all the product results are accumulated to obtain a weighted grayscale value. The weighted grayscale value is normalized to obtain the heat accumulation value of each preset neighborhood window, and a heat accumulation risk map is generated. The heat accumulation risk map generated by this method can comprehensively consider the grayscale distribution within the pixel neighborhood and evaluate the printing heat risk of the local area. The introduction of Gaussian weight can further highlight the influence of the central pixel, while smoothly attenuating the contribution of the neighboring pixels, making the calculation of the heat accumulation value more reasonable. Normalization can map the heat accumulation value to a unified scale, facilitating subsequent threshold comparison and risk assessment.
[0012] Optionally, pixel processing is performed on the preliminary binary image according to the target processing parameter set and the heat accumulation risk map to obtain an optimized binary image, specifically including: extracting a heat value threshold, a local pixel threshold, a preset statistical length, and a preset replacement length from the target processing parameter set; obtaining the heat accumulation value of the target black pixel point at the corresponding position on the heat accumulation risk map, the target black pixel point being any one of the multiple black pixel points included in the preliminary binary image; if the heat accumulation value is greater than or equal to the heat value threshold, a first statistical window with a side length of the preset statistical length is determined with the target black pixel point as the center, and the number of black pixels in the first statistical window is calculated; if the number of black pixels is greater than or equal to the local pixel threshold, a target replacement area with a side length of the preset replacement length is determined with the target black pixel point as the center, and all white pixels in the target replacement area are searched; a target white pixel point is determined from the multiple white pixel points, and the target black pixel point is pixel-position replaced according to the target white pixel point to obtain the optimized binary image.
[0013] By employing the above technical solution, key parameters such as the calorific value threshold, local pixel threshold, preset statistical length, and preset displacement length are first extracted from the target processing parameter set. Then, for each black pixel in the preliminary binary image, the cumulative calorific value at its corresponding location on the cumulative calorific value risk map is obtained and compared with the calorific value threshold. If the cumulative calorific value exceeds the threshold, a first statistical window with a side length of the preset statistical length is defined centered on the black pixel, and the number of black pixels within the window is calculated. If the number of black pixels exceeds the local pixel threshold, a target displacement region with a side length of the preset displacement length is defined centered on the black pixel, and all white pixels within the region are searched. A target white pixel is selected from the white pixels and positionally replaced with the target black pixel to obtain an optimized binary image. This pixel processing method effectively identifies local areas with high thermal risk in the preliminary binary image and reduces local printing heat through pixel replacement, thereby improving printing reliability. Furthermore, the introduction of the target processing parameter set allows the pixel processing process to be adaptively adjusted based on different image types, improving the targeted and effective processing.
[0014] Optionally, determining a target white pixel point from a plurality of white pixel points, and performing pixel position replacement on the target black pixel point according to the target white pixel point to obtain the optimized binary image specifically includes: for each white pixel point, determining a second statistical window with a side length of the preset statistical length with the white pixel point as the center, and calculating the number of black pixels within the second statistical window; counting the number of black pixels within the second statistical window corresponding to each white pixel point in the target replacement area, and determining the white pixel point corresponding to the lowest number of black pixels as the target white pixel point; if the number of black pixels of the target white pixel point in the corresponding second statistical window is less than the number of black pixels of the target black pixel point in the corresponding first statistical window, setting the color of the target black pixel point to white and the color of the target white pixel point to black, completing the pixel position replacement, and obtaining the optimized binary image.
[0015] By adopting the above technical solution, for each white pixel in the target replacement area, a second statistical window with a preset statistical length as the center is determined, and the number of black pixels in the window is calculated. The number of black pixels in the second statistical window corresponding to each white pixel is counted, and the white pixel corresponding to the lowest number of black pixels is determined as the target white pixel. If the number of black pixels in the second statistical window corresponding to the target white pixel is less than the number of black pixels in the first statistical window corresponding to the target black pixel, the pixel position is replaced, and the target black pixel is set to white and the target white pixel is set to black. Selecting the target white pixel in this way can ensure that the density of black pixels in the local area after replacement is reduced, and the printing heat risk is reduced. At the same time, by comparing the number of black pixels in the first statistical window and the second statistical window, unnecessary replacement operations can be avoided and the impact on image quality can be reduced. The optimized binary image obtained after pixel replacement reduces the risk of local high heat and improves printing reliability while maintaining the visual effect of the original image.
[0016] Optionally, the resampling of the optimized binarized image to generate final print image data adapted to the target printer resolution specifically includes: obtaining device resolution information of the target printer; determining a resampling ratio based on the device resolution information; using a preset interpolation algorithm to resample the optimized binarized image according to the resampling ratio to generate an intermediate grayscale image; performing secondary binarization processing on the intermediate grayscale image to generate a final binarized image matching the device resolution information; and converting the final binarized image into a data format supported by the target printer to generate the final print image data.
[0017] By employing the above technical solution, the device resolution information of the target printer is first obtained, and a resampling ratio is determined based on the device resolution information. Then, using a preset interpolation algorithm, the optimized binary image is resampled according to the resampling ratio to generate an intermediate grayscale image. The intermediate grayscale image is then subjected to a secondary binarization process to generate a final binary image that matches the device resolution information. Finally, the final binary image is converted into a data format supported by the target printer to generate the final print image data. The resampling process adjusts the resolution of the optimized binary image to match the target printer, ensuring that the size and pixel density of the printed image conform to the printer's physical characteristics. The interpolation algorithm maintains image smoothness and continuity while adjusting the resolution, reducing aliasing and distortion. The secondary binarization process converts the resampled grayscale image back into a black and white binary image that matches the printer's output characteristics. The resulting print image data, after format conversion, can be correctly recognized and processed by the printer, ensuring accurate and consistent print results.
[0018] Optionally, the secondary binarization processing of the intermediate grayscale image to generate a final binarized image matching the device resolution information specifically includes: obtaining the maximum number of grayscale levels supported by the target printer; dividing the grayscale value range of the intermediate grayscale image into multiple sub-intervals according to the maximum number of grayscale levels, each sub-interval corresponding to a grayscale level; traversing each pixel of the intermediate grayscale image to determine the sub-interval to which the grayscale value of each pixel of the intermediate grayscale image belongs; setting the grayscale level of each pixel of the intermediate grayscale image to the grayscale level corresponding to the sub-interval according to the sub-interval to which the grayscale value of each pixel of the intermediate grayscale image belongs; and generating the final binarized image according to the grayscale level of each pixel of the intermediate grayscale image.
[0019] By employing the above technical solution, the maximum number of grayscale levels supported by the target printer is first determined. Based on this number of grayscale levels, the grayscale value range of the intermediate grayscale image is divided into multiple subranges, each corresponding to a grayscale level. Next, each pixel in the intermediate grayscale image is traversed to determine the subrange to which its grayscale value belongs, and the grayscale level of the pixel is set to the grayscale level of the corresponding subrange. Finally, a final binarized image is generated based on the grayscale level of each pixel. This secondary binarization method fully utilizes the grayscale capabilities of the target printer, mapping continuous grayscale values to discrete grayscale levels and enhancing the image's layering and detail. The grayscale level division takes into account the physical characteristics of the printer, ensuring that the resulting binarized image will display a rich grayscale effect on the printer. Furthermore, by setting the grayscale level, further optimization and enhancement can be performed on the image after binarization, such as contrast adjustment and edge sharpening, to improve the visual quality of the printed image. The resulting binarized image matches the device resolution, enabling high-quality, high-fidelity output on the target printer.
[0020] In a second aspect of the present application, a thermal printing image processing device is provided, which includes an original image acquisition module, a threshold distribution map generation module, a heat accumulation risk map generation module, a preliminary binarization image generation module, a processing parameter determination module, an optimized binarization image generation module and a resampling module, wherein: the original image acquisition module is used to acquire the original image to be printed and convert the original image into a grayscale image; the threshold distribution map generation module is used to calculate the gradient difference value of the grayscale image in each preset pixel area, and generate a threshold distribution map based on the gradient difference value of each preset pixel area; the heat accumulation risk map generation module is used to generate a heat accumulation risk map based on the weighted grayscale value of the grayscale image in each preset neighborhood window, and the preset neighborhood window is a window centered on a first pixel point, and the first pixel point is any one of the multiple pixel points included in the grayscale image ; The preliminary binary image generation module is used to perform an error diffusion dithering algorithm on the grayscale image according to the threshold distribution map to generate a preliminary binary image; the processing parameter determination module is used to extract the image features of the preliminary binary image, and classify the original image according to the texture information of the original image and the image features to obtain the image type of the original image; the processing parameter determination module is also used to call the target processing parameter set corresponding to the image type from multiple preset processing parameter sets according to the image type; the optimized binary image generation module is used to perform pixel processing on the preliminary binary image according to the target processing parameter set and the heat accumulation risk map to obtain an optimized binary image; the resampling module is used to resample the optimized binary image to generate final printing image data adapted to the target printer resolution, and output the final printing image data.
[0021] In the third aspect of the present application, an electronic device is provided, including a processor, a memory, a user interface and a network interface, the memory is used to store instructions, the user interface and the network interface are both used to communicate with other devices, and the processor is used to execute the instructions stored in the memory so that the electronic device performs any of the methods described above.
[0022] In a fourth aspect of the present application, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores instructions. When the instructions are executed, any one of the methods described above is executed.
[0023] In summary, one or more technical solutions provided in this application have at least the following technical effects or advantages: 1. By dynamically generating threshold distribution maps, heat accumulation risk maps, and calling target processing parameter sets based on image classification, combined with error diffusion dithering algorithms and pixel optimization processing, high-quality conversion of grayscale images to binary images is achieved, effectively improving the detail expression and layering of printed images, while alleviating the heat accumulation effect in dark areas and reducing image blur and distortion problems caused by overheating of the print head, thereby significantly improving the overall print quality and print clarity of thermal printing. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 This is a flow chart of a thermal printing image processing method disclosed in an embodiment of the present application; Figure 2 This is a module diagram of a thermal printing image processing device disclosed in an embodiment of the present application; Figure 3 This is a structural diagram of an electronic device disclosed in an embodiment of the present application.
[0025] Explanation of the accompanying drawings: 201, original image acquisition module; 202, threshold distribution map generation module; 203, heat accumulation risk map generation module; 204, preliminary binarized image generation module; 205, processing parameter determination module; 206, optimized binarized image generation module; 207, resampling module; 300, electronic device; 301, processor; 302, communication bus; 303, user interface; 304, network interface; 305, memory. DETAILED DESCRIPTION
[0026] In order to enable those skilled in the art to better understand the technical solutions in this specification, the technical solutions in the embodiments of this specification will be clearly and completely described below in conjunction with the drawings in the embodiments of this specification. Obviously, the described embodiments are only part of the embodiments of this application, not all of the embodiments.
[0027] In the description of the embodiments of this application, words such as "for example" or "for instance" are used to indicate examples, illustrations, or explanations. Any embodiment or design described as "for example" or "for instance" in the embodiments of this application should not be construed as being preferred or advantageous over other embodiments or designs. Rather, the use of words such as "for example" or "for instance" is intended to present the relevant concepts in a concrete manner.
[0028] In the description of the embodiments of the present application, the term "multiple" means two or more. For example, multiple systems refer to two or more systems, and multiple screen terminals refer to two or more screen terminals. In addition, the terms "first" and "second" are used for descriptive purposes only and are not to be understood as indicating or implying relative importance or implicitly indicating the indicated technical features. Thus, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized.
[0029] This application provides a thermal printing image processing method, referring to Figure 1 , Figure 1 This is a flow chart of a thermal printing image processing method provided by an embodiment of the present application. The method is applied to a thermal printer, which is used to execute a thermal printing image processing program. The method includes steps S101 to S108, which are as follows: Step S101: obtaining an original image to be printed, and converting the original image into a grayscale image.
[0030] In step S101, the thermal printer can obtain the original image to be printed in a variety of ways. For example, the original image can be transmitted to the thermal printer via a wired or wireless connection from an external device such as a computer, mobile device, or digital camera. The original image can also be directly read from the thermal printer's own storage medium (such as memory or hard disk). The original image is typically a color image, such as image data in the RGB color space.
[0031] After acquiring the original image, the thermal printer converts it to grayscale. This is because thermal printers typically print only in black and white and cannot directly print color images. Converting a color image to grayscale preserves the image's primary visual information while reducing the complexity of subsequent processing.
[0032] Thermal printers can use a variety of color space conversion algorithms to convert original color images into grayscale images. Conversion methods include but are not limited to: Weighted average method: Based on the different perception of red, green, and blue by the human eye, the pixel values of the three RGB channels are weighted averaged. The commonly used weight coefficients are R: 0.299, G: 0.587, and B: 0.114.
[0033] Maximum value method: take the maximum value of the three RGB channels as the grayscale value.
[0034] Average method: directly calculate the arithmetic mean of the three RGB channels as the grayscale value.
[0035] For example, assume the original image is an 800×600 RGB color image. For the pixel at coordinates (100, 200) on the image, its RGB value is (255, 128, 64). Using the weighted average method, its grayscale value can be calculated as: Gray = R × 0.299 + G × 0.587 + B × 0.114 = 255 × 0.299 + 128 × 0.587 + 64 × 0.114 ≈ 158; therefore, the pixel value at coordinates (100, 200) in the converted grayscale image is 158. The thermal printer performs the above calculations on each pixel of the original image, ultimately producing a grayscale image of the same size as the original image.
[0036] Step S102: calculating the gradient difference value of the grayscale image in each preset pixel area, and generating a threshold distribution map based on the gradient difference value of each preset pixel area.
[0037] In step S102, the gradient difference value of the grayscale image in each preset pixel area is calculated, and a threshold distribution map is generated based on the gradient difference value of each preset pixel area, specifically including: dividing the grayscale image into multiple preset pixel areas of the same size; for each preset pixel area, calculating the grayscale difference between the target pixel point in the preset pixel area and the adjacent pixel point corresponding to the target pixel point, to obtain multiple grayscale difference values in the preset pixel area, where the target pixel point is any pixel point in the preset pixel area; determining the weight coefficient of the corresponding grayscale difference value according to the position of the target pixel point in the preset pixel area; multiplying each grayscale difference value by the corresponding weight coefficient, and summing and averaging them to obtain the gradient difference value of the preset pixel area; determining the grayscale threshold corresponding to the gradient difference value according to a preset threshold distribution function, where the preset threshold distribution function includes the correspondence between the gradient difference value and the grayscale threshold; constructing a threshold distribution matrix according to the grayscale threshold of each preset pixel area, where the elements of the threshold distribution matrix are the grayscale thresholds; and performing interpolation and smoothing processing on the threshold distribution matrix to obtain a threshold distribution map with the same resolution as the original image.
[0038] Specifically, the thermal printer divides the acquired grayscale image into multiple preset pixel areas of the same size. These preset pixel areas can be square, rectangular, or other regular shapes, and the area size can be set according to actual needs, such as 8×8, 16×16, etc.
[0039] Next, for each preset pixel region, the thermal printer calculates the grayscale difference between the target pixel and its adjacent pixels within that region. The target pixel can be any pixel in the preset pixel region, and the adjacent pixels are pixels adjacent to the target pixel in the horizontal, vertical, or diagonal direction. By calculating the grayscale difference between the target pixel and its adjacent pixels, multiple grayscale difference values within the preset pixel region can be obtained.
[0040] To more accurately reflect the local features of an image, the thermal printer determines the weight coefficient for the corresponding grayscale difference based on the target pixel's position within a preset pixel area. For example, a Gaussian weighting function can be used to assign a higher weight to the target pixel at the center of the area and a lower weight to the target pixel at the edge of the area.
[0041] Then, the thermal printer multiplies each grayscale difference by the corresponding weight coefficient, and sums and averages them to obtain the gradient difference value of the preset pixel area. The gradient difference value reflects the severity of the grayscale change of the pixels in the area. The larger the gradient difference value, the more obvious the grayscale change in the area. According to the preset threshold distribution function, the thermal printer can determine the grayscale threshold corresponding to the gradient difference value of each preset pixel area. The threshold distribution function describes the correspondence between the gradient difference value and the grayscale threshold, and can adopt various forms such as linear function, exponential function, logarithmic function, etc. After obtaining the grayscale threshold of each preset pixel area, the thermal printer constructs a threshold distribution matrix based on these grayscale thresholds. The elements of the threshold distribution matrix are the grayscale thresholds corresponding to each preset pixel area.
[0042] Finally, to obtain a threshold distribution map with the same resolution as the original image, the thermal printer performs interpolation and smoothing on the threshold distribution matrix. Interpolation algorithms, such as bilinear interpolation and bicubic interpolation, can generate transition thresholds between preset pixel regions, making the threshold distribution smoother and more natural.
[0043] For example, suppose a 1024×768 grayscale image is divided into 128×96 8×8 preset pixel regions. For the preset pixel region where the target pixel with coordinates (100, 200) is located, the thermal printer calculates the grayscale differences between all pixels in the region and their adjacent pixels, obtaining 64 grayscale differences. The weight coefficient for each grayscale difference is then determined using a Gaussian weight function, and the weighted grayscale differences are summed and averaged to obtain a gradient difference value of 20 for the region. According to the preset linear threshold distribution function, a gradient difference value of 20 corresponds to a grayscale threshold of 180. Repeating the above process yields a 128×96 threshold distribution matrix. Finally, the threshold distribution matrix is smoothed into a 1024×768 threshold distribution map through bilinear interpolation.
[0044] Step S103: Generate a heat accumulation risk map based on the weighted grayscale values of the grayscale image in each preset neighborhood window, where the preset neighborhood window is a window centered on the first pixel point, and the first pixel point is any pixel point among the multiple pixels included in the grayscale image.
[0045] In step S103, a heat accumulation risk map is generated based on the weighted grayscale values of the grayscale image in each preset neighborhood window, specifically including: calculating the Euclidean distance between each second pixel point in the preset neighborhood window and the first pixel point as the center of the window, and based on the Euclidean distance, generating a Gaussian weight coefficient for the first pixel point and the second pixel point by a preset Gaussian function, where the second pixel point is any pixel point in the preset neighborhood window except the first pixel point; multiplying the grayscale values of the first pixel point and the second pixel point by the Gaussian weight coefficient, and accumulating all the product results to obtain a weighted grayscale value; normalizing the weighted grayscale value to obtain the heat accumulation value of the grayscale image in each preset neighborhood window, and generating a heat accumulation risk map based on the heat accumulation value.
[0046] Specifically, the thermal printer uses any pixel in the grayscale image as the first pixel and sets a neighborhood window of a preset size centered on the first pixel. The size of the neighborhood window can be set according to actual needs, with common sizes including 3×3, 5×5, and 7×7.
[0047] Next, for each pixel point within the preset neighborhood window (i.e., the second pixel point), the thermal printer calculates the Euclidean distance between it and the first pixel point, which is the center of the window. The Euclidean distance reflects the spatial distance between two pixels on the image plane and can be calculated using the following formula: distance=sqrt((x1-x2)^2+(y1-y2)^2); Wherein, (x1, y1) and (x2, y2) are the coordinates of the first pixel and the second pixel respectively.
[0048] The thermal printer then generates Gaussian weight coefficients for the first and second pixels based on the calculated Euclidean distance using a preset Gaussian function. The Gaussian function is a commonly used weight assignment function that assigns different weights based on the distance between pixels. Closer pixels have higher weights, while farther pixels have lower weights. The formula for the Gaussian function is as follows: weight=exp(-(distance^2) / (2*sigma^2)); Among them, sigma is the standard deviation of the Gaussian function, which can control the speed of weight decay.
[0049] For each pixel within a preset neighborhood window, the thermal printer multiplies its grayscale value by the corresponding Gaussian weight coefficient and accumulates all the products to obtain the weighted grayscale value of the neighborhood window. The weighted grayscale value comprehensively considers the grayscale information and spatial position of the pixels within the neighborhood, and can better reflect the grayscale distribution characteristics of the local area.
[0050] To facilitate subsequent processing, the thermal printer normalizes the weighted grayscale values and maps them to the range of [0, 1] to obtain the cumulative heat value for the neighborhood window. A larger cumulative heat value indicates a higher grayscale value in the local area, and a greater amount of heat is required during printing.
[0051] By repeating this process, the thermal printer can calculate the cumulative heat value for each pixel in the grayscale image's neighborhood window, generating a cumulative heat risk map of the same size as the grayscale image. The grayscale value of each pixel in the cumulative heat risk map corresponds to the cumulative heat value of the pixel at the same location in the original grayscale image.
[0052] For example, assume that in a 512×512 grayscale image, the pixel at coordinates (100, 200) is the first pixel, and a 5×5 preset neighborhood window is set. The thermal printer first calculates the Euclidean distance between the first pixel and the remaining 24 pixels in the neighborhood window. Then, a weight coefficient is calculated for each pixel using a Gaussian function. If the grayscale value of the first pixel is 150, the grayscale value of its right neighboring pixel is 180, the Euclidean distance between the two pixels is 1, and the standard deviation of the Gaussian function is set to 1.5, then the Gaussian weight coefficient for this neighboring pixel is exp(-(1^2) / (2*1.5^2))≈0.8825. Multiplying 180 by 0.8825 yields a weighted grayscale value of 158.85 for this neighboring pixel. This calculation is repeated for all pixels in the neighborhood window, and the results are summed to obtain the weighted grayscale value of the neighborhood window. After normalization, the cumulative heat value of the neighborhood window is obtained. The above process is repeated for each pixel of the grayscale image to finally generate a cumulative heat risk map.
[0053] Step S104: performing an error diffusion dithering algorithm on the grayscale image according to the threshold distribution map to generate a preliminary binary image.
[0054] In step S104 , the thermal printer obtains the threshold distribution map generated in step S102 . The threshold distribution map has the same resolution as the original grayscale image, and each pixel in the map corresponds to a grayscale threshold.
[0055] The thermal printer then processes each pixel in the grayscale image, one by one, from left to right and top to bottom. For each pixel being processed, the thermal printer compares its grayscale value with the grayscale threshold at the corresponding location in the threshold distribution map. If the pixel's grayscale value is greater than or equal to the corresponding grayscale threshold, the pixel's binarization result is set to 1 (white); otherwise, the pixel's binarization result is set to 0 (black).
[0056] After completing the binarization decision for the current pixel, the thermal printer calculates the quantization error for that pixel, which is the difference between the original grayscale value and the binarized result. For example, if the grayscale value of the current pixel is 200 and the corresponding grayscale threshold is 180, the binarization result of the pixel is 1, and the quantization error is 200-255=-55.
[0057] Next, the thermal printer uses a preset error diffusion filter to distribute the quantization error of the current pixel to its adjacent unprocessed pixels according to certain weights. Common error diffusion filters include the Floyd-Steinberg filter, the Jarvis filter, and the Stucki filter, which define different error distribution weights. Taking the Floyd-Steinberg filter as an example, its error distribution weights are as follows: Among them, * represents the pixel currently being processed, and the values on the right, below, and lower right represent the weights of assigning quantization errors to the pixels at the corresponding positions.
[0058] The thermal printer multiplies the quantization error of the current pixel by the corresponding weight and adds the result to the grayscale values of adjacent unprocessed pixels to influence their subsequent binarization decisions. For example, if the quantization error of the current pixel is -55, the grayscale value of the pixel to its right will increase by -55×7 / 16≈-24, the grayscale value of the pixel below it will increase by -55×5 / 16≈-17, and the grayscale value of the pixel to its lower right will increase by -55×1 / 16≈-3.
[0059] Repeat the above process until all pixels in the grayscale image have been processed, resulting in a preliminary binarized image. Because the error diffusion dithering algorithm considers the mutual influence between pixels and diffuses the quantization error within a local area, the resulting binarized image better preserves the details and texture information of the original image, avoiding the problems of false contours and discontinuities caused by traditional fixed-threshold binarization methods.
[0060] For example, assume that in a 512×512 grayscale image, the grayscale value of the pixel at coordinates (100, 200) is 200, and the grayscale threshold at the corresponding position in the threshold distribution map is 180. According to the binarization decision rule, the binarization result of this pixel is 1, and the quantization error is -55. Using the Floyd-Steinberg filter for error diffusion, the grayscale values of the pixels at coordinates (101, 200), (100, 201), and (101, 201) will increase by -24, -17, and -3, respectively. Performing this process on each pixel in the grayscale image yields a preliminary binarized image.
[0061] Step S105: extracting image features of the preliminary binarized image, and classifying the original image according to the texture information and image features of the original image to obtain the image type of the original image.
[0062] In step S105, the thermal printer performs feature extraction on the preliminary binary image generated in step S104. Contour features: The thermal printer uses a contour extraction algorithm, such as the Canny edge detection algorithm, to extract the target object's contour information from the preliminary binary image and calculate geometric features such as the contour's perimeter, area, and circularity. Skeleton features: The thermal printer uses a skeleton extraction algorithm, such as the Zhang-Suen thinning algorithm, to extract the target object's skeleton information from the preliminary binary image and calculate topological features such as the skeleton's length, number of endpoints, and number of intersections.
[0063] Connected domain features: The thermal printer uses a connected domain analysis algorithm, such as a two-pass scanning algorithm, to extract connected regions from the initial binary image and calculate the number, size, density and other distribution features of the connected regions.
[0064] The thermal printer then extracts texture features from the original image. Texture is the repetitive pattern of grayscale distribution in a local area of the image, reflecting the roughness and directionality of the image surface. Texture feature extraction methods include: Grayscale co-occurrence matrix: The thermal printer constructs a grayscale co-occurrence matrix by calculating the grayscale relationship between pixel pairs in the original image, and extracts texture statistical features such as energy, entropy, contrast, and homogeneity from the matrix.
[0065] Gabor filtering: The thermal printer uses Gabor filters of different scales and directions to convolve the original image to obtain a set of Gabor feature maps, and calculates statistics such as the mean and variance of each feature map to form a Gabor texture feature vector.
[0066] Wavelet transform: The thermal printer performs multi-scale wavelet decomposition on the original image to obtain low-frequency approximate coefficients and high-frequency detail coefficients, and calculates the energy, mean and other statistics of each sub-band coefficient to form a wavelet texture feature vector.
[0067] After extracting the image features of the preliminary binary image and the texture features of the original image, the thermal printer fuses these features to form a comprehensive image feature vector.
[0068] Finally, the thermal printer uses a pre-trained classifier model to classify the image feature vector and determine the image type of the original image. Common image classifiers include support vector machines (SVMs), decision trees, random forests, and convolutional neural networks (CNNs). These classifiers learn from a large number of training samples to establish a mapping between image features and image types, enabling them to automatically classify new images.
[0069] For example, suppose a thermal printer needs to print a photo for an ID card. The thermal printer first extracts outline, skeleton, and connected domain features from the initial binary image of the ID card photo. It finds a clear facial outline, a sparse skeleton, and a large connected domain. The thermal printer then extracts gray-level co-occurrence matrix features, Gabor features, and wavelet features from the original ID card photo, finding that the image has high contrast, vertical texture, and a predominance of low-frequency components. These features are combined into an image feature vector and input into a pretrained SVM classifier, which classifies the ID card photo as a "portrait."
[0070] Step S106: According to the image type, a target processing parameter set corresponding to the image type is called from a plurality of preset processing parameter sets.
[0071] In step S106, the thermal printer's storage unit pre-sets multiple processing parameter sets, each corresponding to a specific image type, such as text, charts, portraits, and landscapes. These processing parameter sets are a combination of parameters designed to optimize printing performance based on extensive printing practice and expert experience, tailored to the characteristics of different image types.
[0072] In this embodiment, the target processing parameter set includes the following key parameters: Heat value threshold: Indicates the critical point of the pixel's heat accumulation value. Pixels exceeding this threshold are considered high-risk points and require special processing to avoid overheating damage during printing.
[0073] Local pixel threshold: This indicates the critical number of black pixels within a local area centered on the target pixel. Areas exceeding this threshold are considered high-density areas, where excessive concentration of black pixels may lead to localized excessive heat generation, requiring pixel replacement to reduce the risk.
[0074] Preset Statistical Length: This parameter specifies the side length of the square window centered on the target pixel used to count the number of black pixels. This parameter determines the size of the local area and affects the accuracy of high-density area determination.
[0075] Preset Displacement Length: This parameter represents the length of the square area centered on the target pixel, used to search for white pixels to replace. This parameter determines the range of pixel displacement and affects the effectiveness and efficiency of the displacement operation.
[0076] The thermal printer searches for a corresponding target processing parameter set from a preset processing parameter set according to the image type identified in step S105, and extracts specific values of the four key parameters.
[0077] For example, suppose a thermal printer needs to print a QR code image. The image classification module identifies the image as a "chart" type. The thermal printer searches the preset processing parameter set for the "chart" type. This set contains the following parameters: calorific value threshold: 0.85; local pixel threshold: 60%; preset statistic length: 5 pixels; and preset displacement length: 11 pixels.
[0078] This means that for each black pixel in the QR code image, if its cumulative heat value exceeds 0.85, special processing is required; if the number of black pixels in a 5×5 pixel window centered on the pixel exceeds 60%, the area is judged as a high-density area and pixel replacement is required; the replacement operation will search for available white pixels in an 11×11 pixel area centered on the pixel.
[0079] The thermal printer passes these parameter values to the subsequent image optimization module to guide the heat distribution optimization and pixel replacement processing of the binary image.
[0080] Step S107: performing pixel processing on the preliminary binary image according to the target processing parameter set and the heat accumulation risk map to obtain an optimized binary image.
[0081] In step S107, pixel processing is performed on the preliminary binary image according to the target processing parameter set and the heat accumulation risk map to obtain an optimized binary image, specifically including: extracting a heat value threshold, a local pixel threshold, a preset statistical length, and a preset replacement length from the target processing parameter set; obtaining the heat accumulation value of the target black pixel point at the corresponding position on the heat accumulation risk map, where the target black pixel point is any one of the multiple black pixel points included in the preliminary binary image; if the heat accumulation value is greater than or equal to the heat value threshold, a first statistical window with a side length of the preset statistical length is determined with the target black pixel point as the center, and the number of black pixels in the first statistical window is calculated; if the number of black pixels is greater than or equal to the local pixel threshold, a target replacement area with a side length of the preset replacement length is determined with the target black pixel point as the center, and all white pixels in the target replacement area are searched; a target white pixel point is determined from the multiple white pixel points, and the pixel position of the target black pixel point is replaced according to the target white pixel point to obtain an optimized binary image.
[0082] Specifically, the thermal printer extracts parameters related to pixel processing from the target processing parameter set called in step S106, including: Heat value threshold: indicates the critical point of the pixel's heat accumulation value. Pixels exceeding this threshold are considered high-risk points and require special processing.
[0083] Local pixel threshold: It indicates the critical point of the number of black pixels in the local area centered on the target pixel. The area exceeding this threshold is considered to be a high-density area and requires pixel replacement.
[0084] Preset statistical length: Indicates the side length of the square window centered on the target pixel, used to count the number of black pixels.
[0085] Preset replacement length: Indicates the side length of the square area centered on the target pixel, used to search for white pixel replacements.
[0086] Then, the thermal printer traverses each black pixel in the preliminary binary image, takes it as the current target black pixel, and obtains the heat accumulation value of the corresponding position of the pixel in the heat accumulation risk map.
[0087] If the cumulative heat value of the target black pixel is greater than or equal to the heat value threshold, it indicates that the printing heat in the area where the pixel is located is too high, which may lead to reduced print quality or damage to the thermal paper. At this time, the thermal printer determines a first counting window with a preset counting length as the center, and counts the number of black pixels within this window.
[0088] If the number of black pixels within the first statistical window is greater than or equal to the local pixel threshold, the density of black pixels surrounding the target black pixel is high, resulting in concentrated printing heat. Pixel replacement is necessary to reduce this local heat. The thermal printer then determines a target replacement area centered on the target black pixel, with a side length of the preset replacement length, and searches for all white pixels within this area.
[0089] From the multiple white pixels within the target replacement area, the thermal printer selects one as the target white pixel based on pre-set rules. After determining the target white pixel, the thermal printer swaps the positions of the target black pixel with the target white pixel, setting the target black pixel to white and the target white pixel to black. This completes a pixel replacement operation, reducing the risk of thermal damage in the localized area.
[0090] Repeat the above process until all black pixels in the initial binary image are processed, resulting in an optimized binary image. Compared to the initial binary image, the optimized binary image maintains the overall visual effect while reducing the risk of printing heat in local high-density areas, thereby improving printing reliability and stability.
[0091] For example, assume that in a 384×384 preliminary binary image, the accumulated heat value of black pixel A at coordinates (100, 200) is 0.8, exceeding the heat value threshold of 0.75. The thermal printer determines a 7×7 first statistical window centered on pixel A and counts 40 black pixels within the window, exceeding the local pixel threshold of 35. The thermal printer then determines a 15×15 target replacement region centered on pixel A and finds 10 white pixels within the region. Based on the closest distance rule, the thermal printer selects white pixel B at coordinates (106, 198) as the target white pixel, sets pixel A to white, and pixel B to black, completing a pixel replacement operation. The above optimization process is performed on all black pixels in the preliminary binary image, ultimately resulting in an optimized binary image with a more balanced printed heat distribution.
[0092] In a possible embodiment, a target white pixel point is determined from a plurality of white pixel points, and a pixel position replacement is performed on a target black pixel point according to the target white pixel point to obtain an optimized binary image, specifically comprising: for each white pixel point, determining a second statistical window with a side length of a preset statistical length with the white pixel point as the center, and calculating the number of black pixels within the second statistical window; counting the number of black pixels within the corresponding second statistical window for each white pixel point in the target replacement area, and determining the white pixel point corresponding to the lowest number of black pixels as the target white pixel point; if the number of black pixels within the corresponding second statistical window of the target white pixel point is less than the number of black pixels within the corresponding first statistical window of the target black pixel point, setting the color of the target black pixel point to white and the color of the target white pixel point to black, completing the pixel position replacement, and obtaining the optimized binary image.
[0093] Specifically, for each white pixel within the target replacement area, the image optimization module determines a second statistical window with the same side length as the first statistical window, centered on that white pixel, and counts the number of black pixels within that window. This step aims to assess the density of black pixels surrounding each white pixel in order to select the target white pixel that will minimize localized heat risk after replacement.
[0094] Next, the image optimization module counts the number of black pixels within the corresponding second statistical window for each white pixel in the target replacement area and finds the minimum value, known as the lowest black pixel count. The white pixel with the lowest black pixel count has the lowest density of surrounding black pixels and, therefore, the smallest impact on local heat after replacement, and is therefore identified as the target white pixel.
[0095] After selecting the target white pixel, the image optimization module further compares the number of black pixels within the second statistical window corresponding to the target white pixel with the number of black pixels within the first statistical window corresponding to the target black pixel. Only when the number of black pixels surrounding the target white pixel is less than the number of black pixels surrounding the target black pixel, the pixel position permutation operation is performed, i.e., the color of the target black pixel is set to white and the color of the target white pixel is set to black. The purpose of this step is to ensure that the density of black pixels in the local area is indeed reduced after the permutation, avoiding unnecessary pixel permutation and image distortion.
[0096] After completing the optimization processing of all target black pixels, the image optimization module outputs an optimized binary image, which realizes the migration of black pixels from local high-density areas to low-density areas, effectively reducing the risk of printing heat while maintaining the overall quality and details of the image as much as possible.
[0097] For example, assume that in a 512×512 initially binarized image, a black pixel P at coordinates (200, 300) is identified as the target black pixel. The corresponding first statistical window size is 9×9, and there are 60 black pixels within the window. Within the 21×21 target replacement region centered on pixel P, the image optimization module finds five white pixels: Q1, Q2, Q3, Q4, and Q5.
[0098] For each white pixel, the image optimization module calculates the number of black pixels in the corresponding 9×9 second statistical window. The results are as follows: Q1: 45 black pixels; Q2: 50 black pixels; Q3: 38 black pixels; Q4: 55 black pixels; Q5: 42 black pixels; Q3 has the fewest black pixels (38) within its corresponding second statistical window and is therefore identified as the target white pixel. Since 38 is less than the 60 black pixels of target black pixel P within the first statistical window, the replacement condition is met. The image optimization module then sets the color of pixel P to white and the color of pixel Q3 to black, completing a pixel position replacement operation.
[0099] This optimization process is repeated for all target black pixels in the initial binary image, ultimately resulting in an optimized binary image with a more balanced local heat distribution. Compared to replacement strategies based on a single distance or heat index, this optimization method based on local black pixel density more comprehensively assesses the impact of the replacement operation on image quality and printing heat. While reducing thermal risks, it better protects the integrity and aesthetics of the image, improving the overall thermal printing effect.
[0100] Step S108: resampling the optimized binary image to generate final printing image data adapted to the target printer resolution, and outputting the final printing image data.
[0101] In step S108, the optimized binarized image is resampled to generate final printing image data adapted to the target printer resolution, specifically including: obtaining the device resolution information of the target printer; determining the resampling ratio based on the device resolution information; using a preset interpolation algorithm to resample the optimized binarized image according to the resampling ratio to generate an intermediate grayscale image; performing secondary binarization processing on the intermediate grayscale image to generate a final binarized image that matches the device resolution information; and converting the final binarized image into a data format supported by the target printer to generate final printing image data.
[0102] Specifically, the thermal printer obtains the target printer's device resolution information. Device resolution indicates the number of dots per inch (dpi) a printer can print in both the horizontal and vertical directions, reflecting the printer's physical output capabilities. For example, a thermal printer might have a device resolution of 203 dpi x 203 dpi, meaning it can print 203 dots per inch in each direction.
[0103] The thermal printer then determines the resampling ratio based on the device resolution information. The resampling ratio represents the scaling relationship between the optimized binary image and the final printed image in terms of resolution. Often, the resolution of the optimized binary image doesn't exactly match the device resolution, necessitating resampling to adjust the image's size and pixel density to accommodate the physical characteristics of the printer.
[0104] After determining the resampling ratio, the thermal printer uses a preset interpolation algorithm to resample the optimized binary image according to the resampling ratio, generating an intermediate grayscale image. Common interpolation algorithms include nearest neighbor interpolation, bilinear interpolation, and bicubic interpolation, each of which offers varying trade-offs between speed and quality. During the resampling process, the interpolation algorithm estimates the pixel values at corresponding locations in the new image based on the positions and grayscale values of pixels in the original image, thereby achieving smooth image scaling.
[0105] Because the resampled intermediate grayscale image contains multiple grayscale levels, and the thermal printer can only output in black and white, the thermal printer needs to perform a secondary binarization process on the intermediate grayscale image to generate a final binary image that matches the device resolution information. The secondary binarization process can use a method similar to step S104, such as a fixed threshold method or an adaptive threshold method, to convert the grayscale image into a binary image containing only black and white pixels.
[0106] Finally, the thermal printer converts the final binary image into a data format supported by the target printer, generating the final print image data. Different thermal printer models may support different data formats, such as ESC / POS, PCL, and ZPL. The thermal printer must encode the binary image data into appropriate print commands and control sequences based on the target printer's specifications and interface to ensure the printer can correctly recognize and process the image data.
[0107] After generating the final print image data, the thermal printer outputs it to the print buffer or data interface for the printer to perform subsequent printing operations.
[0108] For example, assume the thermal printer's device resolution is 300 dpi × 300 dpi, while the optimized binary image's resolution is 200 dpi × 200 dpi. The thermal printer calculates a resampling ratio of 1.5, meaning the image size needs to be enlarged by 1.5 times. The thermal printer then uses a bilinear interpolation algorithm to resample the optimized binary image, generating a mid-grayscale image with a resolution of 300 dpi × 300 dpi. The thermal printer then uses an adaptive thresholding method to binarize the mid-grayscale image, resulting in a final binarized image that matches the device resolution. Finally, the thermal printer converts the final binarized image into ESC / POS format print data and sends it to the thermal printer via a USB interface, completing the image resampling and data preparation process.
[0109] In one possible implementation, a secondary binarization process is performed on the intermediate grayscale image to generate a final binarized image that matches the device resolution information, specifically including: obtaining the maximum number of grayscale levels supported by the target printer; dividing the grayscale value range of the intermediate grayscale image into multiple sub-intervals based on the maximum number of grayscale levels, with each sub-interval corresponding to a grayscale level; traversing each pixel of the intermediate grayscale image to determine the sub-interval to which the grayscale value of each pixel of the intermediate grayscale image belongs; setting the grayscale level of each pixel of the intermediate grayscale image to the grayscale level corresponding to the sub-interval based on the sub-interval to which the grayscale value of each pixel of the intermediate grayscale image belongs; and generating a final binarized image based on the grayscale levels of each pixel of the intermediate grayscale image.
[0110] Specifically, the thermal printer obtains the maximum number of grayscale levels supported by the target printer. Different thermal printer models may support different numbers of grayscale levels, such as 2 (black and white), 4, or 16. A greater number of grayscale levels allows the printer to display a richer range of grayscale levels, but this also places higher demands on image data processing and transmission.
[0111] The thermal printer then divides the grayscale value range of the intermediate grayscale image into multiple subranges based on the maximum number of grayscale levels, with each subrange corresponding to a grayscale level. For example, if the printer supports four grayscale levels, the grayscale value range of 0-255 can be divided into four equal subranges: [0, 63], [64, 127], [128, 191], and [192, 255], corresponding to grayscale levels 0, 1, 2, and 3, respectively.
[0112] After dividing the grayscale value into subintervals, the thermal printer traverses each pixel of the intermediate grayscale image to determine the subinterval to which its grayscale value belongs. This step can be achieved through simple value comparison and range judgment.
[0113] Next, the thermal printer sets the grayscale level of each pixel to the grayscale level corresponding to the subinterval to which the grayscale value belongs. In this way, the originally continuous grayscale value is mapped to a discrete grayscale level, achieving grayscale quantization.
[0114] Finally, the thermal printer generates a final binary image based on the grayscale level of each pixel in the intermediate grayscale image. Specifically, for each pixel, the thermal printer compares its grayscale level with a preset threshold. If the grayscale level is greater than or equal to the threshold, the pixel is set to white (0); otherwise, it is set to black (1). This results in a binary image containing only black and white colors, matching the device resolution information.
[0115] For example, assume that the thermal printer supports 16 grayscale levels and the resolution of the intermediate grayscale image is 300dpi×300dpi. The thermal printer divides the grayscale value range of 0 to 255 into 16 subintervals, each with a span of 16. The thermal printer then traverses each pixel of the intermediate grayscale image. For example, if the grayscale value of pixel A at coordinates (100, 200) is 135, it belongs to the 9th subinterval [128, 143], and the corresponding grayscale level is 8. The thermal printer sets the grayscale level of pixel A to 8. After completing the above processing for all pixels of the intermediate grayscale image, the thermal printer selects grayscale level 8 as the binarization threshold, sets pixels with grayscale levels greater than or equal to 8 to white, and pixels with grayscale levels less than 8 to black, ultimately generating a 300dpi×300dpi binary image.
[0116] Reference Figure 2, the present application also provides a thermal printing image processing device, which is a server, and the server includes an original image acquisition module 201, a threshold distribution map generation module 202, a heat accumulation risk map generation module 203, a preliminary binarized image generation module 204, a processing parameter determination module 205, an optimized binarized image generation module 206 and a resampling module 207, wherein: the original image acquisition module 201 is used to acquire the original image to be printed and convert the original image into a grayscale image; the threshold distribution map generation module 202 is used to calculate the gradient difference value of the grayscale image in each preset pixel area, and generate a threshold distribution map based on the gradient difference value of each preset pixel area; the heat accumulation risk map generation module 203 is used to generate a heat accumulation risk map based on the weighted grayscale value of the grayscale image in each preset neighborhood window, the preset neighborhood window is a window centered on the first pixel point, and the first pixel point is the grayscale image any one of the multiple pixel points included in the image; a preliminary binary image generation module 204 is used to perform an error diffusion dithering algorithm on the grayscale image according to the threshold distribution map to generate a preliminary binary image; a processing parameter determination module 205 is used to extract image features of the preliminary binary image, and classify the original image according to the texture information and image features of the original image to obtain the image type of the original image; the processing parameter determination module 205 is also used to call a target processing parameter set corresponding to the image type from a plurality of preset processing parameter sets according to the image type; an optimized binary image generation module 206 is used to perform pixel processing on the preliminary binary image according to the target processing parameter set and the heat accumulation risk map to obtain an optimized binary image; a resampling module 207 is used to resample the optimized binary image to generate final printing image data adapted to the target printer resolution, and output the final printing image data.
[0117] In a possible implementation, the threshold distribution map generation module 202 calculates the gradient difference value of the grayscale image in each preset pixel area, and generates a threshold distribution map based on the gradient difference value of each preset pixel area, specifically including: the threshold distribution map generation module 202 divides the grayscale image into a plurality of preset pixel areas of the same size; for each preset pixel area, the threshold distribution map generation module 202 calculates the grayscale difference between the target pixel point in the preset pixel area and the adjacent pixel points corresponding to the target pixel point, and obtains a plurality of grayscale differences in the preset pixel area, where the target pixel point is any pixel point in the preset pixel area; the threshold distribution map generation module 202 calculates the grayscale difference between the target pixel point in the preset pixel area and the adjacent pixel points corresponding to the target pixel point according to the grayscale difference between the target pixel point and the adjacent pixel points corresponding to the target pixel point; The position of the image is determined to determine the weight coefficient of the corresponding grayscale difference; the threshold distribution map generation module 202 multiplies each grayscale difference by the corresponding weight coefficient, and sums and averages them to obtain the gradient difference value of the preset pixel area; the threshold distribution map generation module 202 determines the grayscale threshold corresponding to the gradient difference value according to the preset threshold distribution function, and the preset threshold distribution function includes the correspondence between the gradient difference value and the grayscale threshold; the threshold distribution map generation module 202 constructs a threshold distribution matrix according to the grayscale threshold of each preset pixel area, and the elements of the threshold distribution matrix are the grayscale thresholds; the threshold distribution map generation module 202 performs interpolation and smoothing processing on the threshold distribution matrix to obtain a threshold distribution map with the same resolution as the original image.
[0118] In one possible embodiment, the heat accumulation risk map generation module 203 generates a heat accumulation risk map based on the weighted grayscale values of the grayscale image in each preset neighborhood window, specifically including: the heat accumulation risk map generation module 203 calculates the Euclidean distance between each second pixel point in the preset neighborhood window and the first pixel point as the center of the window, and based on the Euclidean distance, generates a Gaussian weight coefficient for the first pixel point and the second pixel point through a preset Gaussian function, where the second pixel point is any pixel point in the preset neighborhood window except the first pixel point; the heat accumulation risk map generation module 203 multiplies the grayscale values of the first pixel point and the second pixel point with the Gaussian weight coefficient, and accumulates all the product results to obtain a weighted grayscale value; the heat accumulation risk map generation module 203 normalizes the weighted grayscale value to obtain the heat accumulation value of the grayscale image in each preset neighborhood window, and generates a heat accumulation risk map based on the heat accumulation value.
[0119] In one possible implementation, the optimized binary image generation module 206 performs pixel processing on the preliminary binary image according to the target processing parameter set and the heat accumulation risk map to obtain an optimized binary image, specifically including: the optimized binary image generation module 206 extracts the heat value threshold, the local pixel threshold, the preset statistical length, and the preset replacement length from the target processing parameter set; the optimized binary image generation module 206 obtains the heat accumulation value of the target black pixel point at the corresponding position on the heat accumulation risk map, where the target black pixel point is any one of the multiple black pixel points included in the preliminary binary image; if the heat accumulation value is greater than or equal to the heat value threshold, then The optimized binary image generation module 206 determines a first statistical window with a side length of a preset statistical length with the target black pixel point as the center, and calculates the number of black pixels in the first statistical window; if the number of black pixels is greater than or equal to the local pixel threshold, the optimized binary image generation module 206 determines a target replacement area with a side length of a preset replacement length with the target black pixel point as the center, and searches for all white pixels in the target replacement area; the optimized binary image generation module 206 determines a target white pixel point from multiple white pixel points, and performs pixel position replacement on the target black pixel point according to the target white pixel point to obtain an optimized binary image.
[0120] In one possible implementation, the optimized binary image generation module 206 determines a target white pixel from a plurality of white pixels, and performs pixel position replacement on a target black pixel according to the target white pixel to obtain an optimized binary image. Specifically, the optimized binary image generation module 206 determines, for each white pixel, a second statistical window with a side length of a preset statistical length centered on the white pixel, and calculates the number of black pixels within the second statistical window; the optimized binary image generation module 206 counts the number of black pixels within the corresponding second statistical window for each white pixel in the target replacement area, and determines the white pixel corresponding to the lowest number of black pixels as the target white pixel; the optimized binary image generation module 206 determines the white pixel corresponding to the lowest number of black pixels as the target white pixel; and if the number of black pixels within the corresponding second statistical window for the target white pixel is less than the number of black pixels within the corresponding first statistical window for the target black pixel, the optimized binary image generation module 206 sets the color of the target black pixel to white and the color of the target white pixel to black, completing the pixel position replacement to obtain the optimized binary image.
[0121] In one possible implementation, the resampling module 207 resamples the optimized binary image to generate final print image data adapted to the target printer resolution, specifically including: the resampling module 207 obtains the device resolution information of the target printer; the resampling module 207 determines the resampling ratio based on the device resolution information; the resampling module 207 uses a preset interpolation algorithm to resample the optimized binary image according to the resampling ratio to generate an intermediate grayscale image; the resampling module 207 performs secondary binarization processing on the intermediate grayscale image to generate a final binarized image that matches the device resolution information; the resampling module 207 converts the final binarized image into a data format supported by the target printer to generate final print image data.
[0122] In one possible implementation, the resampling module 207 performs secondary binarization processing on the intermediate grayscale image to generate a final binarized image that matches the device resolution information, specifically including: the resampling module 207 obtains the maximum number of grayscale levels supported by the target printer; the resampling module 207 divides the grayscale value range of the intermediate grayscale image into multiple sub-intervals according to the maximum number of grayscale levels, and each sub-interval corresponds to a grayscale level; the resampling module 207 traverses each pixel of the intermediate grayscale image to determine the sub-interval to which the grayscale value of each pixel of the intermediate grayscale image belongs; the resampling module 207 sets the grayscale level of each pixel of the intermediate grayscale image to the grayscale level corresponding to the sub-interval according to the sub-interval to which the grayscale value of each pixel of the intermediate grayscale image belongs; the resampling module 207 generates the final binarized image according to the grayscale level of each pixel of the intermediate grayscale image.
[0123] It should be noted that the above embodiments provide devices that implement their functions using only the division of the above functional modules as examples. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. In addition, the device and method embodiments provided in the above embodiments are based on the same concept. The specific implementation process is detailed in the method embodiment and will not be repeated here.
[0124] This application also provides an electronic device. Figure 3 , Figure 3 3. This is a schematic diagram of the structure of an electronic device provided by an embodiment of the present application. The electronic device 300 may include: at least one processor 301, at least one network interface 304, a user interface 303, a memory 305, and at least one communication bus 302.
[0125] The communication bus 302 is used to implement the connection and communication between these components.
[0126] The user interface 303 may include a display screen (Display) and a camera (Camera). Optionally, the user interface 303 may also include a standard wired interface and a wireless interface.
[0127] The network interface 304 may optionally include a standard wired interface or a wireless interface (such as a Wi-Fi interface).
[0128] The processor 301 may include one or more processing cores. Using various interfaces and circuits, the processor 301 connects to various components within the server. It executes instructions, programs, code sets, or instruction sets stored in the memory 305, as well as accesses data stored in the memory 305, to perform various server functions and process data. Optionally, the processor 301 may be implemented using at least one of the following hardware forms: a digital signal processing (DSP), a field-programmable gate array (FPGA), or a programmable logic array (PLA). The processor 301 may integrate one or a combination of a central processing unit (CPU), a graphics processing unit (GPU), and a modem. The CPU primarily processes the operating system, user interface, and application programs; the GPU is responsible for rendering and drawing content displayed on the display screen; and the modem handles wireless communications. It is understood that the modem may not be integrated into the processor 301 but implemented as a separate chip.
[0129] Among them, the memory 305 may include a random access memory (RAM) or a read-only memory (Read-Only Memory). Optionally, the memory 305 includes a non-transitory computer-readable storage medium. The memory 305 can be used to store instructions, programs, codes, code sets or instruction sets. The memory 305 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the above-mentioned various method embodiments, etc.; the data storage area may store data involved in the above-mentioned various method embodiments, etc. The memory 305 may also optionally be at least one storage device located away from the aforementioned processor 301. Refer to Figure 3The memory 305 as a computer storage medium may include an operating system, a network communication module, a user interface module and an application program of a thermal printing image processing method.
[0130] exist Figure 3 In the electronic device 300 shown, the user interface 303 is mainly used to provide an input interface for the user and obtain the data input by the user; and the processor 301 can be used to call an application program for a thermal printing image processing method stored in the memory 305. When executed by one or more processors 301, the electronic device 300 executes one or more of the methods described in the above embodiments. It should be noted that for the aforementioned method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations, but those skilled in the art should know that this application is not limited by the order of the actions described, because according to this application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required for this application.
[0131] The present application further provides a computer-readable storage medium storing instructions, which, when executed by one or more processors 301 , enable the electronic device 300 to perform one or more of the methods described in the above embodiments.
[0132] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0133] In the several embodiments provided in this application, it should be understood that the disclosed devices can be implemented in other ways. For example, the device embodiments described above are merely schematic, such as the division of units, which is only a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some service interface, and the indirect coupling or communication connection of devices or units can be electrical or other forms.
[0134] Units described as separate components may or may not be physically separate, and components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.
[0135] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0136] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable memory. Based on this understanding, the technical solution of this application, or the portion that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a memory and includes several instructions for causing a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the various embodiments of the method of this application. The aforementioned memory includes various media that can store program code, such as USB flash drives, mobile hard drives, magnetic disks, or optical disks.
[0137] The foregoing is merely an exemplary embodiment of the present disclosure and is not intended to limit the scope of the present disclosure. In other words, any equivalent variations and modifications made in accordance with the teachings of the present disclosure are still within the scope of the present disclosure. Those skilled in the art will readily conceive of other embodiments of the present disclosure after considering the disclosure and the practical implications thereof.
[0138] This application is intended to cover any variations, uses, or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art not described herein. The description and examples are to be considered as exemplary only, and the scope and spirit of the present disclosure are to be defined by the claims.
Claims
1. A thermal printing image processing method, characterized in that: The method comprises: Acquire an original image to be printed, and convert the original image into a grayscale image; Calculating the gradient difference value of the grayscale image in each preset pixel area, and generating a threshold distribution map based on the gradient difference value of each preset pixel area; generating a heat accumulation risk map based on weighted grayscale values of the grayscale image within each preset neighborhood window, wherein the preset neighborhood window is a window centered on a first pixel point, and the first pixel point is any one of a plurality of pixels included in the grayscale image; performing an error diffusion dithering algorithm on the grayscale image according to the threshold distribution map to generate a preliminary binary image; Extracting image features of the preliminary binary image, and classifying the original image according to texture information of the original image and the image features to obtain an image type of the original image; According to the image type, calling a target processing parameter set corresponding to the image type from a plurality of preset processing parameter sets; performing pixel processing on the preliminary binarized image according to the target processing parameter set and the heat accumulation risk map to obtain an optimized binarized image; The optimized binary image is resampled to generate final printing image data adapted to the target printer resolution, and the final printing image data is output.
2. The method according to claim 1, characterized in that Calculating the gradient difference value of the grayscale image in each preset pixel area and generating a threshold distribution map based on the gradient difference value of each preset pixel area specifically includes: Dividing the grayscale image into a plurality of preset pixel areas of equal size; For each of the preset pixel areas, calculating a grayscale difference between a target pixel point within the preset pixel area and an adjacent pixel point corresponding to the target pixel point, to obtain a plurality of grayscale differences within the preset pixel area, wherein the target pixel point is any pixel point in the preset pixel area; Determine a weight coefficient of a corresponding grayscale difference value according to a position of the target pixel point in the preset pixel area; Multiplying each grayscale difference by a corresponding weight coefficient, and averaging the sum to obtain a gradient difference value of the preset pixel area; Determining a grayscale threshold corresponding to the gradient difference value according to a preset threshold distribution function, wherein the preset threshold distribution function includes a correspondence between the gradient difference value and the grayscale threshold; Constructing a threshold distribution matrix according to the grayscale thresholds of each of the preset pixel areas, wherein the elements of the threshold distribution matrix are the grayscale thresholds; Interpolation and smoothing processing is performed on the threshold distribution matrix to obtain a threshold distribution map with the same resolution as the original image.
3. The method according to claim 1, characterized in that Generating a heat accumulation risk map based on the weighted grayscale values of the grayscale image in each preset neighborhood window specifically includes: Calculating a Euclidean distance between each second pixel point within the preset neighborhood window and a first pixel point serving as the center of the window, and generating Gaussian weight coefficients for the first pixel point and the second pixel point using a preset Gaussian function based on the Euclidean distance, where the second pixel point is any pixel point within the preset neighborhood window other than the first pixel point; Multiplying the grayscale values of the first pixel and the second pixel by the Gaussian weight coefficient, and accumulating all product results to obtain a weighted grayscale value; The weighted grayscale values are normalized to obtain heat accumulation values of the grayscale image in each of the preset neighborhood windows, and a heat accumulation risk map is generated according to the heat accumulation values.
4. The method according to claim 1, wherein The pixel processing of the preliminary binarized image according to the target processing parameter set and the heat accumulation risk map to obtain an optimized binarized image specifically includes: Extracting a heat value threshold, a local pixel threshold, a preset statistical length, and a preset replacement length from the target processing parameter set; Obtaining a heat accumulation value of a target black pixel at a corresponding position on the heat accumulation risk map, wherein the target black pixel is any one of a plurality of black pixels included in the preliminary binarized image; If the accumulated heat value is greater than or equal to the heat value threshold, a first statistical window with a side length of the preset statistical length is determined with the target black pixel as the center, and the number of black pixels in the first statistical window is calculated; If the number of black pixels is greater than or equal to the local pixel threshold, a target replacement area with a side length of the preset replacement length is determined with the target black pixel as the center, and all white pixels in the target replacement area are searched; A target white pixel point is determined from the plurality of white pixel points, and pixel positions of the target black pixel points are replaced according to the target white pixel point to obtain the optimized binary image.
5. The method according to claim 4, characterized in that The step of determining a target white pixel from the plurality of white pixel points and performing pixel position replacement on the target black pixel point according to the target white pixel point to obtain the optimized binary image specifically includes: For each of the white pixels, determining a second statistical window with a side length of the preset statistical length with the white pixel as the center, and calculating the number of black pixels within the second statistical window; Counting the number of black pixels in a second statistical window corresponding to each white pixel in the target replacement area, and determining the white pixel corresponding to the lowest number of black pixels as the target white pixel; If the number of black pixels in the corresponding second statistical window of the target white pixel point is less than the number of black pixels in the corresponding first statistical window of the target black pixel point, the color of the target black pixel point is set to white, and the color of the target white pixel point is set to black, completing the pixel position replacement to obtain the optimized binary image.
6. The method according to claim 1, characterized in that The resampling of the optimized binary image to generate final printing image data adapted to the target printer resolution specifically includes: Get the device resolution information of the target printer; Determining a resampling ratio according to the device resolution information; Resampling the optimized binary image according to the resampling ratio using a preset interpolation algorithm to generate an intermediate grayscale image; Performing secondary binarization processing on the intermediate grayscale image to generate a final binarized image that matches the device resolution information; The final binarized image is converted into a data format supported by the target printer to generate the final printing image data.
7. The method according to claim 6, characterized in that The performing secondary binarization processing on the intermediate grayscale image to generate a final binarized image matching the device resolution information specifically includes: Get the maximum number of grayscale levels supported by the target printer; Dividing the grayscale value range of the intermediate grayscale image into a plurality of subintervals according to the maximum number of grayscale levels, each subinterval corresponding to a grayscale level; Traversing each pixel of the intermediate grayscale image, and determining the subinterval to which the grayscale value of each pixel of the intermediate grayscale image belongs; According to the subinterval to which the grayscale value of each pixel of the intermediate grayscale image belongs, setting the grayscale level of each pixel of the intermediate grayscale image to the grayscale level corresponding to the subinterval; The final binarized image is generated according to the grayscale level of each pixel point of the intermediate grayscale image.
8. A thermal printing image processing device, characterized in that: The device comprises an original image acquisition module (201), a threshold distribution map generation module (202), a heat accumulation risk map generation module (203), a preliminary binary image generation module (204), a processing parameter determination module (205), an optimized binary image generation module (206), and a resampling module (207), wherein: The original image acquisition module (201) is used to acquire the original image to be printed and convert the original image into a grayscale image; The threshold distribution map generating module (202) is used to calculate the gradient difference value of the grayscale image in each preset pixel area, and generate a threshold distribution map based on the gradient difference value of each preset pixel area; The heat accumulation risk map generating module (203) is used to generate a heat accumulation risk map based on the weighted grayscale values of the grayscale image in each preset neighborhood window, wherein the preset neighborhood window is a window centered on a first pixel point, and the first pixel point is any pixel point among a plurality of pixel points included in the grayscale image; The preliminary binary image generation module (204) is used to perform an error diffusion dithering algorithm on the grayscale image according to the threshold distribution map to generate a preliminary binary image; The processing parameter determination module (205) is used to extract image features of the preliminary binarized image and classify the original image according to the texture information of the original image and the image features to obtain the image type of the original image; The processing parameter determination module (205) is further configured to call a target processing parameter set corresponding to the image type from a plurality of preset processing parameter sets according to the image type; The optimized binary image generation module (206) is used to perform pixel processing on the preliminary binary image according to the target processing parameter set and the heat accumulation risk map to obtain an optimized binary image; The resampling module (207) is used to resample the optimized binary image to generate final printing image data adapted to the target printer resolution, and output the final printing image data.
9. An electronic device, characterized in that: The electronic device (300) comprises a processor (301), a memory (305), a user interface (303) and a network interface (304), wherein the memory (305) is used to store instructions, the user interface (303) and the network interface (304) are used to communicate with other devices, and the processor (301) is used to execute the instructions stored in the memory (305) so that the electronic device (300) executes the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores instructions, and when the instructions are executed, the method according to any one of claims 1 to 7 is executed.
Citation Information
Patent Citations
Denoising and binarization method for ancient book picture
CN110533047A
Image display method and device of ink screen, equipment and storage medium
CN117037724A
Non-standard industrial product defect detection method based on image processing
CN118608525A
Method for generating print data and a color printer system
US20120243051A1
Cited By
Image edge real-time processing method and system for surface exposure photocuring 3D printing
CN121767680A
Power consumption management image data processing method in thermal printing
CN121900714A
Picture data processing method based on Bluetooth printing and related equipment
CN121985078A
Cooperative control method and system for high-speed photographic apparatus and thermal printer
CN122240049A