Image compression method and device, storage medium, equipment and program product
By dividing the image into background and foreground regions and then expanding, merging, and cropping them, the problems of blurred edges and jagged lines in text images in JPEG compression technology are solved, achieving more efficient compression and better image quality preservation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-11
- Publication Date
- 2026-03-13
AI Technical Summary
Existing JPEG image compression technology results in blurred edges and jagged lines in compressed text images, leading to poor compression performance.
The image is divided into background rectangular regions and foreground rectangular regions, and then expanded, merged and cropped respectively. By identifying regions with three consecutive horizontally identical pixels as background regions, the background rectangular regions are expanded. Regions with adjacent dissimilar pixels are identified as foreground regions, which are then merged and cropped. Finally, the region data is encoded.
It improves compression efficiency, preserves the integrity of image quality, significantly reduces the amount of image data after compression, and avoids the loss of details or image blurring caused by over-compression.
Smart Images

Figure CN121661162A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of image processing, and in particular to an image compression method, apparatus, storage medium, device, and program product. Background Technology
[0002] Currently, JPEG image compression technology is widely used due to its high compression ratio, small file size, and strong compatibility. However, because it uses lossy compression, the image quality is irreversibly lost, which can easily lead to blurred text edges and jagged lines after compression, resulting in poor compression performance.
[0003] Therefore, how to improve image compression is a technical problem that urgently needs to be solved by those skilled in the art. Summary of the Invention
[0004] The purpose of this application is to provide an image compression method, apparatus, storage medium, medium, and program product that can improve the compression effect of images and significantly reduce the amount of image data after compression.
[0005] To address the aforementioned technical problems, this application provides an image compression method, the specific technical solution of which is as follows:
[0006] Get the image to be compressed;
[0007] The image to be compressed is traversed pixel by pixel in a preset order;
[0008] If there is a first target region with three consecutive horizontally identical pixels, the target region is used as a background rectangle region. The background rectangle region is expanded vertically or horizontally until different pixels are detected, and the expansion is stopped to obtain the expanded background region.
[0009] If there is a second target region with adjacent and different pixels, the second target region is used as the foreground rectangular region, and the foreground rectangular region is expanded to obtain the expanded foreground region;
[0010] The adjacent expanded foreground regions are merged to obtain the foreground region data;
[0011] The expanded background area is then covered and cropped to obtain the background area data;
[0012] The background and foreground regions are encoded separately to obtain compressed image data.
[0013] Optionally, the expanded background area is overlaid and cropped to obtain background area data including:
[0014] Perform coverage reduction on the background rectangular area;
[0015] If a target expanded background region is completely covered by the expanded foreground region, discard the target expanded background region that is completely covered by the expanded foreground region;
[0016] If the expanded background area of the target is not completely covered by the expanded foreground area, the uncovered areas of the expanded background area of the target are cropped to obtain the background area data.
[0017] Optionally, adjacent expanded foreground regions can be merged to obtain foreground region data including:
[0018] If the first expanded foreground region and the second expanded foreground region are in an inclusive relationship, then the first expanded foreground region and the second expanded foreground region are merged.
[0019] If the edge spacing between the first expanded foreground region and the second expanded foreground region is less than a set value, and the increased background area after merging the first expanded foreground region and the second expanded foreground region is less than a set background threshold, then the first expanded foreground region and the second expanded foreground region are merged.
[0020] Optionally, the foreground rectangular region is expanded to obtain an expanded foreground region including:
[0021] The foreground rectangular region is expanded, and an expansion termination condition is matched in real time during the expansion process; the expansion termination condition includes detecting a number of consecutive identical pixels horizontally or vertically.
[0022] If the expansion termination condition is met, the expanded foreground region is obtained.
[0023] Optionally, the background region data and foreground region data are encoded separately to obtain compressed image data, including:
[0024] Record the rectangular position and size of the foreground region data, and compress and encode it according to the actual image data to obtain the foreground encoded data;
[0025] Record the rectangular position and size of the background area data, and encode the background area data according to the corresponding pixel values to obtain background encoded data;
[0026] The foreground encoded data and the background encoded data are combined to obtain compressed image data.
[0027] Optionally, the rectangular position and size of the foreground region data are recorded, and compressed and encoded according to the actual image data to obtain foreground encoded data, including:
[0028] The rectangular coordinates of the recorded foreground region data;
[0029] Determine the image data type corresponding to the foreground region data;
[0030] If the image data type is text image, the foreground region data is losslessly compressed;
[0031] If the image data type is a non-text image, the foreground region data is subjected to lossless or lossy compression.
[0032] This application also provides an image compression apparatus, comprising:
[0033] The image acquisition module is used to acquire images to be compressed.
[0034] The pixel traversal module is used to traverse the image to be compressed pixel by pixel in a preset order.
[0035] The background expansion module is used to, if there are three horizontally consecutive target regions with the same pixel count, take the target region as a background rectangular region and expand the background rectangular region vertically or horizontally until different pixels are detected, and then stop expanding to obtain the expanded background region.
[0036] The foreground expansion module is used to expand the foreground rectangular region as a foreground rectangular region if there is a second target region with adjacent dissimilar pixels, thereby obtaining an expanded foreground region.
[0037] The foreground merging module is used to merge adjacent expanded foreground regions to obtain foreground region data;
[0038] The background merging module is used to cover and crop the expanded background area to obtain background area data.
[0039] The image compression module is used to encode the background area data and the foreground area data separately to obtain compressed image data.
[0040] This application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the method described above.
[0041] This application also provides an electronic device, including a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the method described above when it invokes the computer program in the memory.
[0042] This application also provides a computer program product, including a computer program that, when executed, implements the steps of the method described above.
[0043] This application provides an image compression method, comprising: acquiring an image to be compressed; traversing the image to be compressed pixel by pixel in a preset order; if there is a first target region with three consecutive horizontally identical pixels, using the target region as a background rectangular region, expanding the background rectangular region vertically or horizontally until different pixels are detected, thereby obtaining an expanded background region; if there is a second target region with adjacent different pixels, using the second target region as a foreground rectangular region, expanding the foreground rectangular region, thereby obtaining an expanded foreground region; merging adjacent expanded foreground regions to obtain foreground region data; covering and cropping the expanded background region to obtain background region data; and encoding the background region data and the foreground region data respectively to obtain compressed image data.
[0044] This application divides an image into background and foreground rectangular regions, and then expands, merges, and crops them separately. During compression, it fully considers the spatial continuity of pixels in the image. Background region identification is performed on regions with three consecutive identical pixels horizontally, effectively capturing large, uniformly colored areas such as the sky, walls, and background cloth. This allows the expanded background rectangular region to cover more identical pixels, reducing the amount of redundant data encoded. Regions with adjacent dissimilar pixels are identified as foreground regions, reflecting details, edges, or object outlines. By expanding the foreground rectangular region and merging adjacent regions, key visual information in the image is better preserved, avoiding detail loss or image blurring due to over-compression. Distinguishing between foreground and background rectangular data makes the compression process more targeted, improving compression efficiency while ensuring image quality integrity. During background region expansion, it continues to expand vertically or horizontally until encountering dissimilar pixels, flexibly adapting to background regions of different shapes and distributions, improving the accuracy of background coverage and compression effect. Finally, the expanded foreground region is merged, effectively reducing the number of fragmented regions, optimizing the encoding structure, and significantly reducing the amount of compressed image data.
[0045] This application also provides an image compression device, a computer-readable storage medium, an electronic device, and a computer program product, which have the above-mentioned beneficial effects, and will not be repeated here. Attached Figure Description
[0046] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of this application. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.
[0047] Figure 1 A flowchart illustrating an image compression method provided in an embodiment of this application;
[0048] Figure 2 This is a flowchart illustrating the foreground region data encoding process provided in an embodiment of this application.
[0049] Figure 3 This is a schematic diagram of an image compression device provided in an embodiment of this application;
[0050] Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0051] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0052] See Figure 1 , Figure 1 A flowchart of an image compression method provided in this application embodiment, the method comprising:
[0053] S101: Obtain the image to be compressed;
[0054] S102: Traverse the image to be compressed pixel by pixel in a preset order;
[0055] S103: If there is a first target region with three consecutive horizontally identical pixels, the target region is used as a background rectangular region. The background rectangular region is expanded vertically or horizontally until different pixels are detected, and the expansion is stopped to obtain the expanded background region.
[0056] S104: If there is a second target region with adjacent dissimilar pixels, take the second target region as the foreground rectangular region, expand the foreground rectangular region, and obtain the expanded foreground region;
[0057] S105: Merge adjacent expanded foreground regions to obtain foreground region data;
[0058] S106: The expanded background area is covered and cropped to obtain background area data;
[0059] S107: Encode the background region data and the foreground region data separately to obtain compressed image data.
[0060] This application can be applied to the server side, particularly for computationally generated or computer-generated images. For example, a remote desktop image, which is a screenshot of the desktop taken on the server, can be transmitted to the client after compression using the method disclosed in this application. Furthermore, this application can also be applied to the terminal, where the compression method disclosed in this application is executed on the terminal, and then transmitted to other terminals via the server.
[0061] It should be noted that the execution order of the steps in this embodiment is only one feasible example. In the actual application of this application, there is no strict predetermined execution order between steps S103 and S106. It is only required that the expanded background region be obtained first, and then the expanded background region be covered and cropped to obtain the background region data. Also, the expanded foreground region be obtained first, and then the foreground region be merged. In other embodiments of this application, parallel execution or other execution orders can be used without affecting the technical effects produced by this application.
[0062] There are no restrictions on how the image to be compressed is obtained; the method can be flexibly chosen based on differences in image source, acquisition path, and data format. For example, it can be read through the local file system, that is, the operating system's standard interface or a third-party file parsing library traverses the specified directory, reads the file stream into memory, and restores it to a pixel array through a format parser, thereby completing the acquisition of the static image. The file format of the image to be compressed can be BMP, TIFF, PNG, JPEG, RAW, or a custom format. In one specific application of this application, the image to be compressed can be a screenshot of the displayed content, including currently displayed text, images, and other content.
[0063] In step S102, the aim is to access each pixel in the image in a preset order. The preset order is not limited, as long as it ensures that the traversal order reaches all pixels. Typically, either the vertical or horizontal order can be used as the basis. For example, a raster-order line-by-line scanning method can be used, starting from the top-left pixel, moving horizontally to the end of the line and then back to the beginning of the next line, repeating this process until the bottom-right pixel. This scanning method closely matches the linear arrangement of memory and can fully utilize the CPU prefetch mechanism, thereby improving the traversal speed.
[0064] In step S103, the aim is to identify large areas of relatively flat, uniformly colored background in the image to be compressed, and aggregate them into connected rectangles through region expansion for subsequent overall encoding. During the detection phase, a single-channel threshold can be set. For each consecutive pixel sequence during pixel traversal, the pairwise differences are calculated in the luminance channel or a custom color difference channel. If the differences are all below the single-channel threshold, they are considered the same pixel, thus determining the first target region. The single-channel threshold can be flexibly adjusted without sacrificing subjective quality, thereby capturing more background pixels.
[0065] In another feasible implementation of this step, a multi-channel joint determination method can be used. This involves separately examining the red, green, and blue components, or the luminance and chrominance components, and considering pixels as the same only when the differences between all components are less than the corresponding component difference threshold. This improves the accuracy of color consistency judgment and avoids mistakenly merging pixels that do not belong to the same background object at gradient edges.
[0066] During the pixel expansion stage, methods including but not limited to horizontal expansion, vertical expansion, and rectangular perimeter expansion can be employed. For example, a four-neighbor or eight-neighbor flooding approach can be used. Starting from an initial three-pixel strip, adjacent pixels in the top, bottom, left, right, or diagonal directions are checked sequentially. If the colors are consistent, the area is included and expansion continues, quickly expanding to the maximum connected rectangle, reducing the number of rectangles required for subsequent encoding, thereby improving the compression ratio. In another implementation, a bidirectional ray expansion approach can be used. This involves first extending horizontally to the left and right ends until encountering dissimilar pixels, then advancing vertically to the top and bottom, shrinking the horizontal boundary in real time during the advancement to maintain the rectangular property. This approach reduces invalid judgments and lowers CPU overhead by using rectangular constraints.
[0067] For high-resolution images to be compressed, a multi-scale expansion strategy can be implemented. This involves first performing background detection and coarse localization on a downsampling layer, then mapping the rectangle back to a full-resolution layer for edge refinement. By utilizing the smoothness of the low-resolution layer to pre-filter large areas of background, the number of full-size pixel comparisons is reduced, thereby improving the detection efficiency of the expanded background area.
[0068] In step S104, the foreground rectangular region is expanded to separate foreground objects with drastic color changes, rich details, or sharp edges from the background. An independent foreground rectangle is formed through region growing, allowing for subsequent use of a different encoding strategy than the background. A second target region, lower than adjacent disparate pixels, is used as the initial foreground rectangular region, thereby expanding the foreground rectangular region to obtain the expanded foreground region.
[0069] In the specific implementation process, the foreground rectangular region can be expanded, and an expansion termination condition can be matched in real time during the expansion process; if the expansion termination condition is met, the expanded foreground region is obtained. The expansion termination condition may include detecting a number of consecutive identical pixels horizontally or vertically. The number of consecutive identical pixels is not specifically limited and can be set by those skilled in the art. After obtaining the expanded foreground region, the position and size of the expanded foreground region can be recorded.
[0070] In the pixel detection stage during the expansion process, the gradient magnitude threshold method can be adopted. By calculating the absolute value of the difference in luminance or chrominance between adjacent pixels, if it exceeds the preset edge sensitivity threshold, it is marked as a foreground seed. In this way, the positions with drastic edge changes can be quickly located, ensuring that high-frequency information such as text, texture, and contour is not misjudged as the background.
[0071] In the foreground rectangle region expansion stage, edge-tracking growth can be adopted, preferentially expanding along the edge direction and keeping the expansion boundary fitting the contour of the real object, thereby reducing the situation of mis-packing background pixels into the foreground rectangle and reducing the redundancy of subsequent coding. Alternatively, the block aggregation method can be adopted, that is, the detected discrete foreground pixels are first classified into grid cells of a fixed size, and then adjacent cells are merged into a larger rectangle, thereby replacing an arbitrary shape with a regular grid, simplifying the rectangle data structure, and facilitating hardware parallel acceleration.
[0072] In addition, the semantic prior method can be introduced, that is, a lightweight classification network is used to generate a foreground mask in advance, and then the high-confidence regions of the foreground mask are mapped into rectangle seeds, thereby excluding the interference of pseudo-edges such as shadows and reflections in the picture to be compressed and improving the accuracy of foreground region detection.
[0073] In steps S105 and S106, the expanded foreground region and the expanded background region need to be processed separately. It should be noted that for special pictures, such as solid-color pictures, it is impossible to confirm their foreground region and background region, and they are not applicable to the processing process of the foreground region in this application. The encoding method for the background region in step S107 can be directly referred to for encoding to obtain the compressed picture. For other special pictures, such as pictures with only a single object (such as a single character), further judgment can be made according to the character structure. For example, for a simple character structure, it can be regarded as the merged expanded foreground region, and for a complex character structure, each part of the character can be regarded as an expanded foreground region, so as to perform the merging of the expanded foreground regions. The judgment criteria for simple and complex character structures can be classified by referring to the combination of strokes and the layout of glyphs. For example, a single-structure character can be regarded as a simple character structure, and characters with left-right structure, up-down structure, surrounded structure, three-in-one structure, interpenetrating structure, and composite structure (such as "赢") can be regarded as complex character structures. For the expanded foreground region, it is necessary to determine whether it can be merged according to the regional relationship between two expanded foreground regions. In a feasible implementation manner, if the first expanded foreground region and the second expanded foreground region are in an inclusion relationship, the first expanded foreground region and the second expanded foreground region are merged.
[0074] If the edge spacing between the first expanded foreground region and the second expanded foreground region is less than a set value, and the increased background area after merging the first expanded foreground region and the second expanded foreground region is less than a set background threshold, then the first expanded foreground region and the second expanded foreground region are merged.
[0075] However, if the edge spacing between the first expanded foreground region and the second expanded foreground region is not less than the set value, then merging is refused.
[0076] In a more preferred embodiment, merging rules or merging priorities can be set for the expanded foreground region. Following the example above, the following merging priorities can be set, with the merging priorities of the four merging rules A, B, C, and D decreasing sequentially:
[0077] A. If the first expanded foreground region and the second expanded foreground region have an inclusion relationship, they can be merged.
[0078] B. If the edge spacing between the first expanded foreground area and the second expanded foreground area is greater than the preset value (e.g., 3 vertically and 16 horizontally), refuse to merge.
[0079] C. If the increased background area after merging the first expanded foreground area and the second expanded foreground area is less than a preset value (e.g., 512), they can be merged to avoid excessive expansion.
[0080] D. In other cases, refuse to merge the first expanded foreground region and the second expanded foreground region.
[0081] After merging, the identifier, bounding rectangle coordinates, area, perimeter, edge point list, or compressed binary payload of each foreground region can be recorded to form foreground region data. This step reduces the number of rectangles required for subsequent encoding by merging fragmented foreground rectangles, thereby reducing syntax element overhead and boundary description complexity, while maintaining the integrity of visually salient regions and improving compression ratio and subjective quality.
[0082] For the expanded background area, it is necessary to perform coverage judgment and cropping. The purpose is to remove the part inside the background rectangle that has been covered by the foreground rectangle, so as to avoid the same pixel being described repeatedly in the background and foreground, thereby eliminating redundancy.
[0083] In one feasible implementation, a rectangle-by-rectangle cropping method can be used. For each background rectangle, the difference operation is performed sequentially with all intersecting foreground rectangles to split the background rectangle into several remaining sub-rectangles, thereby accurately removing the occluded area, ensuring unique pixel assignment, and improving the compression rate.
[0084] After cropping, a fragment sorting method can be further employed to discard remaining background fragments with an area smaller than a preset lower limit, and their pixels can be assigned to adjacent foreground rectangles to reduce the number of excessively small background rectangles, thereby reducing head information overhead. At the same time, the high-fidelity characteristics of foreground encoding are utilized to ensure that visual quality is not affected. Step S106 can remove the covered parts of the background with pixel-level precision, generating background area data without redundancy, providing compact input for subsequent differential encoding.
[0085] In step S107, the overall bitstream is minimized by selecting matching encoding strategies for different regional characteristics. First, for the background rectangle data, a unified color indexing method can be used. That is, for each background rectangle, only the coordinates of the top-left corner, its width and height, and a representative color value are encoded. This replaces a large number of pixels with a very short header and a single color payload, significantly reducing the bit occupancy in flat areas. In one feasible implementation, the rectangle positions and sizes of the foreground region data can be recorded and compressed according to the actual image data to obtain foreground encoded data. Simultaneously, the rectangle positions and sizes of the background region data are recorded, and the background region data is encoded according to the corresponding pixel values to obtain background encoded data. Finally, the foreground encoded data and background encoded data are merged to obtain the compressed image data.
[0086] For more details, see Figure 2 , Figure 2 The flowchart for encoding foreground region data provided in this application embodiment shows that when encoding foreground region data, the rectangular coordinates of the foreground region data can be recorded, and the image data type corresponding to the foreground region data can be determined.
[0087] If the image data type is text image, perform lossless compression on the foreground area data.
[0088] If the image data type is a non-text image, perform lossless or lossy compression on the foreground area data.
[0089] Lossy compression refers to the loss of some data information during the compression process, and the original data cannot be fully recovered after decompression. Lossless compression refers to the complete recovery of the original data after decompression without any data loss. By using lossless compression for text and images, issues such as blurred text edges after compression can be avoided.
[0090] For foreground rectangle data, a unified approach to the region header can be adopted in the bitstream organization. First, the total number of background rectangles and the total number of foreground rectangles are written, and then the encoded data of each rectangle is arranged in sequence. This allows the decoding end to allocate resources in advance and supports parallel decoding.
[0091] After obtaining the compressed image data, the compressed bitstream can be encapsulated into a general container format and a checksum can be appended to the end for integrity verification during transmission and storage, thereby improving the reliability of the compressed data and preventing decoding failure due to channel noise.
[0092] Step S107 employs different encoding strategies for the background rectangular data and the foreground rectangular data to minimize redundancy while maintaining visual quality, ultimately generating compact, reliable, and scalable compressed image data.
[0093] This embodiment divides the image into background rectangular regions and foreground rectangular regions, and performs expansion, merging, and cropping processes on them respectively. During compression, the spatial continuity of pixels in the image is fully considered. Background region identification is performed on regions with three consecutive identical pixels horizontally, effectively capturing large areas of consistent color in the image, such as the sky, walls, and background cloth. This expands the background rectangular region to cover more identical pixels, reducing the amount of redundant data encoded. Regions with adjacent dissimilar pixels are identified as foreground regions, reflecting parts of the image containing details, edges, or object outlines. By expanding the foreground rectangular region and merging adjacent regions, key visual information in the image can be better preserved, avoiding detail loss or image blurring due to over-compression. By distinguishing between foreground and background rectangular data, the compression process becomes more targeted, improving compression efficiency while ensuring the integrity of image quality. During background region expansion, expansion continues vertically or horizontally until dissimilar pixels are encountered, flexibly adapting to background regions of different shapes and distributions, improving the accuracy of background coverage and compression effect. Finally, the expanded foreground region is merged, effectively reducing the number of fragmented regions, optimizing the encoding structure, and significantly reducing the amount of compressed image data.
[0094] See Figure 3 , Figure 3 This is a schematic diagram of an image compression device provided in an embodiment of this application. The device includes:
[0095] The image acquisition module is used to acquire images to be compressed.
[0096] The pixel traversal module is used to traverse the image to be compressed pixel by pixel in a preset order.
[0097] The background expansion module is used to, if there are three horizontally consecutive target regions with the same pixel count, take the target region as a background rectangular region and expand the background rectangular region vertically or horizontally until different pixels are detected, and then stop expanding to obtain the expanded background region.
[0098] The foreground expansion module is used to expand the foreground rectangular region as a foreground rectangular region if there is a second target region with adjacent dissimilar pixels, thereby obtaining an expanded foreground region.
[0099] The foreground merging module is used to merge adjacent expanded foreground regions to obtain foreground region data;
[0100] The background merging module is used to cover and crop the expanded background area to obtain background area data.
[0101] The image compression module is used to encode the background area data and the foreground area data separately to obtain compressed image data.
[0102] This application also provides a computer-readable storage medium and a computer program product, both of which store a computer program. When executed, the computer program can implement the steps of the image compression method provided in the above embodiments. The storage medium may include various media capable of storing program code, such as a USB flash drive, a portable hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.
[0103] This application also provides an electronic device that may include a memory and a processor. The memory stores a computer program, and when the processor invokes the computer program in the memory, it can implement the steps of the method provided in the above embodiments. Of course, the electronic device may also include various network interfaces, power supplies, and other components. Please see [link to relevant documentation]. Figure 4 , Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. The electronic device in this embodiment may include: a processor 2101 and a memory 2102.
[0104] Optionally, the electronic device may also include a communication interface 2103, an input unit 2104, a display 2105, and a communication bus 2106.
[0105] The processor 2101, memory 2102, communication interface 2103, input unit 2104, and display 2105 all communicate with each other through the communication bus 2106.
[0106] In this embodiment of the application, the processor 2101 may be a central processing unit (CPU), an application-specific integrated circuit, a digital signal processor, an off-the-shelf programmable gate array, or other programmable logic devices.
[0107] The processor can call programs stored in memory 2102. Specifically, the processor can execute the operations performed by the electronic device in the above embodiments.
[0108] The memory 2102 is used to store one or more programs, which may include program code, including computer operation instructions. In this embodiment, the memory stores at least a program for implementing the following functions:
[0109] The process involves: acquiring an image to be compressed; traversing the image pixel by pixel in a preset order; if a first target region with three consecutive horizontally identical pixels exists, using this target region as a background rectangle region, and expanding the background rectangle region vertically or horizontally until different pixels are detected, thus obtaining an expanded background region; if a second target region with adjacent different pixels exists, using this second target region as a foreground rectangle region, and expanding the foreground rectangle region, thus obtaining an expanded foreground region; merging adjacent expanded foreground regions to obtain foreground region data; cropping and covering the expanded background region to obtain background region data; and encoding the background region data and foreground region data respectively to obtain compressed image data.
[0110] In one possible implementation, the memory 2102 may include a program storage area and a data storage area, wherein the program storage area may store the operating system and applications required for at least one function; and the data storage area may store data created during the use of the computer.
[0111] In addition, memory 2102 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device or other volatile solid-state storage device.
[0112] The communication interface 2103 can be an interface for a communication module, such as the interface for a GSM module.
[0113] This application may also include a display 2105 and an input unit 2104, etc.
[0114] Figure 4 The structure of the electronic device shown does not constitute a limitation on the electronic device in the embodiments of this application. In practical applications, the electronic device may include more than [other components]. Figure 4 More or fewer components as shown, or combinations of certain components.
[0115] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. As the system provided in the embodiments corresponds to the method provided in the embodiments, the description is relatively simple; relevant parts can be found in the method section.
[0116] This document uses specific examples to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the methods and core ideas of this application. It should be noted that those skilled in the art can make several improvements and modifications to this application without departing from the principles of this application, and these improvements and modifications also fall within the protection scope of this application.
[0117] It should also be noted that, in this specification, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
Claims
1. An image compression method, characterized in that, include: Get the image to be compressed; The image to be compressed is traversed pixel by pixel in a preset order; If there is a first target region with three consecutive horizontally identical pixels, the target region is used as a background rectangle region. The background rectangle region is expanded vertically or horizontally until different pixels are detected, and the expansion is stopped to obtain the expanded background region. If there is a second target region with adjacent and different pixels, the second target region is used as the foreground rectangular region, and the foreground rectangular region is expanded to obtain the expanded foreground region; The adjacent expanded foreground regions are merged to obtain the foreground region data; The expanded background area is then covered and cropped to obtain the background area data; The background and foreground regions are encoded separately to obtain compressed image data.
2. The image compression method according to claim 1, characterized in that, The adjacent expanded foreground regions are merged to obtain foreground region data including: If the first expanded foreground region and the second expanded foreground region are in an inclusive relationship, then the first expanded foreground region and the second expanded foreground region are merged. If the edge spacing between the first expanded foreground region and the second expanded foreground region is less than a set value, and the increased background area after merging the first expanded foreground region and the second expanded foreground region is less than a set background threshold, then the first expanded foreground region and the second expanded foreground region are merged.
3. The image compression method according to claim 1, characterized in that, The expanded background area is then overlaid and cropped to obtain background area data including: Perform coverage reduction on the background rectangular area; If a target expanded background region is completely covered by the expanded foreground region, discard the target expanded background region that is completely covered by the expanded foreground region; If the expanded background area of the target is not completely covered by the expanded foreground area, the uncovered areas of the expanded background area of the target are cropped to obtain the background area data.
4. The image compression method according to claim 1, characterized in that, Expanding the foreground rectangular region yields an expanded foreground region comprising: The foreground rectangular region is expanded, and an expansion termination condition is matched in real time during the expansion process; the expansion termination condition includes detecting a number of consecutive identical pixels horizontally or vertically. If the expansion termination condition is met, the expanded foreground region is obtained.
5. The image compression method according to claim 1, characterized in that, The background and foreground regions are encoded separately to obtain compressed image data, including: Record the rectangular position and size of the foreground region data, and compress and encode it according to the actual image data to obtain the foreground encoded data; Record the rectangular position and size of the background area data, and encode the background area data according to the corresponding pixel values to obtain background encoded data; The foreground encoded data and the background encoded data are combined to obtain compressed image data.
6. The image compression method according to claim 5, characterized in that, The rectangular positions and sizes of the foreground region data are recorded, and compressed and encoded according to the actual image data to obtain foreground encoded data, including: The rectangular coordinates of the foreground region data are recorded; Determine the image data type corresponding to the foreground region data; If the image data type is text image, the foreground region data is losslessly compressed; If the image data type is a non-text image, the foreground region data is subjected to lossless or lossy compression.
7. An image compression device, characterized in that, include: The image acquisition module is used to acquire images to be compressed. The pixel traversal module is used to traverse the image to be compressed pixel by pixel in a preset order. The background expansion module is used to, if there are three horizontally consecutive target regions with the same pixel count, take the target region as a background rectangular region and expand the background rectangular region vertically or horizontally until different pixels are detected, and then stop expanding to obtain the expanded background region. The foreground expansion module is used to expand the foreground rectangular region as a foreground rectangular region if there is a second target region with adjacent dissimilar pixels, thereby obtaining an expanded foreground region. The foreground merging module is used to merge adjacent expanded foreground regions to obtain foreground region data; The background merging module is used to cover and crop the expanded background area to obtain background area data. The image compression module is used to encode the background area data and the foreground area data separately to obtain compressed image data.
8. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor for executing the computer program to implement the steps of the method as claimed in any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed, implements the steps of the method as described in any one of claims 1 to 6.
10. A computer program product, characterized in that, Includes a computer program, which, when executed, implements the steps of the method as described in any one of claims 1 to 6.