matlab实现对图像的各种操作的GUI界面

1)用图像相减的方法检测某个固定场景是否有人出现;

2)选用一幅图像,实现图像旋转;

3)对所给定的图像,采用合适的图像增强算法提高图像质量,并对至少三种增强算法进行效果对比;本项目实现了图像亮度增强,图像缩放增强,图像对比度增强, 图像锐化增强,图像去噪,色彩增强

4)选用一幅图像,采用大津法分割该图像;

5)选用一幅合适的图像,采用基于自适应阈值的图像分割方法完成图像分割。

以下是代码展示

function image_augmentation_gui
    % 创建GUI窗口和下拉菜单
    hFig = figure('Name', 'Image Augmentation GUI', 'NumberTitle', 'off', 'Position', [100, 100, 800, 600]);
    
    % 创建下拉菜单1
    hPopup1 = uicontrol(hFig, 'Style', 'popupmenu', 'String', {'Load Image 1', 'Subtract Image', 'Brightness Enhancement', 'Scale Enhancement', 'Otsu Segmentation', 'Adaptive Threshold Segmentation', 'Contrast Enhancement', 'Sharpening Enhancement', 'Noise Removal', 'Color Enhancement'}, 'Position', [50, 30, 150, 20], 'Callback', @button_callback);
    
    % 创建下拉菜单2
    hPopup2 = uicontrol(hFig, 'Style', 'popupmenu', 'String', {'Load Image 2', 'Rotate Image', 'Brightness Enhancement', 'Scale Enhancement'}, 'Position', [250, 30, 150, 20], 'Callback', @button_callback);

    % 创建用于显示图片的Axes
    hAxes1 = axes('Parent', hFig, 'Position', [0.05, 0.4, 0.25, 0.5]);
    hAxes2 = axes('Parent', hFig, 'Position', [0.4, 0.4, 0.25, 0.5]);
    hAxes3 = axes('Parent', hFig, 'Position', [0.75, 0.4, 0.25, 0.5]);

    % 图片变量
    img1 = [];
    img2 = [];

    % 下拉菜单回调函数
    function button_callback(hObject, ~)
        % 获取当前选中的菜单项
        selectedItem = get(hObject, 'Value');

        switch selectedItem
            case 1 % 加载图片
                [filename, pathname] = uigetfile({'*.jpg;*.png;*.bmp', 'Image Files (*.jpg, *.png, *.bmp)'});
                if isequal(filename, 0) || isequal(pathname, 0)
                    return;
                end
                if hObject == hPopup1
                    img1 = imread(fullfile(pathname, filename));
                    imshow(img1, 'Parent', hAxes1);
                else
                    img2 = imread(fullfile(pathname, filename));
                    imshow(img2, 'Parent', hAxes2);
                end
            case 2 % 图像相减或旋转
                if isempty(img1) || isempty(img2)
                    msgbox('请先加载两张图片', '提示', 'warn');
                    return;
                end
                
                if hObject == hPopup1
                    % 图像相减
                    subtracted_img = imsubtract(img1, img2);
                    
                    % 显示相减结果
                    imshow(subtracted_img, 'Parent', hAxes3);
                else
                    % 获取旋转角度
                    angle = get(hSlider, 'Value');
                    
                    % 旋转图像
                    rotated_img = imrotate(img2, angle);
                    
                    % 显示旋转结果
                    imshow(rotated_img, 'Parent', hAxes3);
                end
            case 3 % 图像亮度增强
                if isempty(img1)
                    msgbox('请先加载图片', '提示', 'warn');
                    return;
                end
                
                % 获取亮度等级
                level = get(hSlider, 'Value');
                
                % 图像亮度增强
                enhanced_img = imadjust(img1, [], [], level/50);
                
                % 显示增强结果
                imshow(enhanced_img, 'Parent', hAxes3);
            case 4 % 图像缩放增强
                if isempty(img1)
                    msgbox('请先加载图片', '提示', 'warn');
                    return;
                end
                
                % 获取缩放因子
                factor = get(hSlider, 'Value');
                
                % 图像缩放增强
                enhanced_img = imresize(img1, 1+factor/50);
                
                % 显示增强结果
                imshow(enhanced_img, 'Parent', hAxes3);
            case 5 % 图像分割 - 大津法
                if isempty(img1)
                    msgbox('请先加载图片', '提示', 'warn');
                    return;
                end
                
                % 图像灰度化
                gray_img = rgb2gray(img1);
                
                % 大津阈值分割
                threshold = graythresh(gray_img);
                segmented_img = imbinarize(gray_img, threshold);
                
                % 显示分割结果
                imshow(segmented_img, 'Parent', hAxes3);
            case 6 % 图像分割 - 自适应阈值
                if isempty(img1)
                    msgbox('请先加载图片', '提示', 'warn');
                    return;
                end
                
                % 图像灰度化
                gray_img = rgb2gray(img1);
                
                % 自适应阈值分割
                block_size = round(size(gray_img, 1)/10);
                if mod(block_size, 2) == 0
                    block_size = block_size + 1;
                end
                segmented_img = adaptthresh(gray_img, 0.5, 'NeighborhoodSize', block_size);
                
                % 显示分割结果
                imshow(segmented_img, 'Parent', hAxes3);
            case 7 % 图像对比度增强
                if isempty(img1)
                    msgbox('请先加载图片', '提示', 'warn');
                    return;
                end
                
                % 获取对比度等级
                level = get(hSlider, 'Value');
                
                % 图像对比度增强
                enhanced_img = imadjust(img1, [], [level/100, 1-level/100]);
                
                % 显示增强结果
                imshow(enhanced_img, 'Parent', hAxes3);
            case 8 % 图像锐化增强
                if isempty(img1)
                    msgbox('请先加载图片', '提示', 'warn');
                    return;
                end
                
                % 创建锐化增强滤波器
                sharpen_filter = fspecial('unsharp');
                
                % 图像锐化增强
                enhanced_img = imfilter(img1, sharpen_filter);
                
                % 显示增强结果
                imshow(enhanced_img, 'Parent', hAxes3);
            case 9 % 图像噪声去除
                if isempty(img1)
                    msgbox('请先加载图片', '提示', 'warn');
                    return;
                end
                
                % 图像去噪
                denoised_img = medfilt2(img1);
                
                % 显示去噪结果
                imshow(denoised_img, 'Parent', hAxes3);
            case 10 % 色彩增强
                if isempty(img1)
                    msgbox('请先加载图片', '提示', 'warn');
                    return;
                end
                
                % 图像色彩增强
                enhanced_img = imadjust(img1, [], [], 1.5);
                
                % 显示增强结果
                imshow(enhanced_img, 'Parent', hAxes3);
        end
    end
    
    % 创建用于选择旋转角度、亮度和缩放的滑块
    hSlider = uicontrol(hFig, 'Style', 'slider', 'Position', [450, 30, 150, 20], 'Min', -50, 'Max', 50, 'Value', 0);
    
    % 添加文本标签
    uicontrol('Style', 'text', 'String', 'Rotation Angle / Brightness Level / Scale Factor / Block Size / Contrast Level', 'Position', [450, 60, 300, 20]);
    
    % 添加按钮
    uicontrol('Style', 'pushbutton', 'String', 'Reset', 'Position', [650, 30, 100, 20], 'Callback', @reset_callback);

    % 重置按钮的回调函数
    function reset_callback(~, ~)
        % 重置所有参数和图像
        img1 = [];
        img2 = [];
        cla(hAxes1);
        cla(hAxes2);
        cla(hAxes3);
        set(hSlider, 'Value', 0);
    end
end

GUI界面

此下拉界面实现对第一张图片的选择

此下拉界面实现对第二张图片的选择

点击subtract image

即可实现对两张图片实现相减,即可实现找出两张图片不同的地方。接着,就可以点击第一个下拉框的各种选项,实现对图片的各种操作。

此处的滑块,是对各种增强效果的程度选择,对应的滑块位置对应不同的图像增强系数。

这是一个重置各种参数的按钮。

总结,本项目是通过matlab的各种函数,实现对图片的各种操作,也可以考虑使用python代码实现,但个人认为matlab函数调用比python代码是实现更加快速,便捷,因为matlab函数已经封装好了,不需要再去编辑函数,同时,处理速度也较快,python有时会处理较慢。

猜你喜欢

转载自blog.csdn.net/m0_67250370/article/details/135424861