资源描述:
《matlab下rle算法的简单实现》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、BY:mayadong7349游程长度编码编码函数:functionret_dat=rle_encode(in_dat)%Runlengthencoding%PCXisakindofimagetypeinwhichRLEiswidelyused.%Compressanimagebyusingrun-length-encodingalgorithm.%BY:mayadong73492011-12-10ret_dat=int32([]);ifndims(in_dat)==3,%RGBisgivenerror('Unsu
2、pportedimagetype.');end;%nothingtodoforintensityimage,indexedimagein_dat=in_dat';in_dat=in_dat(:);len=length(in_dat);c=1;whilec<=len,pix_dat=in_dat(c);count=0;while(c<=len)&&(in_dat(c)==pix_dat),count=count+1;c=c+1;end;ret_dat=[ret_dat,pix_dat,count];end;end%e
3、ndoffunctionrle_encode解码函数:functionret_dat=rle_decode(in_dat,lines,cols,dat_type)%Runlengthdecoding%Uncompressanimagewhichisencodedbyusingrun-length-encodingalgorithm.%BY:mayadong73492011-12-10ret_dat=int32([]);[height,width]=size(in_dat);ifheight~=1,BY:mayado
4、ng7349error('Unsupportedinputdatatype.');elseifmod(width,2)~=0,error('Unsupportedinputdatatype.');end;if~strcmp(dat_type,'uint8')&&~strcmp(dat_type,'logical'),error('Cannotrecognisetheinputdatatype.');endc=1;forindex=1:2:widthpix_count=in_dat(index+1);forn=1:p
5、ix_countret_dat(c)=in_dat(index);c=c+1;endendret_dat=reshape(ret_dat,cols,lines);ret_dat=ret_dat';switchdat_typecase'uint8'ret_dat=uint8(ret_dat);case'logical'ret_dat=logical(ret_dat);endend%endoffunctionrle_decode测试脚本:%examplecloseall;clear;clc;warningoffall;
6、forcount=1:4,ifcount==1,filename='bw_logical.bmp';elseifcount==2,filename='bw_gray.bmp';elseifcount==3,filename='rice_logical.bmp';elseifcount==4,filename='rice_gray.bmp';BY:mayadong7349end;[img_data,map]=imread(filename);[h,w]=size(img_data);time_elapsed=cput
7、ime;arr_encode=rle_encode(img_data);img_data_decode=rle_decode(arr_encode,h,w,class(img_data));time_elapsed=cputime-time_elapsed;figure;imshow(img_data,map);figure;imshow(img_data_decode,map);drawnow;disp([filename,'压缩比例',num2str(length(arr_encode)/length(img_
8、data(:)))]);disp(['对',filename,'进行编码、解码,花费总时间为',num2str(time_elapsed),'秒']);end;%a=[0,0,0,0,0,0,0,0,0,0;...0,0,0,1,1,1,1,1,1,1;...1,1,1,1,1,1,0,0,0,0];a=logical(a)[h,w]=size(a);a_e