classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure              matlab.ui.Figure
        WczytajMenu           matlab.ui.container.Menu
        KluczowanieMenu       matlab.ui.container.Menu
        ProbkowanieMenu       matlab.ui.container.Menu
        PodmianaMenu          matlab.ui.container.Menu
        RTresholdSliderLabel  matlab.ui.control.Label
        RTresholdSlider       matlab.ui.control.Slider
        GTresholdSliderLabel  matlab.ui.control.Label
        GTresholdSlider       matlab.ui.control.Slider
        BTresholdSliderLabel  matlab.ui.control.Label
        BTresholdSlider       matlab.ui.control.Slider
        UIAxes                matlab.ui.control.UIAxes
        UIAxes2               matlab.ui.control.UIAxes
    end

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

        % Menu selected function: WczytajMenu
        function WczytajMenuSelected(app, event)
            global img
            [file,track]=uigetfile('*.jpg;*.png;*.bmp','Open image');
            image=imread([track,file]);
            img=image;
            
            imshow(img,'Parent',app.UIAxes);
        end

        % Menu selected function: ProbkowanieMenu
        function ProbkowanieMenuSelected(app, event)
            global img;
            Tab=impixel(img);
            max_r=max(Tab(:,1));
            min_r=min(Tab(:,1));
            max_g=max(Tab(:,2));
            min_g=min(Tab(:,2));
            max_b=max(Tab(:,3));
            min_b=min(Tab(:,3));
            prog_R=get(app.RTresholdSlider,'Value');
            prog_G=get(app.GTresholdSlider,'Value');
            prog_B=get(app.BTresholdSlider,'Value');
            
            [rows, cols, ~] = size(img);
            for i = 1:rows
                for j = 1:cols
                    if img(i, j, 1) >= min_r && img(i, j, 1) <= max_r && img(i, j, 2) >= min_g && img(i, j, 2) <= max_g && img(i, j, 3) >= min_b && img(i, j, 3) <= max_b
                        img(i, j, :) = [0, 0, 0];
                    end
                end
            end
            
            imshow(img, 'Parent', app.UIAxes2);
        end

        % Menu selected function: PodmianaMenu
        function PodmianaMenuSelected(app, event)
            global img;
            [file,track]=uigetfile('*.jpg;*.png;*.bmp','Open image');
            image=imread([track,file]);
            tlo = image;
            
            [rows, cols, ~] = size(img);
            
            for i = 1:rows
                for j = 1:cols
                    if img(i, j, 1) == 0 && img(i, j, 2) == 0 && img(i, j, 3) == 0
                        img(i, j, :) = tlo(i, j, :);
                    end
                end
            end
            
            imshow(img, 'Parent', app.UIAxes2);
            
        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 1125 698];
            app.UIFigure.Name = 'MATLAB App';

            % Create WczytajMenu
            app.WczytajMenu = uimenu(app.UIFigure);
            app.WczytajMenu.MenuSelectedFcn = createCallbackFcn(app, @WczytajMenuSelected, true);
            app.WczytajMenu.Text = 'Wczytaj';

            % Create KluczowanieMenu
            app.KluczowanieMenu = uimenu(app.UIFigure);
            app.KluczowanieMenu.Text = 'Kluczowanie';

            % Create ProbkowanieMenu
            app.ProbkowanieMenu = uimenu(app.KluczowanieMenu);
            app.ProbkowanieMenu.MenuSelectedFcn = createCallbackFcn(app, @ProbkowanieMenuSelected, true);
            app.ProbkowanieMenu.Text = 'Probkowanie';

            % Create PodmianaMenu
            app.PodmianaMenu = uimenu(app.KluczowanieMenu);
            app.PodmianaMenu.MenuSelectedFcn = createCallbackFcn(app, @PodmianaMenuSelected, true);
            app.PodmianaMenu.Text = 'Podmiana';

            % Create RTresholdSliderLabel
            app.RTresholdSliderLabel = uilabel(app.UIFigure);
            app.RTresholdSliderLabel.HorizontalAlignment = 'right';
            app.RTresholdSliderLabel.Position = [298 57 64 22];
            app.RTresholdSliderLabel.Text = 'R Treshold';

            % Create RTresholdSlider
            app.RTresholdSlider = uislider(app.UIFigure);
            app.RTresholdSlider.Orientation = 'vertical';
            app.RTresholdSlider.Position = [383 66 3 139];
            app.RTresholdSlider.Value = 20;

            % Create GTresholdSliderLabel
            app.GTresholdSliderLabel = uilabel(app.UIFigure);
            app.GTresholdSliderLabel.HorizontalAlignment = 'right';
            app.GTresholdSliderLabel.Position = [478 57 64 22];
            app.GTresholdSliderLabel.Text = 'G Treshold';

            % Create GTresholdSlider
            app.GTresholdSlider = uislider(app.UIFigure);
            app.GTresholdSlider.Orientation = 'vertical';
            app.GTresholdSlider.Position = [563 66 3 143];
            app.GTresholdSlider.Value = 20;

            % Create BTresholdSliderLabel
            app.BTresholdSliderLabel = uilabel(app.UIFigure);
            app.BTresholdSliderLabel.HorizontalAlignment = 'right';
            app.BTresholdSliderLabel.Position = [662 57 63 22];
            app.BTresholdSliderLabel.Text = 'B Treshold';

            % Create BTresholdSlider
            app.BTresholdSlider = uislider(app.UIFigure);
            app.BTresholdSlider.Orientation = 'vertical';
            app.BTresholdSlider.Position = [746 66 3 143];
            app.BTresholdSlider.Value = 20;

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            app.UIAxes.XTick = [];
            app.UIAxes.XTickLabel = '';
            app.UIAxes.YTick = [];
            app.UIAxes.Box = 'on';
            app.UIAxes.Position = [14 305 544 372];

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.UIFigure);
            app.UIAxes2.XTick = [];
            app.UIAxes2.XTickLabel = '';
            app.UIAxes2.YTick = [];
            app.UIAxes2.YTickLabel = '';
            app.UIAxes2.Box = 'on';
            app.UIAxes2.Position = [565 305 527 372];

            % 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 = app1

            % 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