【图像压缩】基于matlab GUI DCT图像压缩【含Matlab源码 842期】
一、DCT图像无损压缩简介
1 图像压缩
图像压缩按照压缩过程中是否有信息的损失以及解压后与原始图像是否有误差可以分为无损压缩和有损压缩两大类。无损压缩是指不损失图像质量的压缩,它是对文件的存储方式进行优化,采用某种算法表示重复的数据信息,文件可以完全还原,不会影响文件内容。一般来说,由于无损压缩只是删除了图像数据中的冗余信息,可以准确地恢复原始图像,所以不可能达到很高的压缩比。有损压缩是指损失图像质量的压缩,它将不相干的信息也删除了,因此解压时只能将原始图像进行近似的还原,它的高压缩比是以牺牲图像质量为代价的。
2 JPRG图像压缩
JPEG 提出的 JPEG 标准是为连续色调图像的压缩提供的公共标准。连续色调图像并不局限于单色调( 黑白) 图像,该标准可适用于各种多媒体存储和通信应用所使用的灰度图像、摄影图像及静止视频压缩文件。
JPEG 标准还提出:
①必须将图像质量控制在可视保真度高的范围内,同时编码器可被参数化,允许设置压缩或质量水平
②压缩标准可以应用于任何一类连续色调数字图像,并不应受到维数、颜色、画面尺寸、内容和色调的限制
③压缩标准必须从完全无损到有损范围内可选,以适应不同的存储 CPU 和显示要求
图像压缩编码方法从压缩编码算法原理上可以分为无损压缩编码、有损压缩编码、混合编码方法。而JPEG 标准就是一种混合编码方法,既有无损的压缩编码又有有损的压缩编码。有损压缩方法是以 DCT 变换为基础的压缩方法,其压缩率比较高,是JPEG 标准的基础。无损压缩方法又称预测压缩方法,是以二维 DPCM 为基础的压缩方式,解码后能完全精确地恢复原图像采样值,其压缩比低于有损压缩方法。
观察下图中的编码器负责降低输入图像的编码、像素间和心理视觉冗余。在编码处理的第一阶段,离散余弦变换器将输入图像变换成一种( 通常不可见的) 格式,以便减少像素间的冗余。在第二阶段,量化器根据预定义的保真度准则来减少映射变换器输出的精确性,以便试图去除心理视觉冗余数据。这种操作是不可逆的,当进行无损压缩时,则必须将其忽略。在第三个即最后一个处理阶段,熵编码器根据所用的码字对量化器输出和离散余弦变换输出创建码字( 减少编码冗余)。
3 二维离散余弦变换
离散余弦变换(Discrete Cosine Transform),简称DCT变换.是一种与傅立叶变换紧密相关的数学运算.在傅立叶级数展开式中,如果被展开的函数是实偶函数,那么其傅立叶级数中只包含余弦项,再将其离散化可导出余弦变换,因此称之为离散余弦变换.余弦变换实际上是傅立叶变换的实数部分,其主要用于图像的压缩,目前国际压缩标准的JPEG格式中就用到了DCT变换。
在编码过程中,首先将输入图像颜色空间转换后分解为8× 8大小的数据块,然后用正向二维DCT把每个块转变成64个DCT系数值,其中1个数值是直流(DC)系数,即8× 8空域图像子块的平均值,其余的63个是交流(AC)系数,接下来对DCT系数进行量化,最后将变换得到的量化的DCT系数进行编码和传送,形成压缩后的图像格式。在解码过程中,先对已编码的量化的DCT系数进行解码,然后使用二维DCT反变换求逆量化并把DCT系数转化为8× 8样本像块,最后将操作完成后的块组合成一个单一的图像。这样就完成了图像的压缩和解压过程.研究表明,DCT将8× 8图像块变换为频域时数值集中在左上角,即低频分量都集中在左上角,高频分量分布在右下脚。而低频部分包含了图像大部分信息,相比之下,高频部分包含的信息量较少。为了压缩数据,往往采用忽略高频系数的办法。而较低频系数的修改对原始数据的影响较小。基于DCT的压缩编码属于有损压缩,通过去除图像本身的冗余量和人的视觉冗余量来达到压缩数据的目的,主要分为以下几个步骤:
(1)正向离散余弦变换
(2)量化
(3)Z字形编码
(4)使用差分脉冲编码调制对直流系数进行编码
(5)使用行程长度编码对交流系数进行编码
(6)熵编码
(7)组成位数据流
4 二维DCT变换
二维离散余弦变换的正变换公式为:
在图像的压缩编码中,N一般取8。
二维DCT的反变换公式为:
以上各式中的系数:
5 Matlab调试
根据JPEG 压缩编码算法,要将一幅灰度图像进行压缩编码,首先把图像分成 8* 8 的像素块,分块进行 DCT 变换后,根据 JPEG 标准量化表对变换系数进行量化,再对直流系数( DC) 进行预测编码,对交流系数( AC) 行 zigzag 扫描和可变长编码,然后根据标准的 Huffman 码表进行熵编码,输出压缩图像的比特序列,实现了图像的压缩。
DCT 变换的特点是变换后图像大部分能量集中在左上角,因为左上角反应原图像低频部分数据,右下角反应原图像高频部分数据,而图像的能量通常集中在低频部分。因此 DCT 变换后,只保留 DCT 系数矩阵最左上角的 10 个系数,然后对每个图像块利用这 10个系数进行 DCT 反变换来重构图像。
其基于 DCT 变换矩阵算法的处理过程如下图:
二、部分源代码
function varargout = dct_encoding(varargin)
% DCT_ENCODING M-file for dct_encoding.fig
% DCT_ENCODING, by itself, creates a new DCT_ENCODING or raises the existing
% singleton*.
%
% H = DCT_ENCODING returns the handle to a new DCT_ENCODING or the handle to
% the existing singleton*.
%
% DCT_ENCODING('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DCT_ENCODING.M with the given input arguments.
%
% DCT_ENCODING('Property','Value',...) creates a new DCT_ENCODING or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before dct_encoding_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to dct_encoding_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 dct_encoding
% Last Modified by GUIDE v2.5 18-Jun-2009 20:11:16
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @dct_encoding_OpeningFcn, ...
'gui_OutputFcn', @dct_encoding_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(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 dct_encoding is made visible.
function dct_encoding_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 dct_encoding (see VARARGIN)
cr = 0.5;
I = imread('lena.bmp');
axes(handles.axes1);
imshow(I);
I = double(I)/255;
t = dctmtx(8);
dctcoe = blkproc(I,[8 8],'P1*x*P2',t,t');
coevar = im2col(dctcoe,[8 8],'distinct');
coe = coevar;
[y,ind] = sort(coevar);
[m,n] = size(coevar);
snum = 64-64*cr;
for i = 1:n
coe(ind(1:snum),i) = 0;
end
B2 = col2im(coe,[8 8],[512 512],'distinct');
I2 = blkproc(B2,[8,8],'P1*x*P2',t',t);
axes(handles.axes2);
imshow(I2);
e = double(I) - double(I2);
[m,n]=size(e);
erms = sqrt(sum(e(:).^2)/(m*n));
set(handles.erms_edit,'string',erms);
set(handles.cr_edit,'string',0.5);
% Choose default command line output for dct_encoding
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes dct_encoding wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = dct_encoding_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;
% --- Executes during object creation, after setting all properties.
function image_pop_menu_CreateFcn(hObject, eventdata, handles)
% hObject handle to image_pop_menu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% --- Executes on selection change in image_pop_menu.
function image_pop_menu_Callback(hObject, eventdata, handles)
% hObject handle to image_pop_menu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
val = get(hObject,'value');
str = get(hObject,'string');
cr = str2num(get(handles.cr_edit,'string'));
switch str{val}
case 'Lena'
I = imread('lena.bmp');
case 'Cameraman'
I = imread('cameraman.tif');
case 'Peppers'
I = imread('peppers.bmp');
case 'Fingerprint'
I = imread('fingerprint.jpg');
case 'Licenceplate'
I = imread('licenceplate.jpg');
case 'Cloudy'
I = imread('cloudy.tif');
end
[M N] = size(I);
axes(handles.axes1);
imshow(I);
I = double(I)/255;
t = dctmtx(8);
dctcoe = blkproc(I,[8 8],'P1*x*P2',t,t');
coevar = im2col(dctcoe,[8 8],'distinct');
coe = coevar;
[y,ind] = sort(coevar);
[m,n] = size(coevar);
snum = 64-64*cr;
for i = 1:n
coe(ind(1:snum),i) = 0;
end
B2 = col2im(coe,[8 8],[M N],'distinct');
I2 = blkproc(B2,[8,8],'P1*x*P2',t',t);
axes(handles.axes2);
imshow(I2);
e = double(I) - double(I2);
[m,n]=size(e);
erms = sqrt(sum(e(:).^2)/(m*n));
set(handles.erms_edit,'string',erms);
set(handles.cr_edit,'string',0.5);
% Hints: contents = get(hObject,'String') returns image_pop_menu contents as cell array
% contents{get(hObject,'Value')} returns selected item from image_pop_menu
% --- Executes during object creation, after setting all properties.
function cr_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to cr_edit (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
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
function cr_edit_Callback(hObject, eventdata, handles)
% hObject handle to cr_edit (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 cr_edit as text
% str2double(get(hObject,'String')) returns contents of cr_edit as a double
% --- Executes on button press in close_button.
function close_button_Callback(hObject, eventdata, handles)
% hObject handle to close_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close(dct_encoding);
% --- Executes on button press in apply_button.
function apply_button_Callback(hObject, eventdata, handles)
% hObject handle to apply_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cr = str2num(get(handles.cr_edit,'string'));
I = getimage(handles.axes1);
I = double(I)/255;
t = dctmtx(8);
dctcoe = blkproc(I,[8 8],'P1*x*P2',t,t');
coevar = im2col(dctcoe,[8 8],'distinct');
coe = coevar;
[y,ind] = sort(coevar);
[m,n] = size(coevar);
snum = 64-floor(64*cr);
for i = 1:n
coe(ind(1:snum),i) = 0;
end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
三、运行结果
四、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.
文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。
原文链接:qq912100926.blog.csdn.net/article/details/116200421
- 点赞
- 收藏
- 关注作者
评论(0)