I=imread('lena.bmp')%待编码的矩阵
imshow(I)
thresh = graythresh(I)%自动确定二值化阈值
I2 = im2bw(I,thresh) %对图像二值化
imshow(I2)
[m,n]=size(I2) %计算矩阵大小
I2=double(I2)
p_table=tabulate(I2(:))%统计矩阵中元素出现的概率,第一列为矩阵元素,第二列为个数,第三列为概率百分数
color=p_table(:,1)'
p=p_table(:,3)'/100 %转换成小数表示的概率
psum=cumsum(p_table(:,3)')%计算数组各行的累加值
allLow=[0,psum(1:end-1)/100]%由于矩阵中元素只有两种,将[0,1)区间划分为两个区域allLow和 allHigh
allHigh=psum/100
numberlow=0 %定义算术编码的上下限numberlow和numberhigh
numberhigh=1
for k=1:m %以下计算算术编码的上下限,即编码结果
for kk=1:n
data=I2(k,kk)
low=allLow(data==color)
high=allHigh(data==color)
range=numberhigh-numberlow
tmp=numberlow
numberlow=tmp+range*low
numberhigh=tmp+range*high
end
end
fprintf('算术编码范围下限为%16.15f\n\n',numberlow)
fprintf('算术编码范围上限为%16.15f\n\n',numberhigh)
Mat=zeros(m,n) %解码
for k=1:m
for kk=1:n
temp=numberlow<low
temp=[temp 1]
indiff=diff(temp)
indiff=logical(indiff)
Mat(k,kk)=color(indiff)
low=low(indiff)
high=allHigh(indiff)
range=high - low
numberlow=numberlow-low
numberlow=numberlow/range
end
end
给你一段程序,自己研究下吧!\x0d\x0a\x0d\x0aclc\x0d\x0aclear\x0d\x0acloseall\x0d\x0a%定义HufData/Len为全局变量的结构体\x0d\x0aglobalHufData\x0d\x0aglobalLen\x0d\x0adisp('计算机正在准备输出哈夫曼编码结果,请耐心等待??')\x0d\x0a%原始码字的灰度\x0d\x0aa=imread('kids.tif')\x0d\x0a\x0d\x0a%分区画出原始图像和灰度直方图\x0d\x0afigure\x0d\x0asubplot(1,2,1)\x0d\x0aimshow(a)\x0d\x0a%取消坐标轴和边框\x0d\x0aaxisoff\x0d\x0aboxoff\x0d\x0atitle('MATLAB自带图像','fontsize',13)\x0d\x0asubplot(1,2,2)\x0d\x0aaxisoff\x0d\x0aboxoff\x0d\x0aimhist(a)\x0d\x0atitle('图像灰度直方图','fontsize',13)\x0d\x0a%图像的灰度统计\x0d\x0aGrayStatistics=imhist(a)\x0d\x0aGrayStatistics=GrayStatistics'\x0d\x0aGrayRatioo=GrayStatistics/sum(GrayStatistics)\x0d\x0aGrayRatioNO=find(GrayRatioo~=0)\x0d\x0aLen=length(GrayRatioNO)\x0d\x0a%初始化灰度集,防止系统随即赋予其垃圾值\x0d\x0aGrayRatio=ones(1,Len)\x0d\x0a\x0d\x0afori=1:Len\x0d\x0aGrayRatio(i)=GrayRatioo(i)\x0d\x0aend\x0d\x0a\x0d\x0aGrayRatio=abs(sort(-GrayRatio))\x0d\x0a%将图像灰度概率赋予结构体\x0d\x0afori=1:Len\x0d\x0aHufData(i).value=GrayRatio(i)\x0d\x0aend\x0d\x0a\x0d\x0a%哈夫曼编码/霍夫曼编码\x0d\x0aHuffmanCode(Len)\x0d\x0a%输出码字\x0d\x0a\x0d\x0azippedHuffman=1\x0d\x0afori=1:Len\x0d\x0atmpData=HufData(i).code\x0d\x0astr=''\x0d\x0aforj=1:length(tmpData)\x0d\x0astr=strcat(str,num2str(tmpData(j)))\x0d\x0azippedHuffman=zippedHuffman+1\x0d\x0aend\x0d\x0adisp(strcat('a',num2str(i),'=',str))\x0d\x0aend\x0d\x0ai\x0d\x0a%计算计算机一共输出多少个哈夫曼编码/霍夫曼编码\x0d\x0azippedHuffman\x0d\x0a%计算在删去0灰度级压缩之前的原始图像字节容量\x0d\x0aunzipped_delete=i*8\x0d\x0a\x0d\x0a%计算压缩比率\x0d\x0aratio_delete=zippedHuffman/unzipped_delete\x0d\x0a\x0d\x0a%计算图像的压缩比率\x0d\x0aad=num2str(ratio_delete*100)\x0d\x0astr2=strcat(ad,'%')\x0d\x0adisp(strcat('哈夫曼编码压缩比率','=',str2))\x0d\x0a\x0d\x0a%子程序:哈夫曼编码/霍夫曼编码函数HuffmanCode.m\x0d\x0afunctionHuffmanCode(OriginSize)\x0d\x0aglobalHufData\x0d\x0aglobalLen\x0d\x0afori=1:Len\x0d\x0a%%霍夫曼编码树左边纪录为1\x0d\x0aHufData(i).left=1\x0d\x0a%%霍夫曼编码树右边纪录为0\x0d\x0aHufData(i).right=0\x0d\x0a%%输出码初始化为0\x0d\x0aHufData(i).code=[]\x0d\x0a%%排序列表初始化\x0d\x0aSortList(i).symbol=i\x0d\x0aSortList(i).value=HufData(i).value\x0d\x0aend\x0d\x0a%初始化原始消息数目\x0d\x0anewsymbol=OriginSize\x0d\x0aforn=OriginSize:-1:2\x0d\x0a%将N个消息进行排序\x0d\x0aSortList=sortdata(SortList,n)\x0d\x0a%将最后两个出现概率最小的消息合成一个消息\x0d\x0anewsymbol=newsymbol+1\x0d\x0aHufData(newsymbol).value=SortList(n-1).value+SortList(n).value\x0d\x0aHufData(newsymbol).left=SortList(n-1).symbol\x0d\x0aHufData(newsymbol).right=SortList(n).symbol\x0d\x0a%将消息添加到列队的最后,为N-1个消息重新排序作好准备\x0d\x0aSortList(n-1).symbol=newsymbol\x0d\x0aSortList(n-1).value=HufData(newsymbol).value\x0d\x0aend\x0d\x0a%遍历霍夫曼树,获得霍夫曼编码/哈夫曼编码\x0d\x0avisit(newsymbol,Len,[])\x0d\x0aend\x0d\x0a\x0d\x0a%子程序:冒泡排序法函数sortdata.m\x0d\x0afunctionreData=sortdata(SortList,n)\x0d\x0a%根据消息概率进行排序\x0d\x0afork=n:-1:2\x0d\x0aforj=1:k-1\x0d\x0amin=SortList(j).value\x0d\x0asbl=SortList(j).symbol\x0d\x0aif(min<SortList(j+1).value)\x0d\x0aSortList(j).value=SortList(j+1).value\x0d\x0aSortList(j+1).value=min\x0d\x0aSortList(j).symbol=SortList(j+1).symbol\x0d\x0aSortList(j+1).symbol=sbl\x0d\x0aend\x0d\x0aend\x0d\x0aend\x0d\x0areData=SortList\x0d\x0aend\x0d\x0a\x0d\x0a%子程序:遍历哈夫曼编码/霍夫曼编码树搜索函数visit.m\x0d\x0afunctionvisit(node,n,ocode)\x0d\x0aglobalHufData\x0d\x0aifnode0)\x0d\x0a%遍历左分支接点输出1,这里采用子函数嵌套调用\x0d\x0aocode1=[ocode1]\x0d\x0avisit(HufData(node).left,n,ocode1)\x0d\x0aend\x0d\x0aif(HufData(node).right>0)\x0d\x0a%遍历右分支接点输出0,这里采用子函数嵌套调用\x0d\x0aocode2=[ocode0]\x0d\x0avisit(HufData(node).right,n,ocode2)\x0d\x0aend\x0d\x0aend\x0d\x0aend欢迎分享,转载请注明来源:内存溢出
评论列表(0条)