function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_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 untitled
% Last Modified by GUIDE v2.5 02-Mar-2016 21:43:40
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_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 untitled is made visible.
function untitled_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 untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_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 button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename,pathname]=uigetfile({'*.jpg';'*.bmp';'*.gif';'*.tif';'*.tiff'},'选择图片');
if isequal(filename,0)
disp('Users Selected Canceled');
else
str=[pathname filename];
im = imread(str);
axes(handles.axes1);%axes1是坐标轴的标示
imshow(im);
title('原图')
imwrite(im,'sss.jpg')
end;
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
l=imread('sss.jpg');%调入图像??
X=rgb2gray(l);
imwrite(X,'原灰度图像.jpg');
[m n]=size(X);%m行n列
Y1=double(X);%读入图像数据??
% lianghua=[16 11 10 16 24 40 51 61;%量化矩阵,量化的程度序决定压缩比?
% 12 12 14 19 26 58 60 55;
% 14 13 16 24 40 57 69 56;
% 14 17 22 29 51 87 80 62;
% 18 22 37 56 68 109 103 77;
% 24 35 55 64 81 104 113 92;
% 49 64 78 87 103 121 120 101;
% 72 92 95 98 112 100 103 99];
lianghua=[1 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0];
% lianghua=[1 1 0 0 0 0 0 0;
% 1 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0];
% lianghua=[1 1 1 0 0 0 0 0;
% 1 1 0 0 0 0 0 0;
% 1 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0];
% lianghua=[1 1 1 1 1 0 0 0;
% 1 1 1 1 0 0 0 0;
% 1 1 1 0 0 0 0 0;
% 1 1 0 0 0 0 0 0;
% 1 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0];
% lianghua=[1 1 1 1 1 1 0 0;
% 1 1 1 1 1 0 0 0;
% 1 1 1 1 0 0 0 0;
% 1 1 1 0 0 0 0 0;
% 1 1 0 0 0 0 0 0;
% 1 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0];
ilianghua=lianghua;%----------------------------------------------------------?%图像压缩?
t=dctmtx(8);
J=blkproc(Y1,[8 8],'P1*x*P2',t,t'); %分成8*8块进行DCT变换?
%M1=blkproc(J,[8 8],'round(x./P1)',lianghua); %量化?
M1=blkproc(J,[8 8],'x.*P1',lianghua);
u=abs(min(min(M1)));
M=(M1./u)+1;
data=uint8(M);%Huffman编码要求为无符号整形数组
M2=M-double(data);
[zipped,info]=huffencode(data); %调用Huffman编码程序进行压缩
unzipped=huffdecode(zipped,info,data); %调用Huffman解码程序进行解压缩
k=1;
for i=1:n
for j=1:m
unzippedray(i,j)=unzipped(k);
k=k+1;
end
end
unzippedray= unzippedray';%对解压缩后得到的一维数组进行变换,得到无损的量化后
%(二维数组,其值与data数组值是一致的,体现了Huffman编码是一种无损编码 )
unzippedray=(double(unzippedray)-1+M2).*u;
T=blkproc(unzippedray,[8 8],'x.*P1',ilianghua); %反量化
I=blkproc(T,[8 8],'P1*x*P2',t',t); %8*8DCT反变换
%[调用Huffman编码程序进行解码
%[显示原始图像和经编码后的图像,显示压缩比,并计算均方根误差得erms=0,表示是Huffman是无失真编码
axes(handles.axes2);%axes1是坐标轴的标示
imshow(Y1,[]);
axis square;
xlabel('原灰度图像');
axes(handles.axes3);%axes1是坐标轴的标示
imshow(I,[]);
axis square;
xlabel('Huffman解压缩后图像');
axes(handles.axes4);%axes1是坐标轴的标示
imshow((Y1-I),[]);
axis square;
xlabel('量化后损失的图像部分');
I1=uint8(I);
imwrite(I1,'Huffman解压缩后图像.jpg');
yasuobi=1/info.ratio;
whos data unzipped zipped %huffencode函数对输入矩阵vector进行Huffman编码,返回%编码后的向量(压缩后数据)及相关信息% [h,k]=hist((Y1-I),m);%生成直方图数据 % subplot(224);% bar(k,h,'k');% xlabel('误差图像直方图');