Matlab 计算机视觉实验-UI

mango57
2022-05-01 / 0 评论 / 1,026 阅读 / 正在检测是否收录...

UI

UI

代码

classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure     matlab.ui.Figure
        UIAxes2      matlab.ui.control.UIAxes
        UIAxes       matlab.ui.control.UIAxes
        Panel        matlab.ui.container.Panel
        Button       matlab.ui.control.Button
        Button_3     matlab.ui.control.Button
        Button_2     matlab.ui.control.Button
        SobelButton  matlab.ui.control.Button
        CannyButton  matlab.ui.control.Button
        Button_4     matlab.ui.control.Button
        Button_5     matlab.ui.control.Button
        Button_6     matlab.ui.control.Button
        Button_7     matlab.ui.control.Button
        Button_8     matlab.ui.control.Button
        Button_9     matlab.ui.control.Button
        Button_10    matlab.ui.control.Button
        Button_12    matlab.ui.control.Button
        Button_13    matlab.ui.control.Button
    end

    
    properties (Access = private)

    end    

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

        % Button pushed function: Button
        function ButtonPushed(app, event)

          global I flie K
          [filename,filepath] = uigetfile({ '*.jpg';'*.bmp';'*.tif';'*.png';'*.jpeg'},'载入图像');
            if isequal(filename,0)||isequal(filepath,0)
                errordlg('没有选中文件','出错');
                return;
            else %file=[pathname,filename];
                file=[filepath,filename];
                I=fullfile(file);
                K=imread(file);
                    imshow(K,'Parent',app.UIAxes2);

            end
        end

        % Button pushed function: Button_2
        function Button_2Pushed(app, event)
           global I BW K
           %[x,map]=imread(K);
            %BW = rgb2gray([x,map]); 
            BW = rgb2gray(K);
            %app.Image2.ImageSource=I1;
            imshow(BW,'Parent',app.UIAxes);
        end

        % Button pushed function: SobelButton
        function SobelButtonPushed(app, event)
           global I K 
           %[x,map]=imread(I);
            %BW = rgb2gray([x,map]);
            BW = rgb2gray(K);
            BW1=edge(BW,'Sobel');
            imshow(BW1,'Parent',app.UIAxes);
        end

        % Button pushed function: CannyButton
        function CannyButtonPushed(app, event)
           global K 
           %[x,map]=imread(I);
            %BW = rgb2gray([x,map]);
            BW = rgb2gray(K); 
            BW2=edge(BW,'Canny');
            imshow(BW2,'Parent',app.UIAxes);            
        end

        % Button pushed function: Button_3
        function Button_3Pushed(app, event)
            global K noise1
            noise1= imnoise(K,'salt & pepper');
            imshow(noise1,'Parent',app.UIAxes);
        end

        % Button pushed function: Button_4
        function Button_4Pushed(app, event)
            global K noise2
            noise2= imnoise(K,'gaussian');
            imshow(noise2,'Parent',app.UIAxes);            
        end

        % Button pushed function: Button_5
        function Button_5Pushed(app, event)
        global K

            faceDetector = vision.CascadeObjectDetector(); % 构造检测器对象。
            facebox = step(faceDetector, K); % 开始检测,将结果存储到facebox变量中
            finalImage = insertShape(K, 'Rectangle', facebox,'LineWidth',5);
            imshow(finalImage,'Parent',app.UIAxes); 
        end

        % Button pushed function: Button_6
        function Button_6Pushed(app, event)
        global vid
        faceDetector = vision.CascadeObjectDetector();
            vid = videoinput('winvideo', 1, 'YUY2_640x480');
            set(vid,'ReturnedColorSpace','rgb');
            vidRes=get(vid,'VideoResolution');
            width=vidRes(1);
            height=vidRes(2);
            nBands=get(vid,'NumberOfBands');
            figure('Name', 'Matlab调用摄像头', 'NumberTitle', 'Off', 'ToolBar', 'None', 'MenuBar', 'None');
            uicontrol('String', 'Close', 'Callback', 'close(gcf)');
            hImage=image(zeros(vidRes(2),vidRes(1),nBands)); 
            preview(vid,hImage);
        end

        % Button pushed function: Button_7
        function Button_7Pushed(app, event)
            faceDetector = vision.CascadeObjectDetector(); %enable viola jones algorithm
            bbox = [100 100 100 100];
            vidDevice = imaq.VideoDevice('winvideo',1,'YUY2_640x480','ROI', [1 1 640 480],'ReturnedColorSpace','rgb');
            boxInserter = vision.ShapeInserter('BorderColor','Custom','CustomBorderColor',[0 255 0]);
            nFrame =300;
            vidInfo = imaqhwinfo(vidDevice);
            vidHeight = vidInfo.MaxHeight;
            vidWidth = vidInfo.MaxWidth;
            videoPlayer = vision.VideoPlayer('Position',[300 100 640+30 480+30]);
 
             for k = 1:nFrame % start recording with 300 frames
             tic; % timer start
            videoFrame = step(vidDevice); % enable the image capture by webcam
             bbox = 4 * faceDetector.step(imresize(videoFrame, 1/4)); % boost video's fps
            videoOut = step(boxInserter, videoFrame, bbox); % highlight the boxes of face at video
             %release(boxInserter);
            step(videoPlayer, videoOut); % display the video live in video player

          end
        end

        % Button pushed function: Button_8
        function Button_8Pushed(app, event)
       global I K
           %[x,map]=imread(I);
            %ori_im = rgb2gray([x,map]); 
            %ori_im = imread(I);
                %ori_im = rgb2gray(uint8(ori_im));
                ori_im = rgb2gray(uint8(K));
