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

灰度圖像的霍夫曼編碼,如何用Matlab實現?

給妳壹個程序,妳自己研究吧!\ x0d \ \ x0d \ CLC \ x0d \ clear \ x0d \ close all;\x0d\%將HufData/Len定義為全局變量的結構\ x0d \ globalHufData\ x0d \ global len \ 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 \ boxoff \ 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 = imhist(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 \ gray ratio = 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(tmp data)\ x0d \ str = strcat(str,num 2 str(tmp data(j)));\ x0d \ zipped Huffman = zipped Huffman+1;\ x0d \ end \ x0d \ disp(strcat(‘a‘,num2 str(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 \ str 2 = strcat(ad,‘%‘);\ x0d \ disp(strcat(‘霍夫曼編碼壓縮比‘,‘=‘,str 2))\ x0d \ x0d \ %子程序:霍夫曼編碼/霍夫曼編碼函數huffmancode.m \ x0d \ FunctionHuffman霍夫曼碼(origin size)\ x0d \ globalhuffman code。\ x0d \ global len \ x0d \ fori = 1:len \ x0d \ %霍夫曼編碼樹左邊的記錄是1 \ x0d \ huf data(I)。left = 1;\x0d\%%霍夫曼編碼樹右側的記錄是0 \ x0d \ huf data(I)。right = 0;\x0d\%%輸出代碼被初始化為0 \ x0d \ huf data(I)。代碼=【】;\x0d\%%排序列表初始化\x0d\排序列表(I)。symbol = I;\ x0d \排序列表(I)。value = huf data(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 \ huf data(new symbol)。值=排序列表(n-1)。值+排序列表(n)。價值;\ x0d \ huf data(new symbol)。left =排序列表(n-1)。符號;\ x0d \ huf data(new symbol)。right = sort list(n)。符號;\x0d\%將郵件添加到隊列末尾,並準備對N-1封郵件進行重新排序\ x0d \ sort list(N-1)。symbol = newsymbol\ x0d \ sort list(n-1)。value = huf data(new symbol)。價值;\x0d\end\x0d\%遍歷霍夫曼樹以獲取霍夫曼編碼/霍夫曼代碼\ x0d \ visit(new symbol,Len,【】);\ x0d \ end \ x0d \%子例程:冒泡排序函數sort data . m \ x0d \ functionredata = sort data(sort list,n)\ x0d \ %根據消息概率排序\ x0d \ fork = n:-1。\ x0d \ sbl = sort list(j)。符號;\ x0d \ if(min & lt;排序列表(j+1)。value)\ x0d \ sort list(j)。value = sort list(j+1)。價值;\ x0d \ sort list(j+1)。value = min\ x0d \ sort list(j)。symbol = sort list(j+1)。符號;\ x0d \ sort list(j+1)。symbol = sbl\ x0d \ end \ x0d \ end \ x0d \ end \ x0d \ reData = sort list;\ x0d \ end \ x0d \%子程序:遍歷霍夫曼編碼/霍夫曼編碼樹搜索函數訪問。m \ x0d \ function visit(node,n,ocode)\ x0d \ globalhufdata \ x0d \ if node 0)\ x0d \ %遍歷左分支。\ x0d \ visit(huf data(node)。left,n,ocode 1);\ x0d \ end \ x0d \ if(huf data(node)。右& gt0)\ x0d \ %遍歷右分支聯系人並輸出0,其中子函數嵌套調用\ x0d \ ocode 2 =【ocode 0】;\ x0d \ visit(huf data(node)。right,n,ocode 2);\x0d\end\x0d\end\x0d\end