function varargout = SVD(varargin)
% HOMEWORK MATLAB code for homework.fig
% HOMEWORK, by itself, creates a new HOMEWORK or raises the existing
% singleton*.
%
% H = HOMEWORK returns the handle to a new HOMEWORK or the handle to
% the existing singleton*.
%
% HOMEWORK('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in HOMEWORK.M with the given input arguments.
%
% HOMEWORK('Property','Value',...) creates a new HOMEWORK or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before homework_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to homework_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 homework
% Last Modified by GUIDE v2.5 11-Feb-2019 20:51:19
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @homework_OpeningFcn, ...
'gui_OutputFcn', @homework_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 homework is made visible.
function homework_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 homework (see VARARGIN)
% Choose default command line output for homework
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes homework wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = homework_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 load.
function load_Callback(hObject, eventdata, handles)
global img
% hObject handle to load (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[fname, pname, index] = uigetfile({'*.jpg'; '*.bmp'}, '选择图片'); %创建图片选择对话框
if index
str = [pname fname];
img = imread(str);
[hei, wid, col] = size(img);
if col == 3
img = rgb2gray(img);
end
fprintf('The size of image is: %d x %d.\n', [hei, wid] );
end
% --- Executes on button press in load_pic.
function load_pic_Callback(hObject, eventdata, handles)
global img
% hObject handle to load_pic (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
imshow(img,'Parent',handles.axes1)
% --- Executes on button press in small.
function small_Callback(hObject, eventdata, handles)
global img
% hObject handle to small (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[hei, wid, col] = size(img);
if col == 3
img = rgb2gray(img);
end
[u, s, v] = svd(double(img));
% reconstruction
recon_nums =80;
img_recon = zeros(hei, wid, length(recon_nums));
for id = 1:length(recon_nums)
for ii = 1: recon_nums(id)
img_temp = s(ii, ii) * u(:, ii) * v(:, ii)';
img_recon(:, :, id) = img_recon(:, :, id) + img_temp;
end
end
% compute compression rate
compre_rate = (hei*wid) ./ ((hei+wid).*recon_nums + recon_nums);
imshow(uint8(img_recon(:, :, id)),'Parent',handles.axes4)
title(['奇异值个数:', num2str(recon_nums(id)), ...
',压缩比:', num2str(compre_rate(id)) ],'Parent',handles.axes4);
% imwrite(uint8(img_recon(:, :, id)),'1.jpg') 输出压缩后的图像
% --- Executes on button press in middle.
function middle_Callback(hObject, eventdata, handles)
global img
% hObject handle to middle (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[hei, wid, col] = size(img);
if col == 3
img = rgb2gray(img);
end
[u, s, v] = svd(double(img));
% reconstruction
recon_nums = 40;
img_recon = zeros(hei, wid, length(recon_nums));
for id = 1:length(recon_nums)
for ii = 1: recon_nums(id)
img_temp = s(ii, ii) * u(:, ii) * v(:, ii)';
img_recon(:, :, id) = img_recon(:, :, id) + img_temp;
end
end
% compute compression rate
compre_rate = (hei*wid) ./ ((hei+wid).*recon_nums + recon_nums);
imshow(uint8(img_recon(:, :, id)),'Parent',handles.axes3)
title(['奇异值个数:', num2str(recon_nums(id)), ...
',压缩比:', num2str(compre_rate(id)) ],'Parent',handles.axes3);
% imwrite(uint8(img_recon(:, :, id)),'2.jpg') 输出压缩后的图像
% --- Executes on button press in big.
function big_Callback(hObject, eventdata, handles)
global img
% hObject handle to big (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[hei, wid, col] = size(img);
if col == 3
img = rgb2gray(img);
end
[u, s, v] = svd(double(img));
% reconstruction
recon_nums =20;
img_recon = zeros(hei, wid, length(recon_nums));
for id = 1:length(recon_nums)
for ii = 1: recon_nums(id)
img_temp = s(ii, ii) * u(:, ii) * v(:, ii)';
img_recon(:, :, id) = img_recon(:, :, id) + img_temp;
end
end
% compute compression rate
compre_rate = (hei*wid) ./ ((hei+wid).*recon_nums + recon_nums);
imshow(uint8(img_recon(:, :, id)),'Parent',handles.axes2)
title(['奇异值个数:', num2str(recon_nums(id)), ...
',压缩比:', num2str(compre_rate(id)) ],'Parent',handles.axes2);
% imwrite(uint8(img_recon(:, :, id)),'3.jpg') 输出压缩后的图像
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
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
set(hObject,'xTick',[]);
set(hObject,'ytick',[]);
set(gca,'XColor',[1 1 1])
set(gca,'YColor',[1 1 1])