第 15 章 基于小波的图像压缩技术–matlab实现深度学习算法整理源码分析
主函数
main.m
clc; clear all; close all;
filename = fullfile(pwd, 'images', 'cameraman.tif');
x = imread(filename);
num = 2;
[cf_vec, dim_vec] = wavedec_process(x, num, 'haar');
th = 10;
y = waverec_process(cf_vec, dim_vec, 'haar', th);
output_img(x, y, filename, th, 'png');
p = PSNR(x,y);
fprintf('\n压缩前后图像的PSNR值为%.2f\n', p);
案例效果展示:
函数目录:
输出子函数:
function output_img(x, y, filename, th, ext)
if nargin < 5
ext = 'png';
end
[~, file, ~] = fileparts(filename);
foldername = fullfile(pwd, 'output');
if ~exist(foldername, 'dir')
mkdir(foldername);
end
file1 = fullfile(foldername, sprintf('%s_origin.%s', file, ext));
file2 = fullfile(foldername, sprintf('%s_wave_%.1f.%s', file, th, ext));
imwrite(x, file1);
imwrite(y, file2);
info1 = imfinfo(file1);
info2 = imfinfo(file2);
fprintf('\n压缩前图像所需存储空间为%.2fbytes', info1.FileSize);
fprintf('\n压缩后图像所需存储空间为%.2fbytes', info2.FileSize);
fprintf('\n文件大小比为%.2f', info1.FileSize/info2.FileSize);
绘图子函数1
function plot_wave_coef(cf_vec)
dn = 3;
num = (length(cf_vec)-1)/dn;
figure;
subplot(num+1, 3, 2);
yt = im2uint8(mat2gray(cf_vec{1}));
imshow(yt, []);
title(sprintf('近似分量A_{%d}', num));
info = {'垂直细节V', '水平细节H', '对角线细节D'};
ps = 2;
for i = 1 : num
for j = 1 : dn
yt = im2uint8(mat2gray(cf_vec{ps}));
subplot(num+1, dn, ps+2);
imshow(yt, []);
title(sprintf('%s_{%d}', info{j}, num-i+1));
ps = ps+1;
end
end
绘图子函数2
function plot_wave_coef_join(cf_vec,dim_vec)
dn = 3;
num = (length(cf_vec)-1)/dn;
tmpa = wkeep(cf_vec{1}, dim_vec(1, :), 'c');
tmpa = im2uint8(mat2gray(tmpa));
tmpa(1, :) = 255; tmpa(end, :) = 255;
tmpa(:, 1) = 255; tmpa(:, end) = 255;
for j = 1:num
tmpv = wkeep(cf_vec{(j-1)*dn+2}, dim_vec(j, :), 'c');
tmph = wkeep(cf_vec{(j-1)*dn+3}, dim_vec(j, :), 'c');
tmpd = wkeep(cf_vec{(j-1)*dn+4}, dim_vec(j, :), 'c');
tmpv = im2uint8(mat2gray(tmpv));
tmph = im2uint8(mat2gray(tmph));
tmpd = im2uint8(mat2gray(tmpd));
tmpv(1, :) = 255; tmpv(end, :) = 255;
tmpv(:, 1) = 255; tmpv(:, end) = 255;
tmph(1, :) = 255; tmph(end, :) = 255;
tmph(:, 1) = 255; tmph(:, end) = 255;
tmpd(1, :) = 255; tmpd(end, :) = 255;
tmpd(:, 1) = 255; tmpd(:, end) = 255;
tmp = [tmpa,tmpv;tmph,tmpd];
stc = size(tmp);
if stc >= dim_vec(j+1, :)
tmpa = tmp(1:dim_vec(j+1, 1), 1:dim_vec(j+1,2));
else
tmp = tmp([1:end-1, end-2:end-1], [1:end-1, end-2:end-1]);
tmpa = tmp(1:dim_vec(j+1, 1), 1:dim_vec(j+1,2));
end
tmpa = im2uint8(mat2gray(tmpa));
tmpa(1, :) = 255; tmpa(end, :) = 255;
tmpa(:, 1) = 255; tmpa(:, end) = 255;
end
figure;
imshow(tmpa, []);
title('小波系数塔式图');
PSNR
离散余弦逆变换中如果不使用8作为系数,而是使用4作为系数的话,图像的画质会变差。来求输入图像和经过离散余弦逆变换之后的图像的峰值信噪比吧!再求出离散余弦逆变换的比特率
峰值信噪比(Peak Signal to Noise Ratio)缩写为PSNR,用来表示信号最大可能功率和影响它的表示精度的破坏性噪声功率的比值,可以显示图像画质损失的程度。
提示:内容整理自:
https://github.com/gzr2017/ImageProcessing100Wen
峰值信噪比越大,表示画质损失越小。峰值信噪比通过下式定义。MAX表示图像点颜色的最大数值。如果取值范围是[0,255]的话,那么MAX的值就为255。参考来源
PSNR处理函数
function S=PSNR(s,t)
[m, n, ~]=size(s);
s = im2uint8(mat2gray(s));
t = im2uint8(mat2gray(t));
s = double(s);
t = double(t);
sd = 0;
mi = m*n*max(max(s.^2));
for u = 1:m
for v = 1:n
sd = sd+(s(u,v)-t(u,v))^2;
end
end
if sd == 0
sd = 1;
end
S = mi/sd;
S = 10*log10(S);
部分参考:https://blog.csdn.net/weixin_29732003/article/details/122569893
SNR是变异系数的倒数,与变异系数一样,消除单位和(或)平均数不同对两个或多个资料变异程度比较的影响,同时在数值上可能与视觉上的画质质量呈现一个对应关系:
SNR越高,像素的离散程度越低,图像质量(可能)越好。
本文代码来源整理–>传送门