观察应力双折射小工具_matlab实现

2024-05-07

用matlab app实现观察应力双折射小工具 ```matlab

classdef stressbri_matlab < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                     matlab.ui.Figure
        college_logo                 matlab.ui.control.Image
        TextArea                     matlab.ui.control.TextArea
        BottomColorButtonGroup       matlab.ui.container.ButtonGroup
        restartButton                matlab.ui.control.Button
        choosebottomimagefileButton  matlab.ui.control.Button
        removethebottomcolorButton   matlab.ui.control.Button
        GrayButtonGroup              matlab.ui.container.ButtonGroup
        histogramButton              matlab.ui.control.Button
        function1Button              matlab.ui.control.Button
        original_grayButton          matlab.ui.control.Button
        ContrastButtonGroup          matlab.ui.container.ButtonGroup
        medianfilterDropDown         matlab.ui.control.DropDown
        medianfilterDropDownLabel    matlab.ui.control.Label
        edgesharpenButton            matlab.ui.control.Button
        original_contrastButton      matlab.ui.control.Button
        SaveButtonGroup              matlab.ui.container.ButtonGroup
        save_imageButton             matlab.ui.control.Button
        save_image_matrixButton      matlab.ui.control.Button
        chooseimagefileButton        matlab.ui.control.Button
        Background_Image             matlab.ui.control.Image
    end


    properties (Access = public)
        image_path
        image_name
        image_bottom_name
        image_bottom_path

        image_origin% 原始的图片
        image_bottom% 原始的底片
        is_subtract = 0% 确保实现了图像相减
        image_subtract% 储存减去原背景的图片

        Length% 定义所选图片的长
        Height% 定义所选图片的高

        image_save% 要保存的图片
        image_matrix_save% 要保存的图片的矩阵形式
    end


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

        % Code that executes after component creation
        function startupFcn(app)
            clc;close all;
            app.UIFigure.Name = "应力双折射实验";
            app.UIFigure.Resize = 'off';%确保窗口不会被放大

            % app.ensure_twoimage_samesize.Enable = 'off';% 确保静态文本
            app.removethebottomcolorButton.Enable = 'off';
            % app.wrong_save_matrix.Enable = 'off';
            % app.OKButton.Enable = 'off';
            app.SaveButtonGroup.Enable = 'off';
            app.ContrastButtonGroup.Enable = 'off';
            app.GrayButtonGroup.Enable = 'off';
            app.choosebottomimagefileButton.Enable = 'off';
            app.restartButton.Enable = 'off';
            % app.SaveButtonGroup.Enable = 'off';

            % set(app.SaveButtonGroup, 'AlphaData', 8);
            % alpha(app.BottomColorButtonGroup, 0.2);
        end

        % Button pushed function: chooseimagefileButton
        function chooseimagefileButtonPushed(app, event)
            [app.image_name, app.image_path] = uigetfile();

            if (ischar(app.image_name) == 0)||(ischar(app.image_path) == 0)
                disp('no file');
                return;
            end

            % image_file_path = [app.image_path app.image_name];
            % disp(image_file_path);
            IMG_path_1 = app.image_path;
            % 去除上级路径中最右边的"\"
            IMG_path = strip(IMG_path_1, "right", "\");
            % disp(IMG_path);
            cd (IMG_path);
            IMG_name = app.image_name;
            app.image_origin = imread(IMG_name);

            app.ContrastButtonGroup.Enable = 'on';
            app.GrayButtonGroup.Enable = 'on';
            app.choosebottomimagefileButton.Enable = 'on';

            %读取所选取向的长宽高
            [app.Height, app.Length, P] = size(app.image_origin);
            % 先高后长!
            % disp(app.Length);
            % 其中P是RGB图像中的3层!
        end

        % Button pushed function: removethebottomcolorButton
        function removethebottomcolorButtonPushed(app, event)
            % push only when the bottom color infect the result!
            IMG_path_1 = app.image_path;
            IMG_path = strip(IMG_path_1, "right", "\");
            cd (IMG_path);
            IMG_name = app.image_name;
            app.image_origin = imread(IMG_name);
            IMG_bottom_path_1 = app.image_bottom_path;
            IMG_bottom_path = strip(IMG_bottom_path_1, "right", "\");
            cd (IMG_bottom_path);
            IMG_bottom_name = app.image_bottom_name;
            app.image_bottom = imread(IMG_bottom_name);


            if size(app.image_origin) ~= size(app.image_bottom)
                error('两张图片的尺寸不一样,无法相减!')
            end


            % A = im2double(app.image_origin);
            A = app.image_origin;
            % 判断是否是由UI界面选取背景颜色图片
                % B = im2double(app.image_bottom);
                B = app.image_bottom;
            % app.image_subtract = imsubtract(A, B);
            % BB = size(B);AA = size(A);
            % disp(BB);disp(AA);
            % !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            % !!!!!!!!这里出错代表背景图片没有被正确缩小或放大!


            gray_img1 = rgb2gray(A);
            gray_img2 = rgb2gray(B);

            hhh = app.Height;
            lll = app.Length;


            img1=imresize(gray_img1,[hhh,lll]);
            img2=imresize(gray_img2,[hhh,lll]);


            aaaa = imsubtract(img1, img2);

            colorImage = ind2rgb(aaaa, jet(256));

            % aaaa(aaaa < 0) = 0;
            figure(2);imshow(colorImage);
            app.image_subtract = colorImage;

            app.removethebottomcolorButton.Enable = 'on';
            app.is_subtract = 1;
        end

        % Button pushed function: choosebottomimagefileButton
        function choosebottomimagefileButtonPushed(app, event)
            [app.image_bottom_name, app.image_bottom_path] = uigetfile();
            % disp(app.image_bottom_path);
            % disp(app.image_bottom_name);
            if (ischar(app.image_bottom_name) == 0)||(ischar(app.image_bottom_path) == 0)
                disp('no file');
                app.restartButton.Enable = 'on';
                return;
            end
            % image_bottom_file_path = [app.image_bottom_path app.image_bottom_name];
            % disp(image_bottom_file_path);
            app.removethebottomcolorButton.Enable = 'on';

            IMG_path_1 = app.image_bottom_path;
            IMG_path = strip(IMG_path_1, "right", "\");
            cd (IMG_path);
            IMG_bottom_name = app.image_bottom_name;
            % app.image_bottom = imread(IMG_bottom_name);
            a_image = imread(IMG_bottom_name);

            % 裁剪所选择的默认颜色图片
            app.image_bottom = imcrop(a_image, [0, 0, app.Length, app.Height]);



            AA = size(app.image_bottom);
            BB = size(app.image_origin);

            disp(AA);disp(BB);



            app.choosebottomimagefileButton.Enable = 'off';
            app.restartButton.Enable = 'on';
            %按比例缩小所选背景颜色图片
            % HH = app.Height;
            % LL = app.Length;
            % app.image_bottom = imresize(a_image, [HH LL]);

            % figure(2);
            % imshow(app.image_bottom);
        end

        % Value changed function: medianfilterDropDown
        function medianfilterDropDownValueChanged(app, event)
            value = app.medianfilterDropDown.Value;
            % 这里不显示图片的原因是imshow()语句显示的都是相减后(及处理后)的图片
            if app.is_subtract
                f2 = im2bw(app.image_subtract);
            else
                f2 = im2bw(app.image_origin);
            end

            IMG_path_1 = app.image_path;
            % 去除上级路径中最右边的"\"
            IMG_path = strip(IMG_path_1, "right", "\");
            % disp(IMG_path);
            cd (IMG_path);
            IMG_name = app.image_name;
            app.image_origin = imread(IMG_name);

            switch value
                case '3x3'
                    figure(1);
                    subplot(1,2,1);
                    if app.is_subtract
                        imshow(app.image_subtract);
                    else
                        imshow(app.image_origin);
                    end
                    title('原始图','FontSize',16,'Color','b');
                    subplot(1,2,2);
                    K1 = medfilt2(f2, [3 3]);
                    imshow(K1);
                    title('3x3中值滤波对比度图','FontSize',16,'Color','b');
                    app.image_save = figure(1);
                    app.image_matrix_save = K1;
                    app.SaveButtonGroup.Enable = 'on';
                case '5x5'
                    figure(1);
                    subplot(1,2,1);
                    if app.is_subtract
                        imshow(app.image_subtract);
                    else
                        imshow(app.image_origin);
                    end
                    title('原始图','FontSize',16,'Color','b');
                    subplot(1,2,2);
                    K1 = medfilt2(f2, [5 5]);
                    imshow(K1);
                    title('5x5中值滤波对比度图','FontSize',16,'Color','b');
                    app.image_save = figure(1);
                    app.image_matrix_save = K1;
                    app.SaveButtonGroup.Enable = 'on';
                case '7x7'
                    figure(1);
                    subplot(1,2,1);
                    if app.is_subtract
                        imshow(app.image_subtract);
                    else
                        imshow(app.image_origin);
                    end
                    title('原始图','FontSize',16,'Color','b');
                    subplot(1,2,2);
                    K1 = medfilt2(f2, [7 7]);
                    imshow(K1);
                    title('7x7中值滤波对比度图','FontSize',16,'Color','b');
                    app.image_save = figure(1);
                    app.image_matrix_save = K1;
                    app.SaveButtonGroup.Enable = 'on';
            end
        end

        % Button pushed function: edgesharpenButton
        function edgesharpenButtonPushed(app, event)
            figure(1);
            subplot(1,2,1);
            if app.is_subtract
                imshow(app.image_subtract);
            else
                imshow(app.image_origin);
            end
            title('原始图','FontSize',18,'Color','b');
            subplot(1,2,2);
            if app.is_subtract
                f2 = im2bw(app.image_subtract);
            else
                f2 = im2bw(app.image_origin);
            end
            w4 = fspecial('laplacian',0);
            g4 = imfilter(f2, w4, 'replicate');
            G4 = f2 - g4;
            imshow(G4);
            title('边缘锐化对比度图','FontSize',18,'Color','b');
            app.image_save = figure(1);
            app.image_matrix_save = G4;
            app.SaveButtonGroup.Enable = 'on';
        end

        % Button pushed function: original_contrastButton
        function original_contrastButtonPushed(app, event)
            figure(1);
            subplot(1,2,1);
            if app.is_subtract
                imshow(app.image_subtract);
            else
                imshow(app.image_origin);
            end
            title('原始图','FontSize',18,'Color','b');
            subplot(1,2,2);
            if app.is_subtract
                original_contrast = im2bw(app.image_subtract);
            else
                original_contrast = im2bw(app.image_origin);
            end
            imshow(original_contrast);
            title('原始对比度图','FontSize',18,'Color','b');
            app.image_save = figure(1);
            app.image_matrix_save = original_contrast;
            app.SaveButtonGroup.Enable = 'on';
        end

        % Button pushed function: function1Button
        function function1ButtonPushed(app, event)
            Img = app.image_origin;
            % if app.is_subtract
            %     % Img = cat(3, app.image_subtract, [], []);
            %     Img = app.image_origin;
            % else
            %     Img = app.image_origin;
            % end
            if length(size(Img))>2
                Img=rgb2gray(Img);
            end
            [height,width]=size(Img);
            [counts1, x] = imhist(Img,256);
            counts2 = counts1/height/width;
            figure(1);
            subplot(2,2,1),
            imshow(Img);title('原始灰度图像');
            subplot(2,2,2),
            stem(x, counts2); title('原始灰度图像直方图');
            NumPixel = zeros(1,256);
            for i = 1:height
                for j = 1: width
                    NumPixel(Img(i,j) + 1) = NumPixel(Img(i,j) + 1) + 1;
                end
            end
            ProbPixel = zeros(1,256);
            for i = 1:256
                ProbPixel(i) = NumPixel(i) / (height * width * 1.0);
            end
            CumuPixel = cumsum(ProbPixel);
            CumuPixel = uint8(255 .* CumuPixel + 0.5);
            for i = 1:height
                for j = 1: width
                    Img(i,j) = CumuPixel(Img(i,j)+1);
                end
            end
            % figure(2),
            subplot(2,2,3),
            imshow(Img); title('直方图均衡化灰度图像');
            [counts1, x] = imhist(Img,256);
            counts2 = counts1/height/width;
            subplot(2,2,4),
            stem(x, counts2); title('直方图均衡化后灰度图像的直方图');
            app.image_save = figure(1);
            app.image_matrix_save = Img;
            app.SaveButtonGroup.Enable = 'on';
        end

        % Button pushed function: histogramButton
        function histogramButtonPushed(app, event)
            figure(1);
            subplot(1,2,1);
            if app.is_subtract
                imshow(app.image_subtract);
            else
                imshow(app.image_origin);
            end
            title('原始图','FontSize',16,'Color','b');
            subplot(1,2,2);
            if app.is_subtract
                K1 = rgb2gray(app.image_subtract);
            else
                K1 = rgb2gray(app.image_origin);
            end
            imhist(K1);
            % K2 = imhist(K1);
            % bar(K2);
            title('原始灰度直方图','FontSize',16,'Color','b');
            app.image_save = figure(1);
            app.image_matrix_save = K1;
            app.SaveButtonGroup.Enable = 'on';
        end

        % Button pushed function: save_image_matrixButton
        function save_image_matrixButtonPushed(app, event)
            IMG_gray = app.image_matrix_save;
            Image_matrix = mat2gray(IMG_gray);
            % uiputfile('*.mat','Image_matrix');
            [matrix_name] = uiputfile("*.mat");
            % matrix_path_1 = ;
            % matrix_path = strip(matrix_path_1, "right", "\");
            % cd (matrix_path);
            save(matrix_name, "Image_matrix");
        end

        % Button pushed function: save_imageButton
        function save_imageButtonPushed(app, event)
            imsave(app.image_save);
        end

        % Button pushed function: original_grayButton
        function original_grayButtonPushed(app, event)
            figure(1);
            subplot(1,2,1);
            if app.is_subtract
                imshow(app.image_subtract);
            else
                imshow(app.image_origin);
            end
            title('原始图','FontSize',16,'Color','b');
            subplot(1,2,2);
            if app.is_subtract
                K1 = rgb2gray(app.image_subtract);
            else
                K1 = rgb2gray(app.image_origin);
            end
            imshow(K1);
            title('原始灰度图','FontSize',16,'Color','b');
            app.image_matrix_save = K1;
            app.image_save = figure(1);
            app.SaveButtonGroup.Enable = 'on';
        end

        % Button pushed function: restartButton
        function restartButtonPushed(app, event)
            app.image_subtract = app.image_origin;
            app.choosebottomimagefileButton.Enable = 'on';
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Get the file path for locating images
            pathToMLAPP = fileparts(mfilename('fullpath'));

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

            % Create Background_Image
            app.Background_Image = uiimage(app.UIFigure);
            app.Background_Image.Position = [-92 -2 914 412];
            app.Background_Image.ImageSource = fullfile(pathToMLAPP, 'BG_pic.png');

            % Create chooseimagefileButton
            app.chooseimagefileButton = uibutton(app.UIFigure, 'push');
            app.chooseimagefileButton.ButtonPushedFcn = createCallbackFcn(app, @chooseimagefileButtonPushed, true);
            app.chooseimagefileButton.BackgroundColor = [1 1 0];
            app.chooseimagefileButton.FontSize = 16;
            app.chooseimagefileButton.Position = [306 276 140 28];
            app.chooseimagefileButton.Text = 'choose image file';

            % Create SaveButtonGroup
            app.SaveButtonGroup = uibuttongroup(app.UIFigure);
            app.SaveButtonGroup.TitlePosition = 'centertop';
            app.SaveButtonGroup.Title = 'Save';
            app.SaveButtonGroup.FontSize = 16;
            app.SaveButtonGroup.Position = [578 106 141 117];

            % Create save_image_matrixButton
            app.save_image_matrixButton = uibutton(app.SaveButtonGroup, 'push');
            app.save_image_matrixButton.ButtonPushedFcn = createCallbackFcn(app, @save_image_matrixButtonPushed, true);
            app.save_image_matrixButton.FontSize = 14;
            app.save_image_matrixButton.Position = [21 55 100 25];
            app.save_image_matrixButton.Text = 'save matrix';

            % Create save_imageButton
            app.save_imageButton = uibutton(app.SaveButtonGroup, 'push');
            app.save_imageButton.ButtonPushedFcn = createCallbackFcn(app, @save_imageButtonPushed, true);
            app.save_imageButton.FontSize = 14;
            app.save_imageButton.Position = [10 14 120 25];
            app.save_imageButton.Text = 'save fixed image';

            % Create ContrastButtonGroup
            app.ContrastButtonGroup = uibuttongroup(app.UIFigure);
            app.ContrastButtonGroup.TitlePosition = 'centertop';
            app.ContrastButtonGroup.Title = 'Contrast';
            app.ContrastButtonGroup.FontSize = 16;
            app.ContrastButtonGroup.Position = [204 78 182 171];

            % Create original_contrastButton
            app.original_contrastButton = uibutton(app.ContrastButtonGroup, 'push');
            app.original_contrastButton.ButtonPushedFcn = createCallbackFcn(app, @original_contrastButtonPushed, true);
            app.original_contrastButton.FontSize = 14;
            app.original_contrastButton.Position = [41 108 100 25];
            app.original_contrastButton.Text = 'original';

            % Create edgesharpenButton
            app.edgesharpenButton = uibutton(app.ContrastButtonGroup, 'push');
            app.edgesharpenButton.ButtonPushedFcn = createCallbackFcn(app, @edgesharpenButtonPushed, true);
            app.edgesharpenButton.FontSize = 14;
            app.edgesharpenButton.Position = [41 67 101 25];
            app.edgesharpenButton.Text = 'edge sharpen';

            % Create medianfilterDropDownLabel
            app.medianfilterDropDownLabel = uilabel(app.ContrastButtonGroup);
            app.medianfilterDropDownLabel.HorizontalAlignment = 'right';
            app.medianfilterDropDownLabel.FontSize = 14;
            app.medianfilterDropDownLabel.Position = [10 26 81 22];
            app.medianfilterDropDownLabel.Text = 'median filter';

            % Create medianfilterDropDown
            app.medianfilterDropDown = uidropdown(app.ContrastButtonGroup);
            app.medianfilterDropDown.Items = {' ', '3x3', '5x5', '7x7'};
            app.medianfilterDropDown.ValueChangedFcn = createCallbackFcn(app, @medianfilterDropDownValueChanged, true);
            app.medianfilterDropDown.FontSize = 14;
            app.medianfilterDropDown.Position = [102 26 71 22];
            app.medianfilterDropDown.Value = ' ';

            % Create GrayButtonGroup
            app.GrayButtonGroup = uibuttongroup(app.UIFigure);
            app.GrayButtonGroup.TitlePosition = 'centertop';
            app.GrayButtonGroup.Title = 'Gray';
            app.GrayButtonGroup.FontSize = 16;
            app.GrayButtonGroup.Position = [385 78 182 171];

            % Create original_grayButton
            app.original_grayButton = uibutton(app.GrayButtonGroup, 'push');
            app.original_grayButton.ButtonPushedFcn = createCallbackFcn(app, @original_grayButtonPushed, true);
            app.original_grayButton.FontSize = 14;
            app.original_grayButton.Position = [41 108 100 25];
            app.original_grayButton.Text = 'original';

            % Create function1Button
            app.function1Button = uibutton(app.GrayButtonGroup, 'push');
            app.function1Button.ButtonPushedFcn = createCallbackFcn(app, @function1ButtonPushed, true);
            app.function1Button.FontSize = 14;
            app.function1Button.Position = [41 26 100 25];
            app.function1Button.Text = 'function1';

            % Create histogramButton
            app.histogramButton = uibutton(app.GrayButtonGroup, 'push');
            app.histogramButton.ButtonPushedFcn = createCallbackFcn(app, @histogramButtonPushed, true);
            app.histogramButton.FontSize = 14;
            app.histogramButton.Position = [41 67 100 25];
            app.histogramButton.Text = 'histogram';

            % Create BottomColorButtonGroup
            app.BottomColorButtonGroup = uibuttongroup(app.UIFigure);
            app.BottomColorButtonGroup.TitlePosition = 'centertop';
            app.BottomColorButtonGroup.Title = 'Bottom Color';
            app.BottomColorButtonGroup.FontSize = 16;
            app.BottomColorButtonGroup.Position = [10 86 182 154];

            % Create removethebottomcolorButton
            app.removethebottomcolorButton = uibutton(app.BottomColorButtonGroup, 'push');
            app.removethebottomcolorButton.ButtonPushedFcn = createCallbackFcn(app, @removethebottomcolorButtonPushed, true);
            app.removethebottomcolorButton.FontSize = 14;
            app.removethebottomcolorButton.Position = [4 51 173 25];
            app.removethebottomcolorButton.Text = 'remove the bottom color';

            % Create choosebottomimagefileButton
            app.choosebottomimagefileButton = uibutton(app.BottomColorButtonGroup, 'push');
            app.choosebottomimagefileButton.ButtonPushedFcn = createCallbackFcn(app, @choosebottomimagefileButtonPushed, true);
            app.choosebottomimagefileButton.FontSize = 14;
            app.choosebottomimagefileButton.Position = [6 92 171 25];
            app.choosebottomimagefileButton.Text = 'choose bottom image file';

            % Create restartButton
            app.restartButton = uibutton(app.BottomColorButtonGroup, 'push');
            app.restartButton.ButtonPushedFcn = createCallbackFcn(app, @restartButtonPushed, true);
            app.restartButton.FontSize = 14;
            app.restartButton.Position = [40 12 106 25];
            app.restartButton.Text = 'restart';

            % Create TextArea
            app.TextArea = uitextarea(app.UIFigure);
            app.TextArea.Editable = 'off';
            app.TextArea.HorizontalAlignment = 'center';
            app.TextArea.FontSize = 24;
            app.TextArea.FontWeight = 'bold';
            app.TextArea.Position = [123 333 485 41];
            app.TextArea.Value = {'观测应力双折射实验图像处理工具'};

            % Create college_logo
            app.college_logo = uiimage(app.UIFigure);
            app.college_logo.Position = [-33 239 280 113];
            app.college_logo.ImageSource = fullfile(pathToMLAPP, 'skd2.png');

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

            % Create UIFigure and components
            createComponents(app)

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

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            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

背景图片

苏科大校徽图片

使用说明