A picture lossless compression and decompression method
By encoding the image in blocks and uniformly storing repeated blocks, the problems of insufficient compression rate and speed in the existing technology are solved, efficient lossless image compression is achieved, and high-beat transmission requirements are met.
Patent Information
- Application Number
- CN202411487498.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-24
- Publication Date
- 2025-10-10
- Estimated Expiration
- 2044-10-24
AI Technical Summary
Existing image compression algorithms have shortcomings in balancing compression rate and compression speed, and are unable to meet the needs of high-frequency image transmission.
A lossless image compression method is adopted. The image is divided into blocks and different encoding formats are selected according to the similarity and grayscale difference of the image blocks. Combined with the unified storage of repeated blocks, the storage length is optimized to improve the compression rate and speed.
It achieves a compression rate similar to PNG and the encoding time is close to LZ4_DFT, meeting the requirements of high-beat image transmission and reducing the burden of image transmission and storage.
Smart Images

Figure CN119342224B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of image compression, and in particular to a method for lossless compression and decompression of images. Background Art
[0002] To reduce transmission and storage burdens, images need to be compressed. Commonly used compression algorithms include JPEG, PNG, ZLIB, and LZ4. In machine vision applications, lossless formats are primarily used for image storage to avoid noise. PNG is a representative lossless compression algorithm, but its pursuit of a high compression rate comes at the expense of compression speed. ZLIB and LZ4 are general-purpose binary compression algorithms, combining Huffman coding with a dictionary method (LZ77). Their compression rates are not stable for images. The GPOS method is relatively fast and offers a relatively high compression rate. However, its compression speed lags significantly behind LZ4_DFT, and its compression rate lags significantly behind PNG. While the BMP format takes less time to encode, its compression rate is too low.
[0003] In actual use, in order to meet the requirements of production rhythm, an image lossless compression algorithm with high compression ratio and fast compression speed is urgently needed. Summary of the Invention
[0004] In order to solve the above technical problems, the present invention provides a method for lossless compression and decompression of images. The compression rate of this method is close to that of PNG. At the same time, the encoding time is close to that of the fastest compression algorithm LZ4_DFT. It is a lossless compression method for images that takes into account both compression rate and compression speed; it reduces the pressure of image transmission and storage, and meets the requirements of high-beat image transmission.
[0005] To this end, the technical solutions of the present invention are as follows:
[0006] A method for lossless compression of an image, comprising the following steps:
[0007] Divide the image into multiple image blocks according to its width and height; then traverse each image block in turn according to the preset traversal order;
[0008] For the currently traversed image block, the following compression processing is performed:
[0009] Determine whether the image block is the same as the previously traversed image block (the grayscale value of each pixel in the current image block corresponds one-to-one with the grayscale value of each pixel in the previously traversed image block):
[0010] If they are the same, encode according to format 1;
[0011] If they are not the same, continue to determine whether the image block is the same as the image block traversed in the adjacent row and the same column (the grayscale value of each pixel in the current image block and the image block directly above / below it corresponds one to one):
[0012] If they are the same, they are encoded according to format 2;
[0013] If they are not the same, the maximum difference of the grayscale values in the image block is calculated. If the maximum difference is equal to 0, encoding is performed according to format three;
[0014] If the maximum difference is greater than or equal to 128, it is encoded according to format 4;
[0015] If the maximum difference is greater than 0 and less than 128, it is encoded according to format five;
[0016] The storage content of the format 1 is: format 1 number;
[0017] The storage content of format 2 is: format 2 number;
[0018] The storage content of format three includes the following: format three number, grayscale value of any point in the current image block;
[0019] The storage content of format 4 includes the following: format 4 number, grayscale value of each pixel in the current image block;
[0020] The storage content of format five includes the following recorded in sequence: format five number, the minimum grayscale value in the current image block, and the difference between the grayscale values of all pixels and the minimum grayscale value.
[0021] Furthermore, format one occupies 1 byte, which is used to store the format one number;
[0022] Format 2 occupies 1 byte, which is used to store the format 2 number;
[0023] Format three occupies 2 bytes, where the first byte is used to store the format two number and the second byte is used to store the grayscale value of any point in the current image block;
[0024] Format four occupies 1+H bytes, where the first byte is used to store the format four number, and the other H bytes are used to store the grayscale values of H pixels in the current image block; that is, the grayscale value of a pixel is represented by 8 bits per pixel.
[0025] Format Five Occupancy bytes, wherein the first byte is used to store the format five number, the second byte is used to store the minimum grayscale value in the current image block, and the other bytes are used to store the grayscale difference values of H pixels in the current image block.
[0026] The present invention also provides a decompression method for the above-mentioned image lossless compression method, comprising the following steps:
[0027] Traverse each image block in sequence according to the preset traversal order;
[0028] For the currently traversed image block, perform the following decompression processing:
[0029] Reading a format number corresponding to an image block, wherein the format number includes format number 1 to format number 5;
[0030] Then determine the corresponding format when encoding based on the format number;
[0031] If the corresponding format is 1, the decompression method is: copying the basic image block to the currently traversed image block; the basic image block is the image block that was most recently decompressed using any one of the formats 2 to 5;
[0032] If it corresponds to format 2, the decompression method is: copy the image blocks in adjacent rows and the same column that have been decompressed to the image block currently traversed;
[0033] If it corresponds to format three, the decompression method is: make the grayscale value of each pixel in the currently traversed image block equal to the grayscale value recorded in format three;
[0034] If it corresponds to format 4, the decompression method is: let the grayscale value of each pixel in the currently traversed image block be assigned to the grayscale value of each pixel recorded in format 4;
[0035] If it corresponds to format five, the decompression method is: restore the grayscale value of each pixel in the currently traversed image block according to the minimum grayscale value recorded in format five and the difference between the grayscale values of all pixels and the minimum grayscale value.
[0036] In order to further improve the compression rate, preferably, if the grayscale values of each pixel in the current image block correspond one-to-one with the grayscale values of each pixel in the previously traversed image block, the current image block is recorded as a repeated block;
[0037] The content stored in the format 1 further includes: a repetition degree M, wherein the repetition degree represents the number M of repeated blocks that appear continuously;
[0038] During the traversal process, if continuous repeated blocks appear, the continuous repeated blocks will be uniformly encoded using format one.
[0039] Furthermore, format one occupies 1+W bytes, the first byte is used to store the format one number, and the other bytes are used to store the repetition degree; the value of W is 1-4.
[0040] Furthermore, a decompression method for the above-mentioned image lossless compression method includes the following steps:
[0041] Traverse each image block in sequence according to the preset traversal order;
[0042] For the currently traversed image block, perform the following decompression processing:
[0043] Reading a format number corresponding to an image block, wherein the format number includes format number 1 to format number 5;
[0044] Then determine the corresponding format when encoding based on the format number;
[0045] If the image corresponds to format 1, the decompression method is as follows: according to the repetition degree M recorded in format 1, the currently traversed image block and the M-1 image blocks to be traversed subsequently are copied as the basic image block; the basic image block is the image block that was most recently decompressed using any format from format 2 to format 5;
[0046] If it corresponds to format 2, the decompression method is: copy the image blocks in adjacent rows and the same columns that have been decompressed to the image block currently traversed;
[0047] If it corresponds to format three, the decompression method is: make the grayscale value of each pixel in the currently traversed image block equal to the grayscale value recorded in format three;
[0048] If it corresponds to format four, the decompression method is: let the grayscale value of each pixel in the currently traversed image block be assigned to the grayscale value of each pixel recorded in format four.
[0049] If it corresponds to format five, the decompression method is: restore the grayscale value of each pixel in the currently traversed image block according to the minimum grayscale value recorded in format five and the difference between the grayscale values of all pixels and the minimum grayscale value.
[0050] Furthermore, the value of the format five number is consistent with the number of bits N corresponding to the maximum difference in the grayscale value in the image block;
[0051] In format five, when storing the difference between the grayscale values of all pixels and the minimum grayscale value, one difference is stored for every N bits.
[0052] Preferably, the size of the image block is k×m, where k=3-8 and m=3-8.
[0053] Furthermore, the preset traversal order is one of: first from left to right and then from top to bottom, first from left to right and then from bottom to top, first from right to left and then from top to bottom, and first from right to left and then from bottom to top.
[0054] Preferably, the image blocks traversed at adjacent rows and the same column are obtained by:
[0055] Search in the encoded image blocks. If the search finds that in a certain image block, the row and column coordinates of the pixel point are different by 1 from the row and column coordinates of the pixel point in the currently traversed image block, but the column coordinates are the same; then the image block is the traversed image block at the adjacent row and the same column.
[0056] This method encodes image blocks in different formats and matches the encoding format with the smallest storage length for each image block, effectively improving the compression speed while ensuring a high compression ratio.
[0057] Storing multiple consecutive reread blocks in the same byte greatly compresses the storage space of repeated blocks, reducing the burden of image transmission and storage, and meeting the requirements of high-beat image transmission and detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0058] Figure 1 A comparison chart of encoding time and compression rate for different compression methods. DETAILED DESCRIPTION
[0059] The technical solution of the present invention is described in detail below with reference to the accompanying drawings and specific embodiments.
[0060] Example 1
[0061] A method for lossless compression of an image, comprising the following steps:
[0062] Divide the image into multiple image blocks according to its width and height; then traverse each image block in turn according to the preset traversal order;
[0063] For the currently traversed image block, the following compression processing is performed:
[0064] Determine whether the image block is the same as the previously traversed image block (the grayscale value of each pixel in the current image block corresponds one-to-one with the grayscale value of each pixel in the previously traversed image block):
[0065] If they are the same, encode according to format 1;
[0066] If they are not the same, continue to determine whether the image block is the same as the image block traversed in the adjacent row and the same column (the grayscale value of each pixel in the current image block and the image block directly above / below it corresponds one to one):
[0067] If they are the same, they are encoded according to format 2;
[0068] If they are not the same, the maximum difference of the grayscale values in the image block is calculated. If the maximum difference is equal to 0, encoding is performed according to format three;
[0069] If the maximum difference is greater than or equal to 128, it is encoded according to format 4;
[0070] If the maximum difference is greater than 0 and less than 128, it is encoded according to format five;
[0071] The storage content of format one is: format one number;
[0072] The storage content of format 2 is: format 2 number;
[0073] The storage content of format three includes the following: format three number, grayscale value of any point in the current image block;
[0074] The storage content of format 4 includes the following: format 4 number, grayscale value of each pixel in the current image block;
[0075] The storage content of format five includes the following recorded in sequence: format five number, the minimum grayscale value in the current image block, and the difference between the grayscale values of all pixels and the minimum grayscale value.
[0076] In specific implementation, the image blocks traversed at adjacent rows and the same column are obtained by comparing the row and column coordinates of the pixel points in the image blocks:
[0077] The specific steps are:
[0078] Search in the encoded image blocks. If the search finds that in a certain image block, the row and column coordinates of the pixel point are different by 1 from the row and column coordinates of the pixel point in the currently traversed image block, but the column coordinates are the same, then it means that the image block is the traversed image block at the adjacent row and the same column.
[0079] More specifically, when the traversal order is from left to right and then from top to bottom or from right to left and then from top to bottom, the image block traversed in the adjacent row and the same column is the image block directly above the currently traversed image block;
[0080] When the traversal order is from left to right and then from bottom to top or from right to left and then from bottom to top, the image block traversed in the adjacent row and the same column is the image block directly below the currently traversed image block;
[0081] This embodiment also discloses a decompression method for the above-mentioned image lossless compression method, comprising the following steps:
[0082] Traverse each image block in sequence according to the preset traversal order;
[0083] For the currently traversed image block, perform the following decompression processing:
[0084] Read the format number corresponding to the image block, the format number includes format number 1 to format number 5;
[0085] Then determine the corresponding format when encoding based on the format number;
[0086] If the corresponding format is 1, the decompression method is: copy the basic image block to the currently traversed image block; the basic image block is the image block that was most recently decompressed using any of the formats 2 to 5;
[0087] If it corresponds to format 2, the decompression method is: copy the image blocks in adjacent rows and the same column that have been decompressed to the image block currently traversed;
[0088] If it corresponds to format three, the decompression method is: make the grayscale value of each pixel in the currently traversed image block equal to the grayscale value recorded in format three;
[0089] If it corresponds to format 4, the decompression method is: let the grayscale value of each pixel in the currently traversed image block be assigned to the grayscale value of each pixel recorded in format 4;
[0090] If it corresponds to format five, the decompression method is: restore the grayscale value of each pixel in the currently traversed image block according to the minimum grayscale value recorded in format five and the difference between the grayscale values of all pixels and the minimum grayscale value.
[0091] The format five numbering includes seven values (for example, 0x01 to 0x07), which are consistent with the number of bits N corresponding to the maximum difference in grayscale values in the image block.
[0092] In format five, when storing the difference between the grayscale values of all pixels and the minimum grayscale value, one difference is stored for every N bits.
[0093] For example: the maximum difference in grayscale values in an image block = 37, which is represented by 100101 in binary, and the corresponding bit number N is 6. Then: the value of format five number is 0x06. When storing the difference between the grayscale value of all pixels and the minimum grayscale value, one difference is stored every 6 bits.
[0094] Specifically, format one occupies 1 byte, which is used to store the format one number;
[0095] Format 2 occupies 1 byte, which is used to store the format 2 number;
[0096] Format three occupies 2 bytes, of which the first byte is used to store the format two number and the second byte is used to store the grayscale value of any point in the current image block;
[0097] Format 4 occupies 1+H bytes, where the first byte is used to store the format 4 number and the other H bytes are used to store the grayscale values of H pixels in the current image block; that is, the grayscale value of one pixel is represented by 8 bits.
[0098] Format Five Occupancy bytes, where the first byte is used to store the format five number, the second byte is used to store the minimum grayscale value in the current image block, and the other bytes are used to store the grayscale differences of H pixels in the current image block.
[0099] Among them, format number 1 to format number 5 are different from each other and can be expressed in binary, octal, or hexadecimal. Taking hexadecimal as an example, format number 1 = 0x0F, format number 2 = 0x0E, format number 3 = 0x00, format number 4 = 0x08, and format number 5 = one of 0x01 to 0x07.
[0100] In a specific implementation, the size of the image block is k×m, where k=3-8 and m=3-8.
[0101] For example, the size of the image block is 4×4 (H=16 at this time), 5×5 (H=25 at this time), or 3×5 (H=15 at this time).
[0102] The preset traversal order is one of left to right and then top to bottom, left to right and then bottom to top, right to left and then top to bottom, or right to left and then bottom to top.
[0103] As a preferred implementation, the traversal order is: first from left to right and then from top to bottom.
[0104] Example 2
[0105] Compared with Example 1, in order to further reduce storage space, this embodiment increases the repetition in the encoding of format 1.
[0106] The specific plan is as follows:
[0107] A method for lossless compression of an image, comprising the following steps:
[0108] Divide the image into multiple image blocks according to its width and height; then traverse each image block in turn according to the preset traversal order;
[0109] For the currently traversed image block, the following encoding process is performed:
[0110] Determine whether the image block is the same as the previously traversed image block (the grayscale value of each pixel in the current image block corresponds one-to-one with the grayscale value of each pixel in the previously traversed image block): record the current image block as a duplicate block;
[0111] If they are the same, encode according to format 1;
[0112] If they are not the same, continue to determine whether the image block is the same as the image block traversed in the adjacent row and the same column (the grayscale value of each pixel in the current image block and the image block directly above / below it corresponds one to one):
[0113] If they are the same, they are encoded according to format 2;
[0114] If they are not the same, the maximum difference of the grayscale values in the image block is calculated. If the maximum difference is equal to 0, encoding is performed according to format three;
[0115] If the maximum difference is greater than or equal to 128, it is encoded according to format 4;
[0116] If the maximum difference is greater than 0 and less than 128, it is encoded according to format five;
[0117] The storage content of format 1 includes the following in sequence: format 1 number, repetition degree M, where the repetition degree indicates the number of consecutive repetition blocks M;
[0118] The storage content of format 2 is: format 2 number;
[0119] The storage content of format three includes the following: format three number, grayscale value of any point in the current image block;
[0120] The storage content of format 4 includes the following: format 4 number, grayscale value of each pixel in the current image block;
[0121] The storage content of format five includes the following recorded in sequence: format five number, the minimum grayscale value in the current image block, and the difference between the grayscale values of all pixels and the minimum grayscale value.
[0122] In this embodiment, if continuous repeated blocks appear during the traversal process, the repeated blocks are uniformly encoded using format one. That is, each continuous repeated block is no longer encoded and stored separately. Instead, multiple repeated blocks are stored uniformly in the same storage area, thereby saving storage space. Especially when there are a large number of identical background areas in the image, the number of continuous repeated blocks is relatively large (the repetition value is large). If the method of embodiment 1 is used to encode and store each repeated block separately, the compression ratio will be reduced. In this case, adopting the encoding method of this embodiment will greatly improve the compression ratio.
[0123] More specifically, format 1 occupies 1+W bytes, the first byte is used to store the format number, and the remaining bytes are used to store the repetition degree; W ranges from 1 to 4;
[0124] Specifically, when the repetition rate is ≤127, W=1; when 128≤repetition rate≤16000-1, W=2; when 16000≤repetition rate≤2×10 6-1, W=3; when 2×10 6 ≤Repeatability≤256×10 6 When -1, W=4;
[0125] Format 2 occupies 1 byte, which is used to store the format 2 number;
[0126] Format three occupies 2 bytes, the first byte is used to store the format two number, and the second byte is used to store the grayscale value of any point in the current image block;
[0127] Format 4 occupies 1+H bytes, the first byte is used to store the format 4 number, and the other H bytes are used to store the grayscale values of H pixels in the current image block;
[0128] Format Five Occupancy bytes, the first byte is used to store the format five number, the second byte is used to store the minimum grayscale value in the current image block, and the other bytes are used to store the grayscale differences of H pixels in the current image block.
[0129] In this embodiment, the decompression method for the above-mentioned image lossless compression method includes the following steps:
[0130] Traverse each image block in sequence according to the preset traversal order;
[0131] For the currently traversed image block, perform the following decompression processing:
[0132] Read the format number corresponding to the image block, the format number includes format number 1 to format number 5;
[0133] Then determine the corresponding format when encoding based on the format number;
[0134] If the image corresponds to format 1, the decompression method is as follows: according to the repetition degree M recorded in format 1, the currently traversed image block and the M-1 image blocks to be traversed subsequently are copied as the base image block; the base image block is the image block that was most recently decompressed using any format from format 2 to format 5;
[0135] If it corresponds to format 2, the decompression method is: copy the image blocks in adjacent rows and the same columns that have been decompressed to the image block currently traversed;
[0136] If it corresponds to format three, the decompression method is: make the grayscale value of each pixel in the currently traversed image block equal to the grayscale value recorded in format three;
[0137] If it corresponds to format four, the decompression method is: let the grayscale value of each pixel in the currently traversed image block be assigned to the grayscale value of each pixel recorded in format four.
[0138] If it corresponds to format five, the decompression method is: restore the grayscale value of each pixel in the currently traversed image block according to the minimum grayscale value recorded in format five and the difference between the grayscale values of all pixels and the minimum grayscale value.
[0139] like Figure 1 As shown in the figure, three images are compressed simultaneously using this method (GDIF) and existing compression methods. By comparing the encoding time and compression rate of each compression method, it can be intuitively found that the compression rate of this method (GDIF) is close to that of PNG. At the same time, the encoding time is close to that of the fastest compression algorithm LZ4_DFT. It is a lossless compression method for images that takes both compression rate and compression speed into consideration; it reduces the pressure on image transmission and storage, and meets the requirements of high-beat image transmission.
[0140] For ease of understanding, this embodiment is exemplarily described below:
[0141] Settings: Format 1 number = 0x0F, Format 2 number = 0x0E, Format 3 number = 0x00, Format 4 number = 0x08, Format 5 number = one of 0x01 to 0x07;
[0142] At this time, the storage content of format 1 includes: repetition degree 2;
[0143] The storage content of format 2 is: format 2 number;
[0144] The storage content of format three includes the following: format three number, grayscale value of any point in the current image block;
[0145] The storage content of format 4 includes the following: format 4 number, grayscale value of each pixel in the current image block;
[0146] The storage content of format five includes the following recorded in sequence: format five number, the minimum grayscale value in the current image block, and the difference between the grayscale values of all pixels and the minimum grayscale value.
[0147] The image is divided into 9 equal blocks (as shown in the table below). The size of each block is 4×4 pixels. The default traversal order is from left to right and then from top to bottom. The numbers in the table represent the order in which the blocks are traversed:
[0148] 1 2 3 4 5 6 7 8 9
[0149] The first image block traversed is as follows:
[0150] 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101
[0151] Among them, the maximum difference of the grayscale value is equal to 0, and it is encoded according to format three. At this time, the storage content of format three includes the first byte storage: 0x00, the second byte storage: 101;
[0152] The second traversed image block is a repeated block (the same as the first traversed image block), the third traversed image block is a repeated block (the same as the second traversed image block), and the fourth traversed image block is a repeated block (the same as the third traversed image block); the fifth traversed image block is not a repeated block. At this time, the second to fourth traversed image blocks are regarded as consecutive repeated blocks, uniformly encoded using format one, and stored in the same storage area; at this time, the storage content of format one is the first byte storage: 0x0F, the first byte storage: 3 (repetition degree = 3, 3 consecutive repeated blocks occupy a total of 2 bytes, reducing storage space).
[0153] The fifth traversed image block is as follows:
[0154] 101 128 192 255 91 101 128 192 91 91 128 128 101 101 128 128
[0155] The maximum difference in grayscale values is equal to 164, and is encoded according to format 4. At this time, the storage content of format 4 is as follows:
[0156] Byte 01 0x08 Byte 02 101 Byte 03 128 Byte 04 192 Byte 05 255 Byte 06 91 Byte 07 101 Byte 08 128 Byte 09 192 Byte 10 91 Byte 11 91 Byte 12 128 Byte 13 128 Byte 14 101 Byte 15 101 Byte 16 128 Byte 17 128
[0157] The sixth traversed image block is as follows:
[0158] 101 128 128 128 91 101 128 128 91 91 128 128 101 101 128 128
[0159] The maximum difference in grayscale values is 37, and is encoded according to format five. At this time, the storage content of format five is as follows:
[0160]
[0161] The seventh traversed image block is a repeated block (the same as the sixth traversed image block); the maximum difference in grayscale values in the eighth traversed image block is equal to 0 and is encoded according to format three; at this time, the seventh traversed image block is encoded using format one and stored in a byte (the content stored in the byte is: format one number, repetition degree = 1).
[0162] The ninth traversed image block is the same as the sixth traversed image block (directly above). The two image blocks have adjacent rows and the same columns and are encoded according to format two. At this time, the storage content of format two is one byte storage: 0x0E.
[0163] Next is the decompression process:
[0164] The format number corresponding to the first traversed image block is 0x00, and it is decompressed according to format three, so that the grayscale values of the 16 pixels in the image block are all equal to the grayscale value 101 recorded in format three.
[0165] The format number corresponding to the second traversed image block is 0x0F, and it is decompressed according to format 1, where the stored repetition degree is 3. Then, the second to fourth traversed image blocks are copied to the base image block respectively. At this time, the base image block is the first traversed image block that has been decompressed.
[0166] The format number corresponding to the fifth traversed image block is format four (0x08). It is decompressed according to format four, and the grayscale values of the 16 pixels in the currently traversed image block are respectively assigned to the grayscale values of the 16 pixels recorded in format four.
[0167] The sixth traversed image block is decompressed according to format five; the grayscale value of each pixel in the currently traversed image block is restored based on the minimum grayscale value 91 recorded in format five and the difference between the grayscale value of all pixels and the minimum grayscale value.
[0168] The format code corresponding to the seventh traversed image block is format 1 (0x0F), which is copied to the base image block. At this time, the base image block is the sixth traversed image block that was decompressed most recently.
[0169] The format number corresponding to the eighth traversed image block is format three (0x00), and it is decoded according to format three. The format number corresponding to the ninth traversed image block is format two (0x0E), and it is decoded according to format two and copied to the image block in the adjacent row and the same column that has been decompressed (the sixth traversed image block).
[0170] The foregoing descriptions of specific exemplary embodiments of the present invention have been presented for purposes of illustration and description. The foregoing descriptions are not intended to be exhaustive or to limit the invention to the precise forms disclosed, and it is apparent that many variations and modifications are possible in light of the foregoing teachings. The exemplary embodiments have been chosen and described in order to explain the specific principles of the invention and their practical application, thereby enabling others skilled in the art to make and utilize the various exemplary embodiments of the invention and various alternatives and modifications thereof. The scope of the invention is intended to be defined by the appended claims and their equivalents.
Claims
1. A method for lossless compression of an image, characterized in that: The steps include: Divide the image into multiple image blocks according to its width and height; then traverse each image block in turn according to the preset traversal order; For the currently traversed image block, the following compression processing is performed: Determine whether the image block is the same as the previously traversed image block: If they are the same, encode according to format 1; If they are not the same, continue to determine whether the image block is the same as the image block traversed in the adjacent row and the same column: If they are the same, they are encoded according to format 2; If they are not the same, the maximum difference of the grayscale values in the image block is calculated. If the maximum difference is equal to 0, encoding is performed according to format three; If the maximum difference is greater than or equal to 128, it is encoded according to format 4; If the maximum difference is greater than 0 and less than 128, it is encoded according to format five; The storage content of the format 1 is: format 1 number; The storage content of format 2 is: format 2 number; The storage content of format three includes the following: format three number, grayscale value of any point in the current image block; The storage content of format 4 includes the following: format 4 number, grayscale value of each pixel in the current image block; The storage content of format five includes the following in sequence: format five number, the minimum grayscale value in the current image block, and the difference between the grayscale values of all pixels and the minimum grayscale value.
2. The image lossless compression method according to claim 1, wherein: Format 1 occupies 1 byte, which is used to store the format 1 number; Format 2 occupies 1 byte, which is used to store the format 2 number; Format three occupies 2 bytes, where the first byte is used to store the format three number and the second byte is used to store the grayscale value of any point in the current image block; Format 4 occupies 1+H bytes, where the first byte is used to store the format 4 number, and the other H bytes are used to store the grayscale values of H pixels in the current image block; that is, each 8 bits represents the grayscale value of a pixel; Format 5 takes up 2+ bytes, where the first byte is used to store the format five number, the second byte is used to store the minimum grayscale value in the current image block, and the other bytes are used to store the grayscale difference of H pixels in the current image block, where N represents the number of bits corresponding to the maximum difference in grayscale values in the image block.
3. The image lossless compression method according to claim 1, wherein: If the grayscale values of each pixel in the current image block correspond one-to-one with the grayscale values of each pixel in the previously traversed image block, the current image block is recorded as a repeated block; The content stored in the format 1 further includes: a repetition degree M, wherein the repetition degree represents the number M of repeated blocks that appear continuously; During the traversal process, if continuous repeated blocks appear, the continuous repeated blocks will be uniformly encoded using format one.
4. The image lossless compression method according to claim 3, wherein: Format 1 occupies 1+W bytes, the first byte is used to store the format 1 number, and the other bytes are used to store the repetition degree; the value of W is 1~4.
5. The image lossless compression method according to claim 1 or 3, wherein: The value of the format five number is consistent with the number of bits N corresponding to the maximum difference in the grayscale value in the image block; In format five, when storing the difference between the grayscale values of all pixels and the minimum grayscale value, one difference is stored for every N bits.
6. The image lossless compression method according to claim 1 or 3, wherein: The size of the image block is k×m, where k=3~8 and m=3~8.
7. The image lossless compression method according to claim 1 or 3, wherein: The preset traversal order is one of left to right and then top to bottom, left to right and then bottom to top, right to left and then top to bottom, or right to left and then bottom to top.
8. The image lossless compression method according to claim 1 or 3, wherein: The image blocks traversed at adjacent rows and the same column are obtained in the following way: Search in the encoded image blocks. If the search finds that in a certain image block, the row and column coordinates of the pixel point are different by 1 from the row and column coordinates of the pixel point in the currently traversed image block, but the column coordinates are the same; then the image block is the traversed image block at the adjacent row and the same column.
9. A decompression method for the image lossless compression method according to claim 3, characterized in that: The following steps are involved: Traverse each image block in sequence according to the preset traversal order; For the currently traversed image block, perform the following decompression processing: Read the format number corresponding to the image block, where the format number includes format number 1 to format number 5; Then determine the corresponding format when encoding based on the format number; If the image corresponds to format 1, the decompression method is as follows: based on the repetition degree M recorded in format 1, the currently traversed image block and the M-1 image blocks to be traversed subsequently are copied as the base image block; the base image block is the image block that was most recently decompressed using any of formats 2 to 5; If it corresponds to format 2, the decompression method is: copy the image blocks in adjacent rows and the same columns that have been decompressed to the image block currently traversed; If it corresponds to format three, the decompression method is: make the grayscale value of each pixel in the currently traversed image block equal to the grayscale value recorded in format three; If it corresponds to format 4, the decompression method is: let the grayscale value of each pixel in the currently traversed image block be assigned to the grayscale value of each pixel recorded in format 4; If it corresponds to format five, the decompression method is: restore the grayscale value of each pixel in the currently traversed image block according to the minimum grayscale value recorded in format five and the difference between the grayscale values of all pixels and the minimum grayscale value.
10. A decompression method for the image lossless compression method according to claim 1, characterized in that: The following steps are involved: Traverse each image block in sequence according to the preset traversal order; For the currently traversed image block, perform the following decompression processing: Read the format number corresponding to the image block, where the format number includes format number 1 to format number 5; Then determine the corresponding format when encoding based on the format number; If the corresponding format is 1, the decompression method is: copy the basic image block to the currently traversed image block; the basic image block is the image block that was most recently decompressed using any one of the formats 2 to 5; If it corresponds to format 2, the decompression method is: copy the image blocks in adjacent rows and the same column that have been decompressed to the image block currently traversed; If it corresponds to format three, the decompression method is: make the grayscale value of each pixel in the currently traversed image block equal to the grayscale value recorded in format three; If it corresponds to format 4, the decompression method is: let the grayscale value of each pixel in the currently traversed image block be assigned to the grayscale value of each pixel recorded in format 4; If it corresponds to format five, the decompression method is: restore the grayscale value of each pixel in the currently traversed image block according to the minimum grayscale value recorded in format five and the difference between the grayscale values of all pixels and the minimum grayscale value.
Citation Information
Patent Citations
Picture compression method
CN114332261A
Image lossless compression method
CN117241031A