classdef new < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure       matlab.ui.Figure
        Image          matlab.ui.control.Image
        Image2         matlab.ui.control.Image
        LoadButton     matlab.ui.control.Button
        SegmentButton  matlab.ui.control.Button
    end

    % Callbacks that handle component events
    methods (Access = private)

        % Image clicked function: Image
        function ImageClicked(app, event)
     
        end

        % Button pushed function: LoadButton
        function LoadButtonPushed(app, event)
                   myFolder = 'D:\regionGrowing_MLT\';
% 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);
end
app.Image.ImageSource=fullFileName;
        end

        % Button pushed function: SegmentButton
        function SegmentButtonPushed(app, event)
            J = imread(app.Image.ImageSource);
J=rgb2gray(J);
x=300; y=340;
a=imgaussfilt(J,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(J,.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');
finalimg = region_seg(I, m1, 30); %-- Run segmentation
imwrite(finalimg, 'tmp_finalimg.png')
app.Image2.ImageSource=finalimg;
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'MATLAB App';

            % Create Image
            app.Image = uiimage(app.UIFigure);
            app.Image.ImageClickedFcn = createCallbackFcn(app, @ImageClicked, true);
            app.Image.Position = [176 275 158 148];

            % Create Image2
            app.Image2 = uiimage(app.UIFigure);
            app.Image2.Position = [397 275 147 148];

            % Create LoadButton
            app.LoadButton = uibutton(app.UIFigure, 'push');
            app.LoadButton.ButtonPushedFcn = createCallbackFcn(app, @LoadButtonPushed, true);
            app.LoadButton.Position = [15 385 100 22];
            app.LoadButton.Text = 'Load';

            % Create SegmentButton
            app.SegmentButton = uibutton(app.UIFigure, 'push');
            app.SegmentButton.ButtonPushedFcn = createCallbackFcn(app, @SegmentButtonPushed, true);
            app.SegmentButton.Position = [15 324 100 22];
            app.SegmentButton.Text = 'Segment';

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = new

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end