Disclosure of Invention
The invention aims to solve the technical problems that the serial image cannot be processed in the prior art, namely, the quality of the image cannot be effectively adjusted in a limited storage space, the compression rate cannot be balanced, and the compression storage is realized.
The technical scheme adopted by the invention for solving the problems is as follows:
in a first aspect, an embodiment of the present invention provides an image compression method for dynamically adjusting a compression rate, where the method includes:
acquiring a serial video stream and acquiring image frame data based on the serial video stream;
acquiring a frequency domain information block based on the image frame data, and dynamically quantizing the frequency domain information block for adjusting the compression rate to acquire a quantized frequency domain information block;
and carrying out compression coding on the image frame data based on the quantized frequency domain information block to obtain a compressed code stream of the serial video stream.
In one implementation, the acquiring the frequency domain information block based on the image frame data includes:
and performing discrete cosine transform on the image frame data and acquiring frequency domain information blocks of the image frame data.
In one implementation, the dynamically quantizing the frequency domain information block for adjusting a compression rate to obtain a quantized frequency domain information block includes:
acquiring adjustment parameters, and calculating quantization parameters based on the adjustment parameters and a standard quantization table;
acquiring a dynamic adjustment quantization table according to the quantization parameter;
and carrying out quantization adjustment on the frequency domain information block based on the dynamic adjustment quantization table to obtain a quantized frequency domain information block.
In one implementation, the compressing encoding the image frame data based on the quantized frequency domain information block to obtain a compressed code stream of the serial video stream includes:
performing ZigZag conversion on the quantized frequency domain information block to obtain a one-dimensional quantized frequency domain information block;
and carrying out entropy coding on the one-dimensional quantized frequency domain information block to obtain a compressed code stream of the serial video stream.
In one implementation, the method further comprises:
monitoring the storage space to obtain the utilization rate of the storage space;
and dynamically adjusting the quantization parameter based on the storage space utilization.
In one implementation, the dynamically adjusting the quantization parameter based on the storage space utilization includes:
if the storage space utilization rate is higher than a first preset value, the quantization parameter is increased;
and if the storage space utilization rate is lower than a second preset value, reducing the quantization parameter.
In one implementation, the method further comprises:
and adding a quantization degree header into the compressed code stream.
In a second aspect, an embodiment of the present invention further provides an image compression system for dynamically adjusting a compression rate, where the system includes:
the acquisition module is used for acquiring a serial video stream and acquiring image frame data based on the serial video stream;
the quantization module is used for acquiring a frequency domain information block based on the image frame data, and carrying out dynamic quantization for adjusting the compression rate on the frequency domain information block to acquire a quantized frequency domain information block;
and the compression coding module is used for carrying out compression coding on the image frame data based on the quantized frequency domain information block to obtain a compressed code stream of the serial video stream.
In one implementation, the acquisition module includes:
and the first transformation unit is used for performing discrete cosine transformation on the image frame data and acquiring frequency domain information blocks of the image frame data.
In one implementation, the quantization module includes:
the calculating unit is used for acquiring the adjusting parameters and calculating quantization parameters based on the adjusting parameters and the standard quantization table;
the acquisition unit is used for acquiring a dynamic adjustment quantization table according to the quantization parameter;
and the quantization unit is used for carrying out quantization adjustment on the frequency domain information block based on the dynamic quantization table to obtain a quantized frequency domain information block.
In one implementation, the compression encoding module includes:
the second transformation unit is used for performing ZigZag transformation on the quantized frequency domain information block to obtain a one-dimensional quantized frequency domain information block;
and the encoding unit is used for entropy encoding the image frame data based on the one-dimensional quantized frequency domain information block to obtain a compressed code stream of the serial video stream.
In one implementation, the system further comprises:
the monitoring module is used for monitoring the storage space and acquiring the utilization rate of the storage space;
and the adjusting unit is used for dynamically adjusting the quantization parameter based on the storage space utilization rate.
In one implementation, the adjustment unit includes:
the first adjusting subunit is used for increasing the quantization parameter if the storage space utilization rate is higher than a first preset value;
and the second adjustment subunit is used for reducing the quantization parameter if the storage space utilization rate is lower than a second preset value.
In one implementation, the system further comprises:
and the header module is used for adding the quantization degree header into the compressed code stream.
In a third aspect, an embodiment of the present invention further provides a terminal device, including a memory, and one or more programs, where the one or more programs are stored in the memory, and configured to be executed by the one or more processors, where the one or more programs include an image compression method for performing the dynamic adjustment of compression rate according to any one of the above.
In a fourth aspect, embodiments of the present invention also provide a non-transitory computer-readable storage medium, which when executed by a processor of an electronic device, enables the electronic device to perform the image compression method of dynamically adjusting a compression rate as set forth in any one of the above.
The invention has the beneficial effects that: compared with the prior art, the invention relates to an image compression method and system for dynamically adjusting compression rate. Firstly, acquiring a serial video stream, and acquiring image frame data based on the serial video stream; then, based on the image frame data, obtaining a frequency domain information block, and carrying out dynamic quantization for adjusting the compression rate on the frequency domain information block to obtain a quantized frequency domain information block; and finally, carrying out compression coding on the image frame data based on the quantized frequency domain information block to obtain a compressed code stream of the serial video stream. After the frequency domain information block is obtained, the dynamic quantization adjustment is carried out on the frequency domain information block, then the image frame is compressed and encoded based on the quantized frequency domain information block, and finally the compressed code stream of the video stream is obtained, and the compression rate can be dynamically kept at a fixed level by dynamically adjusting the image quality. Under the condition of fixed storage space, the invention can ensure the quality of the image to the greatest extent on the premise of avoiding data overflow by adopting dynamic adjustment of the compression rate, and realizes the balance between the storage space and the image quality.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more clear and clear, the present invention will be further described in detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the scope of the invention.
It should be noted that, if directional indications (such as up, down, left, right, front, and rear … …) are included in the embodiments of the present invention, the directional indications are merely used to explain the relative positional relationship, movement conditions, etc. between the components in a specific posture (as shown in the drawings), and if the specific posture is changed, the directional indications are correspondingly changed.
The embodiment provides an image compression method capable of dynamically adjusting compression rate, by which compression of serial images can be achieved. In the implementation, firstly, a serial video stream is acquired, and image frame data is acquired based on the serial video stream; then, obtaining a frequency domain information block based on the image frame data, and carrying out dynamic quantization for adjusting the compression rate on the frequency domain information block to obtain a quantized frequency domain information block; and finally, carrying out compression coding on the image frame data based on the quantized frequency domain information block to obtain a compressed code stream of the serial video stream. The invention carries out dynamic quantization on the frequency domain information block, then carries out compression coding processing on the image based on the quantized frequency domain information block to obtain the compressed code stream of the video to be compressed, and can ensure the quality of the image to the greatest extent on the premise of avoiding data overflow by adopting the dynamic adjustment of the compression rate under the condition of fixed storage space so as to ensure the balance between the storage space and the quality of the image.
Exemplary method
The embodiment provides an image compression method capable of dynamically adjusting compression rate, which can be applied to terminal equipment. As shown in fig. 1, the method includes:
step S100, acquiring a serial video stream and acquiring image frame data based on the serial video stream.
In this embodiment, a video stream to be subjected to image compression is first acquired, and then the video stream is subjected to framing processing so as to acquire image frame data. And carrying out framing treatment on the video to be compressed to obtain image frame data, so that different quantization parameters are adopted for each frame of image to adjust the compression rate, and the balance of the picture quality and the storage space is realized.
In one implementation, the image frame data is discrete cosine transformed and frequency domain information blocks of the image frame data are obtained. Specifically, the image frame data is discrete cosine transformed with 8×8 blocks, and the spatial domain information is converted into frequency domain information, thereby obtaining frequency domain information blocks of the image frame data. The discrete cosine transform has a good degree of frequency domain energy concentration, i.e. it is able to concentrate more important information in the image together. By processing each frame of image by discrete cosine transform, the image with higher energy is obtained on the premise of not damaging key information in the source video in the image compression process, that is to say, the frame image with higher quality is obtained, and the image is ensured to have high quality after compression as far as possible.
Step 200, obtaining a frequency domain information block based on the image frame data, and dynamically quantizing the frequency domain information block for adjusting the compression rate to obtain a quantized frequency domain information block.
In this embodiment, after obtaining image frame data, DCT processing is performed on the image frame data to obtain frequency domain information blocks, dynamic quantization with a compression rate is performed on the frequency domain information blocks to obtain quantized frequency domain information blocks, and compression encoding is performed on an image through the quantized frequency domain information blocks.
In one implementation, an adjustment parameter alpha is obtained, and a quantization parameter beta is calculated based on the adjustment parameter alpha and a standard quantization table; then, a dynamic adjustment quantization table is obtained according to the quantization parameter beta; and carrying out quantization adjustment on the frequency domain information block based on the dynamic adjustment quantization table to obtain a quantized frequency domain information block.
Specifically, after the image frame data is input to the encoder, DCT (Discrete Cosine Transform ) processing is performed in 8×8 blocks, and the spatial domain information is converted into frequency domain information, so that each data of the 8×8 frequency domain information blocks is quantized with different quantization parameters. The quantization parameter is dynamically adjusted based on the adjustment parameter and a standard quantization table. According to the compression quality of the compression algorithm, defining a regulating parameter alpha, wherein the calculating mode is that
α= (100-Quality) ×2, multiplied by 2, is to obtain higher numerical accuracy when designing hardware. Based on the adjusting parameter alpha, a quantization table Q adaptively adjusted according to the storage space can be obtained based on a JPEG standard quantization table as shown in table 1adaptive The calculation mode is that
Qadaptive =(α·Qstandard +50)/100≈IntegerRound(β·Qstandard ) The method comprises the steps of carrying out a first treatment on the surface of the The compression Quality and the standard quantization table Q can be obtained by combining the adjusting parameters and the self-adaptive adjustment quantization table calculation formulastd The relation between the two parameters can further obtain a quantization parameter beta, and the purpose of controlling the compression quality is achieved by adjusting the numerical value of the parameter control quantization table. And the frequency domain information block F is quantized by using a dynamically adjusted quantization table Qadaptive, and the quantized result is rounded to obtain a final quantized frequency domain information block.
Table 1 standard quantization table.
In one implementation, a storage space is monitored, and a storage space utilization rate is obtained; and dynamically adjusting the quantization parameter based on the storage space utilization. The quantization parameter of each frame of image is dynamically adjusted in real time based on the utilization rate of the storage space, so that the quality of the image is ensured to the greatest extent on the premise of avoiding data overflow under the condition of fixed storage space, and the balance between the storage space and the image quality is achieved.
In one implementation manner, if the storage space utilization rate is higher than a first preset value, the quantization parameter is increased, and the quantization parameter is used for reflecting the magnitude of the compression rate; and if the storage space utilization rate is lower than a second preset value, reducing the quantization parameter. The first preset value can be set to 90%, the second preset value can be set to 70%, the values of the first preset value and the second preset value depend on the total amount of the available storage space and the range in which the utilization rate of the storage space is expected to be stable, and the first preset value and the second preset value can be set according to actual requirements by themselves so as to meet the actual requirements.
In particular, the specific adjustment calculation depends on the monitored utilization of the storage space of the storage array, i.e., the compression ratio of the adjacent images of the previous frame. We use gradient thresholds to divide the compression quality into several levels. If the usage rate of the storage space is too high (i.e. the compression rate of the adjacent image of the previous frame is too low), the quantization parameter of the current image is adjusted, the quantization degree is improved to reduce the quality level of the image, so that the higher compression rate is ensured, and the possibility of data overflow is reduced. If the usage rate of the storage space is lower (i.e. the compression rate of the adjacent image of the previous frame is higher), the quantization parameter of the current image is also adjusted, the quantization degree is reduced to improve the quality level of the image, and meanwhile, the compression rate of the image is reduced, so that the image quality is ensured to the greatest extent in a mode of occupying more storage space.
In one implementation, for the image input by the first frame, the compression rate of the adjacent image is not used as a reference, so that compression processing is directly performed by adopting a higher quantization degree, the quantization degree is gradually adjusted by the subsequent image with reference to the compression rate result of the image input by the first frame, and finally, the balance between the storage space and the image quality is achieved.
And step S300, carrying out compression coding on the image frame data based on the quantized frequency domain information block to obtain a compressed code stream of the serial video stream.
In this embodiment, after the quantized frequency domain information block is obtained, compression encoding processing is performed on the image frame data based on the quantized frequency domain information block, so as to obtain a compressed code stream. The compressed code stream obtained by compressing and encoding the video through the quantized frequency domain information blocks can ensure the quality of the image to the greatest extent on the premise of avoiding data overflow, and the balance between the storage space and the image quality is realized.
In one implementation, firstly performing ZigZag conversion on the quantized frequency domain information block to obtain a one-dimensional quantized frequency domain information block; and carrying out entropy coding on the image frame data based on the one-dimensional quantized frequency domain information block to obtain a compressed code stream of the serial video stream. The ZigZag transformation is realized by sequentially scanning and taking the elements in a matrix from the upper left corner according to a ZigZag shape, and the ZigZag transformation algorithm is simple to realize, low in time complexity and beneficial to improving the image processing efficiency.
In one implementation, a quantization level header is added to the compressed bitstream. Quantization degree header as shown in table 1. Because the quantization parameter is dynamically changed, a header marking the quantization degree of the image needs to be added into the compressed code stream so as to facilitate decompression processing. The invention comprises a compression coding module and a decompression module. The finished quantization table needs a larger header, and the compression coding module and the decompression module in the invention are matched, so that a plurality of configurable complete quantization tables are embedded in the compression coding module and the decompression module, and the quantization tables are selected through short marks. In addition, color space information, resolution information, entropy coding tables, and the like of the image are also embedded in the compression coding module and the decompression module. The embedded image compression parameters reduce the storage space required by storing the parameters and the decoding time required by reading the parameters through the parameters in the embedded compression process of the matched compression coding module and decompression module. The embedded mode ensures the consistency of encoding and decoding, reduces the time of decompression and reading while reducing the storage space for the header, and improves the overall operation efficiency of the system. Through the design of integrated circuit hardware, the effects of real-time processing and full data storage on a chip are achieved.
In one implementation, the storage array of the storage space may store one or more frames of images and perform decompression processing to recover the information of the images when needed. The decompression process is the inverse of the compression process, as shown in fig. 2, the header marked with the image quantization degree is read first, the inverse quantization parameters are adjusted, then entropy decoding, inverse quantization and inverse DCT are sequentially performed, and the decompressed original image is obtained.
Exemplary System
As shown in fig. 3, an embodiment of the present invention provides an image compression system for dynamically adjusting an image compression rate, the system comprising: the device comprises an acquisition module S10, a quantization module S20 and a compression coding module S30. Specifically, the acquiring module S10 is configured to acquire a serial video stream, and acquire image frame data based on the serial video stream; the quantization module S20 is configured to obtain a frequency domain information block based on the image frame data, and perform dynamic quantization for adjusting a compression rate on the frequency domain information block to obtain a quantized frequency domain information block; the compression encoding module S30 is configured to perform compression encoding on the image frame data based on the quantized frequency domain information block, and obtain a compressed code stream of the serial video stream.
In one implementation, the acquisition module includes:
and the first transformation unit is used for performing discrete cosine transformation on the image frame data and acquiring frequency domain information blocks of the image frame data.
In one implementation, the quantization module includes:
the calculating unit is used for acquiring the adjusting parameters and calculating quantization parameters based on the adjusting parameters and the standard quantization table;
the acquisition unit is used for acquiring a dynamic adjustment quantization table according to the quantization parameter;
and the quantization unit is used for carrying out quantization adjustment on the frequency domain information block based on the dynamic quantization table to obtain a quantized frequency domain information block.
In one implementation, the compression encoding module includes:
the second transformation unit is used for performing ZigZag transformation on the quantized frequency domain information block to obtain a one-dimensional quantized frequency domain information block;
and the encoding unit is used for entropy encoding the image frame data based on the one-dimensional quantized frequency domain information block to obtain a compressed code stream of the serial video stream.
In one implementation, the system further comprises:
the monitoring module is used for monitoring the storage space and acquiring the utilization rate of the storage space;
and the adjusting unit is used for dynamically adjusting the quantization parameter based on the storage space utilization rate.
In one implementation, the adjustment unit includes:
the first adjusting subunit is used for increasing the quantization parameter if the storage space utilization rate is higher than a first preset value;
and the second adjustment subunit is used for reducing the quantization parameter if the storage space utilization rate is lower than a second preset value.
In one implementation, the system further comprises:
and the header module is used for adding the quantization degree header into the compressed code stream.
Based on the above embodiment, the present invention also provides a terminal device, and a schematic block diagram of the terminal device may be shown as 4. The terminal device may include one or more processors 100 (only one shown in fig. 4), a memory 101, and a computer program 102 stored in the memory 101 and executable on the one or more processors 100, for example, a program for image compression that dynamically adjusts the compression rate. The one or more processors 100, when executing the computer program 102, may implement the various steps in an embodiment of an image compression method that dynamically adjusts compression rate. Alternatively, the functions of the modules/units in the system embodiment of dynamically adjusting compression rate of image compression may be implemented by one or more processors 100 when executing computer program 102, without limitation.
In one embodiment, the processor 100 may be a central processing unit (Central Processing Unit, CPU), but may also be other general purpose processors, digital signal processors (Digital Signal Processor, DSP), application specific integrated circuits (Application Specific Integrated Circuit, ASIC), off-the-shelf programmable gate arrays (Field-Programmable Gate Array, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or the like. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
In one embodiment, the memory 101 may be an internal storage unit of the electronic device, such as a hard disk or a memory of the electronic device. The memory 101 may also be an external storage device of the electronic device, such as a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) card, a flash card (flash card) or the like, which are provided on the electronic device. Further, the memory 101 may also include both an internal storage unit and an external storage device of the electronic device. The memory 101 is used to store computer programs and other programs and data required by the terminal device. The memory 101 may also be used to temporarily store data that has been output or is to be output.
It will be appreciated by persons skilled in the art that the functional block diagram shown in fig. 4 is merely a block diagram of some of the structures associated with the present inventive arrangements and is not limiting of the terminal device to which the present inventive arrangements are applied, and that a particular terminal device may include more or fewer components than shown, or may combine some of the components, or may have a different arrangement of components.
Those skilled in the art will appreciate that implementing all or part of the above-described methods may be accomplished by way of a computer program, which may be stored on a non-transitory computer readable storage medium, that when executed may comprise the steps of the embodiments of the methods described above. Any reference to memory, storage, operational database, or other medium used in embodiments provided herein may include non-volatile and/or volatile memory. The nonvolatile memory can include Read Only Memory (ROM), programmable ROM (PROM), electrically Programmable ROM (EPROM), electrically Erasable Programmable ROM (EEPROM), or flash memory. Volatile memory can include Random Access Memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms such as Static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual operation data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous Link DRAM (SLDRAM), memory bus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), among others.
In summary, the invention discloses an image compression method and system for dynamically adjusting compression rate. Firstly, acquiring a serial video stream, and acquiring image frame data based on the serial video stream; then, based on the image frame data, obtaining a frequency domain information block, and carrying out dynamic quantization for adjusting the compression rate on the frequency domain information block to obtain a quantized frequency domain information block; and finally, carrying out compression coding on the image frame data based on the quantized frequency domain information block to obtain a compressed code stream of the serial video stream. After the frequency domain information block is obtained, the frequency domain information block is dynamically quantized, then the image is compressed and encoded based on the quantized frequency domain information block, and finally the compressed code stream of the video stream is obtained, and the compression rate can be dynamically kept at a fixed level by dynamically adjusting the image quality. Under the condition of fixed storage space, the invention can ensure the quality of the image to the greatest extent on the premise of avoiding data overflow by adopting dynamic quantization adjustment of the compression rate, and realizes the balance between the storage space and the image quality. The invention combines DCT (discrete cosine change), self-adaptive adjustment quantization and entropy coding, and provides an image compression method for dynamically adjusting compression rate.
It is to be understood that the invention is not limited in its application to the examples described above, but is capable of modification and variation in light of the above teachings by those skilled in the art, and that all such modifications and variations are intended to be included within the scope of the appended claims.