基于 MATLAB-GUI 图形界面的数字图像处理软件的代码:实现以下功能: 1)图像的读取、显示和保存; 2)图像基本类型转换; 3)灰度图像直方图统计及分析; 4)图像对比度调整; 5)图像添加高斯噪声/椒盐噪声,其中噪声强度可调; 6)对含噪图像进行滤噪处理; 7)可以对鼠标选中的图像区域进行滤镜处理;
时间: 2024-03-03 20:47:47 浏览: 66
以下是一个基于MATLAB GUI的数字图像处理软件的代码,实现了上述功能:
```matlab
function image_processing_gui
% 创建主窗口
main_window = figure('Name', '数字图像处理软件', 'NumberTitle', 'off', 'Position', [400, 200, 600, 500]);
% 创建菜单栏
file_menu = uimenu(main_window, 'Label', '文件');
uimenu(file_menu, 'Label', '打开', 'Callback', @open_image);
uimenu(file_menu, 'Label', '保存', 'Callback', @save_image);
% 创建图像显示区域
image_axes = axes('Parent', main_window, 'Units', 'pixels', 'Position', [50, 50, 400, 400]);
% 创建控制面板
control_panel = uipanel('Parent', main_window, 'Title', '控制面板', 'Units', 'pixels', 'Position', [460, 50, 120, 400]);
% 创建控制按钮
uicontrol('Parent', control_panel, 'Style', 'pushbutton', 'String', '类型转换', 'Position', [10, 340, 100, 30], 'Callback', @type_conversion);
uicontrol('Parent', control_panel, 'Style', 'pushbutton', 'String', '直方图统计', 'Position', [10, 300, 100, 30], 'Callback', @histogram);
uicontrol('Parent', control_panel, 'Style', 'pushbutton', 'String', '对比度调整', 'Position', [10, 260, 100, 30], 'Callback', @adjust_contrast);
uicontrol('Parent', control_panel, 'Style', 'pushbutton', 'String', '添加高斯噪声', 'Position', [10, 220, 100, 30], 'Callback', @add_gaussian_noise);
uicontrol('Parent', control_panel, 'Style', 'pushbutton', 'String', '添加椒盐噪声', 'Position', [10, 180, 100, 30], 'Callback', @add_salt_and_pepper_noise);
uicontrol('Parent', control_panel, 'Style', 'pushbutton', 'String', '滤波处理', 'Position', [10, 140, 100, 30], 'Callback', @filtering);
uicontrol('Parent', control_panel, 'Style', 'pushbutton', 'String', '选择区域', 'Position', [10, 100, 100, 30], 'Callback', @select_region);
% 图像变量
image_data = [];
noisy_image_data = [];
filtered_image_data = [];
region_data = [];
% 打开图像
function open_image(hObject, eventdata, handles)
[file_name, file_path] = uigetfile({'*.jpg;*.png;*.bmp;*.gif', '所有图像文件'}, '选择一个图像文件');
if file_name ~= 0
image_data = imread(fullfile(file_path, file_name));
noisy_image_data = image_data;
filtered_image_data = image_data;
imshow(image_data, 'Parent', image_axes);
end
end
% 保存图像
function save_image(hObject, eventdata, handles)
if ~isempty(image_data)
[file_name, file_path] = uiputfile({'*.jpg;*.png;*.bmp;*.gif', '所有图像文件'}, '保存图像');
if file_name ~= 0
imwrite(image_data, fullfile(file_path, file_name));
end
end
end
% 类型转换
function type_conversion(hObject, eventdata, handles)
if ~isempty(image_data)
image_data = im2double(image_data);
imshow(image_data, 'Parent', image_axes);
end
end
% 直方图统计
function histogram(hObject, eventdata, handles)
if ~isempty(image_data)
gray_image = rgb2gray(image_data);
[counts, x] = imhist(gray_image);
figure('Name', '直方图', 'NumberTitle', 'off');
bar(x, counts);
end
end
% 对比度调整
function adjust_contrast(hObject, eventdata, handles)
if ~isempty(image_data)
image_data = imadjust(image_data);
imshow(image_data, 'Parent', image_axes);
end
end
% 添加高斯噪声
function add_gaussian_noise(hObject, eventdata, handles)
if ~isempty(image_data)
noise_intensity = inputdlg('请输入噪声强度:', '添加高斯噪声');
if ~isempty(noise_intensity)
noise_intensity = str2double(noise_intensity{1});
noisy_image_data = imnoise(image_data, 'gaussian', 0, noise_intensity);
imshow(noisy_image_data, 'Parent', image_axes);
end
end
end
% 添加椒盐噪声
function add_salt_and_pepper_noise(hObject, eventdata, handles)
if ~isempty(image_data)
noise_intensity = inputdlg('请输入噪声强度:', '添加椒盐噪声');
if ~isempty(noise_intensity)
noise_intensity = str2double(noise_intensity{1});
noisy_image_data = imnoise(image_data, 'salt & pepper', noise_intensity);
imshow(noisy_image_data, 'Parent', image_axes);
end
end
end
% 滤波处理
function filtering(hObject, eventdata, handles)
if ~isempty(noisy_image_data)
filtered_image_data = medfilt2(noisy_image_data);
imshow(filtered_image_data, 'Parent', image_axes);
end
end
% 选择区域
function select_region(hObject, eventdata, handles)
if ~isempty(image_data)
h = imrect(image_axes);
region_data = wait(h);
delete(h);
if ~isempty(region_data)
filtered_image_data(region_data(2):region_data(2)+region_data(4),region_data(1):region_data(1)+region_data(3)) = medfilt2(noisy_image_data(region_data(2):region_data(2)+region_data(4),region_data(1):region_data(1)+region_data(3)));
imshow(filtered_image_data, 'Parent', image_axes);
end
end
end
end
```
希望能够帮助到你!
阅读全文