A thermal printing image processing method, device, equipment and medium
By generating threshold distribution maps and heat accumulation risk maps, and combining error diffusion jitter algorithms and pixel optimization processing, the blurring problem caused by heat accumulation in dark areas of thermal printers is solved, improving image clarity and print quality.
Patent Information
- Application Number
- CN202511286006.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-10
- Publication Date
- 2025-11-25
- Estimated Expiration
- 2045-09-10
AI Technical Summary
Thermal printers suffer from image blurring and distortion due to heat buildup when printing dark areas. Existing technologies struggle to improve image detail while preventing printhead overheating.
By generating threshold distribution maps and heat accumulation risk maps, and combining error diffusion jitter algorithm and pixel optimization processing, the processing parameters are adaptively adjusted to optimize the quality of binarized images and reduce the risk of heat accumulation in dark areas.
It improves the clarity and reliability of thermal printed images, reduces blurring and distortion caused by printhead overheating, and generates high-quality printed image data.
Smart Images

Figure CN120823401B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of thermal printing, in particular to a thermal printing image processing method, device, equipment and medium. BACKGROUND
[0002] Thermal printing technology, as a printing method that displays color by applying heat to special media, has been widely used in commercial bills, logistics labels and portable printing due to its simple structure, rapid printing and no need for ink cartridge consumables. However, thermal printers are essentially binary output devices, and their printing units can only perform the operation of printing black points or leaving white points. Therefore, it is necessary to convert the original grayscale image with rich color tones to a binary image before printing. The traditional method uses a simple fixed threshold method for conversion, which is fast, but causes obvious outline artifacts in the gray transition area of the image and results in a large amount of loss of image detail information, and the quality of the printed product is usually difficult to meet the use requirements.
[0003] To improve the image quality degradation problem caused by the fixed threshold method, the prior art calculates the quantization error generated by the binaryzation of the current pixel point, and distributes this error to the surrounding adjacent pixels that have not been processed according to the preset weight. This method can maintain the local gray energy conservation of the image in a macroscopic view, effectively simulating the continuous gray transition effect, and greatly improving the detail performance and level of the printed image compared with the fixed threshold method.
[0004] However, the above method, while improving the image performance, introduces new problems. Specifically, to maintain local gray energy when processing dark or large-area dark regions of the image, the algorithm will inevitably generate a high-density set of continuous black pixel points. When the printer performs a printing task, this high-density printing instruction will cause the heating units of the print head to work continuously for a long time and at a high frequency, which in turn causes rapid accumulation and diffusion of heat on the print head, i.e., heat accumulation effect. This effect causes the actual printed black points to expand in size and stick to each other due to overheating, ultimately forming a blurred black patch on the paper, which severely damages the clarity of the image outline. SUMMARY
[0005] The present application provides a thermal printing image processing method, device, equipment and medium, which improves the printing clarity.
[0006] In a first aspect of the present application, a thermal printing image processing method is provided, which comprises: obtaining an original image to be printed, and converting the original image into a grayscale image; calculating gradient difference values of the grayscale image in each preset pixel region, and generating a threshold distribution map based on the gradient difference values of each preset pixel region; generating 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 one of a plurality of pixel points 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 the texture information of the original image and the image features to obtain an image type of the original image; calling a target processing parameter set corresponding to the image type from a plurality of preset processing parameter sets according to the image type; 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; resampling the optimized binary image to generate final printing image data adapted to the resolution of a target printer, and outputting the final printing image data.
[0007] By adopting the above technical solution, the original image is first converted into a grayscale image, and then the threshold distribution map is generated by calculating the gradient difference values of the grayscale image in each preset pixel region, which is used for the subsequent error diffusion dithering algorithm to improve the quality of the binary image. At the same time, the heat accumulation risk map is generated based on the weighted grayscale values of the grayscale image in each preset neighborhood window, which is used to evaluate the heat distribution risk in the printing process. After generating the preliminary binary image, the image features are extracted, and the image classification is performed in combination with the texture information of the original image to obtain the image type. According to the image type, the corresponding target processing parameter set is called from the preset processing parameter set, and the pixel processing is performed on the preliminary binary image in combination with the heat accumulation risk map to obtain the optimized binary image. Finally, the optimized binary image is resampled to generate the final printing image data adapted to the resolution of the target printer and output. Through the above series of processing, the method can adaptively adjust the processing parameters according to the characteristics of different images, optimize the binary image quality, consider the heat distribution risk in the printing process, improve the printing reliability, and finally generate high-quality and high-adaptability printing image data to meet different printing needs. The method reduces the heat accumulation effect by adjusting the pixel distribution density in the dark area (high-risk area of heat) and reducing the continuous black points, thereby improving the printing quality and printing clarity.
[0008] Optionally, the gradient difference value of the gray image in each preset pixel region is calculated, and a threshold distribution map is generated based on the gradient difference value of each preset pixel region, specifically including: dividing the gray image into a plurality of preset pixel regions of the same size; for each preset pixel region, calculating the gray difference value between a target pixel point in the preset pixel region and a neighboring pixel point corresponding to the target pixel point, to obtain a plurality of gray difference values in the preset pixel region, the target pixel point being any one pixel point in the preset pixel region; determining a weight coefficient of the corresponding gray difference value according to the position of the target pixel point in the preset pixel region; multiplying each gray difference value by the corresponding weight coefficient and performing sum average to obtain the gradient difference value of the preset pixel region; determining the gray threshold corresponding to the gradient difference value according to a preset threshold distribution function, the preset threshold distribution function including the corresponding relationship between the gradient difference value and the gray threshold; constructing a threshold distribution matrix according to the gray threshold of each preset pixel region, the elements of the threshold distribution matrix being the gray threshold; performing interpolation 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 technical scheme, first, the gray image is divided into a plurality of preset pixel regions of the same size, for each region, the gray difference value between the target pixel point in the region and the neighboring pixel point is calculated, and the weight coefficient of the gray difference value is determined according to the position of the target pixel point. The weighted gray difference value is summed and averaged to obtain the gradient difference value of the preset pixel region. Then, the gray threshold corresponding to the gradient difference value is determined according to the preset threshold distribution function, and the threshold distribution matrix is constructed. Finally, the threshold distribution matrix is subjected to interpolation smoothing processing 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 region of the image, and improve the quality of the binarization image. At the same time, the interpolation smoothing processing can eliminate the mutation and discontinuity in the threshold distribution map, make the threshold distribution more smooth and natural, and reduce the artifacts and noise points in the binarization image.
[0010] Optionally, the heat accumulation risk map is generated based on the weighted gray values of the gray image in each preset neighborhood window, specifically comprising: calculating the Euclidean distance between each second pixel point in the preset neighborhood window and the first pixel point as the window center, and generating a Gaussian weight coefficient for the first pixel point and the second pixel point through a preset Gaussian function based on the Euclidean distance, the second pixel point being any one pixel point in the preset neighborhood window except the first pixel point; multiplying the gray value 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 gray value; normalizing the weighted gray value to obtain the heat accumulation value of the gray image in each preset neighborhood window, and generating a heat accumulation risk map according to the heat accumulation value.
[0011] By adopting the above technical solution, for each pixel point in the gray image, a preset neighborhood window is determined with it as the center, the Euclidean distance between each pixel point in the window and the center pixel point is calculated, and the corresponding Gaussian weight coefficient is generated through a preset Gaussian function. The gray value of the pixel point is multiplied by the Gaussian weight coefficient, and all the product results are accumulated to obtain a weighted gray value. The weighted gray 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 gray distribution in the neighborhood of the pixel point and evaluate the printing heat risk of the local area. The introduction of the Gaussian weight can highlight the influence of the center pixel point and smoothly attenuate the contribution of the neighborhood pixel points, 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, the 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 comprises: 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 corresponding position of a target black pixel point on the heat accumulation risk map, the target black pixel point being any one of a plurality of black pixel points included in the preliminary binary image; if the heat accumulation value is greater than or equal to the heat value threshold, determining a first statistical window with a side length of the preset statistical length and taking the target black pixel point as a center, and calculating a number of black pixel points in the first statistical window; if the number of black pixel points is greater than or equal to the local pixel threshold, determining a target replacement region with a side length of the preset replacement length and taking the target black pixel point as a center, and searching for all white pixel points in the target replacement region; determining a target white pixel point 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.
[0013] By adopting the technical solution, firstly, the heat value threshold, the local pixel threshold, the preset statistical length and the preset replacement length and other key parameters are extracted from the target processing parameter set. Then, for each black pixel point in the preliminary binary image, a heat accumulation value of a corresponding position of the black pixel point on the heat accumulation risk map is obtained and compared with the heat value threshold. If the heat accumulation value exceeds the threshold, a first statistical window with a side length of the preset statistical length is determined and the black pixel point is taken as a center, and a number of black pixel points in the window is calculated. If the number of black pixel points exceeds the local pixel threshold, a target replacement region with a side length of the preset replacement length is determined and the black pixel point is taken as a center, and all white pixel points in the region are searched. A target white pixel point is selected from the white pixel points, and position replacement is performed on the target black pixel point to obtain an optimized binary image. Through this method, a local region with high heat risk in the preliminary binary image can be effectively identified, and the local printing heat is reduced through pixel replacement, thereby improving the printing reliability. Meanwhile, the introduction of the target processing parameter set enables the pixel processing process to be adaptively adjusted according to different image types, thereby improving the pertinence and effectiveness of the processing.
[0014] Optionally, the target white pixel point is determined from the plurality of white pixel points, and pixel position replacement is performed on the target black pixel point according to the target white pixel point to obtain the optimized binary image, and specifically includes: for each white pixel point, a second statistical window with a side length of the preset statistical length is determined with the white pixel point as the center, and the number of black pixel points in the second statistical window is calculated; the number of black pixel points in the second statistical window corresponding to each white pixel point in the target replacement region is counted, and the white pixel point corresponding to the lowest number of black pixel points is determined as the target white pixel point; if the number of black pixel points in the corresponding second statistical window of the target white pixel point is less than the number of black pixel points 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, the pixel position replacement is completed, and the optimized binary image is obtained.
[0015] By adopting the above technical solution, for each white pixel point in the target replacement region, a second statistical window with a preset statistical length is determined with the white pixel point as the center, and the number of black pixel points in the window is calculated. The number of black pixel points in the second statistical window corresponding to each white pixel point is counted, and the white pixel point corresponding to the lowest number of black pixel points is determined as the target white pixel point. If the number of black pixel points in the corresponding second statistical window of the target white pixel point is less than the number of black pixel points in the corresponding first statistical window of the target black pixel point, pixel position replacement is performed, the target black pixel point is set to white, and the target white pixel point is set to black. By this method, the target white pixel point can be selected to ensure that the density of black pixel points in the replaced local region is reduced, and the risk of printing heat is reduced. At the same time, by comparing the number of black pixel points in the first statistical window and the second statistical window, unnecessary replacement operations can be avoided, and the influence on the image quality is reduced. The optimized binary image obtained after pixel replacement maintains the visual effect of the original image while reducing the risk of local high heat and improving the reliability of printing.
[0016] Optionally, the optimized binary image is resampled to generate final printing image data adapted to the resolution of a target printer, and specifically includes: obtaining device resolution information of a target printer; determining a resampling ratio according to the device resolution information; using a preset interpolation algorithm, resampling the optimized binary image according to the resampling ratio to generate an intermediate gray image; performing secondary binaryzation processing on the intermediate gray image to generate a final binary image matched with the device resolution information; and converting the final binary image into a data format supported by the target printer to generate the final printing image data.
[0017] By adopting the technical scheme, firstly, the equipment resolution information of the target printer is acquired, and the resampling ratio is determined according to the equipment resolution information. Then, the preset interpolation algorithm is used to resample the optimized binary image according to the resampling ratio, to generate an intermediate gray-scale image. The intermediate gray-scale image is subjected to secondary binarization processing to generate a final binary image matched with the equipment resolution information. Finally, the final binary image is converted into a data format supported by the target printer, to generate final printing image data. Through the resampling processing, the resolution of the optimized binary image can be adjusted to match the target printer, to ensure that the size and pixel density of the printing image meet the physical characteristics of the printer. The use of the interpolation algorithm can maintain the smoothness and continuity of the image while adjusting the resolution, to reduce the sawtooth and distortion. The secondary binarization processing can convert the resampled gray-scale image into a black-and-white binary image again, to match the output characteristics of the printer. The final printing image data generated after the format conversion can be correctly recognized and processed by the printer, to ensure the accuracy and consistency of the printing result.
[0018] Optionally, the secondary binarization processing on the intermediate gray-scale image to generate a final binary image matched with the equipment resolution information specifically includes: acquiring a maximum gray level number supported by the target printer; dividing a gray value range of the intermediate gray-scale image into a plurality of subintervals according to the maximum gray level number, each subinterval corresponding to a gray level; traversing each pixel point of the intermediate gray-scale image to determine a subinterval to which a gray value of each pixel point of the intermediate gray-scale image belongs; setting a gray level of each pixel point of the intermediate gray-scale image to a gray level corresponding to the subinterval to which the gray value of each pixel point of the intermediate gray-scale image belongs, according to the subinterval; and generating the final binary image according to the gray levels of the pixel points of the intermediate gray-scale image.
[0019] By adopting the technical scheme, firstly, the maximum number of gray scale levels supported by the target printer is acquired, and the gray value range of the intermediate gray scale image is divided into multiple subintervals according to the number of gray scale levels, and each subinterval corresponds to a gray scale level. Then, each pixel point of the intermediate gray scale image is traversed, the subinterval to which the gray value of the pixel point belongs is determined, and the gray scale level of the pixel point is set to the gray scale level of the corresponding subinterval. Finally, the final binary image is generated according to the gray scale level of each pixel point. Through this method of secondary binaryzation processing, the gray scale expression capability of the target printer can be fully utilized, and the continuous gray value can be mapped to the discrete gray scale level, improving the level and detail expression of the image. The division of the gray scale level takes into account the physical characteristics of the printer, ensuring that the final binary image generated can present rich gray scale effects on the printer. At the same time, through the setting of the gray scale level, the image can be further optimized and enhanced on the basis of binaryzation, such as contrast adjustment, edge sharpening, etc., improving the visual quality of the printed image. The final binary image matches the device resolution and can achieve high-quality and high-fidelity output effect on the target printer.
[0020] In a second aspect of the present application, a heat-sensitive printing image processing device is provided, which comprises an original image acquisition module, a threshold distribution map generation module, a heat accumulation risk map generation module, a preliminary binary image generation module, a processing parameter determination module, an optimized binary image generation module, and a resampling module. The original image acquisition module is configured to acquire an original image to be printed and convert the original image into a gray-scale image. The threshold distribution map generation module is configured to calculate gradient difference values of the gray-scale image in each preset pixel region and generate a threshold distribution map based on the gradient difference values of each preset pixel region. The heat accumulation risk map generation module is configured to generate a heat accumulation risk map based on weighted gray-scale values of the gray-scale image in each preset neighborhood window, wherein the preset neighborhood window is a window centered at a first pixel point, and the first pixel point is any one of a plurality of pixel points included in the gray-scale image. The preliminary binary image generation module is configured to generate a preliminary binary image by performing an error diffusion dithering algorithm on the gray-scale image according to the threshold distribution map. The processing parameter determination module is configured to extract image features of the preliminary binary image, classify the original image according to texture information of the original image and the image features, and obtain an image type of the original image. The processing parameter determination module is further configured to call a target processing parameter set corresponding to the image type from a plurality of preset processing parameter sets. The optimized binary image generation module is configured to perform pixel processing on the preliminary binary image according to the target processing parameter set and the heat accumulation risk map, and obtain an optimized binary image. The resampling module is configured to perform resampling on the optimized binary image, generate final printing image data adapted to a resolution of a target printer, and output the final printing image data.
[0021] In a third aspect of the present application, an electronic device is provided, which comprises a processor, a memory, a user interface, and a network interface. The memory is configured to store instructions, and the user interface and the network interface are both configured to communicate with other devices. The processor is configured to execute the instructions stored in the memory, so that the electronic device performs the method according to any one of the above aspects.
[0022] In a fourth aspect of the present application, a computer-readable storage medium is provided, which stores instructions. When the instructions are executed, the method according to any one of the above aspects is performed.
[0023] In summary, the one or more technical solutions provided in the present application have at least the following technical effects or advantages:
[0024] 1. By dynamically generating a threshold distribution map, a heat accumulation risk map, and calling a target processing parameter set based on image classification, combining error diffusion dithering algorithm and pixel optimization processing, high-quality conversion of gray-scale image to binary image is realized, which effectively improves the detail performance and level of printed image, at the same time, the heat accumulation effect in dark area is relieved, the image blur and distortion caused by overheating of print head is reduced, thereby significantly improving the overall printing quality and printing clarity of thermal printing. BRIEF DESCRIPTION OF DRAWINGS
[0025] Figure 1 is a flow diagram of a thermal printing image processing method disclosed by an embodiment of the present application;
[0026] Figure 2 is a module diagram of a thermal printing image processing device disclosed by an embodiment of the present application;
[0027] Figure 3 is a structural diagram of an electronic device disclosed by an embodiment of the present application.
[0028] Mark explanation: 201, original image acquisition module; 202, threshold distribution map generation module; 203, heat accumulation risk map generation module; 204, preliminary binary image generation module; 205, processing parameter determination module; 206, optimized binary image generation module; 207, resampling module; 300, electronic device; 301, processor; 302, communication bus; 303, user interface; 304, network interface; 305, memory. DETAILED DESCRIPTION
[0029] In order for those skilled in the art to better understand the technical solutions in the specification, the technical solutions in the specification will be clearly and completely described below in conjunction with the drawings in the embodiments of the specification. Obviously, the described embodiments are only some of the embodiments of the present application, not all.
[0030] In the description of the embodiments of the present application, the words such as "for example" or "for instance" are used to represent as an example, illustration or description. Any embodiment or design scheme described as "for example" or "for instance" in the embodiments of the present application should not be interpreted as more preferred or more advantageous than other embodiments or design schemes. Rather, the words such as "for example" or "for instance" are intended to present the relevant concept in a specific way.
[0031] In the description of the embodiments of the present application, the term "a plurality of" means two or more. For example, a plurality of systems means two or more systems, and a plurality of screen terminals means two or more screen terminals. In addition, the terms "first", "second", "third", etc. are used only for descriptive purposes and cannot be construed as indicating or implying relative importance or implying the indicated technical features. Therefore, the features defined as "first", "second", etc. can be explicitly or implicitly included one or more of the features. The terms "include", "contain", "have" and their variants mean "include but not limited to", unless otherwise specifically emphasized.
[0032] The present application provides a heat-sensitive printing image processing method, referring to Figure 1 , Figure 1 is a flowchart of a heat-sensitive printing image processing method provided by an embodiment of the present application. The method is applied to a heat-sensitive printer, and the heat-sensitive printer is used to execute a heat-sensitive printing image processing program. The method includes steps S101 to S108, and the steps are as follows:
[0033] Step S101: obtaining an original image to be printed, and converting the original image into a gray-scale image.
[0034] In step S101, the heat-sensitive printer can obtain the original image to be printed in various ways. For example, the original image can come from a computer, a mobile device, a digital camera or other external devices, and be transmitted to the heat-sensitive printer through wired or wireless connection. The original image can also be directly read from the storage medium (such as memory, hard disk, etc.) of the heat-sensitive printer itself. The original image is usually a color image, such as image data in RGB color space.
[0035] After obtaining the original image, the heat-sensitive printer converts the original image into a gray-scale image. This is because the heat-sensitive printer can usually only print black and white, and cannot directly print color images. Converting a color image into a gray-scale image can preserve the main visual information of the image while reducing the complexity of subsequent processing.
[0036] The heat-sensitive printer can use various color space conversion algorithms to convert the original color image into a gray-scale image. The conversion methods include but are not limited to:
[0037] Weighted average method: according to the different perception degrees of the human eye to red, green and blue, the pixel values of the three channels of RGB are weighted and averaged, and the commonly used weight coefficients are R: 0.299, G: 0.587, B: 0.114.
[0038] Maximum value method: taking the maximum value in the three channels of RGB as the gray-scale value.
[0039] Average method: directly calculate the arithmetic mean of RGB three channels as the gray value.
[0040] For example, assume that the original image is an RGB color image with a resolution of 800x600. For a pixel point with coordinates (100, 200) on the image, its RGB value is (255, 128, 64). Using the weighted average method, the gray value can be calculated as: Gray = R x 0.299 + G x 0.587 + B x 0.114 = 255 x 0.299 + 128 x 0.587 + 64 x 0.114 ≈ 158; therefore, the pixel value of the converted gray image at coordinates (100, 200) is 158. The thermal printer performs the above calculation for each pixel point of the original image, and finally obtains a gray image with the same size as the original image.
[0041] Step S102: calculate the gradient difference value of the gray image in each preset pixel region, and generate a threshold distribution map based on the gradient difference value of each preset pixel region.
[0042] In step S102, the gradient difference value of the gray image in each preset pixel region is calculated, and a threshold distribution map is generated based on the gradient difference value of each preset pixel region, which specifically includes: dividing the gray image into a plurality of preset pixel regions with the same size; for each preset pixel region, calculating the gray difference value between the target pixel point in the preset pixel region and the adjacent pixel point corresponding to the target pixel point, obtaining a plurality of gray difference values in the preset pixel region, the target pixel point being any one pixel point in the preset pixel region; determining the weight coefficient of the corresponding gray difference value according to the position of the target pixel point in the preset pixel region; multiplying each gray difference value by the corresponding weight coefficient and summing to obtain the gradient difference value of the preset pixel region; determining the gray threshold corresponding to the gradient difference value according to the preset threshold distribution function, the preset threshold distribution function including the corresponding relationship between the gradient difference value and the gray threshold; constructing a threshold distribution matrix according to the gray threshold of each preset pixel region, the elements of the threshold distribution matrix being the gray threshold; performing interpolation smoothing processing on the threshold distribution matrix to obtain a threshold distribution map with the same resolution as the original image.
[0043] Specifically, the thermal printer divides the acquired gray image into a plurality of preset pixel regions with the same size. These preset pixel regions can be square, rectangular or other regular shapes, and the region size can be set according to actual needs, such as 8x8, 16x16, etc.
[0044] Next, for each preset pixel region, the thermal printer calculates the gray scale difference between the target pixel point and its adjacent pixel points in the region. The target pixel point can be any pixel point in the preset pixel region, and the adjacent pixel points are the pixel points adjacent to the target pixel point in the horizontal, vertical or diagonal direction. By calculating the gray scale difference between the target pixel point and its adjacent pixel points, a plurality of gray scale differences in the preset pixel region can be obtained.
[0045] In order to more accurately reflect the local features of the image, the thermal printer determines the weight coefficient of the corresponding gray scale difference according to the position of the target pixel point in the preset pixel region. For example, a Gaussian weight function can be used to assign a higher weight to the target pixel point located at the center of the region, and a lower weight to the target pixel point located at the edge of the region.
[0046] Then, the thermal printer multiplies each gray scale difference by the corresponding weight coefficient and performs sum average to obtain the gradient difference value of the preset pixel region. The gradient difference value reflects the degree of change of the pixel gray scale in the region, and the larger the gradient difference value, the more obvious the gray scale change in the region. According to the preset threshold distribution function, the thermal printer can determine the gray scale threshold corresponding to the gradient difference value of each preset pixel region. The threshold distribution function describes the correspondence between the gradient difference value and the gray scale threshold, which can be in the form of a linear function, an exponential function, a logarithmic function, etc. After obtaining the gray scale threshold of each preset pixel region, the thermal printer constructs a threshold distribution matrix according to these gray scale thresholds. The elements of the threshold distribution matrix are the gray scale thresholds corresponding to each preset pixel region.
[0047] Finally, in order to obtain a threshold distribution map with the same resolution as the original image, the thermal printer performs interpolation smoothing processing on the threshold distribution matrix. The interpolation algorithm includes bilinear interpolation, bicubic interpolation, etc. Through interpolation, transition thresholds can be generated between the preset pixel regions, making the threshold distribution more smooth and natural.
[0048] For example, assume that a 1024x768 gray scale image is divided into 128x96 8x8 preset pixel regions. For the preset pixel region where the target pixel point with coordinates (100, 200) is located, the thermal printer calculates the gray scale difference between all pixel points in the region and their adjacent pixel points, obtaining 64 gray scale differences. Then, according to the Gaussian weight function, the weight coefficient of each gray scale difference is determined, and the weighted gray scale differences are summed and averaged to obtain the gradient difference value of the region, which is 20. According to the preset linear threshold distribution function, the gray scale threshold corresponding to the gradient difference value of 20 is 180. Repeating the above process, a 128x96 threshold distribution matrix can be obtained. Finally, through bilinear interpolation, the threshold distribution matrix is smoothed to a 1024x768 threshold distribution map.
[0049] Step S103: generating a heat accumulation risk map based on the weighted grayscale values of the grayscale image in each preset neighborhood window, the preset neighborhood window being a window with the first pixel point as the center, and the first pixel point being any one of the plurality of pixel points included in the grayscale image.
[0050] In step S103, 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 generating a Gaussian weight coefficient for the first pixel point and the second pixel point based on the Euclidean distance through a preset Gaussian function, the second pixel point being any one of the pixel points 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 the 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 the heat accumulation risk map according to the heat accumulation value.
[0051] Specifically, the thermal printer sets a neighborhood window of a preset size with any one of the pixel points in the grayscale image as the first pixel point and with the first pixel point as the center. The size of the neighborhood window can be set according to actual needs, and common sizes include 3x3, 5x5, 7x7, etc.
[0052] Next, for each pixel point (i.e., the second pixel point) in the preset neighborhood window, the thermal printer calculates the Euclidean distance between it and the first pixel point as the center of the window. The Euclidean distance reflects the spatial distance between two pixel points on the image plane, and can be calculated by the following formula:
[0053] distance=sqrt((x1-x2)^2+(y1-y2)^2);
[0054] where (x1, y1) and (x2, y2) are the coordinates of the first pixel point and the second pixel point, respectively.
[0055] Then, the thermal printer generates a Gaussian weight coefficient for the first pixel point and the second pixel point based on the calculated Euclidean distance through a preset Gaussian function. The Gaussian function is a commonly used weight distribution function that can assign different weights to pixel points according to their distances, with closer pixel points having higher weights and farther pixel points having lower weights. The formula of the Gaussian function is as follows:
[0056] weight=exp(-(distance^2) / (2*sigma^2));
[0057] wherein sigma is the standard deviation of the Gaussian function, which can control the speed of weight decay.
[0058] For each pixel point within the preset neighborhood window, the thermal printer multiplies its gray value with the corresponding Gaussian weight coefficient, and accumulates all the product results to obtain the weighted gray value of the neighborhood window. The weighted gray value comprehensively considers the gray information and spatial position relationship of the pixel points within the neighborhood, and can better reflect the gray distribution characteristics of the local region.
[0059] In order to facilitate subsequent processing, the thermal printer performs normalization processing on the weighted gray value, maps it to the range of [0, 1], and obtains the heat accumulation value of the neighborhood window. The larger the heat accumulation value is, the higher the gray value of the local region is, and the more heat is required in the printing process.
[0060] By repeating the above process, the thermal printer can calculate the heat accumulation value of the neighborhood window where each pixel point in the gray-scale image is located, and further generate a heat accumulation risk map with the same size as the gray-scale image. The gray value of each pixel point of the heat accumulation risk map corresponds to the heat accumulation value of the pixel point at the same position in the original gray-scale image.
[0061] For example, assuming that in a 512x512 gray-scale image, the pixel point with coordinates (100, 200) is the first pixel point, and a 5x5 preset neighborhood window is set. The thermal printer first calculates the Euclidean distance between the remaining 24 pixel points in the neighborhood window and the first pixel point, and then calculates the weight coefficient of each pixel point through the Gaussian function. If the gray value of the first pixel point is 150, the gray value of the adjacent pixel point to the right is 180, and the Euclidean distance between the two pixel points is 1, and the standard deviation of the Gaussian function is set to 1.5, then the Gaussian weight coefficient of the adjacent pixel point is exp(-(1^2) / (2*1.5^2))≈0.8825. Multiplying 180 by 0.8825, the weighted gray value of the adjacent pixel point is 158.85. Perform the above calculation on all pixel points in the neighborhood window, and accumulate the results to obtain the weighted gray value of the neighborhood window. After normalization processing, the heat accumulation value of the neighborhood window is obtained. Repeat the above process for each pixel point of the gray-scale image to finally generate a heat accumulation risk map.
[0062] Step S104: According to the threshold distribution map, an error diffusion dithering algorithm is performed on the gray-scale image to generate a preliminary binary image.
[0063] In step S104, the thermal printer obtains the threshold distribution map generated in step S102, which has the same resolution as the original gray-scale image, and each pixel point in the map corresponds to a gray threshold.
[0064] Then, the thermal printer processes the pixels in the grayscale image one by one in the order from left to right and from top to bottom. For the currently processed pixel, the thermal printer compares the size relationship between its grayscale value and the grayscale threshold value at the corresponding position in the threshold distribution map. If the grayscale value of the pixel is greater than or equal to the corresponding grayscale threshold value, the binary result of the pixel is set to 1 (white); otherwise, its binary result is set to 0 (black).
[0065] After completing the binary decision of the current pixel, the thermal printer calculates the quantization error of the pixel, which is the difference between the original grayscale value and the binary result. For example, if the grayscale value of the current pixel is 200 and the corresponding grayscale threshold value is 180, the binary result of the pixel is 1 and the quantization error is 200-255=-55.
[0066] Next, the thermal printer distributes the quantization error of the current pixel to its neighboring unprocessed pixels according to the preset error diffusion filter with certain weights. Commonly used error diffusion filters include Floyd-Steinberg filter, Jarvis filter, Stucki filter, etc., which define different error distribution weights. Taking the Floyd-Steinberg filter as an example, its error distribution weights are as follows:
[0067] Where * represents the currently processed pixel, and the values on the right, below, and below-right represent the weights of distributing the quantization error to the corresponding position pixels.
[0068] The thermal printer multiplies the quantization error of the current pixel by the corresponding weight and adds the result to the grayscale value of the neighboring unprocessed pixel to affect its subsequent binary decision. For example, if the quantization error of the current pixel is -55, the grayscale value of the pixel on the right will increase by -55x7 / 16≈-24, the grayscale value of the pixel below will increase by -55x5 / 16≈-17, and the grayscale value of the pixel below-right will increase by -55x1 / 16≈-3.
[0069] Repeat the above process until all the pixels in the grayscale image are processed, and a preliminary binary image can be obtained. Since the error diffusion dithering algorithm considers the mutual influence between pixels and diffuses the quantization error in the local area, the generated binary image can better preserve the details and texture information of the original image, avoiding the problems of pseudo-contour and breakpoint generated by traditional fixed threshold binary method.
[0070] For example, assume that in a 512x512 grayscale image, the pixel at coordinate (100, 200) has a grayscale value of 200, and the corresponding grayscale threshold 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 be increased by -24, -17, and -3, respectively. The above process is performed for each pixel in the grayscale image, and a preliminary binarized image is finally obtained.
[0071] Step S105: Extract image features of the preliminary binarized 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.
[0072] In step S105, the thermal printer performs feature extraction on the preliminary binarized image generated in step S104. Contour features: the thermal printer extracts the contour information of the target object from the preliminary binarized image through a contour extraction algorithm such as the Canny edge detection algorithm, and calculates the geometric features such as the perimeter, area, and circularity of the contour. Skeleton features: the thermal printer extracts the skeleton information of the target object from the preliminary binarized image through a skeleton extraction algorithm such as the Zhang-Suen thinning algorithm, and calculates the topological features such as the length, number of endpoints, and number of intersection points of the skeleton.
[0073] Connected component features: the thermal printer extracts each connected component from the preliminary binarized image through a connected component analysis algorithm such as the two-pass scanning algorithm, and calculates the distribution features such as the number, size, and density of the connected components.
[0074] Then, the thermal printer performs texture feature extraction on the original image. Texture is a repeated pattern of grayscale distribution in a local region of an image, reflecting the roughness and directionality of the image surface. Texture feature extraction methods include:
[0075] Gray level co-occurrence matrix: the thermal printer constructs a gray level co-occurrence matrix by calculating the grayscale relationship between pairs of pixels in the original image, and extracts texture statistical features such as energy, entropy, contrast, and homogeneity from the matrix.
[0076] Gabor filtering: the thermal printer uses Gabor filters of different scales and directions to convolve the original image, obtains a set of Gabor feature maps, and calculates statistical quantities such as the mean and variance of each feature map to form a Gabor texture feature vector.
[0077] Wavelet Transform: The thermal printer performs multi-scale wavelet decomposition on the original image, obtaining low-frequency approximation 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.
[0078] 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.
[0079] Finally, the thermal printer uses a pre-trained classifier model to classify the image feature vector and obtain the image type of the original image. Common image classifiers include Support Vector Machine (SVM), Decision Tree, Random Forest, Convolutional Neural Network (CNN), etc. These classifiers learn from a large number of training samples to establish a mapping relationship between image features and image types, so that new images can be automatically classified.
[0080] For example, suppose the thermal printer needs to print an ID photo. The thermal printer first extracts the contour features, skeleton features, and connected domain features from the preliminary binary image corresponding to the ID photo, and finds that there is a clear face contour, sparse skeleton, and a large connected region in the image. Then, the thermal printer extracts the gray-level co-occurrence matrix features, Gabor features, and wavelet features from the original ID photo, and finds that the image has high contrast, vertical texture, and low-frequency components dominate. Combining these features into an image feature vector and inputting it into a pre-trained SVM classifier, the classification result is obtained that the ID photo belongs to the "portrait" type.
[0081] Step S106: According to the image type, call the target processing parameter set corresponding to the image type from the pre-set multiple processing parameter sets.
[0082] In step S106, the thermal printer's storage unit is pre-set with multiple processing parameter sets, each corresponding to a specific image type, such as text, chart, portrait, landscape, etc. These processing parameter sets are a set of optimized printing parameter combinations for different types of images, based on a large number of printing practices and expert experience.
[0083] In this embodiment, the target processing parameter set includes the following key parameters:
[0084] Heat value threshold: represents the critical point of pixel heat accumulation value, pixel points exceeding this threshold are considered high-risk points and need special processing to avoid overheating damage during printing.
[0085] Local pixel threshold: represents 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 a high-density area, and the black pixels are too concentrated, which may cause the local printing heat to be too high, and pixel replacement is needed to reduce the risk.
[0086] Pre-set statistical length: represents the side length of the square window centered on the target pixel for counting the number of black pixels. This parameter determines the size of the local area and affects the accuracy of the high-density area determination.
[0087] Pre-set replacement length: represents the side length of the square area centered on the target pixel for searching for replaceable white pixels. This parameter determines the range of pixel replacement and affects the effectiveness and efficiency of the replacement operation.
[0088] The thermal printer looks up the target processing parameter set corresponding to the image type identified in step S105 from the pre-set processing parameter set, and extracts the specific values of the above four key parameters.
[0089] For example, assume that the thermal printer needs to print a two-dimensional code image, and the image classification module identifies that the image belongs to the "chart" type. The thermal printer looks up the target processing parameter set corresponding to the "chart" type in the pre-set processing parameter set, which contains the following parameter settings: heat value threshold: 0.85; local pixel threshold: 60%; pre-set statistical length: 5 pixels; pre-set replacement length: 11 pixels.
[0090] This means that for each black pixel in the two-dimensional code image, if its heat cumulative value exceeds 0.85, special processing is needed; if the number of black pixels in the 5x5 pixel window centered on the pixel exceeds 60%, the area is determined to be a high-density area and needs to be replaced by pixels; the replacement operation will search for available white pixels in the 11x11 pixel area centered on the pixel.
[0091] The thermal printer passes these parameter values to the subsequent image optimization module for guiding the heat distribution optimization and pixel replacement processing of the binary image.
[0092] Step S107: Perform pixel processing on the preliminary binary image according to the target processing parameter set and the heat cumulative risk map to obtain an optimized binary image.
[0093] 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 the heat value threshold, the local pixel threshold, the preset statistical length, and the preset replacement length from the target processing parameter set; obtaining the heat accumulation value of the corresponding position of the target black pixel point on the heat accumulation risk map, the target black pixel point being any one of the plurality of 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 preset statistical length is determined with the target black pixel point as the center, and the number of black pixel points in the first statistical window is calculated; if the number of black pixel points is greater than or equal to the local pixel threshold, a target replacement region with a preset replacement length is determined with the target black pixel point as the center, and all white pixel points in the target replacement region are searched; a target white pixel point is determined from the plurality of white pixel points, and pixel position replacement is performed on the target black pixel point according to the target white pixel point to obtain the optimized binary image.
[0094] Specifically, the thermal printer extracts the parameters related to pixel processing from the target processing parameter set called in step S106, including:
[0095] Heat value threshold: represents the critical point of the pixel point heat accumulation value. The pixel point exceeding the threshold is considered to be a high-risk point and needs to be specially processed.
[0096] Local pixel threshold: represents the critical point of the number of black pixel points in the local region centered on the target pixel point. The region exceeding the threshold is considered to be a high-density area and needs to be pixel-replaced.
[0097] Preset statistical length: represents the side length of the square window centered on the target pixel point for counting the number of black pixel points.
[0098] Preset replacement length: represents the side length of the square region centered on the target pixel point for searching for replaceable white pixel points.
[0099] Then, the thermal printer traverses each black pixel point in the preliminary binary image, takes it as the current target black pixel point, and obtains the heat accumulation value of the corresponding position of the pixel point in the heat accumulation risk map.
[0100] If the heat accumulation value of the target black pixel point is greater than or equal to the heat value threshold, it means that the printing heat of the region where the pixel point is located is too high, which may cause the printing quality to decrease or the thermal paper to be damaged. At this time, the thermal printer determines a first statistical window with a preset statistical length with the target black pixel point as the center, and counts the number of black pixel points in the window.
[0101] If the number of black pixel points in the first statistical window is greater than or equal to the local pixel threshold, it indicates that the density of black pixel points around the target black pixel point is high, and the printing heat is concentrated, so pixel replacement is needed to reduce the local heat. At this time, the thermal printer determines a target replacement region with a preset replacement length as the side length centering on the target black pixel point, and searches for all white pixel points in the region.
[0102] From the multiple white pixel points in the target replacement region, the thermal printer selects one as the target white pixel point according to a preset rule. After determining the target white pixel point, the thermal printer exchanges the positions of the target black pixel point and the target white pixel point, i.e., replaces the target black pixel point with white and the target white pixel point with black. In this way, one pixel replacement operation is completed, and the printing heat risk of the local region is reduced.
[0103] The above process is repeated until all black pixel points in the preliminary binary image are processed, i.e., an optimized binary image is obtained. Compared with the preliminary binary image, the optimized binary image reduces the printing heat risk of the local high-density region while maintaining the overall visual effect of the image, improving the reliability and stability of printing.
[0104] For example, assume that in a 384x384 preliminary binary image, the heat cumulative value of black pixel point A at coordinates (100, 200) is 0.8, which exceeds the heat value threshold 0.75. The thermal printer determines a 7x7 first statistical window centering on pixel point A, and counts a total of 40 black pixel points in the window, which exceeds the local pixel threshold 35. Then, the thermal printer determines a 15x15 target replacement region centering on pixel point A, and searches for a total of 10 white pixel points in the region. According to the nearest distance rule, the thermal printer selects white pixel point B at coordinates (106, 198) as the target white pixel point, replaces pixel point A with white and pixel point B with black, and completes one pixel replacement operation. The above optimization processing is performed on all black pixel points in the preliminary binary image, and finally an optimized binary image with more balanced printing heat distribution is obtained.
[0105] In a possible implementation, a target white pixel point is determined from the plurality of white pixel points, and pixel position replacement is performed on the target black pixel point according to the target white pixel point to obtain an optimized binary image, and the method specifically comprises: for each white pixel point, a second statistical window with a preset statistical length is determined with the white pixel point as a center, and the number of black pixel points in the second statistical window is calculated; the number of black pixel points in the corresponding second statistical window of each white pixel point in the target replacement region is counted, and the white pixel point corresponding to the minimum number of black pixel points is determined as the target white pixel point; if the number of black pixel points in the corresponding second statistical window of the target white pixel point is less than the number of black pixel points in the corresponding first statistical window of the target black pixel point, the color of the target black pixel point is set to white, the color of the target white pixel point is set to black, the pixel position replacement is completed, and the optimized binary image is obtained.
[0106] Specifically, for each white pixel point in the target replacement region, the image optimization module determines a second statistical window with the same length as the first statistical window with the white pixel point as a center, and counts the number of black pixel points in the window. The purpose of this step is to evaluate the density of black pixel points around each white pixel point, so as to select the target white pixel point that can reduce the local heat risk to the greatest extent after replacement.
[0107] Next, the image optimization module counts the number of black pixel points in the corresponding second statistical window of each white pixel point in the target replacement region, and finds the minimum value, that is, the minimum number of black pixel points. The white pixel point with the minimum number of black pixel points has the lowest density of black pixel points around it, and has the smallest impact on local heat after replacement, and is therefore determined as the target white pixel point.
[0108] After selecting the target white pixel point, the image optimization module further compares the number of black pixel points in the corresponding second statistical window of the target white pixel point with the number of black pixel points in the corresponding first statistical window of the target black pixel point. Only when the number of black pixel points around the target white pixel point is less than the number of black pixel points around the target black pixel point, the pixel position replacement operation is performed, that is, 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. The purpose of this step is to ensure that the density of black pixel points in the local region is indeed reduced after replacement, and to avoid unnecessary pixel replacement that causes image distortion.
[0109] After completing the optimization processing of all target black pixel points, the image optimization module outputs the optimized binary image, which realizes the migration of black pixel points from the local high-density region to the low-density region, effectively reduces the printing heat risk, and at the same time maintains the overall quality and details of the image as much as possible.
[0110] For example, assume that in a preliminary binary image of 512x512, the black pixel point P with coordinates (200, 300) is determined as the target black pixel point, and the corresponding first statistical window size is 9x9. There are 60 black pixel points in the window. In the target replacement region of 21x21 centered on pixel point P, the image optimization module finds 5 white pixel points, namely Q1, Q2, Q3, Q4, and Q5.
[0111] For each white pixel point, the image optimization module calculates the number of black pixel points in the corresponding 9x9 second statistical window, and the results are as follows:
[0112] Q1: 45 black pixel points; Q2: 50 black pixel points; Q3: 38 black pixel points; Q4: 55 black pixel points; Q5: 42 black pixel points;
[0113] Among them, Q3 has the least 38 black pixel points in its corresponding second statistical window, and is therefore determined as the target white pixel point. Since 38 is less than the 60 black pixel points of the target black pixel point P in the first statistical window, it meets the replacement condition, and the image optimization module sets the color of pixel point P to white and sets the color of pixel point Q3 to black, completing a pixel replacement operation.
[0114] Repeat the above optimization process for all target black pixel points in the preliminary binary image, and finally obtain an optimized binary image with more balanced local heat distribution. Compared with the replacement strategy based on a single distance or heat index, this optimization method based on local black pixel density can more comprehensively evaluate the impact of replacement operation on image quality and printing heat, while reducing heat risk, better protecting the integrity and aesthetics of the image, and improving the overall effect of thermal printing.
[0115] Step S108: Resample the optimized binary image to generate final print image data adapted to the resolution of the target printer, and output the final print image data.
[0116] In step S108, the optimized binary image is resampled to generate final print image data adapted to the resolution of the target printer, which specifically includes: obtaining device resolution information of the target printer; determining a resampling ratio according to the device resolution information; using a preset interpolation algorithm to resample the optimized binary image according to the resampling ratio to generate an intermediate gray-scale image; performing secondary binaryzation processing on the intermediate gray-scale image to generate a final binary image matching the device resolution information; converting the final binary image into a data format supported by the target printer to generate the final print image data.
[0117] Specifically, the thermal printer acquires the device resolution information of the target printer. Device resolution represents the number of dots per inch (dpi) that the printer can print in both horizontal and vertical directions, reflecting the physical output capability of the printer. For example, a thermal printer may have a device resolution of 203 dpi x 203 dpi, indicating that it can print 203 dots per inch in each direction.
[0118] Then, the thermal printer 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 print image in terms of resolution. Generally, the resolution of the optimized binary image does not completely match the device resolution, and the size and pixel density of the image need to be adjusted through the resampling operation to adapt to the physical characteristics of the printer.
[0119] 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, bicubic interpolation, etc., which have different balances in speed and quality. During the resampling process, the interpolation algorithm estimates the pixel value at the corresponding position in the new image based on the position and grayscale value of the pixel points in the original image, thereby achieving smooth scaling of the image.
[0120] Since the resampled intermediate grayscale image contains multiple grayscale levels, and the thermal printer can only output black and white colors, the thermal printer needs to perform a second binaryzation process on the intermediate grayscale image to generate a final binaryzation image that matches the device resolution information. The second binaryzation process can use similar methods as step S104, such as fixed threshold method, adaptive threshold method, etc., to convert the grayscale image into a binary image containing only black and white pixels.
[0121] Finally, the thermal printer converts the final binaryzation image into a data format supported by the target printer, generating final print image data. Different models of thermal printers may support different data formats, such as ESC / POS, PCL, ZPL, etc. The thermal printer needs to encode the binary image data into corresponding print instructions and control sequences according to the specifications and interfaces of the target printer, to ensure that the printer can correctly recognize and process the image data.
[0122] After generating the final print image data, the thermal printer outputs it to the print buffer or data interface for subsequent printing operations by the printer.
[0123] For example, assume that the device resolution of the thermal printer is 300 dpi x 300 dpi, and the resolution of the optimized binary image is 200 dpi x 200 dpi. The thermal printer calculates that the resampling ratio is 1.5, i.e., the size of the image needs to be enlarged by 1.5 times. Then, the thermal printer resamples the optimized binary image using a bilinear interpolation algorithm to generate an intermediate gray-scale image with a resolution of 300 dpi x 300 dpi. Next, the thermal printer binarizes the intermediate gray-scale image using an adaptive threshold method to obtain a final binary image that matches the device resolution. Finally, the thermal printer converts the final binary image into ESC / POS format print data and sends it to the thermal printer through the USB interface, completing the image resampling and data preparation process.
[0124] In one possible implementation, the intermediate gray-scale image is subjected to a secondary binarization process to generate a final binary image that matches the device resolution information, specifically including: obtaining the maximum number of gray scale levels supported by the target printer; dividing the gray scale value range of the intermediate gray-scale image into multiple subintervals according to the maximum number of gray scale levels, each subinterval corresponding to a gray scale level; traversing each pixel point of the intermediate gray-scale image to determine the subinterval to which the gray scale value of each pixel point of the intermediate gray-scale image belongs; setting the gray scale level of each pixel point of the intermediate gray-scale image to the gray scale level corresponding to the subinterval to which the gray scale value of each pixel point of the intermediate gray-scale image belongs according to the subinterval to which the gray scale value of each pixel point of the intermediate gray-scale image belongs; and generating a final binary image according to the gray scale levels of the pixel points of the intermediate gray-scale image.
[0125] Specifically, the thermal printer obtains the maximum number of gray scale levels supported by the target printer. Different models of thermal printers can support different numbers of gray scale levels, such as 2 levels (black and white), 4 levels, 16 levels, etc. The more the number of gray scale levels, the richer the gray scale levels that the printer can represent, but at the same time, it also puts higher requirements on image data processing and transmission.
[0126] Then, the thermal printer divides the gray scale value range of the intermediate gray-scale image into multiple subintervals according to the maximum number of gray scale levels, each subinterval corresponding to a gray scale level. For example, if the printer supports 4 levels of gray scale, the gray scale value range of 0~255 can be equally divided into 4 subintervals: [0, 63], [64, 127], [128, 191], [192, 255], corresponding to gray scale levels 0, 1, 2, 3, respectively.
[0127] After dividing the gray scale value subintervals, the thermal printer traverses each pixel point of the intermediate gray-scale image to determine the subinterval to which its gray scale value belongs. This step can be achieved through simple numerical comparison and range judgment.
[0128] Next, the thermal printer sets the gray scale level of each pixel point to the gray scale level corresponding to the sub-interval to which the gray value of the pixel point belongs. In this way, the originally continuous gray values are mapped to discrete gray scale levels, realizing quantization of the gray scale levels.
[0129] Finally, the thermal printer generates a final binary image according to the gray scale levels of the pixel points of the intermediate gray image. Specifically, for each pixel point, the thermal printer compares its gray scale level with a preset threshold value, and if the gray scale level is greater than or equal to the threshold value, the pixel point is set to white (0), otherwise it is set to black (1). In this way, a binary image containing only black and white colors is obtained, which matches the device resolution information.
[0130] For example, assume that the thermal printer supports 16 gray scale levels and the resolution of the intermediate gray image is 300 dpi x 300 dpi. The thermal printer divides the gray value range of 0-255 into 16 sub-intervals, each with a span of 16. Then, the thermal printer traverses each pixel point of the intermediate gray image. For example, assume that the gray value of pixel point A at coordinates (100, 200) is 135, which belongs to the 9th sub-interval [128, 143] and corresponds to a gray scale level of 8. The thermal printer sets the gray scale level of pixel point A to 8. After completing the above processing for all pixel points of the intermediate gray image, the thermal printer selects the gray scale level 8 as the binary threshold value, sets pixel points with a gray scale level greater than or equal to 8 to white, and sets pixel points with a gray scale level less than 8 to black, finally generating a binary image with a resolution of 300 dpi x 300 dpi.
[0131] Reference Figure 2The application further provides a heat-sensitive printing image processing device, which is a server, and the server 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 for acquiring an original image to be printed and converting the original image into a gray-scale image; the threshold distribution map generation module 202 is used for calculating gradient difference values of the gray-scale image in each preset pixel region and generating a threshold distribution map based on the gradient difference values of each preset pixel region; the heat accumulation risk map generation module 203 is used for generating a heat accumulation risk map based on weighted gray-scale values of the gray-scale image in each preset neighborhood window, the preset neighborhood window being a window with a first pixel point as the center, and the first pixel point being any one of a plurality of pixel points included in the gray-scale image; the preliminary binary image generation module 204 is used for generating a preliminary binary image by performing an error diffusion dithering algorithm on the gray-scale image according to the threshold distribution map; the processing parameter determination module 205 is used for extracting image features of the preliminary binary image, classifying the original image according to texture information and the image features of the original image to obtain an image type of the original image, and further used for calling 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 for 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; and the resampling module 207 is used for resampling the optimized binary image to generate final printing image data adapted to a resolution of a target printer and outputting the final printing image data.
[0132] In a possible implementation, the threshold distribution map generation module 202 calculates gradient difference values of the gray-scale image in each preset pixel region, and generates a threshold distribution map based on the gradient difference values of each preset pixel region, specifically including: the threshold distribution map generation module 202 divides the gray-scale image into a plurality of preset pixel regions of the same size; for each preset pixel region, the threshold distribution map generation module 202 calculates a gray-scale difference value between a target pixel point in the preset pixel region and a neighboring pixel point corresponding to the target pixel point, to obtain a plurality of gray-scale difference values in the preset pixel region, the target pixel point being any one pixel point in the preset pixel region; the threshold distribution map generation module 202 determines a weight coefficient of the corresponding gray-scale difference value according to the position of the target pixel point in the preset pixel region; the threshold distribution map generation module 202 multiplies each gray-scale difference value by the corresponding weight coefficient, and performs summation and averaging to obtain a gradient difference value of the preset pixel region; the threshold distribution map generation module 202 determines a gray-scale threshold corresponding to the gradient difference value according to a preset threshold distribution function, the preset threshold distribution function including a corresponding relationship between the gradient difference value and the gray-scale threshold; the threshold distribution map generation module 202 constructs a threshold distribution matrix according to the gray-scale thresholds of each preset pixel region, the elements of the threshold distribution matrix being the gray-scale thresholds; the threshold distribution map generation module 202 performs interpolation smoothing processing on the threshold distribution matrix to obtain a threshold distribution map with the same resolution as the original image.
[0133] In a possible implementation, the heat accumulation risk map generation module 203 generates a heat accumulation risk map based on weighted gray-scale values of the gray-scale image in each preset neighborhood window, specifically including: the heat accumulation risk map generation module 203 calculates a Euclidean distance between each second pixel point in the preset neighborhood window and a first pixel point as a window center, and generates a Gaussian weight coefficient for the first pixel point and the second pixel point through a preset Gaussian function based on the Euclidean distance, the second pixel point being any one pixel point in the preset neighborhood window except the first pixel point; the heat accumulation risk map generation module 203 multiplies the gray-scale values of the first pixel point and the second pixel point by the Gaussian weight coefficient, and accumulates all the product results to obtain a weighted gray-scale value; the heat accumulation risk map generation module 203 performs normalization processing on the weighted gray-scale value to obtain a heat accumulation value of the gray-scale image in each preset neighborhood window, and generates a heat accumulation risk map according to the heat accumulation value.
[0134] In a possible implementation, the optimization binarization image generation module 206 performs pixel processing on the preliminary binarization image according to the target processing parameter set and the heat accumulation risk map to obtain an optimization binarization image, specifically including: the optimization binarization 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 optimization binarization image generation module 206 obtains a heat accumulation value of a corresponding position of a target black pixel point on the heat accumulation risk map, the target black pixel point being any one of a plurality of black pixel points included in the preliminary binarization image; if the heat accumulation value is greater than or equal to the heat value threshold, the optimization binarization image generation module 206 determines a first statistical window with a preset statistical length as a center of the target black pixel point, and calculates a number of black pixel points in the first statistical window; if the number of black pixel points is greater than or equal to the local pixel threshold, the optimization binarization image generation module 206 determines a target replacement region with a preset replacement length as a center of the target black pixel point, and searches for all white pixel points in the target replacement region; the optimization binarization image generation module 206 determines a target white pixel point from the plurality of white pixel points, and performs pixel position replacement on the target black pixel point according to the target white pixel point to obtain the optimization binarization image.
[0135] In a possible implementation, the optimization binarization image generation module 206 determines a target white pixel point from the plurality of white pixel points, and performs pixel position replacement on the target black pixel point according to the target white pixel point to obtain the optimization binarization image, specifically including: the optimization binarization image generation module 206 determines a second statistical window with a preset statistical length as a center of each white pixel point, and calculates a number of black pixel points in the second statistical window; the optimization binarization image generation module 206 counts the number of black pixel points in the corresponding second statistical window for each white pixel point in the target replacement region, and determines the white pixel point corresponding to the lowest number of black pixel points as the target white pixel point; the optimization binarization image generation module 206 determines the white pixel point corresponding to the lowest number of black pixel points as the target white pixel point; if the number of black pixel points in the corresponding second statistical window of the target white pixel point is less than the number of black pixel points in the corresponding first statistical window of the target black pixel point, the optimization binarization image generation module 206 sets the color of the target black pixel point to white and the color of the target white pixel point to black to complete the pixel position replacement, and obtains the optimization binarization image.
[0136] In a possible implementation, the resampling module 207 resamples the optimized binary image to generate final printing image data adapted to the resolution of the target printer, specifically including: the resampling module 207 acquires device resolution information of the target printer; the resampling module 207 determines a resampling ratio according to the device resolution information; the resampling module 207 resamples the optimized binary image according to the resampling ratio by using a preset interpolation algorithm to generate an intermediate gray image; the resampling module 207 performs secondary binaryzation processing on the intermediate gray image to generate a final binary image matched with the device resolution information; and the resampling module 207 converts the final binary image into a data format supported by the target printer to generate the final printing image data.
[0137] In a possible implementation, the resampling module 207 performs secondary binaryzation processing on the intermediate gray image to generate a final binary image matched with the device resolution information, specifically including: the resampling module 207 acquires a maximum number of gray scale levels supported by the target printer; the resampling module 207 divides a gray value range of the intermediate gray image into a plurality of subintervals according to the maximum number of gray scale levels, and each subinterval corresponds to a gray scale level; the resampling module 207 traverses each pixel point of the intermediate gray image to determine a subinterval to which a gray value of each pixel point of the intermediate gray image belongs; the resampling module 207 sets a gray scale level of each pixel point of the intermediate gray image as a gray scale level corresponding to the subinterval to which the gray value of each pixel point of the intermediate gray image belongs; and the resampling module 207 generates the final binary image according to the gray scale levels of the pixel points of the intermediate gray image.
[0138] It should be noted that the apparatus provided in the above embodiments is only used as an example for dividing the above functional modules, and in actual applications, the above functions can be completed by different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the apparatus and method embodiments provided in the above embodiments belong to the same concept, and the specific implementation process is detailed in the method embodiments, which will not be repeated here.
[0139] The present application also provides an electronic device. Referring to Figure 3 , Figure 3 is a structural schematic diagram of an electronic device provided by an embodiment of the present application. The electronic device 300 can 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.
[0140] The communication bus 302 is configured to realize the connection and communication between the components.
[0141] The user interface 303 can include a display, a camera, and optionally a standard wired interface and a wireless interface.
[0142] The network interface 304 can optionally include a standard wired interface and a wireless interface (e.g., a Wi-Fi interface).
[0143] The processor 301 can include one or more processing cores. The processor 301 connects various parts of the server through various interfaces and lines, and performs various functions of the server and processes data by running or executing instructions, programs, code sets or instruction sets stored in the memory 305, and calling data stored in the memory 305. Optionally, the processor 301 can be implemented in at least one of a hardware form of a digital signal processing (DSP), a field-programmable gate array (FPGA), and a programmable logic array (PLA). The processor 301 can be integrated with a combination of one or more of a central processing unit (CPU), a graphics processing unit (GPU), and a modem. The CPU is mainly used to process an operating system, a user interface, and an application program. The GPU is used to render and draw the content to be displayed on the display. The modem is used to process wireless communication. It can be understood that the above-mentioned modem can also not be integrated into the processor 301, but can be implemented by a separate chip.
[0144] The memory 305 can include a random access memory (RAM) and a read-only memory (ROM). 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 can include a program storage area and a data storage area. The program storage area can store instructions for implementing an operating system, instructions for at least one function (such as a touch function, a sound playing function, an image playing function, etc.), instructions for implementing the above-mentioned various method embodiments, etc. The data storage area can store data involved in the above-mentioned various method embodiments, etc. The memory 305 can optionally be at least one storage device located away from the above-mentioned processor 301. For reference Figure 3The memory 305 as a computer storage medium can include an operating system, a network communication module, a user interface module, and an application of the heat-sensitive printing image processing method.
[0145] In Figure 3 In the electronic device 300 shown in the figure, the user interface 303 is mainly used to provide an interface for user input and obtain data input by the user, and the processor 301 can be used to call the application of the heat-sensitive printing image processing method stored in the memory 305, and when executed by one or more processors 301, the electronic device 300 executes the method described in one or more of the above embodiments. It should be noted that for the foregoing method embodiments, in order to simply describe, they are all expressed as a combination of a series of actions, but those skilled in the art should know that the application is not limited by the order of the described actions, because according to the application, certain steps can be performed in other order or simultaneously. Secondly, those skilled in the art should know that the embodiments described in the specification all belong to preferred embodiments, and the actions and modules involved are not necessarily required by the application.
[0146] The application also provides a computer-readable storage medium, which stores instructions. When executed by one or more processors 301, the electronic device 300 executes the method described in one or more of the above embodiments.
[0147] In the above embodiments, the description of each embodiment has its own focus, and the parts not described in detail in a certain embodiment can be referred to the related description of other embodiments.
[0148] In the several embodiments provided by the present application, it should be understood that the disclosed device can be implemented in other ways. For example, the device embodiments described above are only schematic. The division of the units is only a logical function division. There can be another division manner for actual implementation, for example, a plurality of units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the displayed or discussed mutual couplings or direct couplings or communication connections between different parts can be indirect couplings or communication connections through some interfaces, devices or units, and can be electrical or other forms.
[0149] The units described as separate components can or can not be physically separate, and the components displayed as units can or can not be physical units, that is, they can be located in one place, or can be distributed on a plurality of network units. Part or all of the units can be selected according to actual needs to achieve the purpose of the embodiment scheme.
[0150] In addition, each of the functional units in the various embodiments of the present application can be integrated in one processing unit, or each unit can be physically present separately, or two or more units can be integrated in one unit. The integrated unit can be realized in the form of hardware or in the form of a software functional unit.
[0151] If the integrated unit is realized in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer readable memory. Based on such understanding, the technical solutions of the present application, essentially or in part, or all or part of the technical solutions can be embodied in the form of a software product. The computer software product is stored in a memory and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the various embodiments of the method of the present application. The memory where the computer software product is stored includes: a U disk, a mobile hard disk, a magnetic disk or an optical disk, and various other media that can store program codes.
[0152] The above is only exemplary embodiments of the present disclosure, and cannot limit the scope of the present disclosure. That is, any equivalent changes and modifications made in accordance with the teachings of the present disclosure are still within the scope of the present disclosure. Other embodiments of the present disclosure will be readily apparent to those skilled in the art upon considering the specification and practicing the true principles of the present disclosure.
[0153] The present application is intended to cover any variations, uses, or adaptive changes of the present disclosure that follow the general principles of the present disclosure and include common knowledge or conventional technical means in the technical field of the present disclosure not described in the present disclosure. The specification and examples are only considered as exemplary, and the scope and spirit of the present disclosure are defined by the claims.
Claims
1. A thermal printing image processing method, characterized in that, The method includes: Obtain the original image to be printed and convert the original image into a grayscale image; Calculate the gradient difference value of the grayscale image in each preset pixel region, and generate a threshold distribution map based on the gradient difference value of each preset pixel region; A cumulative heat risk map is generated based on the weighted gray values of the grayscale image within each preset neighborhood window. The preset neighborhood window is a window centered on a first pixel, which is any one of the multiple pixels included in the grayscale image. Based on the threshold distribution map, an error diffusion jitter algorithm is applied to the grayscale image to generate a preliminary binarized image; Extract the 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; Based on the image type, a target processing parameter set corresponding to the image type is called from a set of preset processing parameters; The preliminary binarized image is processed pixel by pixel based on 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 printed image data adapted to the target printer resolution, and the final printed image data is output.
2. The method according to claim 1, characterized in that, The calculation of the gradient difference value of the grayscale image in each preset pixel region, and the generation of a threshold distribution map based on the gradient difference value of each preset pixel region, specifically includes: The grayscale image is divided into multiple preset pixel regions of the same size; For each preset pixel region, the grayscale difference between the target pixel and its adjacent pixels within the preset pixel region is calculated to obtain multiple grayscale differences within the preset pixel region, where the target pixel is any pixel within the preset pixel region. The weighting coefficient of the corresponding grayscale difference is determined based on the position of the target pixel in the preset pixel region; Each grayscale difference is multiplied by its corresponding weight coefficient and then summed and averaged to obtain the gradient difference value of the preset pixel region. According to a preset threshold distribution function, the grayscale threshold corresponding to the gradient difference value is determined, and the preset threshold distribution function includes the correspondence between the gradient difference value and the grayscale threshold. A threshold distribution matrix is constructed based on the grayscale threshold of each preset pixel region, wherein the elements of the threshold distribution matrix are the grayscale thresholds. The threshold distribution matrix is interpolated and smoothed to obtain a threshold distribution map with the same resolution as the original image.
3. The method according to claim 1, characterized in that, The step of generating a cumulative heat risk map based on the weighted grayscale values of the grayscale image within each preset neighborhood window specifically includes: Calculate the Euclidean distance between each second pixel point within the preset neighborhood window and the first pixel point, which is the center of the window, and generate Gaussian weight coefficients for the first pixel point and the second pixel point based on the Euclidean distance using a preset Gaussian function. The second pixel point is any pixel point within the preset neighborhood window other than the first pixel point. Multiply the gray values of the first pixel and the second pixel by the Gaussian weight coefficient, and sum all the product results to obtain the weighted gray value; The weighted gray values are normalized to obtain the cumulative heat value of the gray image in each of the preset neighborhood windows, and a cumulative heat risk map is generated based on the cumulative heat value.
4. The method according to claim 1, characterized in that, The step of performing pixel processing on the preliminary binarized image based on the target processing parameter set and the heat accumulation risk map to obtain an optimized binarized image specifically includes: Extract the heat value threshold, local pixel threshold, preset statistical length, and preset replacement length from the target processing parameter set; Obtain the cumulative heat value of the target black pixel at the corresponding position on the cumulative heat risk map, wherein the target black pixel is any one of the multiple black pixels included in the preliminary binarized image; If the cumulative heat value is greater than or equal to the heat value threshold, then 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, then a target displacement region with a side length of the preset displacement length is determined with the target black pixel as the center, and all white pixels within the target displacement region are searched. The target white pixel is determined from the plurality of white pixels, and the pixel position of the target black pixel is replaced according to the target white pixel to obtain the optimized binarized image.
5. The method according to claim 4, characterized in that, The step of determining a target white pixel from a plurality of white pixels and performing pixel position permutation on the target black pixels based on the target white pixel to obtain the optimized binarized image specifically includes: For each white pixel, a second statistical window with a side length of the preset statistical length is determined with the white pixel as the center, and the number of black pixels in the second statistical window is calculated. The number of black pixels in the corresponding second statistical window for each white pixel in the target replacement region 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 corresponding second statistical window of the target white pixel is less than the number of black pixels in the corresponding first statistical window of the target black pixel, then the color of the target black pixel is set to white and the color of the target white pixel is set to black, completing the pixel position replacement and obtaining the optimized binarized image.
6. The method according to claim 1, characterized in that, The step of resampling the optimized binarized image to generate final printed image data adapted to the target printer resolution specifically includes: Obtain the device resolution information of the target printer; The resampling ratio is determined based on the device resolution information; Using a preset interpolation algorithm, the optimized binarized image is resampled according to the resampling ratio to generate an intermediate grayscale image; The intermediate grayscale image is subjected to secondary binarization 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 printed image data.
7. The method according to claim 6, characterized in that, The step of performing secondary binarization on the intermediate grayscale image to generate a final binarized image that matches the device resolution information specifically includes: Obtain the maximum number of gray levels supported by the target printer; Based on the maximum gray level number, the gray value range of the intermediate grayscale image is divided into multiple sub-intervals, each sub-interval corresponding to a gray level. Traverse each pixel of the intermediate grayscale image to determine the sub-interval to which the grayscale value of each pixel belongs; Based on the sub-interval to which the gray value of each pixel in the intermediate grayscale image belongs, the gray level of each pixel in the intermediate grayscale image is set to the gray level corresponding to the sub-interval; The final binarized image is generated based on the grayscale level of each pixel in the intermediate grayscale image.
8. A thermal printing image processing apparatus, characterized in that, The device 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 region, and generate a threshold distribution map based on the gradient difference value of each preset pixel region. The heat accumulation risk map generation module (203) is used to generate a heat accumulation risk map based on the weighted gray values of the grayscale image in each preset neighborhood window. The preset neighborhood window is a window centered on a first pixel, and the first pixel is any one of the multiple pixels included in the grayscale image. The preliminary binarized image generation module (204) is used to perform an error diffusion jitter algorithm on the grayscale image according to the threshold distribution map to generate a preliminary binarized image; The processing parameter determination module (205) is used to extract the 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 preset set of multiple processing parameters according to the image type; The optimized binarized image generation module (206) is used to perform 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 resampling module (207) is used to resample the optimized binarized image to generate final printed image data adapted to the target printer resolution, and output the final printed image data.
9. An electronic device, characterized in that, The device includes a processor (301), a memory (305), a user interface (303), and a network interface (304). The memory (305) is used to store instructions. The user interface (303) and the network interface (304) are used to communicate with other devices. The processor (301) is used to execute the instructions stored in the memory (305) to cause the electronic device (300) to perform the method as described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores instructions that, when executed, perform the method as described in any one of claims 1-7.
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