直方图均衡化matlab gui代码
时间: 2023-09-03 18:09:18 浏览: 169
以下是一个简单的 MATLAB GUI 代码示例,用于对图像进行直方图均衡化:
```matlab
function varargout = histogram_equalization_gui(varargin)
% HISTOGRAM_EQUALIZATION_GUI MATLAB code for histogram_equalization_gui.fig
% HISTOGRAM_EQUALIZATION_GUI, by itself, creates a new HISTOGRAM_EQUALIZATION_GUI or raises the existing
% singleton*.
%
% H = HISTOGRAM_EQUALIZATION_GUI returns the handle to a new HISTOGRAM_EQUALIZATION_GUI or the handle to
% the existing singleton*.
%
% HISTOGRAM_EQUALIZATION_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in HISTOGRAM_EQUALIZATION_GUI.M with the given input arguments.
%
% HISTOGRAM_EQUALIZATION_GUI('Property','Value',...) creates a new HISTOGRAM_EQUALIZATION_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before histogram_equalization_gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to histogram_equalization_gui_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 histogram_equalization_gui
% Last Modified by GUIDE v2.5 30-Apr-2021 10:28:09
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @histogram_equalization_gui_OpeningFcn, ...
'gui_OutputFcn', @histogram_equalization_gui_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
% --- Executes just before histogram_equalization_gui is made visible.
function histogram_equalization_gui_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 histogram_equalization_gui (see VARARGIN)
% Choose default command line output for histogram_equalization_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes histogram_equalization_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = histogram_equalization_gui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Load an image from file
[filename, pathname] = uigetfile({'*.bmp;*.jpg;*.png;*.gif','All Image Files';...
'*.*','All Files' },'Select an image file');
if filename ~= 0
handles.I = imread(fullfile(pathname, filename));
axes(handles.axes1);
imshow(handles.I);
set(handles.pushbutton2,'Enable','on');
end
guidata(hObject, handles);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Convert image to grayscale
I = rgb2gray(handles.I);
% Perform histogram equalization
J = histeq(I);
% Display results
axes(handles.axes2);
imshow(J);
guidata(hObject, handles);
```
此代码使用了 MATLAB GUI,其中包含两个按钮和两个图像框。
第一个按钮(`pushbutton1`)用于加载图像文件,第二个按钮(`pushbutton2`)用于执行直方图均衡化并显示结果。
在 `pushbutton2` 回调函数中,首先将 RGB 图像转换为灰度图像,然后使用 `histeq` 函数执行直方图均衡化,并将结果显示在第二个图像框中。