Picture lossless compression and decompression method
By dividing the picture into multiple image blocks and selecting an appropriate encoding format for compression processing, the problem in the existing technology that it is difficult to achieve high compression rate and fast compression speed at the same time is solved, and efficient image lossless compression and decompression is achieved to meet the needs of high-performance image processing. Beat image transfer requirements.
Patent Information
- Application Number
- CN202411487498.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-24
- Publication Date
- 2025-01-21
- Estimated Expiration
- 2044-10-24
AI Technical Summary
The prior art is difficult to achieve both high compression rates and fast compression speeds in image compression, especially in machine vision applications, and a lossless compression algorithm that can meet the requirements of high-tempo image transmission.
By dividing the image into multiple image blocks, and selecting different encoding formats according to the difference between the gray value between the image blocks, including the format one to the format five, each format occupies different numbers of bytes, adapts to different situations Demand for compression.
The compression rate is close to that of PNG, and the encoding time is close to the fastest compression algorithm LZ4_DFT, which reduces the image transmission and storage pressure and meets the high-speed image transmission requirements.
Smart Images

Figure CN119342224A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the field of image compression, and in particular to a method for lossless compression and decompression of images. Background Art
[0002] In order to reduce the burden of transmission and storage, images need to be compressed. The commonly used compression algorithms include JPEG, PNG, ZLIB, LZ4, etc. In machine vision applications, in order to avoid the introduction of noise, lossless formats are mainly used for image storage. Among them, the more representative lossless compression is PNG, but it sacrifices compression speed because of its pursuit of a higher compression rate. ZLIB and LZ4 are a combination of Huffman coding and dictionary method (LZ77). It is a general binary compression algorithm, and the compression rate of images is not stable. The GPOS method is a relatively fast method with a relatively high compression rate. However, the compression speed is still far behind LZ4_DFT, and the compression rate is still far behind PNG. Although the BMP format takes less time to encode, the compression rate is too low.
[0003] In actual use, in order to meet the requirements of production cycle, an image lossless compression algorithm with high compression rate 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 lossless compression and decompression method for images. The compression rate of the 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 solution of the present invention is as follows:
[0006] A method for lossless compression of an image comprises 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 in order: format three number, gray 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 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] Further, 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, wherein 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 turn according to a 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 a format number 1 to a 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 columns that have been decompressed to the image blocks currently traversed;
[0033] If it corresponds to format three, the decompression method is: let the grayscale value of each pixel in the currently traversed image block be 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 also includes: a repetition degree M, wherein the repetition degree indicates 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; W takes a value of 1 to 4.
[0040] Furthermore, a decompression method of the above-mentioned image lossless compression method comprises the following steps:
[0041] Traverse each image block in turn according to a 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 a format number 1 to a format number 5;
[0044] Then determine the corresponding format when encoding based on the format number;
[0045] If it corresponds to format 1, the decompression method is: 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 has been decompressed most recently 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 blocks currently traversed;
[0047] If it corresponds to format three, the decompression method is: let the grayscale value of each pixel in the currently traversed image block be 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 bit number N corresponding to the maximum difference of the gray 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, wherein 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 already 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, matches the encoding format with the smallest storage length for each image block, and effectively improves the compression speed while ensuring a high compression ratio;
[0057] Storing multiple consecutive re-read 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 This is 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 in conjunction with the accompanying drawings and specific implementation methods.
[0060] Example 1
[0061] A method for lossless compression of an image comprises 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 in order: format three number, gray 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 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 columns 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 already 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 image block traversed at the adjacent row and the same column.
[0079] More specifically, when the traversal order is first from left to right and then from top to bottom or first from right to left and then from top to bottom, the image block traversed at 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 turn according to a 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 one 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 columns that have been decompressed to the image blocks currently traversed;
[0088] If it corresponds to format three, the decompression method is: let the grayscale value of each pixel in the currently traversed image block be 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] Among them, the values of format five numbers include 7 kinds (for example: 0x01 to 0x07), which are consistent with the number of bits N corresponding to the maximum difference of the gray value 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 the 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 four occupies 1+H bytes, of which 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 each.
[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 difference values of H pixels in the current image block.
[0099] Among them, format number one to format number five are different from each other and can be expressed in binary, octal, or hexadecimal. Taking hexadecimal as an example, format number one = 0x0F, format number two = 0x0E, format number three = 0x00, format number four = 0x08, and format number five = 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 (in this case, H=16), 5×5 (in this case, H=25), or 3×5 (in this case, H=15).
[0102] The preset traversal order is one of: from left to right and then from top to bottom, from left to right and then from bottom to top, from right to left and then from top to bottom, or from right to left and then from 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 one.
[0106] The specific plan is as follows:
[0107] A method for lossless compression of an image comprises 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 to the grayscale value of each pixel in the previously traversed image block): record the current image block as a repeated 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 one includes the following recorded in sequence: format one number, repetition degree M, where the repetition degree indicates the number M of repeated blocks that appear continuously;
[0118] The storage content of format 2 is: format 2 number;
[0119] The storage content of format three includes the following in order: format three number, gray 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 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, during the traversal process, if continuous repeated blocks appear, the continuous repeated blocks are uniformly encoded using format 1. That is, each continuous repeated block is no longer encoded and stored separately, but multiple repeated blocks are uniformly stored in the same storage area, thereby saving storage space. In particular, 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 adopted to encode and store each repeated block separately, the compression rate will be reduced. At this time, the encoding method in this embodiment will greatly improve the compression rate.
[0123] More specifically, 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; 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 difference values 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 turn according to a 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 it corresponds to format 1, the decompression method is: 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 has been decompressed most recently 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 blocks currently traversed;
[0136] If it corresponds to format three, the decompression method is: let the grayscale value of each pixel in the currently traversed image block be 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, the present method (GDIF) and the existing compression method are used to compress three images at the same time. By comparing the encoding time and compression rate of each compression method, it can be intuitively found that the compression rate of the present 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 of image transmission and storage, and meets the requirements of high-beat image transmission.
[0140] For ease of understanding, the present 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 in order: format three number, gray 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 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 following table). The size of each image block is 4×4 pixels. The preset traversal order is from left to right and then from top to bottom. The numbers in the table represent the order in which the image 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 gray 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 traversed image block to the fourth traversed image block are regarded as consecutively appearing 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 of grayscale value is equal to 164, which 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 of grayscale value is equal to 37, which is encoded according to format 5. At this time, the storage content of format 5 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 stored as one byte: 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 = 3, and the second traversed image block to the fourth traversed image block are respectively copied to the basic image block. At this time, the basic 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 according to 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 basic image block. At this time, the basic image block is the sixth traversed image block that has been decompressed most recently;
[0169] The format number corresponding to the eighth traversed image block is format three (0x00), which is decoded according to format three. The format number corresponding to the ninth traversed image block is format two (0x0E), which 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 description of the specific exemplary embodiments of the present invention is presented for the purpose of illustration and description. The foregoing description is not intended to be exhaustive, nor is it intended to limit the present invention to the precise form disclosed, and it is apparent that many changes and variations are possible in light of the above teachings. The exemplary embodiments are selected and described to explain the specific principles of the present invention and its practical application, so that other technicians in the field can realize and utilize the various exemplary embodiments of the present invention and its different selected forms and modified forms. The scope of the present invention is intended to be limited 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 in order: format three number, gray 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, characterized in that: 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, wherein the first byte is used to store the format two number, and the second byte is used to store the gray value of any point in the current image block; 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. 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.
3. 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 turn according to a preset traversal order; For the currently traversed image block, perform the following decompression processing: Reading a format number corresponding to an image block, wherein the format number includes a format number 1 to a 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: 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; 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 blocks currently traversed; If it corresponds to format three, the decompression method is: let the grayscale value of each pixel in the currently traversed image block be 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.
4. The image lossless compression method according to claim 1, characterized in that: 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 also includes: a repetition degree M, wherein the repetition degree indicates 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.
5. The image lossless compression method according to claim 4, characterized in that: 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; W takes a value of 1 to 4.
6. A decompression method for the image lossless compression method according to claim 4, characterized in that: The following steps are involved: Traverse each image block in turn according to a preset traversal order; For the currently traversed image block, perform the following decompression processing: Reading a format number corresponding to an image block, wherein the format number includes a format number 1 to a format number 5; Then determine the corresponding format when encoding based on the format number; If it corresponds to format 1, the decompression method is: 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 has been decompressed most recently using any format from format 2 to format 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 blocks currently traversed; If it corresponds to format three, the decompression method is: let the grayscale value of each pixel in the currently traversed image block be equal to the grayscale value recorded in format three; 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. 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.
7. The image lossless compression method according to claim 1 or 4, characterized in that: The value of the format five number is consistent with the bit number N corresponding to the maximum difference in the gray 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.
8. The image lossless compression method according to claim 1 or 4, characterized in that: The size of the image block is k×m, where k=3-8 and m=3-8.
9. The image lossless compression method according to claim 1 or 4, characterized in that: The preset traversal order is one of: from left to right and then from top to bottom, from left to right and then from bottom to top, from right to left and then from top to bottom, or from right to left and then from bottom to top.
10. The image lossless compression method according to claim 1 or 4, characterized in that: The image blocks traversed in adjacent rows and the same column are obtained in the following way: Search in the already 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.
Citation Information
Patent Citations
Picture compression method
CN114332261A
Image lossless compression method
CN117241031A
ARGB picture block lossless compression and decompression method and system
CN118400540A
Fixed Length Coding Based Image Data Compression
US20110216834A1