MATLAB matlab图像压缩程序--采用嵌入式小波零树编码算法源码
function varargout = ezw(varargin)
% EZW MATLAB code for ezw.fig
% EZW, by itself, creates a new EZW or raises the existing
% singleton*.
%
% H = EZW returns the handle to a new EZW or the handle to
% the existing singleton*.
%
% EZW('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EZW.M with the given input arguments.
%
% EZW('Property','Value',...) creates a new EZW or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ezw_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ezw_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 ezw
% Last Modified by GUIDE v2.5 02-May-2018 09:41:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ezw_OpeningFcn, ...
'gui_OutputFcn', @ezw_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 ezw is made visible.
function ezw_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 ezw (see VARARGIN)
% Choose default command line output for ezw
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ezw wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ezw_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 on selection change in dim.
function dim_Callback(hObject, eventdata, handles)
% hObject handle to dim (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns dim contents as cell array
% contents{get(hObject,'Value')} returns selected item from dim
% --- Executes during object creation, after setting all properties.
function dim_CreateFcn(hObject, eventdata, handles)
% hObject handle to dim (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in times.
function times_Callback(hObject, eventdata, handles)
% hObject handle to times (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns times contents as cell array
% contents{get(hObject,'Value')} returns selected item from times
% --- Executes during object creation, after setting all properties.
function times_CreateFcn(hObject, eventdata, handles)
% hObject handle to times (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- 选择图片按钮
function button1_Callback(hObject, eventdata, handles)
% hObject handle to button1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
filter = { ...
'*.bmp; *.jpg; *.gif; *.png; *.tif', '所有图像文件 (*.bmp; *.jpg; *.gif; *.png; *.tif)'; ...
'*.bmp', '位图文件 (*.bmp)'; ...
'*.jpg', 'JPEG文件 (*.jpg)'; ...
'*.gif', 'GIF文件 (*.gif)'; ...
'*.png', '图元文件 (*.png)'; ...
'*.*', '所有文件 (*.*)' ...
};
%弹出选择图片文件的对话框
[filename, pathname, filterindex] = uigetfile(filter, '选择图片');
global X;
if filename == 0%未选择图片
return;
end
X = imread([pathname filename]);
if numel(size(X))> 2
X=rgb2gray(X);%如果是彩色的转成灰度图
end
global fileSizeIn;
%显示原图大小
d = dir([pathname filename]);
fileSizeIn = d.bytes / 1024;
set(handles.size1, 'vis', 'on');
set(handles.size1, 'String', [sprintf('%.2f', fileSizeIn) ' KB']);
axes(handles.axes1);%这句是定位到对应的坐标轴
imshow(X);
title('原图');
% --- 压缩图片按钮
function button2_Callback(hObject, eventdata, handles)
% hObject handle to button2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global X;
if isempty(X)
msgbox('请先选择图片');
return;
end
global dim; %变换级数
dim = get(handles.dim, 'value');
global r c;
[r,c] = size(X);
x = modmat(X);
[rowX, colX] = size(x);
maxDim = min(log2(rowX), log2(colX));
if dim > maxDim
msgbox(['该图像分解级数最大为' num2str(maxDim) ',请重新选择']);
return ;
end
scanTimes = get(handles.times, 'value');
global scf;%解码时逐层显示用到
[coef,scf]=myWavedec2(x,dim,'db1');
%转换成系数矩阵
cAll = cell2mat(coef,dim);
global row col;
[row, col] = size(cAll);
%获取阈值
maxDecIm = max(max(abs(cAll)));
T = zeros(1, scanTimes);
T(1) = 2 ^ floor(log2(maxDecIm));
%其他层的阈值
for i = 2 : scanTimes
T(i) = T(i - 1) / 2;
end
%编码
[CodeList,LenSubCL,QuantiFlagList,LenSubQFL] = ezwcode(cAll,T,scanTimes,handles);
fileSizeOut = getEncodedSize(LenSubCL, LenSubQFL, scanTimes);
%解码
DecodeMat = ezwdecode(T(1),scanTimes,CodeList,LenSubCL,QuantiFlagList,LenSubQFL,handles);
%在size2上显示输出文件大小
set(handles.size2, 'vis', 'on');
set(handles.size2, 'String', [sprintf('%.2f', fileSizeOut) ' KB']);
global Y;
psnrV = psnr(X, Y);
ssimV = ssim(X, Y);
global fileSizeIn;
text7content = ['压缩比:' sprintf('%.2f', fileSizeIn / fileSizeOut) [' : 1']];
text7content = [text7content char(13,10)' 'PSNR:' sprintf('%.2f', psnrV) [' dB']];
text7content = [text7content char(13,10)' 'SSIM:' sprintf('%.2f', ssimV)];
set(handles.text7, 'vis', 'on');
set(handles.text7, 'string', text7content);
set(handles.news, 'string', ['进度条:已完成']);
% --- 保存图片按钮
function button3_Callback(hObject, eventdata, handles)
% hObject handle to button3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global Y
filter = { ...
'*.bmp; *.jpg; *.gif; *.png; *.tif', '所有图像文件 (*.bmp; *.jpg; *.gif; *.png; *.tif)'; ...
'*.bmp', '位图文件 (*.bmp)'; ...
'*.jpg', 'JPEG文件 (*.jpg)'; ...
'*.gif', 'GIF文件 (*.gif)'; ...
'*.png', '图元文件 (*.png)'; ...
'*.*', '所有文件 (*.*)' ...
};
[filename ,pathname]=uiputfile(filter,'保存');
imwrite(Y, [pathname, filename]);
执行测试程序:
clc
clear
global row col dim;
dim = 5;
times = 10;
X = imread('lena-256.bmp');
figure(1)
imshow(X);
[coef,scf]=myWavedec2(X,dim,'db1');
x = cell2mat(coef,dim);
FlagX = sign(x);
[row,col] = size(x);
MaxX = max(max(abs(x)));
T(1) = 2^floor(log2(MaxX));
for i = 2:times
T(i) = T(i-1)/2;
end
[CodeList,LenSubCL,QuantiFlagList,LenSubQFL] = ezwcode(x,T,times);
DecodeMat = ezwdecode(T(1),times,CodeList,LenSubCL,QuantiFlagList,LenSubQFL);
cell = mat2cell(DecodeMat,dim,scf);
xrec=myWaverec2(cell,scf,dim,'db1');
y = uint8(xrec);
figure(2)
imshow(y);
imwrite(y, ['D:\', '3.jpg']);
msgbox('保存成功');
如需整套源码可点击链接自行下载:
MATLABmatlab图像压缩程序-采用嵌入式小波零树编码算法源码资源-CSDN文库https://download.csdn.net/download/m0_71122770/87952162