基于MATLAB的JPEG压缩编码
- 格式:pdf
- 大小:514.51 KB
- 文档页数:4
图像编码实验报告图像编码实验报告一、引言图像编码是一项重要的技术,它可以将图像数据进行压缩和传输,以节省存储空间和传输带宽。
本实验旨在探究图像编码的原理和方法,并通过实验验证不同编码算法的性能和效果。
二、实验目的1. 理解图像编码的基本原理和概念;2. 掌握JPEG和PNG两种常见的图像编码算法;3. 分析和比较不同编码算法的压缩率和图像质量。
三、实验过程1. 实验环境搭建在本实验中,我们使用MATLAB软件进行图像编码实验。
首先,安装MATLAB 并导入实验所需的图像处理工具箱。
2. 图像压缩选择一张分辨率较高的彩色图像作为实验对象。
首先,使用JPEG编码算法对图像进行压缩。
在压缩过程中,可以调整压缩比例参数,观察压缩后图像的质量变化。
然后,使用PNG编码算法对同一张图像进行压缩,并比较JPEG和PNG 两种算法的压缩率和图像质量。
3. 实验结果分析根据实验结果,我们可以得出以下结论:- JPEG算法在高压缩比下会出现明显的失真,但在适当的压缩比下可以获得较好的图像质量;- PNG算法在压缩过程中不会导致明显的失真,但压缩率相对较低。
四、实验讨论1. 图像编码的原理图像编码是将图像数据转换为二进制码流的过程。
常见的图像编码方法包括无损编码和有损编码。
无损编码可以完全还原原始图像,但压缩率较低;有损编码可以获得较高的压缩率,但会引入一定的失真。
2. JPEG编码算法JPEG是一种常用的有损图像编码算法。
它采用离散余弦变换(DCT)将图像从空间域转换为频域,并通过量化和熵编码实现压缩。
JPEG算法在高频部分进行较大幅度的量化,从而实现高压缩率,但也导致了明显的失真。
3. PNG编码算法PNG是一种无损图像编码算法。
它采用预测编码和差分编码的方法,将图像数据转换为无损的二进制码流。
PNG算法在压缩过程中不引入明显的失真,但压缩率相对较低。
五、实验总结通过本次实验,我们深入了解了图像编码的原理和方法,并通过实验验证了JPEG和PNG两种编码算法的性能和效果。
MATLAB图象压缩预览说明:预览图片所展示的格式为文档的源格式展示,下载源文件没有水印,内容可编辑和复制1.图像压缩的概念减少表示数字图像时需要的数据量2.图像压缩的基本原理去除多余数据.以数学的观点来看,这一过程实际上就是将二维像素阵列变换为一个在统计上无关联的数据集合图像压缩是指以较少的比特有损或无损地表示原来的像素矩阵的技术,也称图像编码.图像数据之所以能被压缩,就是因为数据中存在着冗余。
图像数据的冗余主要表现为:(1)图像中相邻像素间的相关性引起的空间冗余;(2)图像序列中不同帧之间存在相关性引起的时间冗余;(3)不同彩色平面或频谱带的相关性引起的频谱冗余。
3数据压缩的目的就是通过去除这些数据冗余来减少表示数据所需的比特数。
由于图像数据量的庞大,在存储、传输、处理时非常困难,因此图像数据的压缩就显得非常重要。
信息时代带来了“信息爆炸”,使数据量大增,因此,无论传输或存储都需要对数据进行有效的压缩。
在遥感技术中,各种航天探测器采用压缩编码技术,将获取的巨大信息送回地面。
图像压缩是数据压缩技术在数字图像上的应用,它的目的是减少图像数据中的冗余信息从而用更加高效的格式存储和传输数据。
4、图像压缩基本方法图像压缩可以是有损数据压缩也可以是无损数据压缩。
对于如绘制的技术图、图表或者漫画优先使用无损压缩,这是因为有损压缩方法,尤其是在低的位速条件下将会带来压缩失真。
如医疗图像或者用于存档的扫描图像等这些有价值的内容的压缩也尽量选择无损压缩方法。
有损方法非常适合于自然的图像,例如一些应用中图像的微小损失是可以接受的(有时是无法感知的),这样就可以大幅度地减小位速。
从压缩编码算法原理上可以分为以下3类:(1)无损压缩编码种类哈夫曼(Huffman)编码,算术编码,行程(RLE)编码,Lempel zev编码。
(2)有损压缩编码种类预测编码,DPCM,运动补偿;频率域方法:正交变换编码(如DCT),子带编码;空间域方法:统计分块编码;模型方法:分形编码,模型基编码;基于重要性:滤波,子采样,比特分配,向量量化;(3)混合编码。
function jpeg %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% THIS WORK IS SUBMITTED BY:%%%% OHAD GAL%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%close all;% ==================% section 1.2 + 1.3% ==================% the following use of the function:%% plot_bases( base_size,resolution,plot_type )%% will plot the 64 wanted bases. I will use "zero-padding" forincreased resolution% NOTE THAT THESE ARE THE SAME BASES !% for reference I plot the following 3 graphs:% a) 3D plot with basic resolution (64 plots of 8x8 pixels) using "surf" function% b) 3D plot with x20 resolution (64 plots of 160x160 pixels) using "mesh" function% c) 2D plot with x10 resolution (64 plots of 80x80 pixels) using "mesh" function% d) 2D plot with x10 resolution (64 plots of 80x80 pixels) using "imshow" function%% NOTE: matrix size of pictures (b),(c) and (d), can support higher frequency = higher bases% but I am not asked to draw these (higher bases) in this section ! % the zero padding is used ONLY for resolution increase !%% get all base pictures (3D surface figure)plot_bases( 8,1,'surf3d' );% get all base pictures (3D surface figure), x20 resolutionplot_bases( 8,20,'mesh3d' );% get all base pictures (2D mesh figure), x10 resolutionplot_bases( 8,10,'mesh2d' );% get all base pictures (2D mesh figure), x10 resolutionplot_bases( 8,10,'gray2d' );% ==================% section 1.4 + 1.5% ==================% for each picture {'0'..'9'} perform a 2 dimensional dct on 8x8 blocks.% save the dct inside a cell of the size: 10 cells of 128x128 matrix% show for each picture, it's dct 8x8 block transform.for idx = 0:9% load a pictureswitch idxcase {0,1}, input_image_128x128 =im2double( imread( sprintf( '%d.tif',idx ),'tiff' ) );otherwise, input_image_128x128 =im2double( imread( sprintf( '%d.tif',idx),'jpeg' ) );end% perform DCT in 2 dimension over blocks of 8x8 in the given picture dct_8x8_image_of_128x128{idx+1} =image_8x8_block_dct( input_image_128x128 );if (mod(idx,2)==0)figure;endsubplot(2,2,mod(idx,2)*2+1);imshow(input_image_128x128);title( sprintf('image #%d',idx) );subplot(2,2,mod(idx,2)*2+2);imshow(dct_8x8_image_of_128x128{idx+1});title( sprintf('8x8 DCT of image #%d',idx) );end% ==================% section 1.6% ==================% do statistics on the cell array of the dct transforms% create a matrix of 8x8 that will describe the value of each "dct-base"% over the transform of the 10 given pictures. since some of the values are% negative, and we are interested in the energy of the coefficients, we will% add the abs()^2 values into the matrix.% this is consistent with the definition of the "Parseval relation" in Fourier Coefficients% initialize the "average" matrixmean_matrix_8x8 = zeros( 8,8 );% loop over all the picturesfor idx = 1:10% in each picture loop over 8x8 elements (128x128 = 256 * 8x8 elements)for m = 0:15for n = 0:15mean_matrix_8x8 = mean_matrix_8x8 + ...abs( dct_8x8_image_of_128x128{idx}(m*8+[1:8],n*8+[1:8]) ).^2;endendend% transpose the matrix since the order of the matrix is elements along the columns,% while in the subplot function the order is of elements along the rows mean_matrix_8x8_transposed = mean_matrix_8x8';% make the mean matrix (8x8) into a vector (64x1)mean_vector = mean_matrix_8x8_transposed(:);% sort the vector (from small to big)[sorted_mean_vector,original_indices] = sort( mean_vector );% reverse order (from big to small)sorted_mean_vector = sorted_mean_vector(end:-1:1);original_indices = original_indices(end:-1:1);% plot the corresponding matrix as asked in section 1.6figure;for idx = 1:64subplot(8,8,original_indices(idx));axis off;h = text(0,0,sprintf('%4d',idx));set(h,'FontWeight','bold');text(0,0,sprintf('\n_{%1.1fdb}',20*log10(sorted_mean_vector(idx)) ));end% add a title to the figuresubplot(8,8,4);h = title( 'Power of DCT coefficients (section 1.6)' );set( h,'FontWeight','bold' );% ==================% section 1.8% ==================% picture 8 is chosen% In this section I will calculate the SNR of a compressed image againts% the level of compression. the SNR calculation is defined in the header% of the function: <<calc_snr>> which is given below.%% if we decide to take 10 coefficients with the most energy, we will% zeros to the other coefficients and remain with a vector 64 elements long% (or a matrix of 8x8)% load the original imageoriginal_image = im2double( imread( '8.tif','jpeg' ) );% I will use this matrix to choose only the wanted number ofcoefficients% the matrix is initialized to zeros -> don't choose any coefficient at allcoef_selection_matrix = zeros(8,8);% compressed picture set (to show the degrading)compressed_set = [1 3 5 10 15 20 30 40];% this loop will choose each time, the "next-most-energetic"coefficient,% to be added to the compressed image -> and thus to improove the SNRfor number_of_coefficient = 1:64% find the most energetic coefficient from the mean_matrix[y,x] = find(mean_matrix_8x8==max(max(mean_matrix_8x8)));% select if for the compressed imagecoef_selection_matrix(y,x) = 1;% replicate the selection matrix for all the parts of the dct transform% (remember that the DCT transform creates a set of 8x8 matrices, where% in each matrix I need to choose the coefficients defined by the % <<coef_selection_matrix>> matrix )selection_matrix = repmat( coef_selection_matrix,16,16 );% set it as zero in the mean_matrix, so that in the next loop, we will% choose the "next-most-energetic" coefficientmean_matrix_8x8(y,x) = 0;% choose the most energetic coefficients from the original image% (total of <<number_of_coefficient>> coefficients for this run in the loop)compressed_image = image_8x8_block_dct(original_image) .*selection_matrix;% restore the compressed image from the given set of coeficientsrestored_image = image_8x8_block_inv_dct( compressed_image );% calculate the snr of this image (based on the original image)SNR(number_of_coefficient) =calc_snr( original_image,restored_image );if ~isempty(find(number_of_coefficient==compressed_set))if (number_of_coefficient==1)figure;subplot(3,3,1);imshow( original_image );title( 'original image' );endsubplot(3,3,find(number_of_coefficient==compressed_set)+1);imshow( restored_image );title( sprintf('restored image with %dcoeffs',number_of_coefficient) );endend% plot the SNR graphfigure;plot( [1:64],20*log10(SNR) );xlabel( 'numer of coefficients taken for compression' );ylabel( 'SNR [db] ( 20*log10(.) )' );title( 'SNR graph for picture number 8, section 1.8' );grid on; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%% --------------------------------------------------------------------------------%% I N N E R F U N C T I O N I M P L E M E N T A T I O N%% --------------------------------------------------------------------------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%% ---------------------------------------------------------------------------------% pdip_dct2 - implementation of a 2 Dimensional DCT%% assumption: input matrix is a square matrix !% ---------------------------------------------------------------------------------function out = pdip_dct2( in )% get input matrix sizeN = size(in,1);% build the matrixn = 0:N-1;for k = 0:N-1if (k>0)C(k+1,n+1) = cos(pi*(2*n+1)*k/2/N)/sqrt(N)*sqrt(2);elseC(k+1,n+1) = cos(pi*(2*n+1)*k/2/N)/sqrt(N);endendout = C*in*(C');% ---------------------------------------------------------------------------------% pdip_inv_dct2 - implementation of an inverse 2 Dimensional DCT%% assumption: input matrix is a square matrix !% ---------------------------------------------------------------------------------function out = pdip_inv_dct2( in )% get input matrix sizeN = size(in,1);% build the matrixn = 0:N-1;for k = 0:N-1if (k>0)C(k+1,n+1) = cos(pi*(2*n+1)*k/2/N)/sqrt(N)*sqrt(2);elseC(k+1,n+1) = cos(pi*(2*n+1)*k/2/N)/sqrt(N);endendout = (C')*in*C;% ---------------------------------------------------------------------------------% plot_bases - use the inverse DCT in 2 dimensions to plot the base pictures%% Note: we can get resolution be zero pading of the input matrix% that is by calling: in = zeros(base_size*resolution)% where: resolution is an integer > 1% So I will use zero pading for resolution (same as in the fourier theory)% instead of linear interpolation.% ---------------------------------------------------------------------------------function plot_bases( base_size,resolution,plot_type )figure;for k = 1:base_sizefor l = 1:base_sizein = zeros(base_size*resolution);in(k,l) = 1; % "ask" for the "base-harmonic (k,l)"subplot( base_size,base_size,(k-1)*base_size+l );switch lower(plot_type)case'surf3d', surf( pdip_inv_dct2( in ) );case'mesh3d', mesh( pdip_inv_dct2( in ) );case'mesh2d', mesh( pdip_inv_dct2( in ) ); view(0,90);case'gray2d', imshow( 256*pdip_inv_dct2( in ) );endaxis off;end% add a title to the figuresubplot(base_size,base_size,round(base_size/2));h = title( 'Bases of the DCT transform (section 1.3)' );set( h,'FontWeight','bold' );% ---------------------------------------------------------------------------------% image_8x8_block_dct - perform a block DCT for an image% ---------------------------------------------------------------------------------function transform_image = image_8x8_block_dct( input_image )transform_image = zeros( size( input_image,1 ),size( input_image,2 ) ); for m = 0:15for n = 0:15transform_image( m*8+[1:8],n*8+[1:8] ) = ...pdip_dct2( input_image( m*8+[1:8],n*8+[1:8] ) );endend% ---------------------------------------------------------------------------------% image_8x8_block_inv_dct - perform a block inverse DCT for an image% ---------------------------------------------------------------------------------function restored_image = image_8x8_block_inv_dct( transform_image ) restored_image =zeros( size( transform_image,1 ),size( transform_image,2 ) );for m = 0:15for n = 0:15restored_image( m*8+[1:8],n*8+[1:8] ) = ...pdip_inv_dct2( transform_image( m*8+[1:8],n*8+[1:8] ) );endend% ---------------------------------------------------------------------------------% calc_snr - calculates the snr of a figure being compressed%% assumption: SNR calculation is done in the following manner:% the deviation from the original image is considered% to be the noise therefore:%% noise = original_image - compressed_image%% the SNR is defined as:%% SNR = energy_of_image/energy_of_noise%% which yields:% SNR = energy_of_image/((original_image-compressed_image)^2)% ---------------------------------------------------------------------------------function SNR = calc_snr( original_image,noisy_image )original_image_energy = sum( original_image(:).^2 );noise_energy = sum( (original_image(:)-noisy_image(:)).^2 );SNR = original_image_energy/noise_energy;以下是1-9号原图像,放到matlab的.m文件目录里,重命名9个图像名为1、2、3、4、5、6、7、8、9。
MATLAB中的图像压缩和编码方法图像压缩和编码是数字图像处理的重要领域,在各种图像应用中起着至关重要的作用。
在本文中,我们将探讨MATLAB中的图像压缩和编码方法,包括无损压缩和有损压缩,并介绍其中的一些经典算法和技术。
一、图像压缩和编码概述图像压缩是指通过一定的算法和技术来减少图像数据的存储量或传输带宽,以达到节约存储空间和提高传输效率的目的。
而图像编码则是将原始图像数据转换为一系列二进制编码的过程,以便存储或传输。
图像压缩和编码通常可以分为无损压缩和有损压缩两种方法。
无损压缩是指压缩后的数据可以完全还原为原始图像数据,不会引入任何失真或变化。
常见的无损压缩算法有Run-Length Encoding (RLE)、Lempel-Ziv-Welch (LZW)、Huffman编码等。
这些算法通常针对图像中的冗余数据进行编码,如重复的像素值或相似的图像区域。
有损压缩则是在保证一定程度的视觉质量下,通过舍弃或近似原始图像数据来减小存储或传输的数据量。
常见的有损压缩算法有JPEG、JPEG2000、GIF等。
这些算法通过离散余弦变换(DCT)、小波变换或颜色量化等方法,将图像数据转换为频域或颜色空间的系数,并通过量化、编码和压缩等步骤来减小数据量。
二、无损压缩方法1. Run-Length Encoding (RLE)RLE是一种简单高效的无损压缩算法,通过计算连续重复像素值的数量来减小数据量。
在MATLAB中,可以使用`rle`函数实现RLE编码和解码。
例如,对于一幅图像,可以将连续的像素值(如白色)编码为重复的个数,然后在解码时根据重复的个数恢复原始像素值。
2. Lempel-Ziv-Welch (LZW)LZW是一种字典压缩算法,通过将图像中连续的像素序列映射为一个短代码来减小数据量。
在MATLAB中,可以使用`lzwencode`和`lzwdecode`函数实现LZW 编码和解码。
例如,对于一段连续的像素序列,可以将其映射为一个短代码,然后在解码时根据代码恢复原始像素序列。