matlab界面编程实现灰度变换

时间: 2023-10-16 12:21:57 浏览: 53
灰度变换是指将图像的像素值进行线性或非线性的变换,以改变图像的亮度、对比度等属性。在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”按钮,可以保存处理后的图像文件。在滑动条的回调函数中,将图像的像素值进行线性或非线性的变换,并显示处理后的图像。

相关推荐

最新推荐

recommend-type

基于Matlab的FIR型希尔伯特变换器设计

为了实现数字解调,通常需要借助希尔伯特变换器对信号进行分解,利用Matlab设计希尔伯特变换器是一种最为快捷、有效的方法。通过具体的设计、仿真及对原始信号和经过希尔伯特变换器输出延迟信号的比较,说明Matlab是...
recommend-type

腐蚀和膨胀的matlab实现

本文给大家分享了一个腐蚀和膨胀的matlab实现的源代码。
recommend-type

Radon变换说明及matlab例子.doc

Radon变换: 又称为Hough Transform (数字图像处理课程里学过——数字图像处理课件3-P37) 考虑b=ax+y,将原来的XY平面内的点映射到AB平面上。则原来在XY平面上的一条直线的所有的点,在AB平面上都位于同一个点。通过...
recommend-type

用fft算法实现相关的MATLAB仿真

用fft算法实现相关的MATLAB仿真,该方法易于在FPGA上实现相关算法,比直接用相乘来得简单,而且但相关点数越多计算量相对而言比直接求解减少
recommend-type

STK编程MATLAB操作目录.docx

STK二次开发编程中MATLAB部分命令,原Help文档全为英文版,查询相关命令十分不方便,本人将其进行目录翻译,供各位参考,另外需要STK其它学习资料的可以私信
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。