当前位置:网站首页>【第 13 章 基于霍夫曼图像压缩重建--Matlab深度学习实战图像处理应用】
【第 13 章 基于霍夫曼图像压缩重建--Matlab深度学习实战图像处理应用】
2022-06-22 01:02:00 【海宝7号】
基于霍夫曼图像压缩重建
部分参考来源:
https://blog.csdn.net/qq_59747472/article/details/121890265
为了节省空间,在对数据进行编码时,可以对那些经常出现的数据指定较少的位数表示, 而那些不常出现的数据指定较多的位数表示,从而降低冗余,这样从总的效果看就节省了存储空间。基于哈夫曼编码图像压缩的基本原理是频繁使用的数据用较短的代码代替,较少使用的数据用较长的代码代替,每个数据的代码各不相同,这是一种典型的无损编码方式。
部分函数
Mat2Huff.m
function [zvec, zi] = Mat2Huff(vec)
if ~isa(vec,'uint8')
fprintf('\n请确认输入uint8类型数据向量!\n');
return;
end
vec = vec(:)';
f = Frequency(vec);
syminfos = find(f~=0);
f = f(syminfos);
[f, sind] = sort(f);
syminfos = syminfos(sind);
len = length(syminfos);
syminfos_ind = num2cell(1:len);
cw_temp = cell(len,1);
while length(f)>1
ind1 = syminfos_ind{
1};
ind2 = syminfos_ind{
2};
cw_temp(ind1) = AddNode(cw_temp(ind1),uint8(0));
cw_temp(ind2) = AddNode(cw_temp(ind2),uint8(1));
f = [sum(f(1:2)) f(3:end)];
syminfos_ind = [{
[ind1 ind2]} syminfos_ind(3:end)];
[f,sind] = sort(f);
syminfos_ind = syminfos_ind(sind);
end
cw = cell(256,1);
cw(syminfos) = cw_temp;
len = 0;
for i = 1 : length(vec),
len = len+length(cw{
double(vec(i))+1});
end
str_temp = repmat(uint8(0),1,len);
pt = 1;
for index=1:length(vec)
cd = cw{
double(vec(index))+1};
len = length(cd);
str_temp(pt+(0:len-1)) = cd;
pt = pt+len;
end
len = length(str_temp);
pad = 8-mod(len,8);
if pad > 0
str_temp = [str_temp uint8(zeros(1,pad))];
end
cw = cw(syminfos);
cl = zeros(size(cw));
ws = 2.^(0:51);
mcl = 0;
for index = 1:length(cw)
len = length(cw{
index});
if len>mcl
mcl = len;
end
if len>0
cd = sum(ws(cw{
index}==1));
cd = bitset(cd,len+1);
cw{
index} = cd;
cl(index) = len;
end
end
cw = [cw{
:}];
cols = length(str_temp)/8;
str_temp = reshape(str_temp,8,cols);
ws = 2.^(0:7);
zvec = uint8(ws*double(str_temp));
huffcodes = sparse(1,1);
for index = 1:numel(cw)
huffcodes(cw(index),1) = syminfos(index);
end
zi.pad = pad;
zi.huffcodes = huffcodes;
zi.ratio = cols./length(vec);
zi.length = length(vec);
zi.maxcodelen = mcl;
直方图对比函数代码
HisteqContrast.m
function HisteqContrast(Img1, Img2)
figure('Name', '直方图对比', 'NumberTitle', 'Off', ...
'Units', 'Normalized', 'Position', [0.1 0.1 0.5 0.5]);
subplot(2, 2, 1); imshow(mat2gray(Img1)); title('原图像', 'FontWeight', 'Bold');
subplot(2, 2, 2); imshow(mat2gray(Img2)); title('处理后的图像', 'FontWeight', 'Bold');
if ndims(Img1) == 3
Q = rgb2gray(Img1);
else
Q = mat2gray(Img1);
end
if ndims(Img2) == 3
W = rgb2gray(Img2);
else
W = mat2gray(Img2);
end
subplot(2, 2, 3); imhist(Q, 64); title('原灰度直方图', 'FontWeight', 'Bold');
subplot(2, 2, 4); imhist(W, 64); title('处理后的灰度直方图', 'FontWeight', 'Bold');
这些代码都是二进制码,且码字长度是不均匀的、平均码率可以接近信息源熵值的一种编码。编码过程是先对图像数据扫描一遍, 计算出各种像素出现的概率,按概率的大小建立最优二叉树(二叉树的叶子节点刚好表示的图像中的某种像素)并给二叉树的每个分支赋特定权值(0或1), 然后通过遍历二叉树读取从根节点到叶子节点的路径权值字符串,即给每种像素指定了不同长度的唯一编码, 由此得到一张该图像所有像素的哈夫曼编码表。编码后的图像数据记录的是每个像素的码字,而码字与实际像素值的对应关系记录在码表中,码表是附在图像文件中的。


