Matlab的图像压缩技术

合集下载

MATLAB图象压缩

MATLAB图象压缩

MATLAB图象压缩预览说明:预览图片所展示的格式为文档的源格式展示,下载源文件没有水印,内容可编辑和复制1.图像压缩的概念减少表示数字图像时需要的数据量2.图像压缩的基本原理去除多余数据.以数学的观点来看,这一过程实际上就是将二维像素阵列变换为一个在统计上无关联的数据集合图像压缩是指以较少的比特有损或无损地表示原来的像素矩阵的技术,也称图像编码.图像数据之所以能被压缩,就是因为数据中存在着冗余。

图像数据的冗余主要表现为:(1)图像中相邻像素间的相关性引起的空间冗余;(2)图像序列中不同帧之间存在相关性引起的时间冗余;(3)不同彩色平面或频谱带的相关性引起的频谱冗余。

3数据压缩的目的就是通过去除这些数据冗余来减少表示数据所需的比特数。

由于图像数据量的庞大,在存储、传输、处理时非常困难,因此图像数据的压缩就显得非常重要。

信息时代带来了“信息爆炸”,使数据量大增,因此,无论传输或存储都需要对数据进行有效的压缩。

在遥感技术中,各种航天探测器采用压缩编码技术,将获取的巨大信息送回地面。

图像压缩是数据压缩技术在数字图像上的应用,它的目的是减少图像数据中的冗余信息从而用更加高效的格式存储和传输数据。

4、图像压缩基本方法图像压缩可以是有损数据压缩也可以是无损数据压缩。

对于如绘制的技术图、图表或者漫画优先使用无损压缩,这是因为有损压缩方法,尤其是在低的位速条件下将会带来压缩失真。

如医疗图像或者用于存档的扫描图像等这些有价值的内容的压缩也尽量选择无损压缩方法。

有损方法非常适合于自然的图像,例如一些应用中图像的微小损失是可以接受的(有时是无法感知的),这样就可以大幅度地减小位速。

从压缩编码算法原理上可以分为以下3类:(1)无损压缩编码种类哈夫曼(Huffman)编码,算术编码,行程(RLE)编码,Lempel zev编码。

(2)有损压缩编码种类预测编码,DPCM,运动补偿;频率域方法:正交变换编码(如DCT),子带编码;空间域方法:统计分块编码;模型方法:分形编码,模型基编码;基于重要性:滤波,子采样,比特分配,向量量化;(3)混合编码。

图像压缩的matlab代码

图像压缩的matlab代码

