⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image_mva_0_1.m

📁 一个利用Matlab实现PCA(主成分分析)
💻 M
📖 第 1 页 / 共 3 页
字号:
function varargout = image_mva_0_0(varargin)
%  GUI for Multivariate Analysis of images
%  Includes:
% 	PCA, Simplisma (pure variable method) and MCR (Multivariate Curve Resolution) 
%  	Three types of image classification: 2 unsupervised and 1 supervised
% 	Basic image processing:
%	- cropping, resizing
%	- histogram equalization
%	- threshold
%	- profile plotter
%	Filtering:
%	- averaging
%	- contrast enhancement
%	- morphological (opening, closing, eroding, dilation) 
%	- edge extraction
%
%  Works with most types of Image formats:
% 	JPEG, TIFF, BMP and more (see imread for format types)
%  	RGB images are converted to grayscale 
%  	Can open MAT files containing variable IMAGES 
%  	Works with TEXT files containing image arrays or individual files
%   Works with Kratos VAMAS format XPS photoelectron images *.vms
%
%  To run, type:
%  image_mva_0
%
%  created by K.Artyushkova
%  January 2004

% Kateryna Artyushkova
% Postdoctoral Scientist
% Department of Chemical and Nuclear Engineering
% The University of New Mexico
% (505) 277-0750
% kartyush@unm.edu 


% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @image_mva_0_OpeningFcn, ...
                   'gui_OutputFcn',  @image_mva_0_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 image_mva_0 is made visible.
function image_mva_0_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 image_mva_0 (see VARARGIN)

