function varargout = ngui(varargin)
% NGUI MATLAB code for ngui.fig
%      NGUI, by itself, creates a new NGUI or raises the existing
%      singleton*.
%
%      H = NGUI returns the handle to a new NGUI or the handle to
%      the existing singleton*.
%
%      NGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in NGUI.M with the given input arguments.
%
%      NGUI('Property','Value',...) creates a new NGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ngui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ngui_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 ngui

% Last Modified by GUIDE v2.5 30-Mar-2021 22:59:22

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ngui_OpeningFcn, ...
                   'gui_OutputFcn',  @ngui_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 ngui is made visible.
function ngui_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 ngui (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = ngui_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% 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)
% Specify the folder where the files live.
myFolder = 'D:\regionGrowing_MLT\Images';
% Check to make sure that folder actually exists.  Warn user if it doesn't.
if ~isfolder(myFolder)
    errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
    uiwait(warndlg(errorMessage));
    myFolder = uigetdir(); % Ask for a new one.
    if myFolder == 0
         % User clicked Cancel
         return;
    end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
num_files=length(theFiles);
imgs=cell(1,num_files);
for k = 1 : length(theFiles)
    baseFileName = theFiles(k).name;
    fullFileName = fullfile(theFiles(k).folder, baseFileName);
    fprintf(1, 'Now reading %s\n', fullFileName);
    % Now do whatever you want with this file name,
    % such as reading it in as an image array with imread()
    imageArray = imread(fullFileName);
    imshow(imageArray);  % Display image.
    drawnow; % Force display to update immediately.
    InputImage=imageArray;
    
    axes(handles.axes1);
   
    
    handles.InputImage=InputImage;
    handles.num_files=num_files;
end

    % Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
InputImage=handles.InputImage;
num_files=handles.num_files;
for k = 1 : num_files
   

%     InputImage=handles.InputImage;
    
    
    try
        InputImage=rgb2gray(InputImage)
    end
    x=300; y=340;
    a=imgaussfilt(InputImage,2);
    b=adapthisteq(a);
    m=regiongrowing_MLT(b,x,y,12);
    m=imfill(m,'holes');
    % figure,imshow(m)
    bw=imbinarize(m);
    bw=bwareafilt(bw,1);
    % figure,imshow(bw)
    I = imresize(InputImage,.5);  %-- make image smaller 
    m1 = imresize(bw,.5);  %     for fast computation
    % subplot(2,2,1); imshow(I); title('Input Image');
    % subplot(2,2,2); imshow(m1); title('Initialization');
    % subplot(2,2,3); title('Segmentation');
    seg = region_seg(I, m1, 30); %-- Run segmentation
    % subplot(2,2,4); imshow(seg); title('Global Region-Based Segmentation');
    imshow(seg);
    axes(handles.axes2);
    handles.seg=seg;
end
guidata(hObject, handles);