% fx = [5 0 -5;8 0 -8;5 0 -5];          % 高斯函数一阶微分,x方向(用于改进的Harris角点提取算法)
fx = [-2 -1 0 1 2];                 % x方向梯度算子(用于Harris角点提取算法)
Ix = filter2(fx,ori_im);              % x方向滤波
% fy = [5 8 5;0 0 0;-5 -8 -5];          % 高斯函数一阶微分,y方向(用于改进的Harris角点提取算法)
fy = [-2;-1;0;1;2];                 % y方向梯度算子(用于Harris角点提取算法)
Iy = filter2(fy,ori_im);              % y方向滤波
Ix2 = Ix.^2;
Iy2 = Iy.^2;
Ixy = Ix.*Iy;
clear Ix;
clear Iy;
 
h= fspecial('gaussian',[7 7],2);      % 产生7*7的高斯窗函数,sigma=2
 
Ix2 = filter2(h,Ix2);
Iy2 = filter2(h,Iy2);
Ixy = filter2(h,Ixy);
 
height = size(ori_im,1);
width = size(ori_im,2);
result = zeros(height,width);         % 纪录角点位置,角点处值为1
 
R = zeros(height,width);
for i = 1:height
    for j = 1:width
        M = [Ix2(i,j) Ixy(i,j);Ixy(i,j) Iy2(i,j)];             % auto correlation matrix
        R(i,j) = det(M)-0.06*(trace(M))^2;   
    end
end
cnt = 0;
for i = 2:height-1
    for j = 2:width-1
        % 进行非极大抑制,窗口大小3*3
        if  R(i,j) > R(i-1,j-1) && R(i,j) > R(i-1,j) && R(i,j) > R(i-1,j+1) && R(i,j) > R(i,j-1) && R(i,j) > R(i,j+1) && R(i,j) > R(i+1,j-1) && R(i,j) > R(i+1,j) && R(i,j) > R(i+1,j+1)
            result(i,j) = 1;
            cnt = cnt+1;
        end
    end
end
Rsort=zeros(cnt,1);
[posr, posc] = find(result == 1);
for i=1:cnt
    Rsort(i)=R(posr(i),posc(i));
end
[Rsort,ix]=sort(Rsort,1);
Rsort=flipud(Rsort);
ix=flipud(ix);
ps=100;
posr2=zeros(ps,1);
posc2=zeros(ps,1);
for i=1:ps
    posr2(i)=posr(ix(i));
    posc2(i)=posc(ix(i));
end
   
imshow(ori_im,'Parent',app.UIAxes);
hold (app.UIAxes,'on')
 plot(app.UIAxes,posc2,posr2,'g+');
