CN118250475A - Two-dimensional image compression method, decompression method and decompression system - Google Patents
Two-dimensional image compression method, decompression method and decompression system Download PDFInfo
- Publication number
- CN118250475A CN118250475A CN202410210027.7A CN202410210027A CN118250475A CN 118250475 A CN118250475 A CN 118250475A CN 202410210027 A CN202410210027 A CN 202410210027A CN 118250475 A CN118250475 A CN 118250475A
- Authority
- CN
- China
- Prior art keywords
- matrix
- size
- dct
- bit width
- transformation
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Landscapes
- Compression Of Band Width Or Redundancy In Fax (AREA)
Abstract
Description
技术领域Technical Field
本申请涉及图像处理领域,尤其涉及一种二维图像压缩方法、解压方法及解压缩系统。The present application relates to the field of image processing, and in particular to a two-dimensional image compression method, a decompression method and a decompression system.
背景技术Background Art
为方便且高效的传输和储存图像、视频等数字化信息,需要对图像等数据进行压缩处理,以减少存储和传输的数据量。图像压缩的方式包括无损压缩算法、有损压缩算法、特征提取算法、图像压缩编码算法、深度学习算法等。In order to conveniently and efficiently transmit and store digital information such as images and videos, it is necessary to compress images and other data to reduce the amount of data stored and transmitted. Image compression methods include lossless compression algorithms, lossy compression algorithms, feature extraction algorithms, image compression coding algorithms, deep learning algorithms, etc.
对于图像压缩编码算法,包括:离散余弦变换(Discrete Cosine Transform,DCT)和离散余弦反变换(Inverse Discrete Cosine Transform,IDCT),DCT和IDCT具有良好的能量集中特性,数据经过DCT变换后,分成直流分量和交流分量,为进一步压缩起到铺垫作用。Image compression coding algorithms include Discrete Cosine Transform (DCT) and Inverse Discrete Cosine Transform (IDCT). DCT and IDCT have good energy concentration characteristics. After DCT transformation, the data is divided into DC component and AC component, which paves the way for further compression.
DCT和IDCT中涉及大量的矩阵乘运算,也就是需要大量乘法器,乘法器的精度高,但乘法器的硬件复杂度是加法器的数倍,并且硬件需要存储单元来存储相应的变换矩阵数据,无法有效的控制面积,同时在进行乘法运算时,需要从存储单元中读取变换矩阵数据,会给片上计算带来较大的延时,导致图像压缩、解压效率低。DCT and IDCT involve a large number of matrix multiplication operations, which means that a large number of multipliers are required. The multipliers have high precision, but the hardware complexity of the multipliers is several times that of the adders. In addition, the hardware requires storage units to store the corresponding transformation matrix data, and the area cannot be effectively controlled. At the same time, when performing multiplication operations, the transformation matrix data needs to be read from the storage unit, which will cause a large delay in on-chip calculations, resulting in low image compression and decompression efficiency.
发明内容Summary of the invention
本申请提供一种二维图像压缩方法、解压方法及解压缩系统,以解决图像压缩、解压效率低的问题。The present application provides a two-dimensional image compression method, a decompression method and a decompression system to solve the problem of low image compression and decompression efficiency.
第一方面,本申请提供一种二维图像压缩方法,包括:In a first aspect, the present application provides a two-dimensional image compression method, comprising:
获取目标图像;Get the target image;
若所述目标图像为未压缩图像,将所述目标图像分解为像素矩阵;If the target image is an uncompressed image, decomposing the target image into a pixel matrix;
根据DCT转换矩阵的位宽以及尺寸执行不同变换模式的DCT变换,以得到频域信号矩阵;Performing DCT transformations of different transformation modes according to the bit width and size of the DCT transformation matrix to obtain a frequency domain signal matrix;
对所述频域信号矩阵执行数据量化,以得到量化矩阵;Performing data quantization on the frequency domain signal matrix to obtain a quantization matrix;
对所述量化矩阵执行编码,以形成压缩后的图像格式,表征完成目标图像的压缩。The quantization matrix is encoded to form a compressed image format, indicating that the compression of the target image is completed.
在一些可行的实施例中,所述尺寸包括第一尺寸和第二尺寸,所述位宽包括第一位宽和第二位宽;In some feasible embodiments, the size includes a first size and a second size, and the bit width includes a first bit width and a second bit width;
所述根据DCT转换矩阵的位宽以及尺寸执行不同变换模式的DCT变换,以得到频域信号矩阵,包括:The method of performing DCT transformations of different transformation modes according to the bit width and size of the DCT conversion matrix to obtain a frequency domain signal matrix includes:
获取所述像素矩阵的转置矩阵;Obtaining a transposed matrix of the pixel matrix;
预定义DCT转换矩阵;Predefined DCT transformation matrix;
根据所述DCT转换矩阵的尺寸和位宽,获取所述变换模式;Acquire the transform mode according to the size and bit width of the DCT transform matrix;
所述DCT转换矩阵的尺寸为第一尺寸,且所述DCT转换矩阵的位宽为第一位宽,获取第一DCT变换模式;The size of the DCT conversion matrix is a first size, and the bit width of the DCT conversion matrix is a first bit width, obtaining a first DCT transformation mode;
所述DCT转换矩阵的尺寸为第一尺寸,且所述DCT转换矩阵的位宽为第二位宽,获取第二DCT变换模式;The size of the DCT conversion matrix is a first size, and the bit width of the DCT conversion matrix is a second bit width, obtaining a second DCT transformation mode;
所述DCT转换矩阵的尺寸为第二尺寸,且所述DCT转换矩阵的位宽为第一位宽,获取第三DCT变换模式;The size of the DCT conversion matrix is the second size, and the bit width of the DCT conversion matrix is the first bit width, obtaining a third DCT transformation mode;
所述DCT转换矩阵的尺寸为第二尺寸,且所述DCT转换矩阵的位宽为第二位宽,获取第四DCT变换模式。The size of the DCT conversion matrix is the second size, and the bit width of the DCT conversion matrix is the second bit width, and a fourth DCT transformation mode is obtained.
在一些可行的实施例中,所述根据DCT转换矩阵的位宽以及尺寸执行不同变换模式的DCT变换,以得到频域信号矩阵,包括:In some feasible embodiments, performing DCT transformations of different transformation modes according to the bit width and size of the DCT conversion matrix to obtain a frequency domain signal matrix includes:
当所述变换模式为第一DCT变换模式时,将所述DCT转换矩阵按列输入至存算单元;When the transform mode is the first DCT transform mode, the DCT conversion matrix is input into the storage unit by column;
根据所述DCT转换矩阵的元素位置,将所述转置矩阵的每列按照所述存算单元的位置重排,以得到第一重排矩阵;According to the element position of the DCT conversion matrix, each column of the transposed matrix is rearranged according to the position of the storage and calculation unit to obtain a first rearranged matrix;
对所述第一重排矩阵与所述转换矩阵执行第一矩阵乘运算,以得到第一结果矩阵;Performing a first matrix multiplication operation on the first rearrangement matrix and the conversion matrix to obtain a first result matrix;
对所述第一结果矩阵与所述转换矩阵执行第一矩阵乘运算,以得到频域信号矩阵。A first matrix multiplication operation is performed on the first result matrix and the conversion matrix to obtain a frequency domain signal matrix.
在一些可行的实施例中,所述对所述第一重排矩阵与所述转换矩阵执行第一矩阵乘运算,以得到第一结果矩阵,包括:In some feasible embodiments, performing a first matrix multiplication operation on the first rearrangement matrix and the conversion matrix to obtain a first result matrix includes:
将所述第一重排矩阵的每列元素从第0位输入,并与所述DCT转换矩阵的每列元素相乘,以得到多列第一元素,所述第一元素为8bit数;Input each column element of the first rearrangement matrix from bit 0, and multiply it with each column element of the DCT conversion matrix to obtain multiple columns of first elements, where the first elements are 8-bit numbers;
对所述第一元素执行竖向加法,得到第二元素,所述第二元素为11bit数;Perform vertical addition on the first element to obtain a second element, where the second element is an 11-bit number;
对所述第二元素执行取整操作,以得到第一元素;Performing a rounding operation on the second element to obtain the first element;
将所述第一元素输入至移位加法器,以得到第一结果矩阵。The first element is input into a shift adder to obtain a first result matrix.
在一些可行的实施例中,所述对所述第一重排矩阵与所述转换矩阵执行第一矩阵乘运算,以得到第一结果矩阵,包括:In some feasible embodiments, performing a first matrix multiplication operation on the first rearrangement matrix and the conversion matrix to obtain a first result matrix includes:
当所述变换模式为第二DCT变换模式时,将所述DCT转换矩阵的元素位置划分为第一部分位置和第二部分位置;When the transform mode is the second DCT transform mode, dividing the element positions of the DCT conversion matrix into a first part of positions and a second part of positions;
第一部分位置和第二部分位置被写入第二位宽的权重;The first partial position and the second partial position are written with a weight of the second bit width;
将所述第一重排矩阵的每列元素从第0位输入,并与所述第一部分位置的每列元素相乘,生成第三元素,所述第三元素为4bit数;Input each column element of the first rearranged matrix from position 0, and multiply it with each column element of the first partial position to generate a third element, wherein the third element is a 4-bit number;
将两个所述第三元素首尾相接,以得到第一元素;Connecting the two third elements end to end to obtain the first element;
对所述第一元素执行竖向加法,得到第二元素;Performing vertical addition on the first element to obtain a second element;
对所述第二元素执行取整操作,以得到第一元素;Performing a rounding operation on the second element to obtain the first element;
将所述第一元素输入至移位加法器,以得到第一结果矩阵。The first element is input into a shift adder to obtain a first result matrix.
在一些可行的实施例中,所述对所述第一重排矩阵与所述转换矩阵执行第一矩阵乘运算,以得到第一结果矩阵,包括:In some feasible embodiments, performing a first matrix multiplication operation on the first rearrangement matrix and the conversion matrix to obtain a first result matrix includes:
当所述变换模式为第三DCT变换模式时,分割第一位宽的所述转置矩阵为的第一尺寸的矩阵,并存入所述存算单元中;When the transform mode is the third DCT transform mode, dividing the transposed matrix of the first bit width into matrices of the first size and storing them in the storage unit;
分割所述DCT转换矩阵为第一尺寸的矩阵;Splitting the DCT transformation matrix into matrices of a first size;
对每两个所述存算单元内的第一元素执行竖向加法,得到第二元素,并将两个所述第二元素相加,以得到两个第四元素,所述第四元素为12bit数;Performing vertical addition on the first elements in every two of the storage and calculation units to obtain a second element, and adding two of the second elements to obtain two fourth elements, where the fourth elements are 12-bit numbers;
对所述第四元素执行取整操作,以得到第一元素;Performing a rounding operation on the fourth element to obtain the first element;
将所述第一元素输入至移位加法器,以得到第一结果矩阵。The first element is input into a shift adder to obtain a first result matrix.
第一方面提供一种二维图像压缩方法,通过逐位移位加法的形式实现乘法运算,并基于存算单元,加速矩阵运算,减少硬件计算的延时,可以实现实时计算,可实现多模式的DCT计算,以提高图像压缩的效率。The first aspect provides a two-dimensional image compression method, which implements multiplication operations in the form of bit-by-bit shift addition, and based on a storage and calculation unit, accelerates matrix operations, reduces the delay of hardware calculations, can realize real-time calculations, and can realize multi-mode DCT calculations to improve the efficiency of image compression.
第二方面,本申请提供一种二维图像解压方法,包括:In a second aspect, the present application provides a two-dimensional image decompression method, comprising:
获取目标图像;Get the target image;
若所述目标图像为经第一方面所述方法处理得到的已压缩图像,对所述目标图像执行解码,以得到解码矩阵;If the target image is a compressed image obtained by the method of the first aspect, decoding is performed on the target image to obtain a decoding matrix;
根据IDCT转换矩阵的位宽以及尺寸执行不同变换模式的IDCT变换,以形成时域信号矩阵,表征完成所述目标图像的解压。IDCT transformations of different transformation modes are performed according to the bit width and size of the IDCT transformation matrix to form a time domain signal matrix, which represents the decompression of the target image.
在一些可行的实施例中,所述尺寸包括第一尺寸和第二尺寸,所述位宽包括第一位宽和第二位宽;In some feasible embodiments, the size includes a first size and a second size, and the bit width includes a first bit width and a second bit width;
所述根据IDCT转换矩阵的位宽以及尺寸执行不同变换模式的IDCT变换,以形成时域信号矩阵,包括:The IDCT transformation of different transformation modes is performed according to the bit width and size of the IDCT transformation matrix to form a time domain signal matrix, including:
获取所述像素矩阵的转置矩阵;Obtaining a transposed matrix of the pixel matrix;
预定义IDCT转换矩阵;Predefined IDCT transformation matrix;
根据所述转置矩阵的尺寸和位宽,获取所述变换模式;Obtaining the transformation mode according to the size and bit width of the transposed matrix;
所述IDCT转换矩阵的尺寸为第一尺寸,且所述IDCT转换矩阵的位宽为第一位宽,获取第一IDCT变换模式;The size of the IDCT conversion matrix is a first size, and the bit width of the IDCT conversion matrix is a first bit width, and a first IDCT conversion mode is obtained;
所述IDCT转换矩阵的尺寸为第一尺寸,且所述IDCT转换矩阵的位宽为第二位宽,获取第二IDCT变换模式;The size of the IDCT conversion matrix is the first size, and the bit width of the IDCT conversion matrix is the second bit width, obtaining a second IDCT conversion mode;
所述IDCT转换矩阵的尺寸为第二尺寸,且所述IDCT转换矩阵的位宽为第一位宽和第二位宽,获取第三IDCT变换模式。The size of the IDCT conversion matrix is the second size, and the bit width of the IDCT conversion matrix is the first bit width and the second bit width, and a third IDCT transformation mode is obtained.
在一些可行的实施例中,所述根据IDCT转换矩阵的位宽以及尺寸执行不同变换模式的IDCT变换,以形成时域信号矩阵,包括:In some feasible embodiments, performing IDCT transformations of different transformation modes according to the bit width and size of the IDCT transformation matrix to form a time domain signal matrix includes:
当变换模式为第三IDCT变换模式时,将所述IDCT转换矩阵按列输入至存算单元;When the transformation mode is the third IDCT transformation mode, the IDCT transformation matrix is input into the storage unit by column;
根据所述IDCT转换矩阵的元素位置,将所述转置矩阵的每列按照所述存算单元的位置重排,以得到第二重排矩阵;According to the element positions of the IDCT conversion matrix, each column of the transposed matrix is rearranged according to the position of the storage and calculation unit to obtain a second rearranged matrix;
分割第二尺寸的所述第二重排矩阵为第一尺寸的矩阵;dividing the second rearranged matrix of a second size into matrices of a first size;
对每两个所述存算单元内的第一元素执行竖向加法,相加后得到第二元素,并将两个所述第二元素相加,以得到两个第四元素,所述第四元素为12bit数;Performing vertical addition on every two first elements in the storage and calculation units to obtain a second element, and adding two of the second elements to obtain two fourth elements, where the fourth elements are 12-bit numbers;
对所述第四元素执行取整操作,以得到第一元素;Performing a rounding operation on the fourth element to obtain the first element;
将所述第一元素的元素输入至移位加法器,以得到第二结果矩阵;Inputting the elements of the first elements into a shift adder to obtain a second result matrix;
对所述第二结果矩阵与所述IDCT转换矩阵执行第一矩阵乘运算,以得到时域信号矩阵。A first matrix multiplication operation is performed on the second result matrix and the IDCT conversion matrix to obtain a time domain signal matrix.
第二方面提供一种二维图像解压方法,通过逐位移位加法的形式实现乘法运算,并基于存算单元,加速矩阵运算,减少硬件计算的延时,可以实现实时计算,可实现多模式的IDCT计算,以提高图像解压的效率。The second aspect provides a two-dimensional image decompression method, which implements multiplication operations through bit-by-bit shift addition, and based on the storage and calculation unit, accelerates matrix operations, reduces the delay of hardware calculations, can realize real-time calculations, and can realize multi-mode IDCT calculations to improve the efficiency of image decompression.
第三方面,本申请提供一种二维图像解压缩系统,包括:获取模块、解码模块、存算模块、量化模块和编码模块;In a third aspect, the present application provides a two-dimensional image decompression system, including: an acquisition module, a decoding module, a storage module, a quantization module and an encoding module;
所述获取模块用于获取目标图像;The acquisition module is used to acquire the target image;
若所述目标图像为未压缩图像,将所述目标图像分解为像素矩阵;If the target image is an uncompressed image, decomposing the target image into a pixel matrix;
所述存算模块用于根据DCT转换矩阵的位宽以及尺寸执行不同变换模式的DCT变换,以得到频域信号矩阵;The storage and calculation module is used to perform DCT transformations of different transformation modes according to the bit width and size of the DCT transformation matrix to obtain a frequency domain signal matrix;
所述量化模块用于对所述频域信号矩阵执行数据量化,以得到量化矩阵;The quantization module is used to perform data quantization on the frequency domain signal matrix to obtain a quantization matrix;
所述编码模块用于对所述量化矩阵执行编码,以形成压缩后的图像格式,表征完成目标图像的压缩;The encoding module is used to perform encoding on the quantization matrix to form a compressed image format, indicating that the compression of the target image is completed;
若所述目标图像为已压缩图像,所述解码模块用于对所述目标图像执行解码,以得到解码矩阵;If the target image is a compressed image, the decoding module is used to decode the target image to obtain a decoding matrix;
所述存算单元还用于根据IDCT转换矩阵的位宽以及尺寸执行不同变换模式的IDCT变换,以形成时域信号矩阵,表征完成所述目标图像的解压。The storage and calculation unit is also used to perform IDCT transformations of different transformation modes according to the bit width and size of the IDCT transformation matrix to form a time domain signal matrix to represent the decompression of the target image.
由以上技术方案可知,本申请提供二维图像压缩方法、解压方法及解压缩系统,所述系统包括获取模块、解码模块、存算模块、量化模块和编码模块;获取模块用于获取目标图像;若目标图像为未压缩图像,将目标图像分解为像素矩阵;存算模块用于根据DCT转换矩阵的位宽以及尺寸执行不同变换模式的DCT变换,以得到频域信号矩阵;量化模块用于对频域信号矩阵执行数据量化,以得到量化矩阵;编码模块用于对量化矩阵执行编码,以形成压缩后的图像格式,表征完成目标图像的压缩;若目标图像为已压缩图像,解码模块用于对目标图像执行解码,以得到解码矩阵;存算单元还用于根据IDCT转换矩阵的位宽以及尺寸执行不同变换模式的IDCT变换,以形成时域信号矩阵,表征完成目标图像的解压。基于存内计算的硬件技术加速二维图像压缩和解压缩过程中的DCT/IDCT中矩阵运算效率,可以实现端到端的实时运算;基于硬件的可重构技术,使同一块存算单元可以根据配置信号进行不同尺寸、不同位宽矩阵的DCT/IDCT计算、矩阵量化、编码、解码;使用逐位移位加法的形式取代乘法器,实现大量的乘法运算,同时可以进行计算位宽的调节,以解决图像压缩、解压效率低的问题。It can be seen from the above technical scheme that the present application provides a two-dimensional image compression method, a decompression method and a decompression system, and the system includes an acquisition module, a decoding module, a storage and calculation module, a quantization module and an encoding module; the acquisition module is used to acquire a target image; if the target image is an uncompressed image, the target image is decomposed into a pixel matrix; the storage and calculation module is used to perform DCT transformations of different transformation modes according to the bit width and size of the DCT transformation matrix to obtain a frequency domain signal matrix; the quantization module is used to perform data quantization on the frequency domain signal matrix to obtain a quantization matrix; the encoding module is used to encode the quantization matrix to form a compressed image format, indicating that the compression of the target image is completed; if the target image is a compressed image, the decoding module is used to decode the target image to obtain a decoding matrix; the storage and calculation unit is also used to perform IDCT transformations of different transformation modes according to the bit width and size of the IDCT transformation matrix to form a time domain signal matrix, indicating that the decompression of the target image is completed. Hardware technology based on in-memory computing accelerates the efficiency of matrix operations in DCT/IDCT during two-dimensional image compression and decompression, and can achieve end-to-end real-time operations; based on hardware reconfigurable technology, the same memory and computing unit can perform DCT/IDCT calculations, matrix quantization, encoding, and decoding of matrices of different sizes and bit widths according to configuration signals; the multiplier is replaced by bit-by-bit shift addition to achieve a large number of multiplication operations, and the calculation bit width can be adjusted at the same time to solve the problem of low image compression and decompression efficiency.
附图说明BRIEF DESCRIPTION OF THE DRAWINGS
为了更清楚地说明本申请的技术方案,下面将对实施例中所需要使用的附图作简单地介绍,显而易见地,对于本领域普通技术人员而言,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。In order to more clearly illustrate the technical solution of the present application, the drawings required for use in the embodiments are briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
图1为本实施例示出的二维图像压缩方法流程示意图;FIG1 is a schematic flow chart of a two-dimensional image compression method according to the present embodiment;
图2为本实施例示出的像素矩阵分割示意图;FIG2 is a schematic diagram of pixel matrix segmentation shown in this embodiment;
图3为本实施例示出的颜色块划分示意图;FIG3 is a schematic diagram of color block division shown in this embodiment;
图4为本实施例示出的像素矩阵的转置矩阵示意图;FIG4 is a schematic diagram of a transposed matrix of a pixel matrix shown in this embodiment;
图5为本实施例示出的DCT变换模式示意图;FIG5 is a schematic diagram of a DCT transform mode shown in this embodiment;
图6为本实施例示出的生成频域信号矩阵的流程示意图;FIG6 is a schematic diagram of a process of generating a frequency domain signal matrix according to this embodiment;
图7为本实施例示出的转换矩阵存入存算单元的示意图;FIG7 is a schematic diagram of storing a conversion matrix into a storage unit according to the present embodiment;
图8为本实施例示出的DCT变换中转置矩阵的排序方式示意图;FIG8 is a schematic diagram of the sorting method of the transposed matrix in the DCT transformation shown in this embodiment;
图9为本实施例示出的得到矩阵B的示意图;FIG9 is a schematic diagram of obtaining a matrix B according to this embodiment;
图10为本实施例示出的第三DCT变换模式的示意图;FIG10 is a schematic diagram of a third DCT transform mode shown in this embodiment;
图11为本实施例示出的转换矩阵分割示意图;FIG11 is a schematic diagram of conversion matrix partitioning shown in this embodiment;
图12为本实施例示出的转置矩阵分割示意图;FIG12 is a schematic diagram of transposed matrix partitioning shown in this embodiment;
图13为本实施例示出的扫描编码示意图;FIG13 is a schematic diagram of scanning coding shown in this embodiment;
图14为本实施例示出的二维图像解压方法流程示意图;FIG14 is a schematic flow chart of a two-dimensional image decompression method according to this embodiment;
图15为本实施例示出的IDCT变换模式示意图;FIG15 is a schematic diagram of an IDCT transformation mode shown in this embodiment;
图16为本实施例示出的IDCT变换中转置矩阵的排序方式示意图;FIG16 is a schematic diagram showing the ordering method of the transposed matrix in the IDCT transformation in this embodiment;
图17为本实施例示出的IDCT示意图;FIG17 is a schematic diagram of IDCT shown in this embodiment;
图18为本实施例示出的二维图像解压缩系统结构示意图;FIG18 is a schematic diagram of the structure of a two-dimensional image decompression system according to this embodiment;
图19为本实施例示出的二维图像解压缩系统的工作流程。FIG. 19 is a flowchart of the two-dimensional image decompression system according to this embodiment.
图示说明:Illustration Description:
其中,1801-获取模块、1802-解码模块、1803-存算模块、1804-量化模块、1805-编码模块。Among them, 1801-acquisition module, 1802-decoding module, 1803-storage and calculation module, 1804-quantization module, and 1805-encoding module.
具体实施方式DETAILED DESCRIPTION
下面将详细地对实施例进行说明,其示例表示在附图中。下面的描述涉及附图时,除非另有表示,不同附图中的相同数字表示相同或相似的要素。以下实施例中描述的实施方式并不代表与本申请相一致的所有实施方式。仅是与权利要求书中所详述的、本申请的一些方面相一致的系统和方法的示例。The following embodiments are described in detail, and examples thereof are shown in the accompanying drawings. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The implementations described in the following embodiments do not represent all implementations consistent with the present application. They are only examples of systems and methods consistent with some aspects of the present application as detailed in the claims.
DCT/IDCT是运算密集型的运算,涉及大量的矩阵乘运算,若通过硬件直接实现,需要使用大量的乘法器,乘法器相对于加法器具有相对较大的精度优势,但乘法器的硬件复杂度是加法器的数倍;所占用电路面积较大,且功耗较大;快速DCT/IDCT硬件需要专门的存储单元(Read-Only Memory,ROM)来存储变换矩阵数据,无法有效的控制面积,同时,在进行乘法运算时,需从ROM中读取变换矩阵数据,会给片上计算带来较大的延时,计算效率低下。若利用专用集成电路(Application-Specific Integrated Circuit,ASIC)实现DCT,相比于其它实现方案,在处理能力、功耗以及芯片面积等方面的表现都会更好。但是ASIC的实现方式灵活性较差,完成流片阶段后无法对硬件电路改动,也就是说,只能实现单一的固定位宽的DCT计算,导致图像压缩、解压效率低。DCT/IDCT is a computationally intensive operation involving a large number of matrix multiplication operations. If it is directly implemented through hardware, a large number of multipliers are required. Multipliers have a relatively large accuracy advantage over adders, but the hardware complexity of multipliers is several times that of adders; the circuit area occupied is large and the power consumption is large; fast DCT/IDCT hardware requires a special storage unit (Read-Only Memory, ROM) to store the transformation matrix data, which cannot effectively control the area. At the same time, when performing multiplication operations, the transformation matrix data needs to be read from the ROM, which will cause a large delay in on-chip calculations and low calculation efficiency. If DCT is implemented using an application-specific integrated circuit (ASIC), it will perform better in terms of processing power, power consumption, and chip area compared to other implementation schemes. However, the implementation method of ASIC is less flexible, and the hardware circuit cannot be changed after the tape-out stage is completed. In other words, only a single fixed-bit width DCT calculation can be implemented, resulting in low image compression and decompression efficiency.
为解决图像压缩效率低的问题,参见图1,本申请部分实施例提供一种二维图像压缩方法,包括:In order to solve the problem of low image compression efficiency, referring to FIG1 , some embodiments of the present application provide a two-dimensional image compression method, including:
S101:获取目标图像。S101: Acquire a target image.
目标图像为二维图像,也就是说目标图像不包含深度信息的图像,只包含两个维度的长度和宽度信息,示例性的:几何图形、光栅图像、矢量图像等。The target image is a two-dimensional image, that is, the target image does not contain depth information, but only contains length and width information of two dimensions, for example: geometric figures, raster images, vector images, etc.
在本实施例中,目标图像为待压缩图像。In this embodiment, the target image is an image to be compressed.
S102:若目标图像为未压缩图像,将目标图像分解为像素矩阵。S102: If the target image is an uncompressed image, decompose the target image into a pixel matrix.
目标图像具有16行和16列的像素,共256个像素点,也就是说,目标图像为16×16的像素矩阵。The target image has 16 rows and 16 columns of pixels, with a total of 256 pixels, that is, the target image is a 16×16 pixel matrix.
将16×16的像素矩阵分割为四个8×8的像素矩阵。示例性的:第一个8×8矩阵包含16×16矩阵的前8行和前8列的像素点;第二个8×8矩阵包含16×16矩阵的前8行和后8列的像素点;第三个8×8矩阵包含16×16矩阵的后8行和前8列的像素点;第四个8×8矩阵包含16×16矩阵的后8行和后8列的像素点。The 16×16 pixel matrix is divided into four 8×8 pixel matrices. For example: the first 8×8 matrix contains the first 8 rows and first 8 columns of pixels of the 16×16 matrix; the second 8×8 matrix contains the first 8 rows and last 8 columns of pixels of the 16×16 matrix; the third 8×8 matrix contains the last 8 rows and first 8 columns of pixels of the 16×16 matrix; the fourth 8×8 matrix contains the last 8 rows and last 8 columns of pixels of the 16×16 matrix.
参见图2,图2中8×8的像素矩阵具有8行和8列的像素,包含64个像素点,每个像素位宽为8bit(binary digit,二进制位)。Referring to FIG. 2 , the 8×8 pixel matrix in FIG. 2 has 8 rows and 8 columns of pixels, including 64 pixels, and each pixel has a bit width of 8 bits (binary digit).
S103:根据DCT转换矩阵的位宽以及尺寸执行不同变换模式的DCT变换,以得到频域信号矩阵。S103: Perform DCT transformations of different transformation modes according to the bit width and size of the DCT transformation matrix to obtain a frequency domain signal matrix.
DCT需要预定义转换矩阵将输入信号从时域转换到频域。转换矩阵包含DCT变换的基函数,可以是余弦函数的离散形式。在DCT变换中,输入信号会被分解成一系列余弦函数的线性组合,基函数的系数构成转换矩阵。因此,在执行DCT变换前,需要预定义转换矩阵,以便将其应用于输入信号。DCT requires a predefined transformation matrix to transform the input signal from the time domain to the frequency domain. The transformation matrix contains the basis functions of the DCT transform, which can be discrete forms of cosine functions. In the DCT transform, the input signal is decomposed into a series of linear combinations of cosine functions, and the coefficients of the basis functions form the transformation matrix. Therefore, before performing the DCT transform, the transformation matrix needs to be predefined so that it can be applied to the input signal.
在一些实施例中,参见图3,预定义DCT转换矩阵,其中,DCT转换矩阵A具有8行和8列的像素,首先将DCT转换矩阵按照颜色块划分,其中,第一行中的每一个像素块的颜色不同,共8个颜色块,第二行的第一个像素块颜色与第一行的第八个像素块颜色相同,也就是说,下一行的第一个像素块颜色为上一行最后一个像素块颜色。In some embodiments, referring to FIG. 3 , a DCT conversion matrix is predefined, wherein the DCT conversion matrix A has 8 rows and 8 columns of pixels, and the DCT conversion matrix is first divided according to color blocks, wherein each pixel block in the first row has a different color, with a total of 8 color blocks, and the color of the first pixel block in the second row is the same as the color of the eighth pixel block in the first row, that is, the color of the first pixel block in the next row is the color of the last pixel block in the previous row.
第二行的第二个像素块颜色与第一行的第一个像素块颜色相同,第二行的第三个像素块至第八个像素块按照第一行像素块颜色的顺序排列,与第一行的第二个像素块至第七个像素块颜色相同。其他行按照第二行与第一行之间的规则排列。The second pixel block of the second row has the same color as the first pixel block of the first row, and the third to eighth pixel blocks of the second row are arranged in the order of the pixel blocks of the first row, and have the same color as the second to seventh pixel blocks of the first row. The other rows are arranged according to the rule between the second row and the first row.
二维DCT变换的公式为:The formula for two-dimensional DCT transform is:
其中,F(u,v)为DCT变换后坐标(u,v)的频率,c(u)、c(v)为补偿系数,可使DCT变换矩阵为正交矩阵,f(i,j)为坐标(i,j)的像素数据。Among them, F(u,v) is the frequency of the coordinate (u,v) after DCT transformation, c(u) and c(v) are compensation coefficients, which can make the DCT transformation matrix an orthogonal matrix, and f(i,j) is the pixel data of the coordinate (i,j).
二维DCT变换公式是一个矩阵变换公式,可利用矩阵运算替代,参见下述公式:The two-dimensional DCT transformation formula is a matrix transformation formula, which can be replaced by matrix operations, as shown in the following formula:
Y=AXAT;Y = AXAT ;
其中,Y为频域信号矩阵,A为转换矩阵,X为像素矩阵,AT为转换矩阵的转置矩阵。Among them, Y is the frequency domain signal matrix, A is the conversion matrix, X is the pixel matrix, and AT is the transposed matrix of the conversion matrix.
为使两次矩阵乘法复用同样的硬件,即转换矩阵乘一个矩阵的转置,根据线性代数中矩阵运算的规则而进行的恒等变换,参见下述公式:In order to reuse the same hardware for two matrix multiplications, that is, to multiply the conversion matrix by the transpose of a matrix, an identity transformation is performed according to the rules of matrix operations in linear algebra, see the following formula:
Y=AXAT=A(AXT)T;Y = AXAT = A( AXT ) T ;
在一些实施例中,矩阵尺寸包括第一尺寸和第二尺寸,其中,第一尺寸为8×8,第二尺寸为16×16,矩阵位宽包括第一位宽和第二位宽,其中,第一位宽为8bits,第二位宽为16bits。In some embodiments, the matrix size includes a first size and a second size, wherein the first size is 8×8 and the second size is 16×16, and the matrix bit width includes a first bit width and a second bit width, wherein the first bit width is 8 bits and the second bit width is 16 bits.
获取像素矩阵的转置矩阵XT,参见图4。Obtain the transposed matrix XT of the pixel matrix, see FIG4 .
在一些实施例中,根据DCT转换矩阵的尺寸和位宽,获取变换模式,参见图5,本实施例提供四种DCT变换模式,对于不同的矩阵尺寸和位宽可执行不同的DCT变换模式。In some embodiments, the transform mode is obtained according to the size and bit width of the DCT conversion matrix. Referring to FIG. 5 , this embodiment provides four DCT transform modes. Different DCT transform modes can be executed for different matrix sizes and bit widths.
DCT转换矩阵的尺寸为第一尺寸,且DCT转换矩阵的位宽为第一位宽,获取第一DCT变换模式。The size of the DCT conversion matrix is a first size, and the bit width of the DCT conversion matrix is a first bit width, and a first DCT transformation mode is obtained.
参见图6:包括下述步骤:See Figure 6: The following steps are included:
S601:将DCT转换矩阵按列输入至存算单元。S601: Input the DCT conversion matrix into the storage unit column by column.
参见图7,在转换矩阵A划分完成颜色块后,存入存算单元。Referring to FIG. 7 , after the conversion matrix A is divided into color blocks, it is stored in the storage and calculation unit.
S602:根据DCT转换矩阵的元素位置,将转置矩阵的每列按照存算单元的位置重排,以得到第一重排矩阵。S602: According to the element positions of the DCT conversion matrix, each column of the transposed matrix is rearranged according to the position of the storage and calculation unit to obtain a first rearranged matrix.
将转置矩阵自左向右按照列的方式输入存算单元中,由于存算单元中的权重,即转换矩阵A的元素是将转换矩阵打乱,重新排列,因此,需要将装置矩阵的每一路按照存算单元中的位置进行重排,以保证矩阵乘法的准确性。The transposed matrix is input into the storage unit from left to right in columns. Since the weights in the storage unit, that is, the elements of the transformation matrix A, are the transformation matrix that is scrambled and rearranged, each path of the device matrix needs to be rearranged according to the position in the storage unit to ensure the accuracy of the matrix multiplication.
S603:对第一重排矩阵与DCT转换矩阵执行第一矩阵乘运算,以得到第一结果矩阵。S603: Perform a first matrix multiplication operation on the first rearrangement matrix and the DCT conversion matrix to obtain a first result matrix.
当变换模式为第一DCT变换模式时,得到第一结果矩阵包括以下步骤:When the transform mode is the first DCT transform mode, obtaining the first result matrix comprises the following steps:
将第一重排矩阵的每列元素从第0位输入,并与DCT转换矩阵的每列元素相乘,以得到八列第一元素,第一元素为8bit数;Input each column element of the first rearrangement matrix from bit 0 and multiply it with each column element of the DCT conversion matrix to obtain the first element of eight columns, where the first element is an 8-bit number;
对元素执行竖向加法,得到第二元素,第二元素为11bit数;Perform vertical addition on the elements to obtain the second element, which is an 11-bit number.
对第二元素执行取整操作,以得到第一元素;Perform rounding operation on the second element to obtain the first element;
将第一元素的元素输入至移位加法器,以得到第一结果矩阵。The elements of the first element are input to the shift adder to obtain a first result matrix.
示例性的,参见图8、图9将重排后的一列元素从第<0>bit开始输入,一列第<0>bit与第一列权重相乘,可以得到一列元素即8个8bit,将8个8bit进行竖向加法,根据加法的扩位原理,可得到11bit数,将11bit数砍去小数位,最终是8bit,若砍去小数位后没有达到8bit,可继续饱和截位处理。依此类推,直到第<8>bit结束,可以将一列元素输入完毕,得到8个bit数,输入移位加法器,得到AXT相乘后新矩阵B的元素[0,0],其中,矩阵B为第一结果矩阵,依次类推,输入转置矩阵的第二列、第三列、…、第八列,同理,可得到矩阵B的元素[0,1]、[0,2]、…、[0,7],可得到矩阵B的第一行元素。将转置矩阵X所有列与存算单元中的第二列权重相乘,得到矩阵B的第二行,将转置矩阵X中所有列与存算单元中的第三列、…、第八列权重相乘后,得到矩阵B的第三行、…、第八行,得到矩阵B,得到矩阵B后完成第一次矩阵乘法,即B=AXT。For example, referring to FIG8 and FIG9, the rearranged column elements are inputted from the <0>th bit, and the <0>th bit of a column is multiplied by the weight of the first column to obtain a column element, i.e., 8 8-bits. The 8 8-bits are added vertically. According to the bit expansion principle of addition, an 11-bit number can be obtained. The decimal places of the 11-bit number are cut off, and the final number is 8-bit. If the number does not reach 8-bit after the decimal places are cut off, the saturation truncation process can be continued. Similarly, until the end of the <8>th bit, the column elements can be inputted completely to obtain an 8-bit number, which is inputted into the shift adder to obtain the element [0,0] of the new matrix B after the multiplication of AX T , where matrix B is the first result matrix. By analogy, the second column, the third column, ..., and the eighth column of the transposed matrix are inputted. Similarly, the elements [0,1], [0,2], ..., [0,7] of matrix B can be obtained, and the first row element of matrix B can be obtained. All columns of the transposed matrix X are weighted multiplied with the second column in the storage unit to obtain the second row of the matrix B. All columns of the transposed matrix X are weighted multiplied with the third, ..., and eighth columns in the storage unit to obtain the third, ..., and eighth rows of the matrix B to obtain the matrix B. After obtaining the matrix B, the first matrix multiplication is completed, that is, B = AX T .
DCT转换矩阵的尺寸为第一尺寸,且DCT转换矩阵的位宽为第二位宽,获取第二DCT变换模式,当变换模式为第二DCT变换模式时,得到第一结果矩阵包括以下步骤:The size of the DCT conversion matrix is the first size, and the bit width of the DCT conversion matrix is the second bit width, and the second DCT conversion mode is obtained. When the conversion mode is the second DCT conversion mode, obtaining the first result matrix includes the following steps:
将DCT转换矩阵的元素位置划分为第一部分位置和第二部分位置;Dividing the element positions of the DCT conversion matrix into a first part of positions and a second part of positions;
第一部分位置和第二部分位置被写入第二位宽的权重;The first partial position and the second partial position are written with a weight of the second bit width;
将第一重排矩阵的每列元素从第0位输入,并与第一部分位置的每列元素相乘,生成第三元素,第三元素为4bit数;Input each column element of the first rearranged matrix from position 0 and multiply it with each column element of the first part position to generate the third element, which is a 4-bit number;
将两个第三元素首尾相接,以得到第一元素;Connect the two third elements end to end to get the first element;
对第一元素执行竖向加法,得到第二元素;Perform vertical addition on the first element to obtain the second element;
对第二元素执行取整操作,以保持元素为第一元素;Perform rounding operation on the second element to keep the element equal to the first element;
将第一元素输入至移位加法器,以得到第一结果矩阵。The first element is input to the shift adder to obtain a first result matrix.
示例性的,对于DCT转换矩阵的位宽是4bits,继续参见图7,将所有权重原有的8个位置,第0~3个位置写入4bits权重,第4~7个位置再次写入与第0~3个位置相同的4bit权重。与第一DCT变换模式中将输入矩阵逐个bit输入存算单元不同的是,本模式下一次输入两bit作乘法,分别是<0>和<4>,<1>和<5>,<2>和<6>,<3>和<7>,以<0>和<4>为例,第<4>bit和权重的后4bits相乘得到4bits,第<0>bit和权重的前4bits相乘得到4bits,将两个4bit首尾相接,得到8bits。以此类推,共得到4个8bits数,与第一DCT变换模式同理,进行竖向加法,再进行移位加法,即可得到第一结果矩阵。Exemplarily, for a DCT conversion matrix with a bit width of 4 bits, continue to refer to FIG. 7 , write 4-bit weights in the 0th to 3rd positions of all the original 8 weight positions, and write the same 4-bit weights as in the 0th to 3rd positions in the 4th to 7th positions. Unlike the first DCT transformation mode in which the input matrix is input into the storage unit bit by bit, in this mode, two bits are input at a time for multiplication, namely <0> and <4>, <1> and <5>, <2> and <6>, <3> and <7>. Taking <0> and <4> as an example, the <4>th bit is multiplied with the last 4 bits of the weight to obtain 4 bits, and the <0>th bit is multiplied with the first 4 bits of the weight to obtain 4 bits. Connect the two 4 bits end to end to obtain 8 bits. By analogy, a total of 4 8-bit numbers are obtained. Similar to the first DCT transformation mode, vertical addition is performed, and then shift addition is performed to obtain the first result matrix.
当DCT转换矩阵的尺寸为16×16,位宽为8bits时获取第三DCT变换模式。DCT转换矩阵的尺寸为第二尺寸,且DCT转换矩阵的位宽为第一位宽,获取第三DCT变换模式,当变换模式为第三DCT变换模式时,得到第一结果矩阵包括以下步骤:When the size of the DCT conversion matrix is 16×16 and the bit width is 8 bits, the third DCT conversion mode is obtained. The size of the DCT conversion matrix is the second size, and the bit width of the DCT conversion matrix is the first bit width, and the third DCT conversion mode is obtained. When the conversion mode is the third DCT conversion mode, obtaining the first result matrix includes the following steps:
分割第一位宽的DCT转换矩阵为第一尺寸的矩阵,并存入存算单元中;Splitting the first-bit-width DCT conversion matrix into matrices of the first size and storing them in the storage and calculation unit;
分割DCT转换矩阵为第一尺寸的矩阵;Splitting the DCT transformation matrix into a matrix of a first size;
对每两个存算单元内的第一元素执行竖向加法,得到第二元素,并将两个第二元素相加,以得到两个第四元素;Perform vertical addition on the first elements in every two storage and calculation units to obtain the second element, and add the two second elements to obtain two fourth elements;
对第四元素执行取整操作,以保持元素为第一元素;Perform rounding operation on the fourth element to keep the element as the first element;
将第一元素输入至移位加法器,以得到第一结果矩阵。The first element is input to the shift adder to obtain a first result matrix.
示例性的,参见图10、图11、图12,将位宽为8bits的转换矩阵A分割成四个8×8矩阵,分别放入四个存算单元cim_00、cim_01、cim_10、cim_11中,将输入的转置矩阵也分割成四个8*8矩阵,将XT的[0][0]和XT的[0][1]依次按照列的方式输入到cim_00和cim_10,将XT的[1][0]和XT的[1][1]依次按照列的方式输入到cim_01和cim_11;与第一、二DCT变换模式同理,做8×8矩阵乘法计算,根据矩阵乘法运算规则,在进入移位加法之前,需要将cim_00和cim_01中各自8个8bit进行竖向的加法,得到的11bit后,再次相加得到12bit,将cim_10和cim_11中各自8个8bit进行竖向的加法,得到的11bit后,再次相加得到12bit,截去小数位和饱和截位之后保证8bit,各自进入移位加法器;最后可得到16×16矩阵AXT的第一结果矩阵B。For example, referring to FIG. 10 , FIG. 11 , and FIG. 12 , the conversion matrix A with a bit width of 8 bits is divided into four 8×8 matrices, which are respectively placed in four storage units cim_00, cim_01, cim_10, and cim_11. The input transposed matrix is also divided into four 8*8 matrices, and X T [0][0] and X T [0][1] are sequentially input into cim_00 and cim_10 in a column-by-column manner, and X T [1][0] and X T [1][1] are sequentially input into cim_00 and cim_10 in a column-by-column manner. [1][1] of T is input to cim_01 and cim_11 in column order; similarly to the first and second DCT transform modes, 8×8 matrix multiplication is performed. According to the matrix multiplication operation rules, before entering the shift addition, 8 8 bits in cim_00 and cim_01 need to be added vertically to obtain 11 bits, and then added again to obtain 12 bits. 8 8 bits in cim_10 and cim_11 need to be added vertically to obtain 11 bits, and then added again to obtain 12 bits. After truncating the decimal places and saturation truncations, 8 bits are guaranteed, and each enters the shift adder; finally, the first result matrix B of the 16×16 matrix AX T can be obtained.
转换矩阵A与转置矩阵不同的是,也就是说,图11与图12不同的是,图11中转换矩阵为带有颜色块的矩阵。The conversion matrix A is different from the transposed matrix, that is, the difference between FIG. 11 and FIG. 12 is that the conversion matrix in FIG. 11 is a matrix with color blocks.
当DCT转换矩阵的尺寸为16×16,位宽为4bits时获取第四DCT变换模式。DCT转换矩阵的尺寸为第二尺寸,且DCT转换矩阵的位宽为第二位宽,获取第四DCT变换模式,对于权重位宽为4bit的16×16的转换矩阵A,每个4bits权重在四个存算单元中的计算和排布方式与第二DCT变换模式相同,除此之外的数据喂送方式和第三DCT变换模式相同,在此不予赘述。When the size of the DCT conversion matrix is 16×16 and the bit width is 4 bits, the fourth DCT conversion mode is obtained. The size of the DCT conversion matrix is the second size, and the bit width of the DCT conversion matrix is the second bit width, and the fourth DCT conversion mode is obtained. For the 16×16 conversion matrix A with a weight bit width of 4 bits, the calculation and arrangement of each 4-bit weight in the four storage units are the same as the second DCT conversion mode. The data feeding method is the same as the third DCT conversion mode, which will not be repeated here.
S604:对第一结果矩阵与DCT转换矩阵执行第一矩阵乘运算,以得到频域信号矩阵。S604: Perform a first matrix multiplication operation on the first result matrix and the DCT conversion matrix to obtain a frequency domain signal matrix.
在通过不同的DCT变换模式得到第一结果矩阵后,再次执行矩阵乘运算,对于通过第一DCT变换模式和第二DCT变换模式得到的第一结果矩阵,将第一结果矩阵的转置矩阵BT作为之前的转置矩阵,再次存入存算单元,完成对于像素矩阵的一次转换矩阵位宽为8bit的DCT操作。对于通过第三DCT变换模式得到的第一结果矩阵,同理,继续进行第二次矩阵乘法BXT,可完成输入16×16矩阵的DCT变换。After obtaining the first result matrix through different DCT transformation modes, the matrix multiplication operation is performed again. For the first result matrix obtained through the first DCT transformation mode and the second DCT transformation mode, the transposed matrix BT of the first result matrix is used as the previous transposed matrix and stored in the storage unit again to complete the DCT operation of the pixel matrix with a conversion matrix width of 8 bits. For the first result matrix obtained through the third DCT transformation mode, similarly, the second matrix multiplication BX T is continued to complete the DCT transformation of the input 16×16 matrix.
S104:对频域信号矩阵执行数据量化,以得到量化矩阵。S104: Perform data quantization on the frequency domain signal matrix to obtain a quantization matrix.
在本实施例中,频域信号矩阵为DCT系数的集合或排列方式。每个DCT系数对应于频域中的一个特定频率分量,频域信号矩阵则包含所有DCT系数,DCT系数包括一个直流(DC)系数和多个交流(AC)系数。In this embodiment, the frequency domain signal matrix is a set or arrangement of DCT coefficients. Each DCT coefficient corresponds to a specific frequency component in the frequency domain, and the frequency domain signal matrix contains all DCT coefficients, including a direct current (DC) coefficient and multiple alternating current (AC) coefficients.
为压缩数据,对DCT系数量化处理。量化是一个有损过程,减少系数的精度,从而减少了存储和传输所需的数据量。因人眼对细节部分的敏感度较低,高频系数代表图像的细节部分会被更大幅度地量化。To compress data, the DCT coefficients are quantized. Quantization is a lossy process that reduces the precision of the coefficients, thereby reducing the amount of data required for storage and transmission. Because the human eye is less sensitive to details, high-frequency coefficients representing image details will be quantized to a greater extent.
量化算法参见下式:The quantization algorithm is shown in the following formula:
其中,round表示取整函数,用于对输入执行四舍五入。Gij为输入的频域系数,Qij为对应的量化步长,Bij为量化后的输出,i和j表示矩阵中元素的位置。Among them, round represents the rounding function, which is used to round the input. G ij is the frequency domain coefficient of the input, Qij is the corresponding quantization step size, Bij is the quantized output, and i and j represent the position of the elements in the matrix.
矩阵尺寸为8×8量化表如下:The matrix size is 8×8 and the quantization table is as follows:
矩阵尺寸为16×16量化表如下:The matrix size is 16×16 and the quantization table is as follows:
S105:对量化矩阵执行编码,以形成压缩后的图像格式,表征完成目标图像的压缩。S105: Encoding the quantization matrix to form a compressed image format, indicating that compression of the target image is completed.
可对量化矩阵进行数据编码,也为RLC(Run-Length-Coding,游程编码),示例性的,首先对量化矩阵进行Zig-Zag扫描(之字形扫描),参见图13,将量化矩阵转换成一维数组形式进行排列,这样可以尽可能把0放在一起,由于0大部分集中在右下角,因此利用这种由左上角到右下角的顺序,经过这种顺序变换,矩阵变成一个整数数组,例如Zig-Zag扫描后为:The quantization matrix can be data encoded, also known as RLC (Run-Length-Coding). For example, the quantization matrix is first subjected to Zig-Zag scanning (zigzag scanning), see FIG. 13, and the quantization matrix is converted into a one-dimensional array for arrangement, so that 0s can be put together as much as possible. Since most of the 0s are concentrated in the lower right corner, the order from the upper left corner to the lower right corner is used. After this order transformation, the matrix becomes an integer array, for example, after Zig-Zag scanning:
将Zig-Zag扫描后的一维DCT因子数据中的0做压缩,例如:57,45,0,0,0,0,23,0,-30,-16,0,0,1,0,0,0,0,0,0,only 0,0,可以用RLC转换为(0,57),(0,45),(4,23),(1,-30);(0,-16),(2,1);EOB。Compress the 0s in the one-dimensional DCT factor data after Zig-Zag scanning. For example, 57, 45, 0, 0, 0, 0, 23, 0, -30, -16, 0, 0, 1, 0, 0, 0, 0, 0, only 0, 0. It can be converted by RLC to (0, 57), (0, 45), (4, 23), (1, -30); (0, -16), (2, 1); EOB.
(0,57)表示57前面有0个“0”,(1,-30)表示-30前面有1个“0”,以此类推,其中,EOB表示后面都是0,也可以用(0,0)表示,那么就变为(0,57);(0,45);(4,23);(1,-30);(0,-16);(2,1);(0,0)。(0, 57) means there are 0 "0"s before 57, (1, -30) means there is 1 "0" before -30, and so on. EOB means all the numbers following are 0. It can also be represented by (0, 0), which becomes (0, 57); (0, 45); (4, 23); (1, -30); (0, -16); (2, 1); (0, 0).
经过游程编码后的数据会进行熵编码,可使用霍夫曼编码(Huffman Coding)或算术编码(Arithmetic Coding)。熵编码是一种无损压缩技术,根据数据出现的概率来分配不同长度的编码,从而实现最优的数据压缩。可得到压缩后的图像数据。The data after run-length coding will be entropy coded, which can use Huffman Coding or Arithmetic Coding. Entropy coding is a lossless compression technology that assigns codes of different lengths according to the probability of data occurrence to achieve optimal data compression. Compressed image data can be obtained.
可以理解的是,本实施例提供的压缩方法,可同时对四个8×8矩阵或一个16×16的矩阵执行编码。It can be understood that the compression method provided in this embodiment can perform encoding on four 8×8 matrices or one 16×16 matrix at the same time.
基于上述一种二维图像压缩方法,本申请部分实施例提供一种二维图像解压方法,参见图14,包括:Based on the above two-dimensional image compression method, some embodiments of the present application provide a two-dimensional image decompression method, referring to FIG. 14 , including:
S1401:获取目标图像。S1401: Acquire a target image.
与上述二维图像压缩方法同理,目标图像为二维图像,也就是说目标图像不包含深度信息的图像,只包含两个维度的长度和宽度信息,示例性的:几何图形、光栅图像、矢量图像等。Similar to the above-mentioned two-dimensional image compression method, the target image is a two-dimensional image, that is, the target image does not contain depth information, but only contains length and width information of two dimensions, for example: geometric figures, raster images, vector images, etc.
在本实施例中,目标图像可以为根据上述二维图像压缩方法得到的压缩后的图像,压缩后的图像格式可以为:JPEG、PNG、GIF、WebP、TIFF。In this embodiment, the target image may be a compressed image obtained according to the above two-dimensional image compression method, and the compressed image format may be: JPEG, PNG, GIF, WebP, TIFF.
S1402:若目标图像为处理得到的已压缩图像,对目标图像执行解码,以得到解码矩阵。S1402: If the target image is a compressed image obtained through processing, decoding is performed on the target image to obtain a decoding matrix.
数据编码后利于传输,解码为数据编码的逆过程,得到解码矩阵,其中解码矩阵为还原出量化矩阵,本实施例的解压方法,可以选择同时对四个8×8矩阵或者对一个16×16矩阵进行解码。Data encoding facilitates transmission, and decoding is the inverse process of data encoding to obtain a decoding matrix, wherein the decoding matrix is to restore the quantization matrix. The decompression method of this embodiment can choose to decode four 8×8 matrices or one 16×16 matrix at the same time.
S1403:根据IDCT转换矩阵的位宽以及尺寸执行不同变换模式的IDCT变换,以形成时域信号矩阵,表征完成目标图像的解压。S1403: performing IDCT transformations of different transformation modes according to the bit width and size of the IDCT transformation matrix to form a time domain signal matrix, representing the decompression of the target image.
IDCT变换是将信号从频域转换回时域的过程,这个过程需要用到DCT变换矩阵的逆矩阵来执行逆变换操作。因此,在执行IDCT变换前,需要预定义IDCT转换矩阵,IDCT转换矩阵是DCT变换矩阵的逆矩阵。IDCT transformation is the process of converting the signal from the frequency domain back to the time domain. This process requires the inverse matrix of the DCT transformation matrix to perform the inverse transformation operation. Therefore, before performing the IDCT transformation, it is necessary to predefine the IDCT transformation matrix, which is the inverse matrix of the DCT transformation matrix.
在IDCT变换中,IDCT转换矩阵与经过DCT变换后得到的频域信号相乘,从而得到原始的时域信号或近似值。IDCT转换矩阵包含执行IDCT所需的系数,这些系数与DCT变换矩阵的系数存在特定的关系,以确保能够正确的进行逆变换。In the IDCT transform, the IDCT transform matrix is multiplied by the frequency domain signal obtained after the DCT transform to obtain the original time domain signal or an approximation. The IDCT transform matrix contains the coefficients required to perform the IDCT, and these coefficients have a specific relationship with the coefficients of the DCT transform matrix to ensure that the inverse transform can be performed correctly.
IDCT是DCT的逆向操作,将图像的频率数据转换成像素数据,公式如下:IDCT is the inverse operation of DCT, which converts the frequency data of the image into pixel data. The formula is as follows:
其中,F(u,v)为坐标(u,v)的频率数据,c(u)、c(v)为补偿系数,可使DCT变换矩阵为正交矩阵,f(i,j)为坐标(i,j)的像素数据。Among them, F(u,v) is the frequency data of the coordinate (u,v), c(u) and c(v) are compensation coefficients that can make the DCT transformation matrix an orthogonal matrix, and f(i,j) is the pixel data of the coordinate (i,j).
由于DCT变换的公式同理,IDCT的变换公式为:Since the formula of DCT transformation is the same, the transformation formula of IDCT is:
X=ATYA=AT(ATyT)T;X=A T YA=A T (A T y T ) T ;
其中,Y为频域信号矩阵,A为转换矩阵,X为像素矩阵,AT为转换矩阵的转置矩阵。Among them, Y is the frequency domain signal matrix, A is the conversion matrix, X is the pixel matrix, and AT is the transposed matrix of the conversion matrix.
在一些实施例中,IDCT转换矩阵的尺寸包括第一尺寸和第二尺寸,其中,第一尺寸为8×8,第二尺寸为16×16,矩阵位宽包括第一位宽和第二位宽,其中,第一位宽为8bits,第二位宽为16bits。In some embodiments, the size of the IDCT conversion matrix includes a first size and a second size, wherein the first size is 8×8 and the second size is 16×16, and the matrix bit width includes a first bit width and a second bit width, wherein the first bit width is 8 bits and the second bit width is 16 bits.
获取像素矩阵的转置矩阵XT,再根据IDCT转换矩阵的尺寸和位宽,获取变换模式,参见图15,本实施例提供四种IDCT变换模式,对于不同的矩阵尺寸和位宽可执行不同的IDCT变换模式,其中,四种IDCT变换模式当IDCT转换矩阵的尺寸为16×16,位宽为8bits或4bits时,利用同一种IDCT变换模式,即第三IDCT变换模式。The transposed matrix XT of the pixel matrix is obtained, and then the transformation mode is obtained according to the size and bit width of the IDCT transformation matrix. Referring to FIG. 15 , this embodiment provides four IDCT transformation modes. Different IDCT transformation modes can be executed for different matrix sizes and bit widths. Among them, when the size of the IDCT transformation matrix is 16×16 and the bit width is 8 bits or 4 bits, the four IDCT transformation modes use the same IDCT transformation mode, i.e., the third IDCT transformation mode.
对于IDCT转换矩阵的尺寸为第一尺寸,且IDCT转换矩阵的位宽为第一位宽,获取第一IDCT变换模式,第一IDCT变换模式与第一DCT变换模式的过程相同,参见图15,将图中改变每一列的重排,按照IDCT的排序方式,可实现第一IDCT变换模式。When the size of the IDCT conversion matrix is the first size and the bit width of the IDCT conversion matrix is the first bit width, the first IDCT conversion mode is obtained. The process of the first IDCT conversion mode is the same as that of the first DCT conversion mode, see Figure 15. By changing the rearrangement of each column in the figure and following the IDCT sorting method, the first IDCT conversion mode can be realized.
对于IDCT转换矩阵的尺寸为第一尺寸,且IDCT转换矩阵的位宽为第二位宽,获取第二IDCT变换模式,第二IDCT变换模式与第二DCT变换模式的过程相同,参见图15,将图中改变每一列的重排,按照IDCT的排序方式,可实现第一IDCT变换模式。When the size of the IDCT conversion matrix is the first size and the bit width of the IDCT conversion matrix is the second bit width, a second IDCT conversion mode is obtained. The process of the second IDCT conversion mode is the same as that of the second DCT conversion mode, see Figure 15. By changing the rearrangement of each column in the figure and following the IDCT sorting method, the first IDCT conversion mode can be realized.
对于IDCT转换矩阵的尺寸为第二尺寸,且IDCT转换矩阵的位宽为第一位宽和第二位宽,获取第三IDCT变换模式,当变换模式为第三IDCT变换模式时,包括下述步骤:When the size of the IDCT conversion matrix is the second size and the bit width of the IDCT conversion matrix is the first bit width and the second bit width, obtaining a third IDCT conversion mode, when the conversion mode is the third IDCT conversion mode, includes the following steps:
将IDCT转换矩阵按列输入至存算单元;Input the IDCT conversion matrix into the storage unit column by column;
根据IDCT转换矩阵的元素位置,将转置矩阵的每列按照存算单元的位置重排,以得到第二重排矩阵;According to the element position of the IDCT conversion matrix, each column of the transposed matrix is rearranged according to the position of the storage unit to obtain a second rearranged matrix;
分割第二尺寸的第二重排矩阵为第一尺寸的矩阵;dividing the second rearranged matrix of the second size into matrices of the first size;
对每两个存算单元内的第一元素执行竖向加法,相加后得到第二元素,并将两个第二元素相加,以得到两个第四元素,第四元素为12bit数;Perform vertical addition on the first elements in every two storage units to obtain the second element, and add the two second elements to obtain two fourth elements, where the fourth elements are 12-bit numbers.
对第四元素执行取整操作,以保持元素为第一元素;Perform rounding operation on the fourth element to keep the element as the first element;
将第一元素输入至移位加法器,以得到第二结果矩阵;Inputting the first element into the shift adder to obtain a second result matrix;
对第二结果矩阵与IDCT转换矩阵执行第一矩阵乘运算,以得到时域信号矩阵。A first matrix multiplication operation is performed on the second result matrix and the IDCT conversion matrix to obtain a time domain signal matrix.
示例性的,参见图17,将输入矩阵也分割成四个8×8矩阵,将YT[0][0]和YT[0][1]依次按列输入到cim_00和cim_01,将YT[1][0]和YT[1][1]依次按列输入到cim_19和cim_11;根据矩阵乘法运算规则,在进入移位加法器之前,需要将cim_00和cim_10各自由8个8bit进行竖向的加法得到的11bit再次相加得到12bit,将cim_01和cim_11各自由8个8bit进行竖向的加法得到的11bit再次相加得到12bit,截去小数位和饱和截位之后保证8bit,各自进入移位加法器后,得到ATYT相乘后新矩阵B1的元素[0,0],其中,矩阵B1的为第二结果矩阵,参见图9,依此类推,输入转置矩阵的第二列、第三列、…、第八列,同理,可得到矩阵B的元素[0,1]、[0,2]、…、[0,7],可得到矩阵B1的的第一行元素。将转置矩阵YT所有列与存算单元中的第二列权重相乘,得到矩阵B1的的第二行,将转置矩阵YT中所有列与存算单元中的第三列、…、第八列权重相乘后,得到矩阵B1的的第三行、…、第八行,得到矩阵B1的,得到矩阵B1的后完成第一次矩阵乘法,即B1=ATYT。Exemplarily, referring to FIG. 17 , the input matrix is also divided into four 8×8 matrices, and Y T [0][0] and Y T [0][1] are input into cim_00 and cim_01 in columns, and Y T [1][0] and Y T [1][1] are input into cim_19 and cim_11 in columns; according to the matrix multiplication operation rule, before entering the shift adder, it is necessary to vertically add 8 8-bit cim_00 and cim_10 to obtain 11 bits again to obtain 12 bits, and vertically add 8 8-bit cim_01 and cim_11 to obtain 11 bits again to obtain 12 bits, and after truncating the decimal places and saturation truncations, 8 bits are guaranteed. After entering the shift adder, each of them obtains the element [0,0] of the new matrix B1 after A T Y T multiplication, where matrix B 1 is the second result matrix, see Figure 9, and so on, input the second column, the third column, ..., the eighth column of the transposed matrix, and similarly, the elements [0,1], [0,2], ..., [0,7] of the matrix B can be obtained, and the first row element of the matrix B 1 can be obtained. Multiply all the columns of the transposed matrix Y T with the weights of the second column in the storage unit to obtain the second row of the matrix B 1. Multiply all the columns of the transposed matrix Y T with the weights of the third column, ..., the eighth column in the storage unit to obtain the third row, ..., the eighth row of the matrix B 1 , and obtain the matrix B 1. After obtaining the matrix B 1 , the first matrix multiplication is completed, that is, B 1 = A T Y T .
参见图16,在通过不同的IDCT变换模式得到第二结果矩阵后,再次执行矩阵乘运算,对于通过第一IDCT变换模式、第二DCT变换模式和第三IDCT变换模式得到的第二结果矩阵,将第二结果矩阵的转置矩阵B1 T作为之前的转置矩阵,再次存入存算单元,继续进行第二次矩阵乘法B1 TAT,得到时域信号矩阵,可完成矩阵位宽为8bit和4bit的16×16矩阵的IDCT变换,完成图像解压。Referring to FIG. 16 , after obtaining the second result matrix through different IDCT transform modes, the matrix multiplication operation is performed again. For the second result matrix obtained through the first IDCT transform mode, the second DCT transform mode and the third IDCT transform mode, the transposed matrix B 1 T of the second result matrix is used as the previous transposed matrix and is stored in the storage unit again. The second matrix multiplication B 1 T A T is continued to be performed to obtain the time domain signal matrix. The IDCT transform of the 16×16 matrix with a matrix bit width of 8 bits and 4 bits can be completed, and the image decompression is completed.
本申请部分实施例还提供一种二维图像解压缩系统,参见图18、图19,包括:获取模块1801、解码模块1802、存算模块1803、量化模块1804和编码模块1805;Some embodiments of the present application also provide a two-dimensional image decompression system, see Figures 18 and 19, including: an acquisition module 1801, a decoding module 1802, a storage and calculation module 1803, a quantization module 1804 and an encoding module 1805;
获取模块1801用于获取目标图像;The acquisition module 1801 is used to acquire a target image;
若目标图像为未压缩图像,将目标图像分解为像素矩阵;If the target image is an uncompressed image, decompose the target image into a pixel matrix;
存算模块1803用于根据DCT转换矩阵的位宽以及尺寸执行不同变换模式的DCT变换,以得到频域信号矩阵;The storage and calculation module 1803 is used to perform DCT transformations of different transformation modes according to the bit width and size of the DCT transformation matrix to obtain a frequency domain signal matrix;
量化模块1804用于对频域信号矩阵执行数据量化,以得到量化矩阵;The quantization module 1804 is used to perform data quantization on the frequency domain signal matrix to obtain a quantization matrix;
编码模块1805用于对量化矩阵执行编码,以形成压缩后的图像格式,表征完成目标图像的压缩;The encoding module 1805 is used to perform encoding on the quantization matrix to form a compressed image format, indicating that the compression of the target image is completed;
若目标图像为已压缩图像,解码模块1802用于对目标图像执行解码,以得到解码矩阵;If the target image is a compressed image, the decoding module 1802 is used to perform decoding on the target image to obtain a decoding matrix;
存算单元1803还用于根据IDCT转换矩阵的位宽以及尺寸执行不同变换模式的IDCT变换,以形成时域信号矩阵,表征完成目标图像的解压。The storage and calculation unit 1803 is also used to perform IDCT transformations of different transformation modes according to the bit width and size of the IDCT transformation matrix to form a time domain signal matrix to represent the decompression of the completed target image.
本申请提供二维图像压缩方法、解压方法及解压缩系统,系统包括获取模块1801、解码模块1802、存算模块1803、量化模块1804和编码模块1805;获取模块1801用于获取目标图像;若目标图像为未压缩图像,将目标图像分解为像素矩阵;存算模块1803用于根据DCT转换矩阵的位宽以及尺寸执行不同变换模式的DCT变换,以得到频域信号矩阵;量化模块1804用于对频域信号矩阵执行数据量化,以得到量化矩阵;编码模块1805用于对量化矩阵执行编码,以形成压缩后的图像格式,表征完成目标图像的压缩;若目标图像为已压缩图像,解码模块1802用于对目标图像执行解码,以得到解码矩阵;存算单元1803还用于根据IDCT转换矩阵的位宽以及尺寸执行不同变换模式的IDCT变换,以形成时域信号矩阵,表征完成目标图像的解压。基于存内计算的硬件技术加速二维图像压缩和解压缩过程中的DCT/IDCT中矩阵运算效率,基于硬件的可重构技术,使同一块存算单元可以根据配置信号进行不同尺寸矩阵的DCT/IDCT计算、矩阵量化、编码、解码;使用逐位移位加法的形式取代乘法器,实现大量的乘法运算,同时可以进行计算位宽的调节,以解决图像压缩、解压效率低的问题。The present application provides a two-dimensional image compression method, a decompression method and a decompression system, and the system includes an acquisition module 1801, a decoding module 1802, a storage and calculation module 1803, a quantization module 1804 and an encoding module 1805; the acquisition module 1801 is used to acquire a target image; if the target image is an uncompressed image, the target image is decomposed into a pixel matrix; the storage and calculation module 1803 is used to perform DCT transformations of different transformation modes according to the bit width and size of the DCT transformation matrix to obtain a frequency domain signal matrix; the quantization module 1804 is used to perform data quantization on the frequency domain signal matrix to obtain a quantization matrix; the encoding module 1805 is used to perform encoding on the quantization matrix to form a compressed image format, indicating that the compression of the target image is completed; if the target image is a compressed image, the decoding module 1802 is used to perform decoding on the target image to obtain a decoding matrix; the storage and calculation unit 1803 is also used to perform IDCT transformations of different transformation modes according to the bit width and size of the IDCT transformation matrix to form a time domain signal matrix, indicating that the decompression of the target image is completed. Hardware technology based on in-memory computing accelerates the efficiency of matrix operations in DCT/IDCT during two-dimensional image compression and decompression. Based on hardware reconfigurable technology, the same memory computing unit can perform DCT/IDCT calculations, matrix quantization, encoding, and decoding of matrices of different sizes according to configuration signals. The multiplier is replaced by bit-by-bit shift addition to achieve a large number of multiplication operations. At the same time, the calculation bit width can be adjusted to solve the problem of low image compression and decompression efficiency.
本申请提供的实施例之间的相似部分相互参见即可,以上提供的具体实施方式只是本申请总的构思下的几个示例,并不构成本申请保护范围的限定。对于本领域的技术人员而言,在不付出创造性劳动的前提下依据本申请方案所扩展出的任何其他实施方式都属于本申请的保护范围。Similar parts between the embodiments provided in this application can be referenced to each other. The specific implementation methods provided above are only a few examples under the general concept of this application and do not constitute a limitation on the protection scope of this application. For those skilled in the art, any other implementation methods expanded based on the scheme of this application without creative work belong to the protection scope of this application.
Claims (10)
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202410210027.7A CN118250475A (en) | 2024-02-26 | 2024-02-26 | Two-dimensional image compression method, decompression method and decompression system |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202410210027.7A CN118250475A (en) | 2024-02-26 | 2024-02-26 | Two-dimensional image compression method, decompression method and decompression system |
Publications (1)
Publication Number | Publication Date |
---|---|
CN118250475A true CN118250475A (en) | 2024-06-25 |
Family
ID=91550147
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202410210027.7A Pending CN118250475A (en) | 2024-02-26 | 2024-02-26 | Two-dimensional image compression method, decompression method and decompression system |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN118250475A (en) |
-
2024
- 2024-02-26 CN CN202410210027.7A patent/CN118250475A/en active Pending
Similar Documents
Publication | Publication Date | Title |
---|---|---|
Kovac et al. | JAGUAR: A fully pipelined VLSI architecture for JPEG image compression standard | |
US6643408B2 (en) | Image coding and decoding apparatus, method of image coding and decoding, and recording medium for recording program for image coding and decoding | |
US6456744B1 (en) | Method and apparatus for video compression using sequential frame cellular automata transforms | |
US5659362A (en) | VLSI circuit structure for implementing JPEG image compression standard | |
JPH11163733A (en) | Encoding method and device | |
US6067384A (en) | Fast scaling of JPEG images | |
KR100944928B1 (en) | Apparatus and method for encoding and computing a discrete cosine transform using a butterfly processor | |
US6393154B1 (en) | Method and apparatus for digital image compression using a dynamical system | |
KR102447425B1 (en) | Method, coding method, devices, and associated computer programs for decoding a digital image | |
CN113473139B (en) | Image processing method and image processing device | |
Chiou et al. | A complexity analysis of the JPEG image compression algorithm | |
JPH11177985A (en) | Method and device for high speed image compression | |
US5838825A (en) | Apparatus for decompressing image data which has been compressed using a linear transform | |
US6396955B1 (en) | Image compression and expansion device | |
US6996595B2 (en) | Apparatus and method for consolidating output data from a plurality of processors | |
CN118055256B (en) | DCT (discrete cosine transform) transformed image compression coding method and DCT transformed image compression coding system | |
US6766341B1 (en) | Faster transforms using scaled terms | |
WO2001050769A9 (en) | Method and apparatus for video compression using multi-state dynamical predictive systems | |
CN118250475A (en) | Two-dimensional image compression method, decompression method and decompression system | |
KR100561392B1 (en) | Fast Inverse Discrete Cosine Conversion Method and Apparatus | |
US6400766B1 (en) | Method and apparatus for digital video compression using three-dimensional cellular automata transforms | |
JP3837197B2 (en) | Image data decompression device | |
Zhou | A new highly efficient algorithm for lossless binary image compression. | |
CN113068033B (en) | Multimedia inverse quantization processing method, device, equipment and storage medium | |
Islam et al. | A proposed modification of baseline JPEG standard image compression technique |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |