function varargout = imDCT(varargin)
% IMDCT M-file for imDCT.fig
% IMDCT, by itself, creates a new IMDCT or raises the existing
% singleton*.
%
% H = IMDCT returns the handle to a new IMDCT or the handle to
% the existing singleton*.
%
% IMDCT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in IMDCT.M with the given input arguments.
%
% IMDCT('Property','Value',...) creates a new IMDCT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before imDCT_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to imDCT_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 imDCT
% Last Modified by GUIDE v2.5 07-Dec-2009 19:54:09
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @imDCT_OpeningFcn, ...
'gui_OutputFcn', @imDCT_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 imDCT is made visible.
function imDCT_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 imDCT (see VARARGIN)
% Choose default command line output for imDCT
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes imDCT wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = imDCT_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;
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
ImgPath=get(handles.edit1,'String');
CompressRate=uint8(fix(str2num(get(handles.edit2,'String'))));
if CompressRate>64
CompressRate=64;
end
if CompressRate<1
CompressRate=1;
end
set(handles.edit2,'String',num2str(CompressRate));
img=imread(ImgPath);
axes(handles.axes1);
imshow(img);
set(handles.text7,'Visible','off');
set(handles.text7,'String','处理状态:正在处理中...');
set(handles.text7,'Visible','on');
pause(0.1);
Block=ceil(size(img)/8);
ImgDCT=zeros(Block*8);
ImgRestoreTemp=zeros(Block*8);
ImgRestore=zeros(size(img));
ImgBlock=zeros(8);
DCTBlock=zeros(8);
vector=zeros(1,64);
for ii=1:Block(1)
for jj=1:Block(2)
ImgBlock(:,:)=zeros(8);
if ii==Block(1)
if jj==Block(2)
ImgBlock(1:size(img,1)-(ii-1)*8,1:size(img,2)-(jj-1)*8)=img((ii-1)*8+1:size(img,1),(jj-1)*8+1:size(img,2));
else
ImgBlock(1:size(img,1)-(ii-1)*8,:)=img((ii-1)*8+1:size(img,1),(jj-1)*8+1:jj*8);
end
else
if jj==Block(2)
ImgBlock(:,1:size(img,2)-(jj-1)*8)=img((ii-1)*8+1:ii*8,(jj-1)*8+1:size(img,2));
else
ImgBlock(:,:)=img((ii-1)*8+1:ii*8,(jj-1)*8+1:jj*8);
end
end
DCTBlock(:,:)=dct2(ImgBlock(:,:));
if CompressRate~=64
vector(1:64)=m2v(DCTBlock(:,:));
vector(CompressRate+1:64)=0;
DCTBlock(:,:)=v2m(vector(1:64));
end
ImgDCT((ii-1)*8+1:ii*8,(jj-1)*8+1:jj*8)=DCTBlock(:,:);
end
end
for ii=1:Block(1)
for jj=1:Block(2)
DCTBlock(:,:)=ImgDCT((ii-1)*8+1:ii*8,(jj-1)*8+1:jj*8);
ImgBlock(:,:)=idct2(DCTBlock(:,:));
ImgRestoreTemp((ii-1)*8+1:ii*8,(jj-1)*8+1:jj*8)=ImgBlock(:,:);
end
end
ImgRestore(:,:)=ImgRestoreTemp(1:size(ImgRestore,1),1:size(ImgRestore,2));
ImgRestore=uint8(ImgRestore);
ErrorTemp=imabsdiff(img,ImgRestore);
MSE=sum(sum(ErrorTemp.*ErrorTemp))/(size(img,1)*size(img,2));
PSNR=10*log(65535/MSE);
strPSNR=['PSNR:',num2str(PSNR)];
axes(handles.axes2);
imshow(ImgRestore);
imwrite(ImgRestore,'GIRL2.BMP','bmp');
set(handles.text7,'String','处理状态:处理完成!');
set(handles.text8,'String',strPSNR);
set(handles.text8,'Visible','on');
% 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)
function vector=m2v(matrix)
subscript=[1,2,6,7,15,16,28,29;...
3,5,8,14,17,27,30,43;...
4,9,13,18,26,31,42,44;...
10,12,19,25,32,41,45,54;...
11,20,24,33,40,46,53,55;...
21,23,34,39,47,52,56,61;...
22,35,38,48,51,57,60,62;...
36,37,49,50,58,59,63,64];
vectorTemp=zeros(1,64);
for ii=1:8
for jj=1:8
vectorTemp(subscript(ii,jj))=matrix(ii,jj);
end
end
vector=vectorTemp;
function matrix=v2m(vector)
subscript=[1,2,6,7,15,16,28,29;...
3,5,8,14,17,27,30,43;...
4,9,13,18,26,31,42,44;...
10,12,19,25,32,41,45,54;...
11,20,24,33,40,46,53,55;...
21,23,34,39,47,52,56,61;...
22,35,38,48,51,57,60,62;...
36,37,49,50,58,59,63,64];
matrixTemp=zeros(8,8);
for ii=1:8
for jj=1:8
matrixTemp(ii,jj)=vector(subscript(ii,jj));
end
end
matrix=matrixTemp;