% Choose default command line output for image_mva_0
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes image_mva_0 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = image_mva_0_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 open_image_Callback(hObject, eventdata, handles)
% hObject    handle to open_image (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
M = inputdlg('how many Images to open?');
M=str2double(M);
if M==1;
    HANDLE = errordlg('More than 1 image has to be open in order to apply MVA','Opening Images');
else
    for i=1:M
     [filename, pathname] = uigetfile('*.*');
     cd(pathname)
     image=imread(filename);
     [n,m,p]=size(image);
     if p==3
         image=rgb2gray(image);
         data(:,:,i)=double(image);
     else
         data(:,:,i)=double(image);
     end
 end
end
handles.or_image=data;
handles.image=data;
handles.N=1;
[n,m,p]=size(data);
set(handles.Min,'string',1);
set(handles.Max,'string',p);
set(handles.current,'string',1);
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imshow(data(:,:,1),[min(min(data(:,:,1))) max(max(data(:,:,1)))])
set(handles.current,'string',1);
guidata(hObject,handles)

% --------------------------------------------------------------------
function open_ascii_Callback(hObject, eventdata, handles)
% hObject    handle to open_ascii (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

method=questdlg('Are all images in one file?', 'Open text images','Yes','No ','Yes');
if method=='Yes'
     [filename, pathname] = uigetfile('*.txt');
     cd(pathname)
     image=load(filename);
     prompt={'X:','Y:','Z:'};
     def={'256','256','20'};
     dlgTitle='Enter the size of the array';
     lineNo=1;
     answer=inputdlg(prompt,dlgTitle,lineNo,def);
     M=str2double(answer);
     [n,m]=size(image);
     if m==M(1)
        k=M(2)*M(3);
        if n==k
            image=image';
            data=reshape(image,[M(1) M(2) M(3)]);
        else
            HANDLE = errordlg('The number of elements in array does not correspond to X Y Z values','Opening Images');
        end
    elseif m==1
        k=M(1)*M(2)*M(3);
        if n==k
             data=reshape(image,[M(1) M(2) M(3)]);
         else
             HANDLE = errordlg('The number of elements in array does not correspond to X Y Z values','Opening Images');
        end
    elseif m==M(2)
        k=M(1)*M(3);
        if n==k
           data=reshape(image,[M(1) M(2) M(3)]);
        else
            HANDLE = errordlg('The number of elements in array does not correspond to X Y Z values','Opening Images');
        end
    elseif m==M(3)
         k=M(1)*M(2);
        if n==k
           data=reshape(image,[M(1) M(2) M(3)]);
        else
            HANDLE = errordlg('The number of elements in array does not correspond to X Y Z values','Opening Images');
        end
    else
    end
    
else
     M = inputdlg('how many Images to open?');
    M=str2double(M);
    if M==1;
        HANDLE = errordlg('More than 1 image has to be open in order to apply MVA','Opening Images');
    else
        prompt={'X:','Y:'};
        def={'256','256'};
        dlgTitle='Enter the size of image';
        lineNo=1;
         answer=inputdlg(prompt,dlgTitle,lineNo,def);
         N=str2double(answer);
         for i=1:M
             [filename, pathname] = uigetfile('*.txt');
             cd(pathname)
             image=load(filename);
             [n,m]=size(image);
             if m==1
                 k=N(1)*N(2);
                 if k==n
                      data(:,:,i)=reshape(image,[N(1) N(2)]);
                  else
                      HANDLE = errordlg('The number of elements in array does not correspond to X Y values','Opening Images');
                  end
              elseif n==N(1)
                  data(:,:,i)=image;
              elseif n==N(2);
                  data(:,:,i)=image';
              else
                  HANDLE = errordlg('The number of elements in array does not correspond to X Y values','Opening Images');
              end
          end
      end
  end



handles.or_image=data;
handles.image=data;
handles.N=1;
[n,m,p]=size(data);
set(handles.Min,'string',1);
set(handles.Max,'string',p);
set(handles.current,'string',1);
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imshow(data(:,:,1),[min(min(data(:,:,1))) max(max(data(:,:,1)))])
set(handles.current,'string',1);
guidata(hObject,handles) 
        
       

% --------------------------------------------------------------------
function open_vms_Callback(hObject, eventdata, handles)
% hObject    handle to open_vms (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
M = inputdlg('how many vms images to open?');
M=str2double(M);
if M==1;
    HANDLE = errordlg('More than 1 image has to be open in order to apply MVA','Opening Images');
else
    for i=1:M
      [datafile,datapath] = uigetfile('*.*','Choose a vms file');
      cd(datapath)
     data(:,:,i)=vms_im_read(datafile,0);
end
end

handles.or_image=data;
handles.image=data;
handles.N=1;
[n,m,p]=size(data);
set(handles.Min,'string',1);
set(handles.Max,'string',p);
set(handles.current,'string',1);
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imshow(data(:,:,1),[min(min(data(:,:,1))) max(max(data(:,:,1)))])
set(handles.current,'string',1);
guidata(hObject,handles)



% --------------------------------------------------------------------
function open_mat_Callback(hObject, eventdata, handles)
% hObject    handle to open_mat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[datafile,datapath] = uigetfile('*.mat*','Choose a data file');
cd(datapath)
temp=load(datafile);
names=fieldnames(temp);
clear I
I=strmatch('images', names);
a=isempty(I);
if a==1
    HANDLE = errordlg('The images in MAT file should be named IMAGES in order to be opened','Opening MAT file');
else
   data = getfield(temp,names{I});
    handles.image=data;
handles.or_image=data;
handles.N=1;
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imshow(data(:,:,1),[min(min(data(:,:,1))) max(max(data(:,:,1)))])
[n,m,p]=size(data);
set(handles.Min,'string',1);
set(handles.Max,'string',p);
set(handles.current,'string',1);
guidata(hObject,handles)
end

    
% --------------------------------------------------------------------
function save_spectra_Callback(hObject, eventdata, handles)
% hObject    handle to save_spectra (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
datapath = uigetdir;
cd(datapath)
images=handles.image;
[filename, pathname] = uiputfile('*.mat', 'Save images as');
save(filename)


% --- Executes during object creation, after setting all properties.
function image_selection_CreateFcn(hObject, eventdata, handles)
% hObject    handle to image_selection (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, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function image_selection_Callback(hObject, eventdata, handles)
% hObject    handle to image_selection (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


image=handles.image;
[n,m,p]=size(image);

set(handles.Min,'string',1);
set(handles.Max,'string',p);

step=1/p;
slider_step(1)=step;
slider_step(2)=step;
if step==1;
    set(handles.image_selection, 'SliderStep', slider_step, 'Max', 2, 'Min',0,'Value',1)
    i=1;
else
    set(handles.image_selection, 'SliderStep', slider_step, 'Max', p, 'Min',0)
    i=get(hObject,'Value');
    i=round(i);
    if i==0
        i=1;
    elseif i>=p
        i=p;
    else i=i;
    end
end
set(handles.current,'string',i);

axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
if p==1;
    imshow(image,[min(min(image)), max(max(image))])
else
    imshow(image(:,:,i),[min(min(image(:,:,i))) max(max(image(:,:,i)))])
end
handles.N=i;
guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function pca_Ncomp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pca_Ncomp (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


function pca_Ncomp_Callback(hObject, eventdata, handles)
% hObject    handle to pca_Ncomp (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 pca_Ncomp as text
%        str2double(get(hObject,'String')) returns contents of pca_Ncomp as a double

Npca=str2double(get(hObject,'String')) ;
handles.Npca=Npca;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function pca_scal_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pca_scal (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function pca_scal_Callback(hObject, eventdata, handles)
% hObject    handle to pca_scal (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 pca_scal as text
%        str2double(get(hObject,'String')) returns contents of pca_scal as a double
scal=get(hObject,'String');
handles.scal=scal;
guidata(hObject,handles)



% --- Executes on button press in pca_main.
function pca_main_Callback(hObject, eventdata, handles)
% hObject    handle to pca_main (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

Npca=handles.Npca;
scal=handles.scal;
data=handles.image;
[n,m,p]=size(data);
data=reshape(data,[n*m p]);
h = waitbar(0,'Please wait while PCA is performed...');
if scal=='none'
     [Y, LBD, C2, CC] = pca(data,Npca);
     scores=reshape(Y,[n, m, Npca]);
     loads =CC;
else
     [Y, lbd, cos2, CC] = pca_mean(data,Npca);
     scores=reshape(Y,[n, m, Npca]);
     loads =CC;
 end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -