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

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

functionjpeg%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%THISWORKISSUBMITTEDBY:%%%%OHADGAL%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%closeall;%==================%section1.2+1.3%==================%thefollowinguseofthefunction:%%plot_bases(base_size,resolution,plot_type)%%willplotthe64wantedbases.Iwillusezero-paddingforincreasedresolution%NOTETHATTHESEARETHESAMEBASES!%forreferenceIplotthefollowing3graphs:%a)3Dplotwithbasicresolution(64plotsof8x8pixels)usingsurffunction%b)3Dplotwithx20resolution(64plotsof160x160pixels)usingmeshfunction%c)2Dplotwithx10resolution(64plotsof80x80pixels)usingmeshfunction%d)2Dplotwithx10resolution(64plotsof80x80pixels)usingimshowfunction%%NOTE:matrixsizeofpictures(b),(c)and(d),cansupporthigherfrequency=higherbases%butIamnotaskedtodrawthese(higherbases)inthissection!%thezeropaddingisusedONLYforresolutionincrease!%%getallbasepictures(3Dsurfacefigure)plot_bases(8,1,'surf3d');%getallbasepictures(3Dsurfacefigure),x20resolutionplot_bases(8,20,'mesh3d');%getallbasepictures(2Dmeshfigure),x10resolutionplot_bases(8,10,'mesh2d');%getallbasepictures(2Dmeshfigure),x10resolutionplot_bases(8,10,'gray2d');%==================%section1.4+1.5%==================%foreachpicture{'0'..'9'}performa2dimensionaldcton8x8blocks.%savethedctinsideacellofthesize:10cellsof128x128matrix%showforeachpicture,it'sdct8x8blocktransform.foridx=0:9%loadapictureswitchidxcase{0,1},input_image_128x128=im2double(imread(sprintf('%d.tif',idx),'tiff'));otherwise,input_image_128x128=im2double(imread(sprintf('%d.tif',idx),'jpeg'));end%performDCTin2dimensionoverblocksof8x8inthegivenpicturedct_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('8x8DCTofimage#%d',idx));end%==================%section1.6%==================%dostatisticsonthecellarrayofthedcttransforms%createamatrixof8x8thatwilldescribethevalueofeachdct-base%overthetransformofthe10givenpictures.sincesomeofthevaluesare%negative,andweareinterestedintheenergyofthecoefficients,wewill%addtheabs()^2valuesintothematrix.%thisisconsistentwiththedefinitionoftheParsevalrelationinFourierCoefficients%initializetheaveragematrixmean_matrix_8x8=zeros(8,8);%loopoverallthepicturesforidx=1:10%ineachpictureloopover8x8elements(128x128=256*8x8elements)form=0:15forn=0:15mean_matrix_8x8=mean_matrix_8x8+...abs(dct_8x8_image_of_128x128{idx}(m*8+[1:8],n*8+[1:8])).^2;endendend%transposethematrixsincetheorderofthematrixiselementsalongthecolumns,%whileinthesubplotfunctiontheorderisofelementsalongtherowsmean_matrix_8x8_transposed=mean_matrix_8x8';%makethemeanmatrix(8x8)intoavector(64x1)mean_vector=mean_matrix_8x8_transposed(:);%sortthevector(fromsmalltobig)[sorted_mean_vector,original_indices]=sort(mean_vector);%reverseorder(frombigtosmall)sorted_mean_vector=sorted_mean_vector(end:-1:1);original_indices=original_indices(end:-1:1);%plotthecorrespondingmatrixasaskedinsection1.6figure;foridx=1:64subplot(8,8,original_indices(idx));axisoff;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%addatitletothefiguresubplot(8,8,4);h=title('PowerofDCTcoefficients(section1.6)');set(h,'FontWeight','bold');%==================%section1.8%==================%picture8ischosen%InthissectionIwillcalculatetheSNRofacompressedimageagaints%thelevelofcompression.theSNRcalculationisdefinedintheheader%ofthefunction:calc_snrwhichisgivenbelow.%%ifwedecidetotake10coefficientswiththemostenergy,wewilladd%zerostotheothercoefficientsandremainwithavector64elementslong%(oramatrixof8x8)%loadtheoriginalimageoriginal_image=im2double(imread('8.tif','jpeg'));%Iwillusethismatrixtochooseonlythewantednumberofcoefficients%thematrixisinitializedtozeros-don'tchooseanycoefficientatallcoef_selection_matrix=zeros(8,8);%compressedpictureset(toshowthedegrading)compressed_set=[1351015203040];%thisloopwillchooseeachtime,thenext-most-energeticcoefficient,%tobeaddedtothecompressedimage-andthustoimproovetheSNRfornumber_of_coefficient=1:64%findthemostenergeticcoefficientfromthemean_matrix[y,x]=find(mean_matrix_8x8==max(max(mean_matrix_8x8)));%selectifforthecompressedimagecoef_selection_matrix(y,x)=1;%replicatetheselectionmatrixforallthepartsofthedcttransform%(rememberthattheDCTtransformcreatesasetof8x8matrices,where%ineachmatrixIneedtochoosethecoefficientsdefinedbythe%coef_selection_matrixmatrix)selection_matrix=repmat(coef_selection_matrix,16,16);%setitaszerointhemean_matrix,sothatinthenextloop,wewill%choosethenext-most-energeticcoefficientmea

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功