Image compression method and device, computer readable storage medium and terminal
By grouping image pixels and difference coding, using lossless or lossy coding methods, the problem of error diffusion in image compression is solved, and image quality and storage efficiency are improved.
Patent Information
- Application Number
- CN202310196809.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-01
- Publication Date
- 2024-09-03
AI Technical Summary
In existing image compression technology, variable-length encoding can easily lead to the spread of bit errors, leading to distortion of the decompressed image, and error correction methods require additional resource overhead and are inefficient.
By grouping the pixels of the image to be compressed, selecting reference pixels, determining the pixel difference, and performing lossless or lossy encoding according to the difference interval, using a fixed-length encoding method to ensure that the total encoding length of each group of pixels meets the preset length, avoiding Error propagation.
It effectively reduces the storage space of encoding results, limits the scope of influence of encoding errors, improves the quality of decompressed images, and avoids distortion caused by error diffusion.
Smart Images

Figure CN118590655A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to an image compression method and device, a computer-readable storage medium, and a terminal. Background Art
[0002] With the advent of the digital information age, the storage and transmission needs of massive data are an imminent challenge. Data compression has become one of the indispensable technologies. Compressing images is significantly different from compressing raw binary data. Since some unimportant details in the image can be sacrificed, this also means that the image can be compressed to a certain extent. In image compression technology, how to solve the image distortion problem caused by compression while ensuring high performance, low complexity and reducing storage space is of great research value.
[0003] In the prior art, the continuous tone still image lossless compression (Joint Photographic Experts Group-Lossless, JPEG-LS) algorithm is usually used for image compression. The JPEG-LS algorithm mainly consists of two independent and different stages: modeling and encoding, and relies on prediction, residual modeling and context-based residual coding. However, the encoding used by the JPEG-LS algorithm is variable-length encoding, which is very sensitive to errors in the bitstream. When the encoding is wrong, especially when the encoding bit that records the encoding length information is wrong, this error can easily spread. Specifically, in the subsequent decoding / decompression and restoration of the image, if the encoding length information of a certain segment of encoding is wrong, it may cause errors in the decoding of other encodings after the segment of encoding, and even the error may spread to the entire image, thereby causing distortion of the image restored after decoding.
[0004] In response to the above error propagation problem, some related studies have proposed methods for correcting coding errors. For example, an existing image lossless compression error correction coding and decoding method that is resistant to channel errors adopts entropy coding and decoding, block error detection, (P, Q 2 ) Parity check error detection and correction, encoding and decoding combined multi-level coding and decoding method, error detection and correction of errors caused by bit errors. However, this error correction method requires additional resource overhead and can usually only correct single-bit errors in pixel sub-blocks that cause coding length information errors. Summary of the invention
[0005] The technical problem solved by the embodiments of the present invention is how to avoid the error propagation problem in image compression, thereby preventing the distortion of the decompressed image caused by the error propagation.
[0006] To solve the above technical problems, an embodiment of the present invention provides an image compression method, comprising the following steps: grouping pixels of an image to be compressed to obtain multiple groups of pixels; for each group of pixels, selecting a reference pixel from the group of pixels, and determining the pixel difference between the pixel value of each other pixel in the group of pixels and the pixel value of the reference pixel; if each pixel difference in the current group of pixels belongs to a preset difference interval, encoding each pixel difference based on a preset coding length so that the total coding length of the current group of pixels meets the preset total length; if at least one of the pixel differences in the current group of pixels does not belong to the difference interval, performing a numerical transformation on each pixel difference, and then encoding each transformed pixel difference based on the coding length so that the total coding length of the current group of pixels meets the preset total length; wherein the transformed pixel difference belongs to the difference interval.
[0007] Optionally, for each group of pixels, the selected reference pixel satisfies any one of the following: the reference pixel is a pixel with the smallest pixel value in the group of pixels; the reference pixel is a pixel at a preset position in the group of pixels.
[0008] Optionally, for each group of pixels, the selected reference pixel is the pixel with the smallest pixel value in the group of pixels; the difference interval is selected from [0, 2 n -1], wherein n represents the encoding length and n is a positive integer.
[0009] Optionally, for each group of pixels, the selected reference pixel is a pixel at a preset position in the group of pixels; the difference interval is selected from [-(2 m-1 -1), (2 m-1 -1)], wherein m represents the coding length and m is a positive integer greater than 1.
[0010] Optionally, grouping pixels of the image to be compressed to obtain multiple groups of pixels includes: grouping multiple pixels in the image to be compressed whose positions are within a preset pixel number range and have the same color into one group to obtain multiple groups of pixels.
[0011] Optionally, each group of pixels is arranged in a rectangular array, and each group of pixels includes a×b pixels.
[0012] Optionally, each group of pixels includes 2×2 pixels.
[0013] Optionally, the numerical transformation of each pixel difference includes: setting multiple numerical intervals and setting a corresponding linear transformation rate for each numerical interval; for each pixel difference, determining the numerical interval to which the pixel difference belongs, and then performing a numerical transformation on the pixel difference based at least on the linear transformation rate corresponding to the numerical interval.
[0014] Optionally, the following formula is used to perform numerical transformation on each pixel difference:
[0015] y=k1×x,x∈[x1,x2];
[0016]
[0017]
[0018]
[0019] Among them, x represents the pixel difference, y represents the transformed pixel difference, [x1, x2] represents the first numerical interval, k1 represents the linear transformation rate corresponding to the first numerical interval, [x3, x4] represents the second numerical interval, k2 represents the linear transformation rate corresponding to the second numerical interval, [x5, x6] represents the third numerical interval, k3 represents the linear transformation rate corresponding to the third numerical interval, [x7, x8] represents the fourth numerical interval, k4 represents the linear transformation rate corresponding to the fourth numerical interval, wherein x and y are both integers.
[0020] Optionally, the first numerical interval is [0,8], and the corresponding linear transformation rate is 1; the second numerical interval is [9,56], and the corresponding linear transformation rate is 1 / 4; the third numerical interval is [57,1016], and the corresponding linear transformation rate is 1 / 16; the fourth numerical interval is [1017,4024], and the corresponding linear transformation rate is 1 / 64.
[0021] Optionally, the method further includes: for other pixel difference values not included in the multiple numerical intervals, using one or more preset fixed values as the transformed pixel difference values.
[0022] Optionally, the method also includes: for each group of pixels, using the original bit number of the pixel value of the reference pixel in the group of pixels as the encoding length of the pixel value of the reference pixel, and encoding the pixel value of the reference pixel; determining the encoding result of the current group of pixels based on the first encoding result of the pixel value of the reference pixel in the current group of pixels and the second encoding result of each pixel difference in the current group of pixels; and determining the final encoding result of the image to be compressed based on the encoding results of each group of pixels.
[0023] Optionally, the encoding result of the group of pixels is determined based on the first encoding result of the pixel value of the reference pixel in the current group of pixels and the second encoding result of the difference between each pixel in the current group of pixels, including: splicing the first encoding result and the second encoding result to obtain a first encoding stream; adding a pixel position identifier at a first preset position of the first encoding stream to obtain the encoding result of the current group of pixels; wherein the pixel position identifier is used to indicate the position of the reference pixel in the current group of pixels.
[0024] Optionally, based on the encoding results of each group of pixels, the final encoding result of the image to be compressed is determined, including: if the encoding result of the current group of pixels is obtained by encoding each pixel difference in the current group of pixels based on a preset encoding length, then a first flag is added to a second preset position of the encoding result of the current group of pixels; if the encoding result of the current group of pixels is obtained by encoding each transformed pixel difference in the current group of pixels based on the encoding length, then a second flag is added to the second preset position of the encoding result of the current group of pixels; the encoding results of each group of pixels to which the flag is added are spliced to obtain a second encoding stream; based on the second encoding stream, the final encoding result of the image to be compressed is determined; wherein the first flag is different from the second flag.
[0025] An embodiment of the present invention also provides an image compression device, comprising: a pixel grouping module, used to group pixels of an image to be compressed to obtain multiple groups of pixels; a pixel difference determination module, used to select a reference pixel from each group of pixels, and determine the pixel difference between the pixel value of each other pixel in the group of pixels and the pixel value of the reference pixel; a lossless coding module, used to encode each pixel difference based on a preset coding length if each pixel difference in a current group of pixels belongs to a preset difference interval, so that the total coding length of the current group of pixels meets the preset total length; a lossy coding module, used to perform numerical transformation on each pixel difference if at least one of the pixel differences in the current group of pixels does not belong to the difference interval, and then encode each transformed pixel difference based on the coding length, so that the total coding length of the current group of pixels meets the preset total length; wherein the transformed pixel difference belongs to the difference interval.
[0026] An embodiment of the present invention further provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of the above-mentioned image compression method are executed.
[0027] An embodiment of the present invention further provides a terminal, including a memory and a processor, wherein the memory stores a computer program that can be run on the processor, and the processor executes the steps of the above-mentioned image compression method when running the computer program.
[0028] Compared with the prior art, the technical solution of the embodiment of the present invention has the following beneficial effects:
[0029] An embodiment of the present invention provides an image compression method, which groups pixels of an image to be compressed to obtain multiple groups of pixels; for each group of pixels, selects a reference pixel from the group of pixels, and determines the pixel difference between the pixel value of each other pixel in the group of pixels and the pixel value of the reference pixel; then, for a current group of pixels, determines whether each pixel difference in the current group of pixels belongs to a preset difference interval; if the determination result is yes, encodes each pixel difference (i.e., the original value of each pixel difference) based on a preset coding length (this coding method may be called "lossless compression") so that the total coding length of the current group of pixels meets the preset total length; if the determination result is no, encodes each transformed pixel difference based on the coding length (this coding method may be called "lossy compression") so that the total coding length of the current group of pixels meets the preset total length; wherein the transformed pixel difference belongs to the difference interval.
[0030] In an embodiment of the present invention, a pixel group coding method is adopted, and different coding methods ("lossless compression" or "lossy compression" method) are adopted according to the size of each pixel difference to be encoded in the current group of pixels. If the absolute values of each pixel difference are small (belonging to the difference interval), the number of coding bits occupied is small, the required storage space is small, and "lossless compression" can be performed; if the absolute value of at least one pixel difference is relatively large (not belonging to the difference interval), the number of coding bits occupied is large, the required storage space is large, and "lossy compression" can be performed. In this way, the storage space occupied by the encoding result can be effectively reduced. Furthermore, by adopting a fixed-length coding method, the total coding length of each group of pixels is a preset total length. Even if the coding length information of one or more groups of pixels is wrong, the impact range of the error is limited to the pixel group, and will not affect the decoding accuracy of other pixel groups. In this way, the error propagation problem in image compression can be effectively solved, so that the decompressed image is as close to the original image as possible.
[0031] Further, the pixels of the image to be compressed are grouped to obtain multiple groups of pixels, including: a plurality of pixels in the image to be compressed whose positions are within a preset pixel number range and have the same color are divided into one group to obtain multiple groups of pixels. It is understandable that in the process of encoding the pixel value of the reference pixel in the current group of pixels or the difference between each pixel, encoding errors may occur due to various factors. If the number of pixels contained in each group of pixels is smaller, the impact range of the encoding error is smaller. Based on this, the number of pixels contained in each group of pixels can be appropriately set according to the requirements of the actual application scenario, for example, in combination with factors such as encoding complexity, encoding efficiency, and fault tolerance. In this way, the impact range of the encoding error can be minimized as much as possible, and the image effect after decompression can be improved.
[0032] Furthermore, the numerical transformation of each pixel difference value includes: setting a plurality of numerical intervals, and setting a corresponding linear transformation rate for each numerical interval; for each pixel difference value, determining the numerical interval to which the pixel difference value belongs, and then numerically transforming the pixel difference value based on at least the linear transformation rate corresponding to the numerical interval. In the embodiment of the present invention, by adopting a piecewise linear transformation method to perform linear transformation on each pixel difference value, the numerical value of each pixel difference value can be quickly and accurately mapped to the preset difference interval, thereby reducing the storage space occupied by each pixel difference value in subsequent encoding.
[0033] Further, the first encoding result based on the pixel value of the reference pixel in the current group of pixels and the second encoding result of each pixel difference in the current group of pixels is used to determine the encoding result of the group of pixels, including: splicing the first encoding result and the second encoding result to obtain a first encoding stream; adding a pixel position identifier at a first preset position of the first encoding stream to obtain the encoding result of the current group of pixels; wherein the pixel position identifier is used to indicate the position of the reference pixel in the current group of pixels. In an embodiment of the present invention, by adding a pixel position identifier (i.e., a reference pixel position identifier) at a preset position of the first encoding stream, it is helpful to quickly locate the reference pixel in the current group of pixels and the encoding segments corresponding to each pixel difference in subsequent decoding, thereby improving decoding efficiency and decoding accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] Figure 1 is a flow chart of an image compression method in an embodiment of the present invention;
[0035] Figure 2 yes Figure 1 A flowchart of a specific implementation of step S15;
[0036] Figure 3 is a partial flow chart of another image compression method in an embodiment of the present invention;
[0037] Figure 4 is a schematic diagram of the encoding result of a group of pixels of the image to be compressed in an embodiment of the present invention;
[0038] Figure 5 It is a structural schematic diagram of an image compression device in an embodiment of the present invention. DETAILED DESCRIPTION
[0039] As mentioned in the background technology, in image compression technology, how to solve the image distortion problem caused by compression while ensuring high performance, low complexity and reducing storage space is of great research value.
[0040] In the prior art, the Joint Photographic Experts Group–Lossless (JPEG-LS) algorithm is often used for image compression. The JPEG-LS algorithm mainly consists of two independent and different stages: modeling and coding, and relies on prediction, residual modeling, and context-based residual coding. However, the coding used by the JPEG-LS algorithm is variable-length coding, which is very sensitive to errors in the bitstream. When the coding is wrong, especially when the coding bit that records the coding length information is wrong, this error can easily spread to the entire image.
[0041] The main reason for the above problem is that for variable-length coding, it is usually necessary to store / record the coding length information of different coding segments (for example, in the entire bitstream obtained by encoding the image, a preset number of flag bits are used to record the coding length information of each coding segment). If the coding length information of a coding segment is recorded incorrectly, in the subsequent decoding and image restoration process, not only will the decoding error of the coding segment to which the incorrect coding length information belongs be caused, but also the decoding error of other coding segments located after the coding segment will be caused, thereby causing the error to spread, and even spread to the entire image, and then causing the image restored after decoding to be distorted.
[0042] To solve the above technical problems, an embodiment of the present invention provides an image compression method, which specifically includes: grouping pixels of an image to be compressed to obtain multiple groups of pixels; for each group of pixels, selecting a reference pixel from the group of pixels, and determining the pixel difference between the pixel value of each other pixel in the group of pixels and the pixel value of the reference pixel; if the pixel differences in the current group of pixels all belong to a preset difference interval, encoding the pixel differences based on a preset coding length so that the total coding length of the current group of pixels meets the preset total length; if at least one of the pixel differences in the current group of pixels does not belong to the difference interval, numerically transforming the pixel differences, and then encoding the transformed pixel differences based on the coding length so that the total coding length of the current group of pixels meets the preset total length; wherein the transformed pixel differences belong to the difference interval.
[0043] From the above, in the embodiment of the present invention, a pixel group coding method is adopted, and different coding methods ("lossless compression" or "lossy compression" method) are adopted according to the size of each pixel difference to be encoded in the current group of pixels. If the absolute values of each pixel difference are small (belonging to the difference interval), the number of coding bits occupied is small, the required storage space is small, and "lossless compression" can be performed; if the absolute value of at least one pixel difference is relatively large (not belonging to the difference interval), the number of coding bits occupied is large, the required storage space is large, and "lossy compression" can be performed. In this way, the storage space occupied by the encoding result can be effectively reduced.
[0044] Furthermore, by adopting a fixed-length coding method, the total coding length of each group of pixels is a preset total length. Even if the coding length information of one or more groups of pixels is wrong, the impact of the error is limited to the pixel group and will not affect the decoding accuracy of other pixel groups. In this way, the error propagation problem in image compression can be effectively solved, making the decompressed image as close to the original image as possible.
[0045] In order to make the above-mentioned objects, features and beneficial effects of the present invention more obvious and understandable, specific embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0046] Reference Figure 1 , Figure 1 1 is a flowchart of an image compression method in an embodiment of the present invention. The image compression method can be applied to various terminal devices capable of realizing data processing functions, and the terminal devices may include but are not limited to: computers, mobile phones, tablet computers, wearable terminals, vehicle-mounted terminals, servers, cloud platforms, etc.
[0047] The method may include steps S11 to S13, and step S14 or step S15:
[0048] Step S11: grouping pixels of the image to be compressed to obtain multiple groups of pixels;
[0049] Step S12: for each group of pixels, select a reference pixel from the group of pixels, and determine a pixel difference between the pixel value of each other pixel in the group of pixels and the pixel value of the reference pixel;
[0050] Step S13: determining whether the pixel differences in the current group of pixels all belong to a preset difference interval;
[0051] If the judgment result is yes, then execute step S14, if the judgment result is no, then execute step S15;
[0052] Step S14: encoding each pixel difference based on a preset encoding length so that the total encoding length of the current group of pixels meets the preset total length;
[0053] Step S15: numerically transform each pixel difference, and then encode each transformed pixel difference based on the encoding length so that the total encoding length of the current group of pixels meets the preset total length, wherein the transformed pixel difference belongs to the difference interval.
[0054] In the specific implementation of step S11, the image to be compressed may be an original image captured by an image sensor. The pixels of the image to be compressed are different from the pixels of an image in a color mode such as RGB or HSV (each pixel has at least three color channels). In the embodiment of the present invention, each pixel of the image to be compressed has a single color channel (that is, has a unique pixel value).
[0055] Specifically, the image to be compressed can be the original image output by the color filter array (CFA). CFA is a complex of color filters that filters light according to wavelength, can remove some components in the spectrum, and retain only one color component (or color channel) for each pixel. General photoelectric sensors can only sense the intensity of light, but cannot distinguish the wavelength of light. Therefore, the image sensor needs to pass through a color filter, such as a Bayer filter, to obtain the color information of the pixel.
[0056] Furthermore, the step S11 may include: dividing a plurality of pixels in the image to be compressed whose positions are within a preset pixel number range and have the same color into one group to obtain a plurality of groups of pixels.
[0057] In a specific embodiment, two imaging pixel arrays in the image to be compressed are adjacent to each other, and the adjacent imaging pixel arrays are pixels whose positions are within a preset pixel quantity range.
[0058] In another specific embodiment, the two imaging pixel arrays in the image to be compressed are separated by other types of pixels, and the two imaging pixel arrays are pixels whose positions are within a preset pixel number range.
[0059] In some non-limiting embodiments, each group of pixels may be arranged in a rectangular array, and each group of pixels includes a×b pixels. It should be noted that in practical applications, each group of pixels may be divided into other regular or irregular shapes according to the arrangement characteristics of the imaging pixels of the image to be compressed.
[0060] It is understandable that in the process of encoding the pixel values of the reference pixels in the current group of pixels or the pixel differences, encoding errors may occur due to various factors. If the number of pixels contained in each group of pixels is smaller, the impact range of the encoding error is smaller. Based on this, the number of pixels contained in each group of pixels can be appropriately set according to the actual application scenario requirements, such as combining factors such as encoding complexity, encoding efficiency, and fault tolerance (for example, each group of pixels can contain only 2×2 pixels at a minimum). In this way, the impact range of the encoding error can be reduced as much as possible, and the image effect after decompression can be improved.
[0061] In a specific implementation of step S12, for each group of pixels, a reference pixel is selected from the group of pixels.
[0062] In a specific implementation, the selected reference pixel may be the pixel with the smallest pixel value in the group of pixels. In this way, the pixel difference between the pixel value of each other pixel in the group of pixels and the pixel value of the reference pixel is a positive number, so there is no need to use additional coding bits (e.g., bits) to record the positive or negative sign information. In this case, the difference interval may be selected from [0, 2 n -1], wherein n represents the encoding length and n is a positive integer.
[0063] For example, if the encoding length is set to 7, the difference interval can be set to [0, 127]; for another example, if the encoding length is set to 8, the difference interval can be set to [0, 255].
[0064] In another specific embodiment, the selected reference pixel may be an image at a preset position in the group of pixels, and the preset position may be determined according to actual needs. For example, a pixel located at the upper left corner or lower right corner or other appropriate position in each group of pixels may be selected as the reference pixel. Since the pixel difference between the pixel value of each other pixel in the group of pixels and the pixel value of the reference pixel may be a positive number or a negative number, an additional 1-bit encoding bit (for example, 1 bit) is required to record the positive or negative sign information. In this case, the difference interval is selected from [-(2m-1 -1), (2 m-1 -1)], wherein m represents the coding length and m is a positive integer greater than 1.
[0065] For example, if the encoding length is set to 7, the difference interval can be set to [-63, 63]; for another example, if the encoding length is set to 8, the difference interval can be set to [-127, 127].
[0066] In the specific implementation of step S13, it is determined whether the pixel differences in the current group of pixels all belong to a preset difference range.
[0067] In the specific implementation of step S14, if the absolute values of the pixel differences in the current group of pixels are small (all belong to the difference interval), the number of coding bits occupied by the pixel differences is small, the required storage space is small, and the current group of pixels can be "losslessly compressed". Specifically, the pixel differences in the current group of pixels (that is, the original values of the pixel differences) are encoded based on a preset coding length.
[0068] In the specific implementation of step S15, if the absolute value of at least one pixel difference in the current group of pixels is large (not belonging to the difference interval), the pixel difference with a large absolute value occupies more coding bits and requires a large storage space, and the current group of pixels can be "lossily compressed". Specifically, each pixel difference in the current group of pixels is first numerically transformed, and then each transformed pixel difference is encoded based on the coding length.
[0069] The preset total length in the embodiment of the present invention can be determined according to the number of pixels included in the current group of pixels, the coding length for encoding each pixel difference of the current group of pixels, and the coding length for encoding the reference pixels of the current group of pixels.
[0070] Reference Figure 2 , Figure 2 yes Figure 1 The flowchart of a specific implementation of step S15 in the embodiment of the present invention. The numerical transformation of each pixel difference in step S15 may include steps S21 to S22.
[0071] In step S21, a plurality of numerical intervals are set, and a corresponding linear conversion rate is set for each numerical interval.
[0072] The linear transformation rate corresponding to the numerical interval may be understood as the slope of the linear transformation function corresponding to the numerical interval.
[0073] In step S22, for each pixel difference value, the numerical interval to which the pixel difference value belongs is determined, and then the pixel difference value is numerically transformed based at least on the linear transformation rate corresponding to the numerical interval.
[0074] Furthermore, the following formula can be used to perform numerical transformation on each pixel difference:
[0075] y=k1×x,x∈[x1,x2];
[0076]
[0077]
[0078]
[0079] Among them, x represents the pixel difference, y represents the transformed pixel difference, [x1, x2] represents the first numerical interval, k1 represents the linear transformation rate corresponding to the first numerical interval, [x3, x4] represents the second numerical interval, k2 represents the linear transformation rate corresponding to the second numerical interval, [x5, x6] represents the third numerical interval, k3 represents the linear transformation rate corresponding to the third numerical interval, [x7, x8] represents the fourth numerical interval, k4 represents the linear transformation rate corresponding to the fourth numerical interval, wherein x and y are both integers.
[0080] As a non-limiting embodiment, the pixel with the smallest pixel value in the current group of pixels is selected as the reference pixel, and 7 bits are used as the encoding length of the difference between each pixel in the current group. The preset difference interval is [0,127], and different linear transformation rates are required to transform the pixel differences belonging to different numerical intervals into the range of [0,127]. Among them, the first numerical interval is [0,8], and the corresponding linear transformation rate is 1; the second numerical interval is [9,56], and the corresponding linear transformation rate is 1 / 4; the third numerical interval is [57,1016], and the corresponding linear transformation rate is 1 / 16; the fourth numerical interval is [1017,4024], and the corresponding linear transformation rate is 1 / 64.
[0081] Furthermore, for other pixel differences not included in the multiple value intervals, a preset fixed value may be used as the pixel difference after transformation. The fixed value may be different for different reference pixels. For example, in different scenes, the fixed value may be selected from the maximum value or the minimum value in the difference interval to which the pixel difference after transformation belongs.
[0082] For example, if the pixel with the smallest pixel value in the current group of pixels is used as the reference pixel, then all pixel differences in the current group of pixels are non-negative numbers. If a certain pixel difference in the current group of pixels does not belong to a preset numerical interval with a corresponding linear transformation rate, for example, a certain pixel difference is in the interval [4025,4095] (the interval does not have a corresponding linear transformation rate), then the first fixed value (for example, the numerical interval [0,2 n -1] the maximum value 2 n -1), as the transformed pixel difference, where n is the encoding length.
[0083] Without limitation, when n is 7, the first fixed value used is 2 7 -1=127.
[0084] For another example, if a pixel at a preset position in the previous group of pixels is used as a reference pixel, the pixel difference values in the current group of pixels may be positive or negative. If a pixel difference value in the current group of pixels is positive and does not belong to a preset positive value interval with a corresponding linear transformation rate, a second fixed value may be used (for example, the value interval [-(2 m-1 -1),(2 m-1 -1)]] m-1 -1)) as the pixel difference after transformation; if a pixel difference in the current group of pixels is negative and does not belong to the preset negative value interval with the corresponding linear transformation rate, a third fixed value may be used (for example, the value interval [-(2 m-1 -1),(2 m-1 -1)] among the minimum values -(2 m-1 -1)) as the transformed pixel difference; wherein m is the coding length.
[0085] Without limitation, when m is 7, the second fixed value used is (2 7-1 -1)=63, the third fixed value used is -(2 7-1 -1)=-63.
[0086] In an embodiment of the present invention, by adopting a piecewise linear transformation method to perform linear transformation on each pixel difference, the value of each pixel difference can be quickly and accurately mapped to the preset difference interval, thereby reducing the storage space occupied by each pixel difference in subsequent encoding.
[0087] It should be noted that, in addition to using the above-mentioned piecewise linear transformation method to transform the values of each pixel difference, other appropriate numerical transformation methods can also be used in combination with actual needs. For example, for each pixel difference belonging to different numerical intervals, different fixed values are respectively mapped as the transformed pixel difference.
[0088] Reference Figure 3 , Figure 3 is a partial flow chart of another image compression method in an embodiment of the present invention. The another image compression method may include Figure 1 The steps S11 to S13, and step S14 or step S15 shown in the embodiment may further include steps S31 to S33. There is no order of execution between step S31 and step S13. Figure 1 The different contents in are explained.
[0089] In step S31, for each group of pixels, the original bit number of the pixel value of the reference pixel in the group of pixels is used as the encoding length of the pixel value of the reference pixel, and the pixel value of the reference pixel is encoded.
[0090] Wherein, for each pixel in the image to be compressed, the pixel value of the pixel is stored / recorded by a certain number of bits. It can be understood that the original number of bits of the pixel value of the reference pixel may refer to the number of bits occupied by storing the pixel value of the reference pixel.
[0091] In step S32, the encoding result of the current group of pixels is determined based on the first encoding result of the pixel value of the reference pixel in the current group of pixels and the second encoding result of each pixel difference in the current group of pixels.
[0092] Further, the step S32 may specifically include: splicing the first encoding result and the second encoding result to obtain a first encoding stream; adding a pixel position identifier at a first preset position of the first encoding stream to obtain the encoding result of the current group of pixels; wherein the pixel position identifier is used to indicate the position of the reference pixel in the current group of pixels.
[0093] In a specific implementation, the number of bits included in the pixel position identifier can be determined based on the number of pixels included in the current group of pixels. The first preset position for adding the pixel position identifier can be before the first bit of the first coded stream, or after the last bit of the first coded stream, or other appropriate positions of the first coded stream.
[0094] In one embodiment, the current group of pixels includes 2×4=8 pixels, and 3 bits may be used as the pixel position identification bits.
[0095] In another embodiment, the current group of pixels includes 2×2=4 pixels, and two bits can be used as the pixel position identification bits. Specifically, in this embodiment, "00" can indicate that the position of the reference pixel in the current group of pixels is the upper left corner, "01" can indicate that the position of the reference pixel in the current group of pixels is the upper right corner, "10" can indicate that the position of the reference pixel in the current group of pixels is the lower left corner, and "11" can indicate that the position of the reference pixel in the current group of pixels is the lower right corner.
[0096] It should be noted that the positions of the reference pixels specifically indicated by the pixel position identification bits listed above are provided only as examples. In practical applications, the positions of the reference pixels indicated by different pixel position identification bits may also be set in other ways.
[0097] In step S33, based on the encoding results of each group of pixels, a final encoding result of the image to be compressed is determined.
[0098] In an embodiment of the present invention, by adding a position identifier of a reference pixel at a preset position in the first coding stream, it helps to quickly locate the reference pixel in the current group of pixels and the coding segment corresponding to each pixel difference in subsequent decoding, thereby improving decoding efficiency and decoding accuracy.
[0099] Further, the step S33 may specifically include: if the encoding result of the current group of pixels is obtained by encoding each pixel difference in the current group of pixels based on a preset encoding length, then adding a first flag bit at a second preset position of the encoding result of the current group of pixels; if the encoding result of the current group of pixels is obtained by encoding each transformed pixel difference in the current group of pixels based on the encoding length, then adding a second flag bit at the second preset position of the encoding result of the current group of pixels; splicing the encoding results of each group of pixels to which the flag bit is added to obtain a second encoding stream; based on the second encoding stream, determining the final encoding result of the image to be compressed; wherein the first flag bit is different from the second flag bit (different values, but the same position).
[0100] The selection of the second preset position may refer to the method for determining the first preset position in the above step S32, which will not be described in detail here.
[0101] Among them, based on the second coding stream, determining the final coding result of the image to be compressed may specifically include: directly using the second coding stream as the final coding result of the image to be compressed; or, according to actual needs, processing the second coding stream (for example, adding required information) to obtain the final coding result of the image to be compressed.
[0102] Reference Figure 4 , Figure 4 It is a schematic diagram of the encoding result of a group of pixels of the image to be compressed according to an embodiment of the present invention.
[0103] Without limitation, Figure 4 The group of pixels in the illustrated embodiment includes 4 pixels, among which the pixel with the smallest pixel value is selected as the reference pixel. In the encoding result of the group of pixels, the first bit is used as a "lossless" flag (i.e., the first flag) or a "lossy" flag (i.e., the second flag); the second to third bits are used as pixel position flags, specifically used to indicate the position of the reference pixel in the group of pixels; the fourth to (4+p-1) bits are the encoding results of the reference pixel (i.e., the first encoding result), and p is the number of original bits occupied by the reference pixel; the next three segments of q bits are the encoding results of the three pixel differences (i.e., the first pixel difference, the second pixel difference, and the third pixel difference) in the group of pixels (i.e., the second encoding result), and q is the preset encoding length.
[0104] It can be understood that the first flag is used to indicate that the current group of pixels (specifically, the differences between each pixel in the current group of pixels) is encoded based on a "lossless compression" method; the second flag is used to indicate that the current group of pixels (specifically, the differences between each pixel in the current group of pixels) is encoded based on a "lossy compression" method. Among them, the number of bits occupied by the first flag and the second flag can both be 1 bit. In an embodiment of the present invention, during the encoding process, adding a flag for indicating the encoding method before each group of pixels currently being encoded helps to quickly and accurately determine the corresponding decoding method in the subsequent decoding process, thereby achieving efficient and accurate decoding.
[0105] Reference Figure 5 , Figure 5 : is a schematic diagram of the structure of an image compression device in an embodiment of the present invention. The image compression device may include:
[0106] A pixel grouping module 51 is used to group pixels of the image to be compressed to obtain multiple groups of pixels;
[0107] A pixel difference determination module 52, for selecting a reference pixel from each group of pixels, and determining a pixel difference between the pixel value of each other pixel in the group of pixels and the pixel value of the reference pixel;
[0108] A lossless coding module 53, configured to encode each pixel difference value based on a preset coding length if each pixel difference value in the current group of pixels belongs to a preset difference interval, so that the total coding length of the current group of pixels satisfies the preset total length;
[0109] A lossy coding module 54 is configured to perform numerical transformation on each pixel difference value if at least one of the pixel differences in the current group of pixels does not belong to the difference interval, and then encode each transformed pixel difference value based on the encoding length so that the total encoding length of the current group of pixels satisfies the preset total length;
[0110] The transformed pixel difference belongs to the difference interval.
[0111] For the principle, specific implementation and beneficial effects of the image compression device, please refer to the previous article and Figures 1 to 3 The related description about the image compression method shown is not repeated here.
[0112] The embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program is executed when the processor is running. Figures 1 to 3 The steps of the image compression method are shown. The computer-readable storage medium may include a non-volatile memory or a non-transitory memory, and may also include an optical disk, a mechanical hard disk, a solid-state hard disk, etc.
[0113] Specifically, in the embodiment of the present invention, the processor may be a central processing unit (CPU), and the processor may also be other general-purpose processors, digital signal processors (DSP), application specific integrated circuits (ASIC), field programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.
[0114] It should also be understood that the memory in the embodiments of the present application may be a volatile memory or a non-volatile memory, or may include both volatile and non-volatile memories. Among them, the non-volatile memory may be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory may be a random access memory (RAM), which is used as an external cache. By way of example but not limitation, many forms of random access memory (RAM) are available, such as static RAM (SRAM), dynamic random access memory (DRAM), synchronous DRAM (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link DRAM (SLDRAM) and direct rambus RAM (DR RAM).
[0115] The embodiment of the present invention further provides a terminal, including a memory and a processor, wherein the memory stores a computer program that can be run on the processor, and the processor executes the above-mentioned Figures 1 to 3 The steps of the image compression method are shown. The terminal may include but is not limited to terminal devices such as mobile phones, computers, tablet computers, etc., and may also be a server, a cloud platform, etc.
[0116] It should be understood that the term "and / or" in this article is only a description of the association relationship of the associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in this article indicates that the associated objects before and after are in an "or" relationship.
[0117] The "plurality" appearing in the embodiments of the present application refers to two or more.
[0118] The first, second, etc. descriptions appearing in the embodiments of the present application are only used for illustration and distinction of the description objects. There is no order, nor do they indicate any special limitation on the number of devices in the embodiments of the present application, and cannot constitute any limitation on the embodiments of the present application.
[0119] It should be pointed out that the serial numbers of the steps in this embodiment do not limit the execution order of the steps.
[0120] Although the present invention is disclosed as above, the present invention is not limited thereto. Any person skilled in the art can make various changes and modifications without departing from the spirit and scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the scope defined by the claims.
Claims
1. An image compression method, characterized in that: include: Grouping pixels of the image to be compressed to obtain multiple groups of pixels; For each group of pixels, a reference pixel is selected from the group of pixels, and a pixel difference between the pixel value of each other pixel in the group of pixels and the pixel value of the reference pixel is determined; If each pixel difference value in the current group of pixels belongs to a preset difference interval, each pixel difference value is encoded based on a preset encoding length so that the total encoding length of the current group of pixels satisfies the preset total length; If at least one of the pixel differences in the current group of pixels does not belong to the difference interval, numerically transform the pixel differences, and then encode the transformed pixel differences based on the encoding length so that the total encoding length of the current group of pixels meets the preset total length; The transformed pixel difference belongs to the difference interval.
2. The method according to claim 1, characterized in that For each group of pixels, the selected reference pixels satisfy any of the following: The reference pixel is a pixel with the smallest pixel value in the group of pixels; The reference pixel is a pixel at a preset position in the group of pixels.
3. The method according to claim 2, characterized in that For each group of pixels, the selected reference pixel is the pixel with the smallest pixel value in the group of pixels; The difference interval is selected from [0, 2 n -1], wherein n represents the encoding length and is a positive integer.
4. The method according to claim 2, characterized in that For each group of pixels, the selected reference pixel is a pixel at a preset position in the group of pixels; The difference interval is selected from [-(2 m-1 -1), (2 m-1 -1)], where m represents the coding length, m is a positive integer greater than 1.
5. The method according to claim 1, characterized in that The pixels of the image to be compressed are grouped to obtain multiple groups of pixels, including: A plurality of pixels in the image to be compressed whose positions are within a preset pixel quantity range and have the same color are divided into one group to obtain a plurality of groups of pixels.
6. The method according to claim 1 or 5, characterized in that: Each group of pixels is arranged in a rectangular array, and each group of pixels includes a×b pixels.
7. The method according to claim 6, characterized in that Each group of pixels contains 2×2 pixels.
8. The method according to claim 1, characterized in that The step of performing numerical transformation on each pixel difference includes: Set multiple numerical intervals, and set a corresponding linear transformation rate for each numerical interval; For each pixel difference value, the numerical interval to which the pixel difference value belongs is determined, and then the pixel difference value is numerically transformed based at least on the linear transformation rate corresponding to the numerical interval.
9. The method according to claim 8, characterized in that The following formula is used to transform the value of each pixel difference: y=k1×x,x∈[x1,x2]; Among them, x represents the pixel difference, y represents the transformed pixel difference, [x1, x2] represents the first numerical interval, k1 represents the linear transformation rate corresponding to the first numerical interval, [x3, x4] represents the second numerical interval, k2 represents the linear transformation rate corresponding to the second numerical interval, [x5, x6] represents the third numerical interval, k3 represents the linear transformation rate corresponding to the third numerical interval, [x7, x8] represents the fourth numerical interval, k4 represents the linear transformation rate corresponding to the fourth numerical interval, wherein x and y are both integers.
10. The method according to claim 9, characterized in that The first numerical interval is [0,8], and the corresponding linear transformation rate is 1; The second numerical interval is [9,56], and the corresponding linear transformation rate is 1 / 4; The third numerical interval is [57,1016], and the corresponding linear transformation rate is 1 / 16; The fourth numerical interval is [1017, 4024], and the corresponding linear transformation rate is 1 / 64.
11. The method according to claim 8, characterized in that The method further comprises: For other pixel difference values not included in the multiple value intervals, one or more preset fixed values are used as the transformed pixel difference values.
12. The method according to claim 1, characterized in that The method further comprises: For each group of pixels, using the original bit number of the pixel value of the reference pixel in the group of pixels as the encoding length of the pixel value of the reference pixel, and encoding the pixel value of the reference pixel; Determine an encoding result of the current group of pixels based on a first encoding result of a pixel value of the reference pixel in the current group of pixels and a second encoding result of each pixel difference in the current group of pixels; Based on the encoding results of each group of pixels, a final encoding result of the image to be compressed is determined.
13. The method according to claim 12, characterized in that The determining the encoding result of the group of pixels based on the first encoding result of the pixel value of the reference pixel in the current group of pixels and the second encoding result of each pixel difference in the current group of pixels comprises: The first encoding result and the second encoding result are spliced to obtain a first encoding stream; a pixel position identification bit is added to a first preset position of the first encoding stream to obtain an encoding result of a current group of pixels; The pixel position identifier is used to indicate the position of the reference pixel in the current group of pixels.
14. The method according to claim 12, characterized in that Determining a final encoding result of the image to be compressed based on the encoding results of each group of pixels includes: If the encoding result of the current group of pixels is obtained by encoding the pixel differences in the current group of pixels based on a preset encoding length, then a first flag bit is added to a second preset position of the encoding result of the current group of pixels; If the encoding result of the current group of pixels is obtained by encoding the pixel difference values after each transformation in the current group of pixels based on the encoding length, then adding a second flag bit at the second preset position of the encoding result of the current group of pixels; The coding results of each group of pixels with the flag bit added are spliced to obtain a second coding stream; Determining a final encoding result of the image to be compressed based on the second encoding stream; The first flag bit is different from the second flag bit.
15. An image compression device, characterized in that: include: A pixel grouping module, used for grouping pixels of the image to be compressed to obtain multiple groups of pixels; A pixel difference determination module, for each group of pixels, selecting a reference pixel from the group of pixels, and determining a pixel difference between a pixel value of each other pixel in the group of pixels and a pixel value of the reference pixel; A lossless coding module, configured to encode each pixel difference value based on a preset coding length if each pixel difference value in the current group of pixels belongs to a preset difference interval, so that the total coding length of the current group of pixels satisfies the preset total length; a lossy encoding module, configured to perform numerical transformation on each pixel difference value if at least one of the pixel differences in the current group of pixels does not belong to the difference interval, and then encode each transformed pixel difference value based on the encoding length so that the total encoding length of the current group of pixels satisfies the preset total length; The transformed pixel difference belongs to the difference interval.
16. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the image compression method according to any one of claims 1 to 14 are executed.
17. A terminal comprising a memory and a processor, wherein the memory stores a computer program that can be run on the processor, characterized in that: When the processor runs the computer program, the steps of the image compression method according to any one of claims 1 to 14 are performed.