得到后续的直方图对比,效果如图
算法流程
系统GUI初始界面
保存截图函数:
function SnapImage()
imagesPath = '.\\snap_images';
if ~exist(imagesPath, 'dir')
mkdir(imagesPath);
end
[FileName,PathName,FilterIndex] = uiputfile({
'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files' },'保存截图',...
'.\\snap_images\\temp.jpg');
if isequal(FileName, 0) || isequal(PathName, 0)
return;
end
fileStr = fullfile(PathName, FileName);
f = getframe(gcf);
f = frame2im(f);
imwrite(f, fileStr);
msgbox('抓图文件保存成功!', '提示信息');
保存图像函数:
function SaveImage(Img)
imagesPath = '.\\results';
if ~exist(imagesPath, 'dir')
mkdir(imagesPath);
end
[FileName,PathName,FilterIndex] = uiputfile({
'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files' },'保存截图',...
'.\\results\\result.jpg');
if isequal(FileName, 0) || isequal(PathName, 0)
return;
end
fileStr = fullfile(PathName, FileName);
imwrite(mat2gray(Img), fileStr);
结果展示:

本文源代码下载地址–>传送门
边栏推荐
- 一条短视频成本几十万元,虚拟数字人凭“实力”出圈
- 基于 LVM 创建和扩展 XFS 文件系统
- MySql DUMP 自动备份数据库 Shell 脚本
- Record the use process of webscraper
- 動態規劃-01背包,分割等和子集,最後一塊石頭的重量
- pytorch中copy_()、detach()、data()和clone()操作区别小结
- 刚学了一个炫酷3D三棱锥立体特效,快来看看
- 技术探秘: 360数科夺得ICDAR OCR竞赛世界第一
- 第 25 章 基于小波变换的数字水印技术
- 出现UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe9 in position 0: ordinal not in range解决方法
猜你喜欢

LeetCode 5218. Sum of integers with K digits (enumeration)

带你区分几种并行

数学知识复习:三重积分

华为云发布桌面IDE-CodeArts

Brief introduction to jpom: simple and light low intrusive online construction, automatic deployment, daily operation and maintenance, and project monitoring software

内网学习笔记(3)

How to use the low code platform of the Internet of things for report management?

Shardingsphere-proxy-5.0.0 implementation of distributed hash modulo fragmentation (4)

初识Unity3D(项目结构、ProBuilder第三方插件)

ASEMI快恢复二极管FR107参数,FR107实物,FR107应用
随机推荐
亚马逊测评系统哪个好?
Differences among active window, focused window and foreground window
Find find files with different extensions
第 24 章 基于 Simulink 进行图像和视频处理--matlab深度学习实战整理
Yang Bing: oceanbase helps digital transformation, and native distributed database becomes the first choice for core system
毕业后5年,我终于变成了月薪13000的软件测试工程师
pyechart 绘制词云图
2022年Q1手机银行用户规模达6.5亿,加强ESG个人金融产品创新
Record the use process of webscraper
SQL操作:WITH表达式及其应用
Classes and objects (Part 2)
Making unequal interval histogram with Matplotlib
Problems and solutions of non debug mode execution failure when using gomonkey
Shardingsphere-proxy-5.0.0 implementation of distributed hash modulo fragmentation (4)
Huawei cloud releases desktop ide codearts
【ÑÖÏ模拟赛】花萎(矩阵加速,循环卷积,高斯消元)
1876. 长度为三且各字符不同的子字符串
全行业数字化转型加速,到底什么存储会更吃香?
飞桨中国行-苏州站,线上线下限时报名通道已开启!
Pytorch神经网络【手写数字识别】