【图像重建】霍夫曼图像重建(带面板)【含GUI Matlab源码 1168期】

在这里插入图片描述

⛄一、霍夫曼图像重建简介

1 案例背景
一幅普通的未经压缩的图片可能需要占几兆的存储空间,一个时长仅为1秒的未经压缩的视频文件所占的存储空间甚至能达到上百兆字节,这给普通PC的存储空间和常用网络的传输带宽带来了巨大的压力。其中,静止图像是不同媒体的构建基础,对其进行压缩不仅是各种媒体压缩和传输的基础,其压缩效果也是影响媒体压缩效果好坏的关键因素。基于这种考虑,本案例主要研究静止图像的压缩技术。随着人们对图像压缩技术的重视,目前已经提出了多种压缩编码方法。如果以不同种类的媒体信息为处理对象,则每种压缩编码方法都有其自身的优势和特点,如编码复杂度和运行效率的改善、解码正确性的提高、图像恢复的质量提升等"。特别是,随着互联网信息量的不断增大,高效能信息检索的质量也与压缩编码方法存在越来越紧密的联系。从发展的现状来看,采用分形和小波混合图像编码方法能充分发挥小波和分形编码的优点,弥补相互的不足,因此成为图像压缩的一个重要研究方向,但是依然存在某些不足之处,有待进一步提高。

2 理论基础
压缩,根据编码前后数据损失的角度可分为无损压缩编码和有损压缩编码;根据已知编码方法分类的实用性角度可分为统计编码、预测编码和变换编码。所谓无损压缩,就是利用数据的统计冗余信息进行压缩,且能够在不引起任何失真的前提下完全恢复原始数据。无损压缩法广泛用于文本、程序和特殊应用场合的图像数据(如指纹图像、医学图像等) 的压缩。常用的无损压缩编码方法有香农(Shannon-Fano) 编码、霍夫曼(Huffman)编码、行程(Run-length) 编码、LZW(Lempel-Ziv-Welch) 编码和算术编码等。霍夫曼编码完全依据字符出现的概率来构造异字头的平均长度最短的码字,有时称之为最佳编码。霍夫曼编码将使用次数较多的代码用长度较短的编码代替,将使用次数较少的代码用较长的编码代替,并且确保编码的唯一可解性。其根本原则是压缩编码的长度(即字符的统计数字x字符的编码长度)最小,也就是权值和最小。

3 霍夫曼编码的步骤
霍夫曼图像压缩重建,过程为:1.符号概率 2.合并概率 3.更新概率 4.分配码字 。
霍夫曼编码是一种无损压缩方法,其一般算法如下。
(1)符号概率
统计信源中各符号出现的概率,按符号出现的概率从大到小排序。
(2)合井概率
提取最小的两个概率并将其相加,合并成新的概率,再将新概率与剩余的概率组成新的概率集合,
(3)更新概率
将合并后的概率集合重新排序,再次提取其中最小的两个概率,相加得到新的概率,进而组成新的概率集合。如此重复进行,直到剩余最后两个概率之和为1.
(4)分配码字
分配码字从最后一步开始逆向进行,对于每次相加的两个概率,大概率赋0,小概率赋1,当然也可以反过来赋值,即大概率赋1,小概率赋0;特别是,如果两个概率相等,则从中任选一个赋0,另一个赋1.依次重复该步骤,从第一次赋值开始进入循环处理,直到最后的码字概率和为1时结束。将中间过程中所遇到的0和1按从最低位到最高位的顺序排序,就得到了符号的霍夫曼编码。