(1) file_name='baboon.bmp';H=imread(file_name);H=double(H);Grgb=0.2990*H(:,:,1)+0.5870*H(:,:,2)+0.1140*H(:,:,3); NbColors=255;%对矩阵进行量化编码G=wcodemat(Grgb,NbColors);%gray线性的灰阶色调map2=gray(NbColors);%建立图形窗口1figure(1);%建立图像Gimage(G);%应用调色板colormap(map2);title('原图像的灰度图');%显示workplace的变量的详细信息whos('G');%转换成为灰度级索引图像%dwt2单尺度二维离散小波变换[CA1,CH1,CV1,CD1]=dwt2(G,'bior3.7');%从分解系数中提取近似和细节% upcoef2二维系数的直接小波重构A1=upcoef2('a',CA1,'bior3.7',1);H1=upcoef2('h',CH1,'bior3.7',1);V1=upcoef2('v',CV1,'bior3.7',1);D1=upcoef2('d',CD1,'bior3.7',1);%第二幅图像%显示近似和细节figure (2);colormap(map2);subplot(2,2,1);%对矩阵进行量化编码image(wcodemat(A1,192));title('近似A1');subplot(2,2,2);image(wcodemat(H1,192));title('水平细节H1');subplot(2,2,3);image(wcodemat(V1,192));title('垂直细节V1');subplot(2,2,4);image(wcodemat(D1,192));title('对角细节D1');%对图像进行多尺度分解[C,S]=wavedec2(G,2,'bior3.7');%提取分解后的近似和细节系数%提取一维小波变换低频系数CA2=appcoef2(C,S,'bior3.7',2);%提取小波变换高频系数[CH2,CV2,CD2]=detcoef2('all',C,S,2); [CH1,CV1,CD1]=detcoef2('all',C,S,1); %从系数C重构第二层近似A2=wrcoef2('a',C,S,'bior3.7',2);H1=wrcoef2('h',C,S,'bior3.7',1);V1=wrcoef2('v',C,S,'bior3.7',1);D1=wrcoef2('d',C,S,'bior3.7',1);H2=wrcoef2('h',C,S,'bior3.7',2);V2=wrcoef2('v',C,S,'bior3.7',2);D2=wrcoef2('d',C,S,'bior3.7',2);%第三幅图像%显示多尺度分解的结果figure (3);colormap(map2);subplot(2,4,1);image(wcodemat(A1,192));title('近似A1');subplot(2,4,2);image(wcodemat(H1,192));title('水平细节H1');subplot(2,4,3);image(wcodemat(V1,192));title('垂直细节V1');subplot(2,4,4);image(wcodemat(D1,192));title('对角细节D1');subplot(2,4,5);image(wcodemat(A2,192));title('近似A2');subplot(2,4,6);image(wcodemat(H2,192));title('水平细节H2');subplot(2,4,7);image(wcodemat(V2,192));title('垂直细节V2');subplot(2,4,8);image(wcodemat(D2,192));title('对角细节D2');%第四幅图像%从多尺度分解后的系数重构原始图像并显示结果G0=waverec2(C,S,'bior3.7');%建立图形窗口4figure (4);%建立图像G0image(G0);%应用调色板colormap(map2);%绘制调色板的内容colorbar;whos('G0')(2)file_name=('bab.bmp');H=imread(file_name);H=double(H);ca=0.2990*H(:,:,1)+0.5870*H(:,:,2)+0.1140*H(:,:,3);NbColors=255;G=wcodemat(ca,NbColors);map2=gray(NbColors);figure(1);image(G);colormap(map2);title('原图像的灰度图');whos('G');%对图像进行多尺度二维小波分解[c,s]=wavedec2(G,2,'bior3.7');ca1=appcoef2(c,s,'bior3.7',1);ch1=detcoef2('h',c,s,1);cv1=detcoef2('v',c,s,1);cd1=detcoef2('d',c,s,1);%对各频率进行小波重构a1=wrcoef2('a',c,s,'bior3.7',1);h1=wrcoef2('h',c,s,'bior3.7',1);v1=wrcoef2('v',c,s,'bior3.7',1);d1=wrcoef2('d',c,s,'bior3.7',1);G1=[a1,h1;v1,d1];figure(2);image(G1);colormap(map2);axis square;title('分解后低频和高频信息') whos('G1');ca1=appcoef2(c,s,'bior3.7',1);ca1=wcodemat(ca1,440,'mat',1);ca2=0.6*ca1;figure(3);image(ca2);colormap(map2);title('低频压缩图像');whos('ca2');ca3=appcoef2(c,s,'bior3.7',2);ca3=wcodemat(ca3,440,'mat',0); ca4=0.5*ca3;figure(4);image(ca4);title('二层分解后低频压缩图像'); colormap(map2);whos('ca4');。

matlab实现jpeg算法进行图像压缩的源代码

matlab实现jpeg算法进行图像压缩的源代码

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进行图像压缩的技巧

使用Matlab进行图像压缩的技巧引言图像是一种重要的信息表达方式,广泛应用于数字媒体、通信和计算机视觉等领域。

然而,由于图像所占用的存储空间较大,如何有效地进行图像压缩成为了一个重要的问题。

Matlab作为一种强大的数学计算和数据处理工具,可以提供多种图像压缩的技巧,本文将介绍一些常用且有效的图像压缩技巧。

一、离散余弦变换(Discrete Cosine Transformation, DCT)离散余弦变换是一种将空间域中图像转换为频域中的图像的技术。

在Matlab中,可以通过dct2函数实现离散余弦变换。

该函数将图像分块,并对每个块进行DCT变换,然后将变换后的系数进行量化。

通过调整量化步长,可以实现不同程度的压缩。

DCT在图像压缩中的应用广泛,特别是在JPEG压缩中得到了广泛的应用。

二、小波变换(Wavelet Transformation)小波变换是一种将时域信号转换为时频域信号的技术。

