Code covered by the BSD License  

Highlights from
GUI for Multivariate Image Analysis of Multispectral Images

image thumbnail
from GUI for Multivariate Image Analysis of Multispectral Images by Kateryna Artyushkova
A GUI for MIA of multispectral image data sets (PCA, Simplisma, MCR, classification).

image_mva(varargin)
function varargout = image_mva(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
%
%  created by K.Artyushkova
%  September 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_OpeningFcn, ...
                   'gui_OutputFcn',  @image_mva_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 is made visible.
function image_mva_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 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = image_mva_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)
[filename, pathname]=uigetfiles('*.*','Open images');
cd(pathname)
[N,M]=size(filename);
if M==1;
    HANDLE = errordlg('More than 1 image has to be open in order to apply MVA','Opening Images');
else
    image=imread(char(filename(:,1)));
    [n,m,p]=size(image);
     if p==3
         image=rgb2gray(image);
         data(:,:,1)=double(image);
         data(:,:,M)=double(image);
     else
         data(:,:,1)=double(image);
         data(:,:,M)=double(image);
     end
    for i=2:M
        image=imread(char(filename(:,i)));
       [n,m,p]=size(image);
        if p==3
         image=rgb2gray(image);
         data(:,:,i-1)=double(image);
     else
         data(:,:,i-1)=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')
imagesc(data(:,:,1)), colormap(gray)
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')
imagesc(data(:,:,1)), colormap(gray)
set(handles.current,'string',1);
guidata(hObject,handles) 
        
       
% --------------------------------------------------------------------
function open_tiff_Callback(hObject, eventdata, handles)
% hObject    handle to open_tiff (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('*.tif');
cd(pathname)
M = inputdlg('how many images are in array?');
M=str2double(M);
for i=1:M
    data(:,:,i)=imread(filename,i);
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')
imagesc(data(:,:,1)), colormap(gray)
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)
[filename, pathname]=uigetfiles('*.vms','Open vms files');
cd(pathname)
[N,M]=size(filename);
if M==1;
    HANDLE = errordlg('More than 1 image has to be open in order to apply MVA','Opening Images');
else
    data(:,:,M)=vms_im_read(char(filename(:,1)),0);
    for i=2:M
        data(:,:,i-1)=vms_im_read(char(filename(:,i)),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')
imagesc(data(:,:,1)), colormap(gray)
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)
data=lddlgpls;
data=double(data);
handles.image=data;
handles.or_image=data;
handles.N=1;
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data(:,:,1)), colormap(gray)
[n,m,p]=size(data);
set(handles.Min,'string',1);
set(handles.Max,'string',p);
set(handles.current,'string',1);
guidata(hObject,handles)


    
% --------------------------------------------------------------------
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, 'images')


% --- 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;
   imagesc(image), colormap(gray)

else
   imagesc(image(:,:,i)), colormap(gray)
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;
h = waitbar(0,'Please wait while PCA is performed...');
model=imgpca(data,scal,Npca);

scores=double(model.scores);
loads=model.loads;

