當前位置:成語大全網 - 書法字典 - 如何用Matlab對灰度圖像進行霍夫曼編碼?

如何用Matlab對灰度圖像進行霍夫曼編碼?

給妳壹個方案,妳自己研究吧!\ x0d \ \ x0d \ CLC \ x0d \ clear \ x0d \ close all;\x0d\%將HufData/Len定義為全局變量的結構\ x0d \ globalHufData\x0d\globalLen\x0d\disp('計算機正在準備輸出霍夫曼編碼結果,請耐心等待?');\x0d\%原始碼字的灰度級\ x0d \ a = im read(' kids . TIF ');\x0d\\x0d\%分區繪制原始圖像和灰度直方圖\ x0d \ figure\x0d\subplot(1,2,1)\ x0d \ im show(a);\x0d\%取消軸和邊框\ x0d \ axis off \ x0d \ box off \ x0d \ title(' MATLAB自帶圖像',' fontsize ',13);\ x0d \支線劇情(1,2,2);\ x0d \ axis off \ x0d \ box off \ x0d \ im hist(a);\x0d\title('圖像灰度直方圖',' fontsize ',13);\x0d\%圖像的灰度統計\ x0d \ gray statistics = im hist(a);\ x0d \ gray statistics = gray statistics ';\ x0d \ gray ratio = gray statistics/sum(gray statistics);\ x0d \ gray ratio no = find(gray ratio ~ = 0);\ x0d \ Len = length(gray ratio no);\x0d\%初始化灰度設置,防止系統給它壹個垃圾值\ x0d \ grayratio = Ones (1,len);\ x0d \ \ x0d \ fori = 1:Len \ x0d \ gray ratio(I)= gray ratio(I);\ x0d \ end \ x0d \ \ x0d \ gray ratio = ABS(sort(-gray ratio));\x0d\%為結構\ x0d \ fori = 1:len \ x0d \ huf data(I)提供圖像灰度概率。值= gray ratio(I);\ x0d \ end \ x0d \%霍夫曼碼/霍夫曼編碼\ x0d \ Huffman code(Len);\x0d\%輸出碼字\ x0d \ x0d \ zipped Huffman = 1;\ x0d \ fori = 1:Len \ x0d \ tmpData = huf data(I)。代碼;\ x0d \ str =“”;\ x0d \ forj = 1:length(tmpData)\ x0d \ str = strcat(str,num 2 str(tmpData(j));\ x0d \ zipped Huffman = zipped Huffman+1;\x0d\end\x0d\disp(strcat('a ',num2str(i),' = ',str))\ x0d \ end \ x0d \ I;\x0d\%計算計算機輸出多少個霍夫曼代碼/霍夫曼編碼\ x0d \ zippedHuffman\x0d\%計算刪除前原始圖像的字節容量0灰度壓縮\ x0d \ unzipped _ delete = I * 8;\x0d\\x0d\%計算壓縮比\ x0d \ ratio _ delete = zipped Huffman/unzipped _ delete;\x0d\\x0d\%計算圖像的壓縮比\ x0d \ ad = num 2 str(ratio _ delete * 100);\x0d\str2=strcat(ad,' % ');\x0d\disp(strcat('霍夫曼編碼壓縮比',' = ',STR2)) \ x0d \ x0d \%子程序:霍夫曼編碼/霍夫曼編碼函數Huffman code . m \ x0d \ function Huffman code(原點大小)\ x0d \ globalhuffmancode。\ x0d \ global len \ x0d \ fori = 1:len \ x0d \ %霍夫曼編碼樹左邊的記錄是1 \ x0d \ hufdata (i)。左= 1;\x0d\%%霍夫曼編碼樹右側的記錄是0\x0d\HufData(i)。右= 0;\x0d\%%輸出代碼被初始化為0\x0d\HufData(i)。代碼=[];\x0d\%%排序列表初始化\x0d\SortList(i)。symbol = I;\x0d\SortList(i)。value=HufData(i)。價值;\x0d\end\x0d\%已初始化的原始郵件數\ x0d \ x0d \ newsymbol = OriginSize\ x0d \ forn = original size:-1:2 \ x0d \ % Sort n messages \ x0d \ Sort list = Sort data(Sort list,n);\x0d\%將出現概率最小的最後兩條消息合成壹條消息\ x0d \ new symbol = new symbol+1;\x0d\HufData(newsymbol)。值=排序列表(n-1)。值+排序列表(n)。價值;\x0d\HufData(newsymbol)。left=SortList(n-1)。符號;\x0d\HufData(newsymbol)。right=SortList(n)。符號;\x0d\%將消息添加到隊列的末尾,並為N-1消息的重新排序做準備\ x0d \ sortlist (n-1)。symbol = newsymbol\x0d\SortList(n-1)。value=HufData(newsymbol)。價值;\x0d\end\x0d\%遍歷霍夫曼樹獲取霍夫曼編碼/霍夫曼碼\x0d\visit(newsymbol,Len,[]);\ x0d \ end \ x0d \%子例程:冒泡排序函數sort data . m \ x0d \ functionredata = sort data(sort list,n) \ x0d \%根據消息概率排序\ x0d \ fork = n:-1。\x0d\sbl=SortList(j)。符號;\x0d\if(min0)\x0d\%遍歷左側分支觸點並輸出1。這裏子函數是嵌套的,調用\ x0d \ ocode 1 =[ocode 1];\x0d\visit(HufData(node))。left,n,ocode 1);\x0d\end\x0d\if(HufData(node)。右& gt0)\x0d\%遍歷右分支觸點,輸出0,其中子函數嵌套調用\ x0d \ ocode 2 =[ocode 0];\x0d\visit(HufData(node))。右,n,ocode 2);\x0d\end\x0d\end\x0d\end