如何使用Matlab灰度分层变换伪彩图像处理系统GUI
这款MATLAB灰度分层变换伪彩图像处理系统GUI软件是一款简单易用的图像处理工具,专为灰度伪彩色图像处理设计。软件包含灰度分层和灰度变换功能,并提供滤波亮度调节模块。界面设计直观,包含多种按钮和参数设置,方便用户进行图像处理操作。
主要功能:
该软件支持灰度伪彩色图像处理,主要功能包括灰度分层和灰度变换方法。
推荐系统要求: MATLAB R2024a或以上版本 - 支持Windows、macOS操作系统。
文章目录
以下是基于Matlab的灰度分层变换伪彩图像处理系统的GUI设计及代码实现。此系统旨在通过灰度分层变换技术,将灰度图像转换为伪彩色图像,并提供用户交互界面。
功能概述
- 图像加载:用户可以选择本地图片。
- 灰度化:将彩色图像转换为灰度图像。
- 灰度分层变换:根据灰度值范围分配不同的颜色。
- 伪彩显示:生成并显示伪彩色图像。
- 保存结果:允许用户保存处理后的伪彩色图像。
GUI界面设计
使用Matlab的GUIDE
工具创建图形用户界面,包含以下组件:
- 按钮:
- 加载图片
- 开始处理
- 保存结果
- 退出程序
- 图像显示区域:
- 原始图片
- 灰度图片
- 伪彩色图片
代码实现
1. GUI界面初始化
function varargout = PseudoColorGUI(varargin)
% PSEUDOCOLORGUI MATLAB code for PseudoColorGUI.fig
% PSEUDOCOLORGUI, by itself, creates a new PSEUDOCOLORGUI or raises the existing
% singleton*.
%
% H = PSEUDOCOLORGUI returns the handle to a new PSEUDOCOLORGUI or the handle to
% the existing singleton*.
%
% PSEUDOCOLORGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in PSEUDOCOLORGUI.M with the given input arguments.
%
% PSEUDOCOLORGUI('Property','Value',...) creates a new PSEUDOCOLORGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before PseudoColorGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to PseudoColorGUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help PseudoColorGUI
% Last Modified by GUIDE v2.5 20-Mar-2025 14:25:36
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @PseudoColorGUI_OpeningFcn, ...
'gui_OutputFcn', @PseudoColorGUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
2. 回调函数
function PseudoColorGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to PseudoColorGUI (see VARARGIN)
% Choose default command line output for PseudoColorGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UI Initialization
set(handles.axes1, 'Visible', 'off'); % Original Image
set(handles.axes2, 'Visible', 'off'); % Grayscale Image
set(handles.axes3, 'Visible', 'off'); % Pseudo Color Image
3. 图片加载功能
function loadImage_Callback(hObject, eventdata, handles)
% hObject handle to loadImage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.jpg;*.png;*.bmp', 'Image Files (*.jpg, *.png, *.bmp)'}, '选择图片');
if isequal(filename, 0)
return;
end
fullpath = fullfile(pathname, filename);
img = imread(fullpath);
% Display original image
axes(handles.axes1);
imshow(img);
title('原始图片');
set(handles.axes1, 'Visible', 'on');
% Store image in handles
handles.img = img;
guidata(hObject, handles);
4. 灰度分层变换与伪彩处理
function processImage_Callback(hObject, eventdata, handles)
% hObject handle to processImage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~isfield(handles, 'img')
errordlg('请先加载图片!', '错误');
return;
end
img = handles.img;
% Convert to grayscale
grayImg = rgb2gray(img);
% Display grayscale image
axes(handles.axes2);
imshow(grayImg);
title('灰度图片');
set(handles.axes2, 'Visible', 'on');
% Gray level slicing (pseudo-color transformation)
pseudoColorImg = gray2pseudocolor(grayImg);
% Display pseudo-color image
axes(handles.axes3);
imshow(pseudoColorImg);
title('伪彩色图片');
set(handles.axes3, 'Visible', 'on');
% Store processed image in handles
handles.pseudoColorImg = pseudoColorImg;
guidata(hObject, handles);
灰度分层变换函数
function pseudoColorImg = gray2pseudocolor(grayImg)
% GRAY2PSEUDOCOLOR Convert grayscale image to pseudo-color image using gray level slicing.
% Define color map for pseudo-color transformation
colorMap = [
0 0 1; % Blue for low intensity
0 1 0; % Green for medium intensity
1 0 0]; % Red for high intensity
% Normalize gray image to [0, 1]
grayImg = double(grayImg) / 255;
% Assign colors based on intensity ranges
pseudoColorImg = zeros([size(grayImg), 3]);
pseudoColorImg(repmat(grayImg < 0.33, [1, 1, 3])) = repmat(colorMap(1, :), [sum(grayImg(:) < 0.33), 1]);
pseudoColorImg(repmat(grayImg >= 0.33 & grayImg < 0.67, [1, 1, 3])) = repmat(colorMap(2, :), [sum(grayImg(:) >= 0.33 & grayImg(:) < 0.67), 1]);
pseudoColorImg(repmat(grayImg >= 0.67, [1, 1, 3])) = repmat(colorMap(3, :), [sum(grayImg(:) >= 0.67), 1]);
% Convert to uint8 for display
pseudoColorImg = uint8(pseudoColorImg * 255);
end
5. 保存处理结果
function saveImage_Callback(hObject, eventdata, handles)
% hObject handle to saveImage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~isfield(handles, 'pseudoColorImg')
errordlg('请先处理图片!', '错误');
return;
end
[filename, pathname] = uiputfile({'*.png', 'PNG Image (*.png)'; '*.jpg', 'JPEG Image (*.jpg)'}, '保存图片');
if isequal(filename, 0)
return;
end
fullpath = fullfile(pathname, filename);
imwrite(handles.pseudoColorImg, fullpath);
msgbox('图片已保存!', '提示');
6. 退出功能
function exitButton_Callback(hObject, eventdata, handles)
% hObject handle to exitButton (见 GCBO)
% eventdata 保留 - 在未来版本的MATLAB中定义 (见 VARARGIN)
% handles 包含句柄和用户数据的结构体 (见 GUIDATA)
close(gcf);
总结
上述代码实现了基于Matlab的灰度分层变换伪彩图像处理系统,包括GUI设计、灰度化、伪彩处理和保存功能。用户可以加载图片并获得伪彩色图像的处理结果。