handles.scores=scores;
handles.loads=loads;
handles.modelpca=model;
close(h)
H.Position=[633 109 274 414];
figure(H)
subplot(2,1,1)
iptsetpref('ImshowAxesVisible', 'on')
imshow(scores(:,:,1),[min(min(scores(:,:,1))) max(max(scores(:,:,1)))])
colormap(gray)
subplot(2,1,2)
plot(loads(:,1))
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function pca_disp_N_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pca_disp_N (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_disp_N_Callback(hObject, eventdata, handles)
% hObject    handle to pca_disp_N (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_disp_N as text
%        str2double(get(hObject,'String')) returns contents of pca_disp_N as a double


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



% --- Executes on button press in pca_display.
function pca_display_Callback(hObject, eventdata, handles)
% hObject    handle to pca_display (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
scores=handles.scores;
loads=handles.loads;
Icomp=handles.Icomp;

H.Position=[633 109 274 414];
figure(H)
subplot(2,1,1)
iptsetpref('ImshowAxesVisible', 'on')
imshow(scores(:,:,Icomp),[min(min(scores(:,:,Icomp))) max(max(scores(:,:,Icomp)))])
subplot(2,1,2)
plot(loads(:,Icomp))


% --------------------------------------------------------------------
function save_pca_Callback(hObject, eventdata, handles)
% hObject    handle to save_pca (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
modelpca=handles.modelpca;
svdlgpls(modelpca,'save pca model into mat file or workspace')


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


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


% --------------------------------------------------------------------
function normalize_Callback(hObject, eventdata, handles)
% hObject    handle to normalize (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.image;
N=handles.N;
[n,p,q]=size(data);
figure(1)
[a,rect]=imcrop(uint8(data(:,:,N)));
for i=1:q;
   image_crop(:,:,i)=imcrop(data(:,:,i),rect);
end
close(1)
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(image_crop(:,:,N)), colormap(gray)
handles.image=image_crop;
guidata(hObject,handles)

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


% --------------------------------------------------------------------
function smooth_Callback(hObject, eventdata, handles)
% hObject    handle to smooth (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.image;
[n,p,q]=size(data);
type=questdlg('How do you want resize the image?','Image resize','Increase', 'Shrink  ', 'Increase');
if type=='Increase'
    N=inputdlg('Enter N times to increase the image');  
    N=str2double(N);
    for i=1:q;
        data_res(:,:,i) = imresize(data(:,:,i),N,'bicubic');
    end
else
    N=inputdlg('Enter N times to shrink the image');  
    N=str2double(N);
    for i=1:q;
        data_res(:,:,i) = imresize(data(:,:,i),1/N,'bicubic');
    end
end
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data_res(:,:,1)), colormap(gray)
handles.image=data_res;
guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function simp_N_CreateFcn(hObject, eventdata, handles)
% hObject    handle to simp_N (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 simp_N_Callback(hObject, eventdata, handles)
% hObject    handle to simp_N (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 simp_N as text
%        str2double(get(hObject,'String')) returns contents of simp_N as a double
Nsimp=str2double(get(hObject,'String'));
handles.der=0;
handles.simp_N=Nsimp;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function simp_offset_CreateFcn(hObject, eventdata, handles)
% hObject    handle to simp_offset (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 simp_offset_Callback(hObject, eventdata, handles)
% hObject    handle to simp_offset (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 simp_offset as text
%        str2double(get(hObject,'String')) returns contents of simp_offset as a double

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


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

% Hint: get(hObject,'Value') returns toggle state of simpl_2nd


der=get(hObject,'Value');
handles.der=der;
guidata(hObject,handles)


% --- Executes on button press in simplisma_main.
function simplisma_main_Callback(hObject, eventdata, handles)
% hObject    handle to simplisma_main (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
model=handles.modelpca;
offset=handles.offset;
Nsimp=handles.simp_N;
der=handles.der;
images=handles.image;
[n,m,p]=size(images);
data=reshape(images,[n*m p]);
if der==0
    [purspec,purint,purity_spec]=simplisma(data,[1:p], offset,Nsimp);
else
   data2=invder(data');
   [purspec,purint,purity_spec]=simplisma(data,[1:p], offset,Nsimp,data2');
end

pur_images=reshape(purint,[n m Nsimp]);
pur_int=purspec';

H.Position=[633 109 274 414];
figure(H)
subplot(2,1,1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(pur_images(:,:,1)), colormap(gray)
subplot(2,1,2)
plot(pur_int(:,1))
model.scores=uint8(pur_images);
model.loads=pur_int;
handles.modelsimp=model;

handles.pur_images=pur_images;
handles.pur_int=pur_int;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function simp_disp_N_CreateFcn(hObject, eventdata, handles)
% hObject    handle to simp_disp_N (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 simp_disp_N_Callback(hObject, eventdata, handles)
% hObject    handle to simp_disp_N (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 simp_disp_N as text
%        str2double(get(hObject,'String')) returns contents of simp_disp_N as a double

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

% --- Executes on button press in simp_disp.
function simp_disp_Callback(hObject, eventdata, handles)
% hObject    handle to simp_disp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
pur_images=handles.pur_images;
pur_int=handles.pur_int;
Isimp=handles.Isimp;

H.Position=[633 109 274 414];
figure(H)
subplot(2,1,1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(pur_images(:,:,1)), colormap(gray)
subplot(2,1,2)
plot(pur_int(:,Isimp))


% --------------------------------------------------------------------
function save_simpl_Callback(hObject, eventdata, handles)
% hObject    handle to save_simpl (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
modelsimp=handles.modelsimp;
svdlgpls(modelsimp,'save Simplisma model into mat file or workspace')

% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit8 (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 edit8_Callback(hObject, eventdata, handles)
% hObject    handle to edit8 (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 edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double


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

model=handles.modelpca;
images=handles.image;
[n,m,p]=size(images);
data=reshape(images,[n*m p]);

opt=questdlg('Which intialization you want to use?','MCR','Random   ', 'PCA      ', 'Simplisma', 'Random   ');
if opt=='Random   ';
    N=inputdlg('Enter the number of components');  
    Nmcr=str2double(N);
    c0=rand(Nmcr,p);
elseif opt=='PCA      '    
    loads=handles.loads;
    c0=loads';
else
    pur_int=handles.pur_int;
    c0=pur_int';
end

opt=questdlg('Do you want to apply nonnegativity to Concentrations?','MCR','Yes', 'No ', 'Yes');
if opt=='Yes'
   options.ccon='nonneg';
else
   options.ccon='none';
end

opt=questdlg('Do you want to apply nonnegativity to Spectra?','MCR','Yes', 'No ', 'Yes');
if opt=='Yes'
   options.scon='nonneg';
else
   options.scon='none';
end
options.display='off';
options.plots='final';
[c,s] = als(data,c0,options);
[n1,p]=size(c);
mcr_images=reshape(c,[n m p]);
mcr_int=s';

H.Position=[633 109 274 414];
figure(H)
subplot(2,1,1)
iptsetpref('ImshowAxesVisible', 'on')
imshow(mcr_images(:,:,1),[min(min(mcr_images(:,:,1))) max(max(mcr_images(:,:,1)))])
subplot(2,1,2)
plot(mcr_int(:,1))
model.scores=mcr_images;
model.loads=mcr_int;
handles.modelmcr=model;
handles.mcr_images=mcr_images;
handles.mcr_int=mcr_int;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function mcr_disp_N_CreateFcn(hObject, eventdata, handles)
% hObject    handle to mcr_disp_N (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 mcr_disp_N_Callback(hObject, eventdata, handles)
% hObject    handle to mcr_disp_N (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 mcr_disp_N as text
%        str2double(get(hObject,'String')) returns contents of mcr_disp_N as a double
Imcr=str2double(get(hObject,'String')) ;
handles.Imcr=Imcr;
guidata(hObject,handles)



% --- Executes on button press in mcr_disp.
function mcr_disp_Callback(hObject, eventdata, handles)
% hObject    handle to mcr_disp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
mcr_images=handles.mcr_images;
mcr_int=handles.mcr_int;
Imcr=handles.Imcr;

H.Position=[633 109 274 414];
figure(H)
subplot(2,1,1)
iptsetpref('ImshowAxesVisible', 'on')
imshow(mcr_images(:,:,Imcr),[min(min(mcr_images(:,:,Imcr))) max(max(mcr_images(:,:,Imcr)))])
subplot(2,1,2)
plot(mcr_int(:,Imcr))



% --------------------------------------------------------------------
function save_mcr_Callback(hObject, eventdata, handles)
% hObject    handle to save_mcr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
modelmcr=handles.modelmcr;
svdlgpls(modelmcr,'save MCR model into mat file or workspace')


% --------------------------------------------------------------------
function save_Callback(hObject, eventdata, handles)
% hObject    handle to save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%image=handles.image;
%scores=handles.scores;
%loads=handles.loads;
%pur_images=handles.pur_images;
%pur_int=handles.pur_int;
%mcr_images=handles.mcr_images;
%mcr_int=handles.mcr_int;
%class=handles.C;
datapath = uigetdir;
cd(datapath)
[filename, pathname] = uiputfile('*.mat', 'Save results as');
save(filename)


% --- Executes when figure1 window is resized.
function figure1_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


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


% --------------------------------------------------------------------
function Profiler_Callback(hObject, eventdata, handles)
% hObject    handle to Profiler (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.image;
profiles(data,'1')

% --------------------------------------------------------------------
function adjust_levels_Callback(hObject, eventdata, handles)
% hObject    handle to adjust_levels (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.image;
[m,n,p]=size(data);
for i=1:p
    a(i)=max(max(data(:,:,i)));
end
[y,i]=max(a);

k=y/255;

for i=1:p
    data_div(:,:,i)=data(:,:,i)./k;
end
handles.image=data_div;
N=handles.N;
axes(handles.axes1);
iptsetpref('ImshowAxesVisible', 'on')
imagesc(data_div(:,:,N)), colormap(gray)
guidata(hObject,handles);


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

data=handles.image;
data_eq=histogram(data,'1');
handles.image=data_eq;
guidata(hObject,handles);

% --------------------------------------------------------------------
function threshold_Callback(hObject, eventdata, handles)
% hObject    handle to threshold (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.image;
data_thr=threshold(data,'1');
method=questdlg('Do you want to replace images by thresholded?', 'Replace','Yes','No ','No ');
if method=='Yes'
        handles.image_thr=data_thr;
        handles.image=data_thr;
else
        handles.image_thr=data_thr;
        handles.image=data;
end
guidata(hObject,handles);


% --------------------------------------------------------------------
function Undo_Callback(hObject, eventdata, handles)
% hObject    handle to Undo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.or_image;
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
N=handles.N;
imagesc(data(:,:,N)),colormap(gray)
handles.image=data;
guidata(hObject,handles)


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


% --------------------------------------------------------------------
function filter_ave_Callback(hObject, eventdata, handles)
% hObject    handle to filter_ave (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.image;
[m,n,p]=size(data);
Hsize=inputdlg('Enter the kernel size');  
Hsize=str2double(Hsize);
H = fspecial('average',Hsize);
for i=1:p
    data_filt(:,:,i) = imfilter(data(:,:,i),H);
end
N=handles.N;
imagesc(data_filt(:,:,N)),colormap(gray)
handles.image=data_filt;
guidata(hObject,handles)


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

data=handles.image;
[m,n,p]=size(data);
Hsize=inputdlg('Enter the kernel size');  
Hsize=str2double(Hsize);
H = fspecial('gaussian',Hsize);
for i=1:p
    data_filt(:,:,i) = imfilter(data(:,:,i),H);
end
N=handles.N;
imagesc(data_filt(:,:,N)),colormap(gray)
handles.image=data_filt;
guidata(hObject,handles)

% --------------------------------------------------------------------
function unsharp_Callback(hObject, eventdata, handles)
% hObject    handle to unsharp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.image;
[m,n,p]=size(data);
alpha=inputdlg('Enter the alpha parameter of Laplacian filter (from 0.1-1) (default=0.2)');  
alpha=str2double(alpha);
H = fspecial('unsharp',alpha);
for i=1:p
    data_filt(:,:,i) = imfilter(data(:,:,i),H);
end
N=handles.N;
imagesc(data_filt(:,:,N)),colormap(gray)
handles.image=data_filt;
guidata(hObject,handles)


% --------------------------------------------------------------------
function edge_Callback(hObject, eventdata, handles)
% hObject    handle to edge (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
data=handles.image;
[m,n,p]=size(data);
method=questdlg('Select the method for edge extraction?', 'Edge extraction','sobel  ','prewitt','roberts','sobel  ');
if method=='sobel  '
    type='sobel';
elseif method=='prewitt'
    type='prewitt';
else
    type='roberts';
end

for i=1:p
    data_filt(:,:,i) = edge(data(:,:,i),type);
end
N=handles.N;
imagesc(data_filt(:,:,N)),colormap(gray)
handles.image=data_filt;
guidata(hObject,handles)


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


% --------------------------------------------------------------------
function dilation_Callback(hObject, eventdata, handles)
% hObject    handle to dilation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
method=questdlg('Select structuring element', 'Dilation','square','disk  ','ball  ','square');
if method=='square'
     n=inputdlg('Enter the size of box');  
     n=str2double(n);
     se = strel('square',n);
elseif method=='disk  '
     n=inputdlg('Enter the radius of disk');  
     n=str2double(n);
     se = strel('disk',n);
else
     n=inputdlg('Enter the radius of ball');  
     n=str2double(n);
     h=inputdlg('Enter the height of ball');  
     h=str2double(h);
     se = strel('ball',n,h);
 end
data=handles.image;
[m,n,p]=size(data);
for i=1:p
    data_filt(:,:,i) = imdilate(data(:,:,i),se);
end
N=handles.N;
imagesc(data_filt(:,:,N)),colormap(gray)
handles.image=data_filt;
guidata(hObject,handles)

 
 
% --------------------------------------------------------------------
function erosion_Callback(hObject, eventdata, handles)
% hObject    handle to erosion (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
method=questdlg('Select structuring element', 'Erosion','square','disk  ','ball  ','square');
if method=='square'
     n=inputdlg('Enter the size of box');  
     n=str2double(n);
     se = strel('square',n);
elseif method=='disk  '
     n=inputdlg('Enter the radius of disk');  
     n=str2double(n);
     se = strel('disk',n);
else
     n=inputdlg('Enter the radius of ball');  
     n=str2double(n);
     h=inputdlg('Enter the height of ball');  
     h=str2double(h);
     se = strel('ball',n,h);
 end
data=handles.image;
[m,n,p]=size(data);
for i=1:p
    data_filt(:,:,i) = imerode(data(:,:,i),se);
end
N=handles.N;
imagesc(data_filt(:,:,N)),colormap(gray)
handles.image=data_filt;
guidata(hObject,handles)


% --------------------------------------------------------------------
function opening_Callback(hObject, eventdata, handles)
% hObject    handle to opening (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
method=questdlg('Select structuring element', 'Opening','square','disk  ','ball  ','square');
if method=='square'
     n=inputdlg('Enter the size of box');  
     n=str2double(n);
     se = strel('square',n);
elseif method=='disk  '
     n=inputdlg('Enter the radius of disk');  
     n=str2double(n);
     se = strel('disk',n);
else
     n=inputdlg('Enter the radius of ball');  
     n=str2double(n);
     h=inputdlg('Enter the height of ball');  
     h=str2double(h);
     se = strel('ball',n,h);
 end
data=handles.image;
[m,n,p]=size(data);
for i=1:p
    data_filt(:,:,i) = imopen(data(:,:,i),se);
end
N=handles.N;
imagesc(data_filt(:,:,N)),colormap(gray)
handles.image=data_filt;
guidata(hObject,handles)

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

method=questdlg('Select structuring element', 'Opening','square','disk  ','ball  ','square');
if method=='square'
     n=inputdlg('Enter the size of box');  
     n=str2double(n);
     se = strel('square',n);
elseif method=='disk  '
     n=inputdlg('Enter the radius of disk');  
     n=str2double(n);
     se = strel('disk',n);
else
     n=inputdlg('Enter the radius of ball');  
     n=str2double(n);
     h=inputdlg('Enter the height of ball');  
     h=str2double(h);
     se = strel('ball',n,h);
 end
data=handles.image;
[m,n,p]=size(data);
for i=1:p
    data_filt(:,:,i) = imclose(data(:,:,i),se);
end
N=handles.N;
imagesc(data_filt(:,:,N)),colormap(gray)
handles.image=data_filt;
guidata(hObject,handles)




% --- Executes during object creation, after setting all properties.
function class_N_CreateFcn(hObject, eventdata, handles)
% hObject    handle to class_N (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 class_N_Callback(hObject, eventdata, handles)
% hObject    handle to class_N (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 class_N as text
%        str2double(get(hObject,'String')) returns contents of class_N as a double
Nclass=str2double(get(hObject,'String')) ;
handles.Nclass=Nclass;
guidata(hObject,handles)


% --- Executes on button press in classify.
function classify_Callback(hObject, eventdata, handles)
% hObject    handle to classify (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
image=handles.image;
[m,n,p]=size(image);
Data=reshape(image, [m*n p]);
method=questdlg('Select classification method', 'Classification','K means','Fuzzy c','MLE    ', 'K means');
if method=='K means'
    h = waitbar(0,'Please wait while classification is performed...');
    Nclass=handles.Nclass;
    classes=dcKMeans(Data,Nclass);
    C=reshape(classes,[m n]);
    close(h)
elseif method=='Fuzzy c'
    h = waitbar(0,'Please wait while classification is performed...');
    Nclass=handles.Nclass;
    [classes,U] = dcFuzzy(Data,Nclass);
     C=reshape(classes,[m n]);
     close(h)
 else
     C=im_class_MLE(image,'0');
end

H.Position=[563 192 322 297];
figure(H)
imagesc(C)
handles.C=C;
guidata(hObject,handles)
    



% --- Executes on button press in class_disp.
function class_disp_Callback(hObject, eventdata, handles)
% hObject    handle to class_disp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
C=handles.C;
H.Position=[563 192 322 297];
figure(H)
imagesc(C)


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

model=handles.modelpca;
assignin('base','model_pca',model)
imagegui



% --- Executes on button press in explore_simplisma.
function explore_simplisma_Callback(hObject, eventdata, handles)
% hObject    handle to explore_simplisma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
model=handles.modelsimp;
assignin('base','model_simp',model)
imagegui


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

model=handles.modelmcr;
assignin('base','model_mcr',model)
imagegui




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

image=handles.image;
N=inputdlg('Enter the number of slices to combine into one');  
N=str2double(N);
image_subset=array_downsizing(image, N);
axes(handles.axes1)
iptsetpref('ImshowAxesVisible', 'on')
imagesc(image_subset(:,:,1)),colormap(gray)
handles.image=image_subset;
guidata(hObject,handles)

Contact us