当前位置:网站首页>[Chapter 13 image compression and reconstruction based on Hoffman -- image processing application of MATLAB deep learning practice]
[Chapter 13 image compression and reconstruction based on Hoffman -- image processing application of MATLAB deep learning practice]
2022-06-22 01:55:00 【Haibao 7】
Image compression and reconstruction based on Huffman
Some reference sources :
https://blog.csdn.net/qq_59747472/article/details/121890265
To save space , When encoding data , You can specify fewer digits for data that often occurs , Those data that do not often appear specify more digits to represent , This reduces redundancy , In this way, the overall effect is to save storage space . The basic principle of image compression based on Huffman coding is to replace frequently used data with shorter code , Less used data is replaced by longer code , The code of each data is different , This is a typical lossless coding method .
Partial function
Mat2Huff.m
function [zvec, zi] = Mat2Huff(vec)
if ~isa(vec,'uint8')
fprintf('\n Please confirm the input uint8 Type data vector !\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;
Histogram comparison function code
HisteqContrast.m
function HisteqContrast(Img1, Img2)
figure('Name', ' Histogram comparison ', 'NumberTitle', 'Off', ...
'Units', 'Normalized', 'Position', [0.1 0.1 0.5 0.5]);
subplot(2, 2, 1); imshow(mat2gray(Img1)); title(' Original image ', 'FontWeight', 'Bold');
subplot(2, 2, 2); imshow(mat2gray(Img2)); title(' Processed image ', '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(' Original gray histogram ', 'FontWeight', 'Bold');
subplot(2, 2, 4); imhist(W, 64); title(' Processed gray histogram ', 'FontWeight', 'Bold');
These codes are binary codes , And the codeword length is uneven 、 An encoding in which the average bit rate is close to the entropy of the information source . The encoding process is to scan the image data first , Calculate the probability of various pixels , The optimal binary tree is established according to the size of probability ( The leaf node of the binary tree just represents a certain pixel in the image ) And assign specific weights to each branch of the binary tree (0 or 1), Then read the path weight string from the root node to the leaf node by traversing the binary tree , That is, each pixel is assigned a unique encoding of different lengths , Thus, a Huffman coding table of all pixels of the image is obtained . The encoded image data records the codeword of each pixel , The corresponding relationship between the codeword and the actual pixel value is recorded in the code table , The code table is attached to the image file .


Get the subsequent histogram comparison , The effect is as shown in the picture 
Algorithm flow 
System GUI Initial interface 
Save screenshot function :
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' },' Save the screenshot ',...
'.\\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(' The snapshot file was saved successfully !', ' Prompt information ');
Save image function :
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' },' Save the screenshot ',...
'.\\results\\result.jpg');
if isequal(FileName, 0) || isequal(PathName, 0)
return;
end
fileStr = fullfile(PathName, FileName);
imwrite(mat2gray(Img), fileStr);
Result display :

Source code of this article Download address –> Portal
边栏推荐
- 【第 13 章 基于霍夫曼图像压缩重建--Matlab深度学习实战图像处理应用】
- Intranet learning notes (3)
- 功能测试——MySQL数据库简介
- 第 25 章 基于小波变换的数字水印技术
- 当零售数字化进入到全新的发展阶段,我们需要将公域和私域进行打通
- acwing 837. Number of points in connected blocks (additional information maintained by querying sets - number of sets)
- Shell脚本语法概览
- NOIP 提高组 初赛 三、问题求解 习题集NOIP1995-NOIP2018
- 五笔 第一讲 指法
- 【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
猜你喜欢
随机推荐
程序员常用的组件、框架、开发工具下载链接大全
Pytoch neural network [handwritten digit recognition]
ASEMI快恢复二极管FR107参数,FR107实物,FR107应用
Mathematical knowledge of Sinorgchem in the first round of noip preliminary csp-j1 csp-s1 (I)
2020 CSP-J1 CSP-S1 第1轮 初赛 答案解析及总结、视频等
GAMES-101-个人总结归纳-Shading
Mysql数据库轻松学06—数据分析师常用:数据查询语言DQL之单表查询
【 NVIDIA development history record 618-01 】
Five years after graduation, I finally became a software testing engineer with a monthly salary of 13000
阿里,腾讯,百度软件测试工程师推荐——软件测试模型之瀑布模型
Test APK exception control sensor attacker development
第 21 章 路面裂缝检测识别系统设计--matlab深度学习实战
第 25 章 基于小波变换的数字水印技术
稳扎稳打学爬虫08—Selenium的使用方法详解
acwing 835. Trie string statistics
NOIP 提高组 初赛 三、问题求解 习题集NOIP1995-NOIP2018
【随笔】昨天研究了一天 RN 生态的 Expo 的确牛逼,从开发构建到部署一条龙,很好使。
Download links to components, frameworks and development tools commonly used by programmers
Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
Packet capturing tool: Fiddler, a necessary skill for Software Test Engineer