toc;


        end

        % Button pushed function: Button_9
        function Button_9Pushed(app, event)
            cameraCalibrator;
        end

        % Button pushed function: Button_10
        function Button_10Pushed(app, event)
            global I K
            %moveImg=imread(I);
            moveImg=rgb2gray(K);
            [imagePoints,boardSize] = detectCheckerboardPoints(moveImg);%检测棋盘格角点
            %figure(1)
            %imshow(moveImg);
            %hold on;
            %plot(imagePoints(:,1),imagePoints(:,2),'ro');
            movingPoints=imagePoints;
            %hold off
            %
            fixedImg=imread('4.jpg');
            % fixedImg=rgb2gray(fixedImg);
            [imagePoints_fixedImg,boardSize_fixedImg] = detectCheckerboardPoints(fixedImg);
            %figure(2)
            %imshow(fixedImg);
            %hold on;
            %plot(imagePoints_fixedImg(:,1),imagePoints_fixedImg(:,2),'ro');
            %hold off
            tempPoints1=reshape(imagePoints_fixedImg(:,1),boardSize_fixedImg(1)-1,boardSize_fixedImg(2)-1);
            tempPoints2=reshape(imagePoints_fixedImg(:,2),boardSize_fixedImg(1)-1,boardSize_fixedImg(2)-1);
            fixedPoints1=tempPoints1(1:boardSize(1)-1,1:boardSize(2)-1);
            fixedPoints2=tempPoints2(1:boardSize(1)-1,1:boardSize(2)-1);
            fixedPoints=[fixedPoints1(:),fixedPoints2(:)];

            mytform = cp2tform(movingPoints,fixedPoints, 'projective');%求出投影变化的矩阵
            registered = imtransform(moveImg, mytform, 'bicubic');%进行变化
            imshow(imadjust(registered),'Parent',app.UIAxes);


        end

        % Button pushed function: Button_12
        function Button_12Pushed(app, event)
            global I K
           %[x,map]=imread(I);
            %BW = rgb2gray([x,map]);
            BW = rgb2gray(K); 
            W = fspecial('gaussian',[5,5],1); 
            G = imfilter(BW, W, 'replicate');
            imshow(G,'Parent',app.UIAxes);  
        end

        % Button pushed function: Button_13
        function Button_13Pushed(app, event)
        global K
            video_obj = videoinput('winvideo', 1, 'YUY2_640x480');