4 霍夫曼编码的特点
霍夫曼编码是最佳的变长编码,其特点如下回。
(1)可重复性
霍夫曼编码不唯一,具有可重复性。
(2)效率差异性
霍夫曼编码对于不同的信源往往具有不同的编码效率,具有效率差异性。
(3)不等长性
霍夫曼编码的输出内容不等长,因此给硬件实现带来一定的困难,也在一定程度上造成了误码传播的严重性。
(4)信源依赖性
霍夫曼编码的运行效率往往要比其他编码算法高,是最佳的变长编码。但是,霍夫曼编码以信源的统计特性为基础,必须先统计信源的概率特性才能编码,因此具有对信源的依赖性,这也在一定程度上限制了霍夫曼编码的实际应用。
如图所示是一个霍夫曼编码的例子。从图中二叉树的形状分步可以发现,符号只能出现在树叶上,且每个字符的路径都不允许是其他字符路径的前缀路径,因此能够成功构造前缀编码。该二叉树在数据结构中被称为霍夫曼树,一般应用于最佳判定的场景,是最优二叉树的一种,而且带权路径长度最短。其中,二叉树的带权路径长度由树中所有叶节点的权值乘以其到根节点的路径长度来获得,假设根节点为0层,则叶节点到根节点的路径长度就是叶节点的层数值。因此, 二叉树的带权路径长度记作WPL, 其计算公式为:WPL=(WxL+Wx Ly+…+W、xLy) .如果由N个权值W(i=1, 2, …, n) 构成的一棵包含N个节点的二叉树,则相应树节点的路径长度为L(i=1,2,…,n),选择霍夫曼编码得出的
WPL值最小。
在这里插入图片描述
图13-1霍夫曼编码实例
在实际应用中,霍夫曼编码在预处理之前需要知道叶节点(即信源数据符号)的概率,这往往会对实时性要求较高的应用场景带来困扰。因此,人们在实时编码的应用场景中往往会采用所谓的准可变字长码,如采用双字长编码,并且通过从短码集合中选出一个码字,作为长码字头,进而保证码字的非续长特性2]。此外,在数字图像通信中所使用的三类传真机中包含的MH码采用了多字长VLC技术, 该技术根据一系列标准图像进行统计数据分析而得出,通过预先在其IC芯片中加入号码表,使得实际的编码解码过程简化为一个查表过程,从而确保了高速、实时应用场景的需要。

⛄二、部分源代码

function varargout = MainForm(varargin)
% MAINFORM MATLAB code for MainForm.fig
% MAINFORM, by itself, creates a new MAINFORM or raises the existing
% singleton*.
%
% H = MAINFORM returns the handle to a new MAINFORM or the handle to
% the existing singleton*.
%
% MAINFORM(‘CALLBACK’,hObject,eventData,handles,…) calls the local
% function named CALLBACK in MAINFORM.M with the given input arguments.
%
% MAINFORM(‘Property’,‘Value’,…) creates a new MAINFORM or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before MainForm_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to MainForm_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE’s Tools menu. Choose “GUI allows only one
% instance to run (singleton)”.
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help MainForm

