CHƯƠNG TRÌNH nén ẢNH KHÔNG tổn HAO dựa TRÊN THUẬT HUFFMAN FULL

14 601 4
CHƯƠNG TRÌNH nén ẢNH KHÔNG tổn HAO dựa TRÊN THUẬT HUFFMAN FULL

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

chương trình nén ảnh không tổn hao dựa trên thuật Huffman kèm theo giao diện chương trình và code thuật huffman full chi tiết...Một số chương trình hiện nay chưa đáp ứng được yêu cầu trong khi các bạn làm tiểu luận hay đồ án, tài liệu này được tổng hợp từ rất nhiều nguồn cùng với vốn hiểu biết của tg hy vọng sẽ là tài liệu bổ ích cho các bạn sinh viên học tập nghiên cứu

Chương Trình Nén/Giải Nén không tổn hao dựa trên thuật Huffman Giao diện chương trình : Giải Thuật của chương trình :  Nút Select Image -> chọn ảnh cần mã hóa Code : % Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global S; global FileSize; % Chon file giainen [filename,pathname]=uigetfile({ '*.tif;*.bmp;*.gif;*.jpg' }, 'Select Image' ); S=imread([pathname,filename]); FileInfo = dir([pathname,filename]); FileSize = FileInfo.bytes; % Hien thi giainen vua chon len anhchon axes(handles.anhchon); imshow(S); set(handles.Status, 'String' ,strcat('Da chon anh: "' ,filename, '"' )); set(handles.Status, 'ForegroundColor' ,[0 0 1]); % Chuyen bien S vao workspace assignin('base' , 'anh_goc' ,S); Giao diện sau khi chọn ảnh : Khi nhấn nút Select Image để chọn ảnh thì ở workspace của matlab sẽ xuất hiện thêm biến anh_goc.biến này chứa ma trận điểm ảnh và giá trị thang xám của nó.  Nút Creat Huffman có chức năng tạo mã nén Huffman dựa trên thống kê xác xuất của các thang xám trong ma trận S. Code : function pushbutton6_Callback(hObject, eventdata, handles) % hObject handle to pushbutton6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global S; global table; global tree; global y; % Chuyen file giainen S ve dang ma tran 1xN va dinh nghia lai dang cell y_pixel = S(:)'; y = reshape(cellstr(num2str(y_pixel(:))), size(y_pixel)); % Lay tat ca cac Symbol co trong y Symbol_y = unique(y); % Tinh xac xuat cua tung Symbol trong y tmp = unique(y_pixel); for i = 1:1:length(tmp) t(i) = length(find(y_pixel==tmp(i))); end Prob_y = t/length(y_pixel); % Tao bang ma Huffman [tree, table] = hufftree(Symbol_y,Prob_y); set(handles.Status, 'String' , 'Tao bang ma thanh cong !' ); set(handles.Status, 'ForegroundColor' ,[0 0 1]); assignin('base' , 'Prob_y' ,Prob_y); assignin('base' , 'Symbol_y' ,Symbol_y); assignin('base' , 'table' ,table); % hufftree.m % % given alphabet and probabilities: create huffman-tree function [tree, table] = hufftree(alphabet,prob) for l=1:length(alphabet) % create a vector of nodes (leaves), one for each letter leaves(l).val = alphabet{l}; leaves(l).zero= ''; leaves(l).one=''; leaves(l).prob = prob(l); end % combine the two nodes with lowest probability to a new node with the summed prob. % repeat until only one node is left while length(leaves)>1 [dummy,I]=sort(prob); prob = [prob(I(1))+prob(I(2)) prob(I(3:end))]; node.zero = leaves(I(1)); node.one = leaves(I(2)); node.prob = prob(1); node.val = ''; leaves = [node leaves(I(3:end))]; end % pass through the tree, % remove unnecessary information % and create table recursively (depth first) table.val={}; table.code={}; [tree, table] = descend(leaves(1),table,''); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% function [tree, table] = descend(oldtree, oldtable, code) table = oldtable; if(~isempty(oldtree.val)) tree.val = oldtree.val; table.val{end+1} = oldtree.val; table.code{end+1} = code; else [tree0, table] = descend(oldtree.zero, table, strcat(code,'0')); [tree1, table] = descend(oldtree.one, table, strcat(code,'1')); tree.zero=tree0; tree.one= tree1; end Giao diện chương trình : Giao diện tại workSpase : Workspace xuất hiện thêm các biến mới :  Symbol_y là ma trận chứa giá trị của các thang xám  Prob_y là xác suất xuất hiện của thang xám tương ứng trong ảnh Table có 2 kiểu cấu trúc, sẽ chứa 2 giá trị tương ứng là thang xám và mã Huffman  Nút Encode có chức năng mã hóa thông tin ảnh và tính tỉ số nén dựa trên cây Huffman đã xây dựng. Code : function pushbutton7_Callback(hObject, eventdata, handles) % hObject handle to pushbutton7 (see GCBO)global S; % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global S; global y; global table; global code; code = huffencode(y,table); set(handles.Status, 'String' , 'Nen thanh cong !' ); set(handles.Status, 'ForegroundColor' ,[0 0 1]); assignin('base' , 'code' ,code); % So bit cua code size_compressed = length(code); [width,height,rgb] = size(S); pixels_image = width * height * rgb; Bitrate = size_compressed / pixels_image; % Tinh so bit cua giainen goc filesize = pixels_image * 8; Comp_ratio = ( filesize / size_compressed ); set(handles.Bitrate, 'String' ,Bitrate); set(handles.file_ratio, 'String' ,Comp_ratio); % huffencode.m % % takes a cell-vector and a huffman-table % returns a huffman encoded bit-string function bitstring = huffencode(input, table) bitstring = ''; for l=1:length(input) for j = 1:length(table.val) if (strcmp(table.val{j},input{l})) bitstring = strcat(bitstring,table.code{j}); % omits letters that are not in alphabet end; end; end; Giao diện chương trình : [...]...Lệnh Encode được thực thi thì trên giao diện xuất hiện tỉ số nén và trên workspace xuất hiện thêm biến code chứa chuỗi bít mã hóa từ các giá trị thang xám Nút Decode có chức năng giải mã dựa trên code mã hóa (image_code) và cây Huffman đã xây dựng Code : % - Executes on button press in pushbutton8 function pushbutton8_Callback(hObject,... function message = huffmandeco(bitstring, tree) treepos = tree; counter = 1; for l=1:length(bitstring) if(bitstring(l) == '1') treepos = treepos.one; else treepos = treepos.zero; end if(isfield(treepos,'val')) message{counter} = treepos.val; counter = counter+1; treepos = tree; end end Giao diên Chương trình : Cửa sổ WorkSpace xuất hiện thêm các biến mới lưu trữ ma trận điểm ảnh sau khi giải nén ... to pushbutton8 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global code; global S; global tree; decoded = huffmandeco(code,tree); for i=1:length(decoded) matrix(i) = str2num(decoded{i}); end matrix_pixel = reshape(matrix, size(S)); image_decode = uint8(matrix_pixel); assignin('base' ,'matrix',matrix); assignin('base' . Chương Trình Nén/ Giải Nén không tổn hao dựa trên thuật Huffman Giao diện chương trình : Giải Thuật của chương trình :  Nút Select Image -> chọn ảnh cần mã hóa Code : %. tương ứng trong ảnh Table có 2 kiểu cấu trúc, sẽ chứa 2 giá trị tương ứng là thang xám và mã Huffman  Nút Encode có chức năng mã hóa thông tin ảnh và tính tỉ số nén dựa trên cây Huffman đã xây. that are not in alphabet end; end; end; Giao diện chương trình : Lệnh Encode được thực thi thì trên giao diện xuất hiện tỉ số nén và trên workspace xuất hiện thêm biến code chứa chuỗi bít

Ngày đăng: 08/05/2015, 18:22

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan