麻烦整理一份matlab gui图像处理重用函数
时间: 2023-07-06 22:34:57 浏览: 97
好的,下面整理了一份MATLAB GUI图像处理重用函数的示例代码:
```matlab
function varargout = ImageProcessingGUI(varargin)
% IMAGEPROCESSINGGUI MATLAB code for ImageProcessingGUI.fig
% IMAGEPROCESSINGGUI, by itself, creates a new IMAGEPROCESSINGGUI or raises the existing
% singleton*.
%
% H = IMAGEPROCESSINGGUI returns the handle to a new IMAGEPROCESSINGGUI or the handle to
% the existing singleton*.
%
% IMAGEPROCESSINGGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in IMAGEPROCESSINGGUI.M with the given input arguments.
%
% IMAGEPROCESSINGGUI('Property','Value',...) creates a new IMAGEPROCESSINGGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ImageProcessingGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ImageProcessingGUI_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 ImageProcessingGUI
% Last Modified by GUIDE v2.5 29-May-2021 20:22:11
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ImageProcessingGUI_OpeningFcn, ...
'gui_OutputFcn', @ImageProcessingGUI_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 ImageProcessingGUI is made visible.
function ImageProcessingGUI_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 ImageProcessingGUI (see VARARGIN)
% Choose default command line output for ImageProcessingGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ImageProcessingGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% Initialize variables
handles.originalImage = [];
handles.processedImage = [];
handles.ROI = [];
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = ImageProcessingGUI_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 loadImageButton.
function loadImageButton_Callback(hObject, eventdata, handles)
% hObject handle to loadImageButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Open file dialog to choose image file
[filename, pathname] = uigetfile({'*.jpg;*.png;*.bmp;*.tif'}, 'Select Image File');
if isequal(filename,0) || isequal(pathname,0)
return;
end
% Load image
handles.originalImage = imread(fullfile(pathname, filename));
% Display image
axes(handles.originalImageAxes);
imshow(handles.originalImage);
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in resetButton.
function resetButton_Callback(hObject, eventdata, handles)
% hObject handle to resetButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Reset original image and processed image
handles.originalImage = [];
handles.processedImage = [];
% Clear axes
cla(handles.originalImageAxes, 'reset');
cla(handles.processedImageAxes, 'reset');
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in grayscaleButton.
function grayscaleButton_Callback(hObject, eventdata, handles)
% hObject handle to grayscaleButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Check if image is loaded
if isempty(handles.originalImage)
return;
end
% Convert to grayscale
handles.processedImage = rgb2gray(handles.originalImage);
% Display processed image
axes(handles.processedImageAxes);
imshow(handles.processedImage);
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in thresholdButton.
function thresholdButton_Callback(hObject, eventdata, handles)
% hObject handle to thresholdButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Check if image is loaded
if isempty(handles.originalImage)
return;
end
% Convert to grayscale if necessary
if size(handles.originalImage, 3) == 3
handles.processedImage = rgb2gray(handles.originalImage);
else
handles.processedImage = handles.originalImage;
end
% Calculate threshold value
level = graythresh(handles.processedImage);
% Threshold image
handles.processedImage = imbinarize(handles.processedImage, level);
% Display processed image
axes(handles.processedImageAxes);
imshow(handles.processedImage);
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in cropButton.
function cropButton_Callback(hObject, eventdata, handles)
% hObject handle to cropButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Check if image is loaded
if isempty(handles.originalImage)
return;
end
% Get ROI from user
h = imrect(handles.originalImageAxes);
handles.ROI = wait(h);
% Crop image
handles.processedImage = imcrop(handles.originalImage, handles.ROI);
% Display processed image
axes(handles.processedImageAxes);
imshow(handles.processedImage);
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in saveButton.
function saveButton_Callback(hObject, eventdata, handles)
% hObject handle to saveButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Check if image is loaded
if isempty(handles.originalImage)
return;
end
% Open file dialog to choose save location
[filename, pathname] = uiputfile({'*.jpg;*.png;*.bmp;*.tif'}, 'Save Image As');
if isequal(filename,0) || isequal(pathname,0)
return;
end
% Save processed image
imwrite(handles.processedImage, fullfile(pathname, filename));
% Update handles structure
guidata(hObject, handles);
```
这是一个简单的图像处理GUI,包含以下功能:
- 加载图像
- 灰度化
- 阈值分割
- 裁剪
- 保存图像
可以根据需要进行添加或修改。
阅读全文