定义:设A是秩为r的m×n复矩阵,的特征值为,
,
则称 为矩阵A的奇异值。其中
称为A的正奇异值,通常称之为奇异值。
定义:设A是秩为r的m×n复矩阵,则存在m阶酉矩阵U和n阶酉矩阵V ,使得 ,成为A的奇异值分解。
function A=svd_pic(a,yasuo)
%a为图片的数据
%yasuo为自定义的压缩比
%分别读取图片数据的RGB值
A1=double(a(:,:,1));
A2=double(a(:,:,2));
A3=double(a(:,:,3));
%对其进行奇异值分解
[u1,s1,v1]=svd(A1);
[u2,s2,v2]=svd(A2);
[u3,s3,v3]=svd(A3);
%初始化新的s1 s2 s3
bz1=0;
bz2=0;
bz3=0;
%当新的奇异值>压缩比时退出
for i=1:min(size(s1))
S1=sum(diag(s1));
S2=sum(diag(s2));
S3=sum(diag(s3));
bz1=bz1+s1(i,i);
bz2=bz2+s2(i,i);
bz3=bz3+s3(i,i);
if bz1/S1 > yasuo && bz2/S2 > yasuo && bz3/S3 > yasuo
break;
end
end
%初始化新的A1 A2 A3
A11=0;
A22=0;
A33=0;
for j=1:i
A11=A11+s1(j,j)*u1(:,j)*(v1(:,j)');
A22=A22+s2(j,j)*u2(:,j)*v2(:,j)';
A33=A33+s3(j,j)*u3(:,j)*v3(:,j)';
end
%整型
A(:,:,1)=uint8(A11);
A(:,:,2)=uint8(A22);
A(:,:,3)=uint8(A33);
函数的调用(注意:将要压缩的图片与函数文件放在同一路径之下)
%命令行空间输入
a=imread('ting.jpg'); %输入要读取的图片名
imshow(a);%显示图片
pause(3);显示3秒,之后进行压缩
svd_pic(a,0.6)%对图片进行压缩
imshow(a);%显示压缩之后的图片
1、创建空的guide文件
2、在【工具】中选择【菜单编辑器】,设置如图所示菜单。
3、利用滑动条,静态文本和坐标区布置如下界面
4、分别编写其控件回调函数的内容
function varargout = sv(varargin)
% SV MATLAB code for sv.fig
% SV, by itself, creates a new SV or raises the existing
% singleton*.
%
% H = SV returns the handle to a new SV or the handle to
% the existing singleton*.
%
% SV('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SV.M with the given input arguments.
%
% SV('Property','Value',...) creates a new SV or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before sv_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to sv_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 sv
% Last Modified by GUIDE v2.5 15-Jul-2018 16:21:00
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @sv_OpeningFcn, ...
'gui_OutputFcn', @sv_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 sv is made visible.
function sv_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 sv (see VARARGIN)
% Choose default command line output for sv
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes sv wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = sv_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 file_Callback(hObject, eventdata, handles)%文件菜单回调函数
% hObject handle to file (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function dakai_Callback(hObject, eventdata, handles) %打开菜单回调函数
% hObject handle to dakai (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global pic %设置图片信息为全局变量
[filename,pathname]=uigetfile({'*.jpg','*.jpg';'*.png','*.png'},'打开');
if isequal(filename,0)|| isequal(pathname,0)
return;
end
name=[pathname,filename];
pic=imread(name); %读取图片信息
axes(handles.axes1);%设置当前坐标去为原图坐标区
imshow(pic);%显示图片
% --------------------------------------------------------------------
function baocun_Callback(hObject, eventdata, handles)%保存菜单回调函数
% hObject handle to baocun (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global A %设置压缩后图片信息为全局变量
[filename,pathname]=uiputfile({'*.jpg','*.jpg';'*.png','*.png'},'打开');
name=[pathname,filename];
imwrite(A,name) %保存压缩之后的图片信息
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global pic A
yasuo=handles.slider1.Value; %设置yasuo的值为滑动条控件的值
A=svd_pic(pic,yasuo);%调用图片压缩函数
axes(handles.axes2)%设置当前坐标去为压缩图坐标区
imshow(A)%显示图片
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
5、效果