在图像压缩中,小波变换可以将图像表示为不同尺度和频率的小波系数。

通过对小波系数进行量化和编码,可以实现图像的有效压缩。

Matlab提供了多种小波变换函数,如wavedec2和waverec2。

这些函数可以对图像进行多尺度小波分解和重构,从而实现图像的压缩。

三、奇异值分解(Singular Value Decomposition, SVD)奇异值分解是一种将矩阵分解为三个矩阵乘积的技术。

在图像压缩中,可以将图像矩阵进行奇异值分解,并保留较大的奇异值,从而实现图像的压缩。

Matlab提供了svd函数,可以方便地实现奇异值分解。

通过调整保留的奇异值个数,可以实现不同程度的图像压缩。

四、量化(Quantization)量化是将连续数值转换为离散数值的过程。

在图像压缩中,量化用于将变换后的图像系数转换为整数值。

通过调整量化步长,可以实现不同程度的压缩。

在JPEG压缩中,量化是一个重要的步骤,通过调整量化表的参数,可以实现不同质量的压缩图像。

MATLAB中的图像压缩和编码方法

MATLAB中的图像压缩和编码方法

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 编码和解码。

例如,对于一段连续的像素序列,可以将其映射为一个短代码,然后在解码时根据代码恢复原始像素序列。

图像编程霍夫曼图像压缩重建【matlab源码】

图像编程霍夫曼图像压缩重建【matlab源码】

