matlab界面编程实现灰度变换
时间: 2023-10-16 15:21:57 浏览: 117
灰度变换是指将图像的像素值进行线性或非线性的变换,以改变图像的亮度、对比度等属性。在Matlab中,可以使用界面编程实现灰度变换。
以下是一个基于Matlab界面编程实现灰度变换的示例代码:
```matlab
function varargout = Grayscale_Transformation(varargin)
% GRAYSCALE_TRANSFORMATION MATLAB code for Grayscale_Transformation.fig
% GRAYSCALE_TRANSFORMATION, by itself, creates a new GRAYSCALE_TRANSFORMATION or raises the existing
% singleton*.
%
% H = GRAYSCALE_TRANSFORMATION returns the handle to a new GRAYSCALE_TRANSFORMATION or the handle to
% the existing singleton*.
%
% GRAYSCALE_TRANSFORMATION('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GRAYSCALE_TRANSFORMATION.M with the given input arguments.
%
% GRAYSCALE_TRANSFORMATION('Property','Value',...) creates a new GRAYSCALE_TRANSFORMATION or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Grayscale_Transformation_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Grayscale_Transformation_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 Grayscale_Transformation
% Last Modified by GUIDE v2.5 19-Oct-2021 02:53:16
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Grayscale_Transformation_OpeningFcn, ...
'gui_OutputFcn', @Grayscale_Transformation_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 Grayscale_Transformation is made visible.
function Grayscale_Transformation_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 Grayscale_Transformation (see VARARGIN)
% Choose default command line output for Grayscale_Transformation
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Grayscale_Transformation wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% Set default values
set(handles.slider1,'Value',1);
set(handles.slider2,'Value',1);
set(handles.slider3,'Value',1);
% --- Outputs from this function are returned to the command line.
function varargout = Grayscale_Transformation_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 slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get slider value
val1 = get(hObject,'Value');
% Update text box and image
set(handles.edit1,'String',num2str(val1));
update_image(handles);
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get slider value
val2 = get(hObject,'Value');
% Update text box and image
set(handles.edit2,'String',num2str(val2));
update_image(handles);
% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider3_Callback(hObject, eventdata, handles)
% hObject handle to slider3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get slider value
val3 = get(hObject,'Value');
% Update text box and image
set(handles.edit3,'String',num2str(val3));
update_image(handles);
% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- 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 image
[filename,pathname] = uigetfile('*.jpg;*.png;*.bmp','Select image file');
if isequal(filename,0) || isequal(pathname,0)
return;
end
img = imread(fullfile(pathname,filename));
% Display image
axes(handles.axes1);
imshow(img);
title('Original Image');
% Save image data
handles.img = img;
% Set default values
set(handles.slider1,'Value',1);
set(handles.slider2,'Value',1);
set(handles.slider3,'Value',1);
set(handles.edit1,'String',num2str(1));
set(handles.edit2,'String',num2str(1));
set(handles.edit3,'String',num2str(1));
guidata(hObject,handles);
% Update image
update_image(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)
% Save image
[filename,pathname] = uiputfile('*.jpg;*.png;*.bmp','Save image file');
if isequal(filename,0) || isequal(pathname,0)
return;
end
imwrite(handles.img,fullfile(pathname,filename));
% --- Update image
function update_image(handles)
% Get slider values
val1 = get(handles.slider1,'Value');
val2 = get(handles.slider2,'Value');
val3 = get(handles.slider3,'Value');
% Get image data
img = handles.img;
% Apply linear transformation
img = val1*img + val2;
img = img.^val3;
% Display image
axes(handles.axes2);
imshow(img);
title('Transformed Image');
```
该代码创建了一个包含三个滑动条和两个按钮的GUI界面。通过拖动滑动条,可以改变图像的亮度、对比度和Gamma值。通过“Load Image”按钮,可以选择要处理的图像文件。通过“Save Image”按钮,可以保存处理后的图像文件。在滑动条的回调函数中,将图像的像素值进行线性或非线性的变换,并显示处理后的图像。
阅读全文