对灰度图像进行霍夫曼编码,用Matlab怎么 *** 作?

对灰度图像进行霍夫曼编码,用Matlab怎么 *** 作?,第1张

给你一段程序,自己研究下吧!\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

function [h,l]=huffman(p)

if (length(find(p<0))~=0)

error('Not a prob,negative component')

end

if (abs(sum(p)-1)>10e-10)

error('Not a prob.vector,component do not add to 1')

end

n=length(p)

q=p

m=zeros(n-1,n)

for i=1:n-1

[q,l]=sort(q)

m(i,:)=[l(1:n-i+1),zeros(1,i-1)]

q=[q(1)+q(2),q(3:n),1]

end

for i=1:n-1

c(i,:)=blanks(n*n)

end

c(n-1,n)='0'

c(n-1,2*n)='1'

for i=2:n-1

c(n-i,1:n-1)=c(n-i+1,n*(find(m(n-i+1,:)==1))...

-(n-2):n*(find(m(n-i+1,:)==1)))

c(n-i,n)='0'

c(n-i,n+1:2*n-1)=c(n-i,1:n-1)

c(n-i,2*n)='1'

for j=1:i-1

c(n-i,(j+1)*n+1:(j+2)*n)=c(n-i+1,...

n*(find(m(n-i+1,:)==j+1)-1)+1:n*find(m(n-i+1,:)==j+1))

end

end

for i=1:n

h(i,1:n)=c(1,n*(find(m(1,:)==i)-1)+1:find(m(1,:)==i)*n)

ll(i)=length(find(abs(h(i,:))~=32))

end

l=sum(p.*ll)

h

l

参考资料:赵永强

Matlab自带Huffman函数(ps:你拼写错了)

huffmandeco Huffman decoder

huffmandict Generate Huffman code dictionary for a source with known probability model

huffmanenco Huffman encoder

密码生成:

symbols = [1 2 3]% Data symbols

p = [0.1 0.1 0.8]% Probability of each data symbol

dict = huffmandict(symbols,p) % Create the dictionary.

dict{1,:} % Show one row of the dictionary.

加密解密:

sig = repmat([3 3 1 3 3 3 3 3 2 3],1,50)% Data to encode

symbols = [1 2 3]% Distinct data symbols appearing in sig

p = [0.1 0.1 0.8]% Probability of each data symbol

dict = huffmandict(symbols,p)% Create the dictionary.

hcode = huffmanenco(sig,dict)% Encode the data.

dhsig = huffmandeco(hcode,dict)% Decode the code.


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11625335.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存