毕业论文(设计)题目学院学院专业学生姓名学号年级级指导教师教务处制表matlab图像编程霍夫曼图像压缩重建一、程序说明本团队长期从事matlab编程与仿真工作,擅长各类毕业设计、数据处理、图表绘制、理论分析等,程序代做、数据分析具体信息联系二、程序示例function SnapImage()imagesPath = '.\\snap_images';if ~exist(imagesPath, 'dir')mkdir(imagesPath);end[FileName,PathName,FilterIndex] = uiputfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...'*.*','All Files' },'保存截图',...'.\\snap_images\\temp.jpg');if isequal(FileName, 0) || isequal(PathName, 0)return;endfileStr = fullfile(PathName, FileName);f = getframe(gcf);f = frame2im(f);imwrite(f, fileStr);msgbox('抓图文件保存成功!', '提示信息');function SaveImage(Img)imagesPath = '.\\results';if ~exist(imagesPath, 'dir')mkdir(imagesPath);end[FileName,PathName,FilterIndex] = uiputfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...'*.*','All Files' },'保存截图',...'.\\results\\result.jpg');if isequal(FileName, 0) || isequal(PathName, 0)return;endfileStr = fullfile(PathName, FileName);imwrite(mat2gray(Img), fileStr);function S=PSNR(sss,aaa)[m n p]=size(sss);A=double(sss);B=double(aaa);sumaDif=0;maxI=m*n*max(max(A.^2));for u=1:mfor v=1:nsumaDif=sumaDif+(A(u,v)-B(u,v))^2;endendif (sumaDif==0)sumaDif=1;endS=maxI/sumaDif;S=10*log10(S);function [zvec, zi] = Mat2Huff(vec)if ~isa(vec,'uint8')fprintf('\n请确认输入uint8类型数据向量!\n');return;endvec = vec(:)';f = Frequency(vec);syminfos = find(f~=0);f = f(syminfos);[f, sind] = sort(f);syminfos = syminfos(sind);len = length(syminfos);syminfos_ind = num2cell(1:len);cw_temp = cell(len,1);while length(f)&gt;1ind1 = syminfos_ind{1};ind2 = syminfos_ind{2};cw_temp(ind1) = AddNode(cw_temp(ind1),uint8(0));cw_temp(ind2) = AddNode(cw_temp(ind2),uint8(1));f = [sum(f(1:2)) f(3:end)];syminfos_ind = [{[ind1 ind2]} syminfos_ind(3:end)]; [f,sind] = sort(f);syminfos_ind = syminfos_ind(sind);endcw = cell(256,1);cw(syminfos) = cw_temp;len = 0;for i = 1 : length(vec),len = len+length(cw{double(vec(i))+1}); endstr_temp = repmat(uint8(0),1,len);pt = 1;for index=1:length(vec)cd = cw{double(vec(index))+1};len = length(cd);str_temp(pt+(0:len-1)) = cd;pt = pt+len;endlen = length(str_temp);pad = 8-mod(len,8);if pad &gt; 0str_temp = [str_temp uint8(zeros(1,pad))]; endcw = cw(syminfos);cl = zeros(size(cw));ws = 2.^(0:51);mcl = 0;for index = 1:length(cw)len = length(cw{index});if len&gt;mclmcl = len;endif len&gt;0cd = sum(ws(cw{index}==1));cd = bitset(cd,len+1);cw{index} = cd;cl(index) = len;endendcw = [cw{:}];cols = length(str_temp)/8;str_temp = reshape(str_temp,8,cols);ws = 2.^(0:7);zvec = uint8(ws*double(str_temp));huffcodes = sparse(1,1);for index = 1:numel(cw)huffcodes(cw(index),1) = syminfos(index);endzi.pad = pad;zi.huffcodes = huffcodes;zi.ratio = cols./length(vec);zi.length = length(vec);zi.maxcodelen = mcl;function vec = Huff2Mat(zvec, zi)if ~isa(zvec,'uint8')fprintf('\n请确认输入uint8类型数据向量!\n');return;endlen = length(zvec);str_tmp = repmat(uint8(0),1,len.*8);bi = 1:8;for index = 1:lenstr_tmp(bi+8.*(index-1)) = uint8(bitget(zvec(index),bi));endstr_tmp = logical(str_tmp(:)');len = length(str_tmp);str_tmp((len-zi.pad+1):end) = [];len = length(str_tmp);vec = repmat(uint8(0),1,zi.length);vi = 1;ci = 1;cd = 0;for index = 1:lencd = bitset(cd,ci,str_tmp(index));ci = ci+1;byte = Decode(bitset(cd,ci),zi);if byte &gt; 0vec(vi) = byte-1;ci = 1;cd = 0;vi = vi+1;endendfunction InitFig(hObject,handles)axes(handles.axes1);cla; axis on; box on;set(gca, 'Color', [0.8039 0.8784 0.9686]);set(gca, 'XTickLabel', [], 'YTickLabel', [], 'XTick', [], 'YTick', []);axes(handles.axes2);cla; axis on; box on;set(gca, 'Color', [0.8039 0.8784 0.9686]);set(gca, 'XTickLabel', [], 'YTickLabel', [], 'XTick', [], 'YTick', []);set(handles.textInfo, 'String', ...'图像压缩系统,载入图像,选择压缩算法,比较压缩效果。

MATLAB通过DCT对图像进行区域编码以及门限编码压缩

MATLAB通过DCT对图像进行区域编码以及门限编码压缩

实验作业7分别用区域编码和阈值编码方法实现图像压缩,用8×8DCT变换,保留50%的大系数,并对解码图像进行比较。

要求:DCT要自己实现,不能用matlab中的DCT函数区域编码程序代码:clear;I=imread('d:\3.jpg');I=double(rgb2gray(I));figure(1);imshow(uint8(I));title('原图像');Y=zeros(8,8);for i=1:8for j=1:8if i==1Y(i,j)=sqrt(1/8);elseY(i,j)=sqrt(2/8)*cos((pi*(2*(j-1)+1)*(i-1))/16);endendends=blkproc(I,[8 8],'P1*x*P2',Y,Y'); figure(2);imshow(uint8(s));for j=1:8for i=1:8if j<=8-i+1a(i,j)=1;elsea(i,j)=0;end;end;end;s=blkproc(s,[8 8],'P1.*x',a); figure(3);imshow(uint8(s));s=blkproc(s,[8 8],'P1*x*P2',Y',Y); figure(4);imshow(uint8(s));title('经过压缩处理的图像')运行结果:阈值编码程序代码clear;I=imread('d:\3.jpg'); I=rgb2gray(I); imshow(uint8(I)); title('原图像'); I=double(I); for i=1:8 for j=1:8 if (i==1)Y(i,j)=sqrt(1/8); elseY(i,j)=sqrt(2/8)*cos((i-1)*(2*j-1)*pi/(2*8)); end; end; end; s=blkproc(I,[8 8],'P1*x*P2',Y,Y'); a=ones(8,8); b=reshape(Y,1,64); midvalue=median(b); for i=1:8 for j=1:8if(abs(Y(i,j))<midvalue) a(i,j)=0; end; end; end;s=blkproc(s,[8 8],'P1.*x',a); s=blkproc(s,[8 8],'P1*x*P2',Y',Y); figure(2); imshow(uint8(s));title('被与之编码方式压缩的图像');运行结果:心得体会:由于第八章内容上课听的不是很明白,所以作业题拿到之后不知道怎么做,重新把第八章看了一遍,可是很多地方看了好久好多次还是不明白其原理,就像这次所涉及的DCT (虽然会做作业,但是实在是不理解),区域编码,门限编码,都是不明白什么意思!后来网上搜罗资料,看了颇久,请教了同学,才慢慢知道是什么一回事,做这题目的时候,遇到过不知道怎么分块的问题,后来也是同学告诉有个blkproc 的函数可以用,才使到程序精简化。

基于MATLAB的图像压缩技术研究

基于MATLAB的图像压缩技术研究
第 1 2期 20 0 8年 l 2月

山 西 焦 煤 科 技
S a x k ng Co lSce c & Te h o o y h n iCo i a i n e c n lg
No 2 .1 De . 00 e2 8
试 验研究 ・
基于 M TA A L B的图像压缩技术研究
吕金 花①
( 山西汾 西煤化 高级技 工学校 )
摘 要
介 绍 了 MA L B在 图像 压 缩研 究方 面 的应 用 , 讨 基 于 B TA 探 P人 工神 经 网络 的 图像 压 缩
的 MA L B 实现 和 基 于小 波 的 图像 压缩技 术 。MA L B软件 使 用 MA L 2 0 a版本 , TA TA T AB R 0 7 并且 通 过
介休 0 20 30 0
B P网络 是 目前最 为 常用 的一 种人 工 神经 网络模
① 作 者简介 : 花 女 吕金
15 9 9年出生 19 年毕业于 山西矿业学 院 工程师 98

3 6・
ห้องสมุดไป่ตู้
山 西 焦 煤 科 技
20 0 8年第 l 2期
2 基于 B P人工 神经 网络 图像压 缩 的实现
能 , 本 上囊 括 了 目前 很 多 典 型应 用 的数 字 图 形 处 基 理 。在 MA L B的神 经 网络工 具 箱 中提 供 了很 多用 TA 于 图像 处 理 的函数 。MA L BR 07 T A 2 0 a中的小 波工 具 箱 ( vl olo . 中包 含 的各 种 小 波 分 析 函 Waee To x3 0) t b 数, 可用 于对信 号 与 图像 的压 缩 处 理 , 缩 后 能 保 持 压 信 号 与图像 的特 征基 本 不变 , 压缩 比高 , 缩速 度 快 , 压 且 在传 递过 程 中具 有 抗 干 扰 能 力 。本 文 讨 论 了应 用 M T A 中的 B A LB P神 经 网络 工 具 箱 函数 和 小 波 工 具
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Matlab的图像压缩技术
一.目的要求
掌握Matlab图像图像压缩技术原理和方法。

理解有损压缩和无损压缩的概念,了解几种常用的图像压缩编码方式,利用matlab进行图像压缩算法验证。

二.实验内容
1、观察颜色映像矩阵的元素
>> hot(8)
ans =
0.3333 0 0
0.6667 0 0
1.0000 0 0
1.0000 0.3333 0
1.0000 0.6667 0
1.0000 1.0000 0
1.0000 1.0000 0.5000
1.0000 1.0000 1.0000
数据显示第一行是1/3红色,最后一行是白色。

2、pcolor显示颜色映像
>> n=16;
>> colormap(jet(n));
>> pcolor([1:n+1;1:n+1]);
>> title('Using Pcolor to Display a Color )Map');
图2 显示颜色映像
3、colorbar显示当当前坐标轴的颜色映像>> [x,y,z]=peaks;
>> mesh(x,y,z);
>> colormap(hsv);
>> axis([-3 3 -3 3 -6 8]);
>> colorbar;
图3 显示当前坐标轴的颜色映像4、图像格式转换
g=rgb2gray(I);
g=rgb2gray(I);
>> imshow(g),colorbar;
图4-1 原图像saturn.png
图4-2转换后的图像
5、求解图像的二唯傅里叶频谱
I=imread('cameraman.tif');
>> imshow(I)
>>
J=fftshift(fft2(I));
>> figure;
>> imshow(log(abs(J)),[8,10])
图5-1 原图像cameraman.png
图5-2 频谱图
将图像更换一下
I=imread('pout.tif'); imshow(I)
J=fftshift(fft2(I));
figure;
imshow(log(abs(J)),[8,10])
图5-3 原始图pout.tif
图5-4 频谱图
6、利用DCT变换进行图像压缩
RGB=imread('peppers.png');
>> I=rgb2gray(RGB);
>> J=dct2(I);
>> imshow(log(abs(J)),[]),colormap(jet(64)),colorbar
图6-1 DCT变换后二维变换谱
J(abs(J)<10)=0;
>> K=idct2(J); %逆变换
>> figure,imshow(I)
figure,imshow(K,[0 255])
图6-2 原始图像
图6-3 压缩还原后的图像
仔细看我们可以发现,压缩还原后的图像比较模糊。

7、利用离散余弦变换进行JPEG图像压缩
>> I=imread('cameraman.tif');
>> I=im2double(I);
>> T=dctmtx(8); %产生二维DCT变换矩阵
>> B=blkproc(I,[8 8],'P1.*x',T,T'); %改成点乘
>> mask=[1 1 1 1 0 0 0 0;1 1 1 0 0 0 0 0;1 1 0 0 0 0 0 0;1 0 0 0 0 0 0 0 ;zeros(4,8)] %二值掩膜用来压缩DCT系数
mask =
1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 >> B2=blkproc(B,[8 8],'P1*x',mask); %只保留10个系数>> I2=blkproc(B2,[8 8],'P1*x*P2',T,T'); %逆DCT
>> subplot(1,2,1);
>> imshow(I);title('原图像');
>> subplot(1,2,2);
>> imshow(I2);title('压缩图像');
图7-1 原图像和第一种压缩后的图像>> mask=[1 1 0 0 0 0 0 0;1 0 0 0 0 0 0 0 ;zeros(6,8)]
mask =
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
图7-2 原图像和第二种压缩后的图像
>> mask=[1 0 0 0 0 0 0 0 ;zeros(7,8)]
mask =
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
00 0 0 0 0 0 0
图7-3 原图像和第三种压缩后的图像
比较三种情况。

mask舍弃系数越多,图像越模糊。

压缩应在最合理近似原图像下使用最少系数。

1、图像中哪些信息主要,哪些信息次要?
需要传达给别人的部分是主要的。

其他是次要的。

例如肖像图片,肖像部分是主要的,其背景是次要的。

为了证明当时的场景,场景就是主要的了,而人物就变成次要的。

描述风景,人物是次要的,背景是主要的。

主要和次要是相对的。

DCT变换后图像变成了1.0 代号0 的就是次要冗余信息1 就是主要信息
DCT变换主要作用就是把图像信息集中在较小的一部分利于图像处理。

2、简述离散余弦变换原理。

DCT变换和FFT变换都属于变换压缩方法(TransformCompression),变换压缩的一个特点是将从前密度均匀的信息分布变换为密度不同的信息分布。

在图像中,低频部分的信息量要大于高频部分的信息量,尽管低频部分的数据量比高频部分的数据量要小的多。

例如删除掉占50%存储空间的高频部分,信息量的损失可能还不到5%。

压缩过程为:
(1)首次将输入图像分解为8*8或16*16的块,然后对每个子块进行DCT 变换。

(2)将变换后得到的量化的DCT系数进行编码和传送形成压缩后电脑图像格式。

解压过程:
(1)对每个8*8或16*16的块进行DCT反变换。

(2)将每个反变换矩阵的块合成一个单一的图像。

余弦变换有把高度相关数据能量集中的趋势,DCT变换后能量集中在矩阵的左上角,右下角大多数DCT系数接近于0。

对于通常图像,舍弃这些系数,并不会对重构图像的画面质量带来显著下降。

所以利用DCT变换可以节约大量压缩空间。

压缩应在最合理近似原图像下使用最少系数。

相关文档
最新文档