set(video_obj,'ReturnedColorSpace','rgb');
videoRes = get(video_obj, 'VideoResolution');
nBands = get(video_obj, 'NumberOfBands');
            K = getsnapshot(video_obj);
            imshow(K,'parent',app.UIAxes2);
            imwrite(K,'C:\Users\17709\Desktop\gui\paizhao.jpg')

        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 855 563];
            app.UIFigure.Name = 'UI Figure';

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.UIFigure);
            title(app.UIAxes2, '原图')
            xlabel(app.UIAxes2, '')
            ylabel(app.UIAxes2, '')
            app.UIAxes2.FontUnits = 'points';
            app.UIAxes2.FontSize = 9;
            app.UIAxes2.CLim = [0 1];
            app.UIAxes2.XColor = 'none';
            app.UIAxes2.YColor = 'none';
            app.UIAxes2.TitleFontWeight = 'bold';
            app.UIAxes2.Position = [31 294 377 270];

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, '处理后的图')
            xlabel(app.UIAxes, '')
            ylabel(app.UIAxes, '')
            app.UIAxes.AmbientLightColor = 'none';
            app.UIAxes.MinorGridLineStyle = 'none';
            app.UIAxes.GridColor = 'none';
            app.UIAxes.MinorGridColor = 'none';
            app.UIAxes.XColor = 'none';
            app.UIAxes.YColor = 'none';
            app.UIAxes.TitleFontWeight = 'bold';
            app.UIAxes.BackgroundColor = [0.9412 0.9412 0.9412];
            app.UIAxes.Position = [472 294 371 270];

            % Create Panel
            app.Panel = uipanel(app.UIFigure);
            app.Panel.ForegroundColor = [1 0 0];
            app.Panel.TitlePosition = 'centertop';
            app.Panel.Title = '功能区';
            app.Panel.BackgroundColor = [0.9412 0.9412 0.9412];
            app.Panel.FontSize = 18;
            app.Panel.Position = [31 33 796 238];

            % Create Button
            app.Button = uibutton(app.Panel, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.Button.Position = [670 12 113 47];
            app.Button.Text = '打开图片';

            % Create Button_3
            app.Button_3 = uibutton(app.Panel, 'push');
            app.Button_3.ButtonPushedFcn = createCallbackFcn(app, @Button_3Pushed, true);
            app.Button_3.Position = [26 149 100 25];
            app.Button_3.Text = '椒盐噪声';

            % Create Button_2
            app.Button_2 = uibutton(app.Panel, 'push');
            app.Button_2.ButtonPushedFcn = createCallbackFcn(app, @Button_2Pushed, true);
            app.Button_2.Position = [195 149 100 25];
            app.Button_2.Text = '灰度化';

            % Create SobelButton
            app.SobelButton = uibutton(app.Panel, 'push');
            app.SobelButton.ButtonPushedFcn = createCallbackFcn(app, @SobelButtonPushed, true);
            app.SobelButton.Position = [195 107 100 22];
            app.SobelButton.Text = 'Sobel';

            % Create CannyButton
            app.CannyButton = uibutton(app.Panel, 'push');
            app.CannyButton.ButtonPushedFcn = createCallbackFcn(app, @CannyButtonPushed, true);
            app.CannyButton.Position = [195 58 100 22];
            app.CannyButton.Text = 'Canny';

            % Create Button_4
            app.Button_4 = uibutton(app.Panel, 'push');
            app.Button_4.ButtonPushedFcn = createCallbackFcn(app, @Button_4Pushed, true);
            app.Button_4.Position = [26 106 100 25];
            app.Button_4.Text = '高斯噪声';

            % Create Button_5
            app.Button_5 = uibutton(app.Panel, 'push');
            app.Button_5.ButtonPushedFcn = createCallbackFcn(app, @Button_5Pushed, true);
            app.Button_5.Position = [348 106 100 25];
            app.Button_5.Text = '人脸检测';

            % Create Button_6
            app.Button_6 = uibutton(app.Panel, 'push');
            app.Button_6.ButtonPushedFcn = createCallbackFcn(app, @Button_6Pushed, true);
            app.Button_6.Position = [499 105 100 26];
            app.Button_6.Text = '摄像头预览';

            % Create Button_7
            app.Button_7 = uibutton(app.Panel, 'push');
            app.Button_7.ButtonPushedFcn = createCallbackFcn(app, @Button_7Pushed, true);
            app.Button_7.Position = [499 57 100 25];
            app.Button_7.Text = '摄像头人脸检测';

            % Create Button_8
            app.Button_8 = uibutton(app.Panel, 'push');
            app.Button_8.ButtonPushedFcn = createCallbackFcn(app, @Button_8Pushed, true);
            app.Button_8.Position = [348 149 100 25];
            app.Button_8.Text = '角点检测';

            % Create Button_9
            app.Button_9 = uibutton(app.Panel, 'push');
            app.Button_9.ButtonPushedFcn = createCallbackFcn(app, @Button_9Pushed, true);
            app.Button_9.Position = [642 149 100 25];
            app.Button_9.Text = '相机标定';

            % Create Button_10
            app.Button_10 = uibutton(app.Panel, 'push');
            app.Button_10.ButtonPushedFcn = createCallbackFcn(app, @Button_10Pushed, true);
            app.Button_10.Position = [642 105 100 26];
            app.Button_10.Text = '倾斜矫正';

            % Create Button_12
            app.Button_12 = uibutton(app.Panel, 'push');
            app.Button_12.ButtonPushedFcn = createCallbackFcn(app, @Button_12Pushed, true);
            app.Button_12.Position = [26 56 100 26];
            app.Button_12.Text = '高斯平滑滤波';

            % Create Button_13
            app.Button_13 = uibutton(app.Panel, 'push');
            app.Button_13.ButtonPushedFcn = createCallbackFcn(app, @Button_13Pushed, true);
            app.Button_13.Position = [499 148 100 26];
            app.Button_13.Text = '拍照';

            % 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
0

评论 (0)

取消