% Last Modified by GUIDE v2.5 19-Mar-2022 11:20:45

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(‘gui_Name’, mfilename, …
‘gui_Singleton’, gui_Singleton, …
‘gui_OpeningFcn’, @MainForm_OpeningFcn, …
‘gui_OutputFcn’, @MainForm_OutputFcn, …
‘gui_LayoutFcn’, [] , …
‘gui_Callback’, []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% — Executes just before MainForm is made visible.
function MainForm_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to MainForm (see VARARGIN)

% Choose default command line output for MainForm
handles.output = hObject;
clc;
% axis(handles.axes1); cla reset; box on;
% set(gca, ‘XTickLabel’, ‘’, ‘YTickLabel’, ‘’);
%
% axis(handles.axes2); cla reset; box on;
% set(gca, ‘XTickLabel’, ‘’, ‘YTickLabel’, ‘’);
handles.Img = 0;
handles.ImgH = 0;
handles.ImgL = 0;
handles.strH = 0;
handles.strL = 0;
InitFig(hObject,handles);
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes MainForm wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% — Outputs from this function are returned to the command line.
function varargout = MainForm_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% --------------------------------------------------------------------
function File_Callback(hObject, eventdata, handles)
% hObject handle to File (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------
function OpenFile_Callback(hObject, eventdata, handles)
% hObject handle to OpenFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 打开
warning off all;
% 载入图像
[FileName,PathName,FilterIndex] = uigetfile({‘.bmp;.jpg;.tif;.png;.gif’, …
‘所有图像文件’;…
'
.*’,‘所有文件’ },‘载入图像’,…
‘.\images\lena.bmp’);
if isequal(FileName, 0) || isequal(PathName, 0)
return;
end
InitFig(hObject, handles);
Img = imread(fullfile(PathName, FileName));
axes(handles.axes1);
imshow(Img, []);
handles.Img = Img;
handles.ImgH = 0;
handles.ImgL = 0;
guidata(hObject, handles);

function textInfo_Callback(hObject, eventdata, handles)
% hObject handle to textInfo (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,‘String’) returns contents of textInfo as text
% str2double(get(hObject,‘String’)) returns contents of textInfo as a double

% — Executes during object creation, after setting all properties.
function textInfo_CreateFcn(hObject, eventdata, handles)
% hObject handle to textInfo (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,‘BackgroundColor’), get(0,‘defaultUicontrolBackgroundColor’))
set(hObject,‘BackgroundColor’,‘white’);
end

% --------------------------------------------------------------------
function SnapImg_Callback(hObject, eventdata, handles)
% hObject handle to SnapImg (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 截图
SnapImage();

% --------------------------------------------------------------------
function SaveImg_Callback(hObject, eventdata, handles)
% hObject handle to SaveImg (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 保存
if isequal(handles.ImgH, 0) && isequal(handles.ImgL, 0)
msgbox(‘请进行压缩处理!’, ‘提示信息’);
return;
end
if ~isequal(handles.ImgH, 0)
SaveImage(handles.ImgH);
end
if ~isequal(handles.ImgL, 0)
SaveImage(handles.ImgL);
end
msgbox(‘处理结果保存成功!’, ‘提示信息’);

% --------------------------------------------------------------------
function Exit_Callback(hObject, eventdata, handles)
% hObject handle to Exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close();

% --------------------------------------------------------------------
function CompressAlgorithm_Callback(hObject, eventdata, handles)
% hObject handle to CompressAlgorithm (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------
function Huffman_Callback(hObject, eventdata, handles)
% hObject handle to Huffman (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isequal(handles.Img, 0)
msgbox(‘请载入图像!’, ‘提示信息’);
return;
end
if isequal(handles.ImgH, 0)

    dataImg = rgb2gray(dataImg);
end
sz = size(dataImg);
[zipped,info] = Mat2Huff(dataImg);
unzipped = Huff2Mat(zipped, info);
dataImg = double(reshape(dataImg, sz));
unzipped = double(reshape(unzipped, sz));
info_dataImg = whos('dataImg');
info_zipped = whos('zipped');
info_unzipped = whos('unzipped');
S = PSNR(dataImg, unzipped);
str0 = sprintf('-----------------霍夫曼压缩-----------------\n');
str1 = sprintf('原始数据维数为:%s,占用空间大小为:%d\n', num2str(info_dataImg.size), info_dataImg.bytes);
str2 = sprintf('解压数据维数为:%s,占用空间大小为:%d\n', num2str(info_unzipped.size), info_unzipped.bytes);
str3 = sprintf('压缩数据维数为:%s,占用空间大小为:%d\n', num2str(info_zipped.size), info_zipped.bytes);
str4 = sprintf('压缩比为:%.3f%%\n', info_zipped.bytes/info_dataImg.bytes*100);
str5 = sprintf('PSNR:%.3f\n', S);
strH = [str0 str1 str2 str3 str4 str5];
set(handles.textInfo, 'String', strH);
 
handles.strH = strH;
handles.ImgH = tm;
guidata(hObject, handles);

else
set(handles.textInfo, ‘String’, handles.strH);
axes(handles.axes2); imshow(mat2gray(handles.ImgH), []);
end

⛄三、运行结果

在这里插入图片描述

⛄四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1]蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.

3 备注
简介此部分摘自互联网,仅供参考,若侵权,联系删除

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matlab领域

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值