Matlab函数句柄调用图像K均值分类

来源:互联网 发布:装饰公司网络推广方案 编辑:程序博客网 时间:2024/05/22 12:33


Matlab函数句柄


参考:《Matlab实用教程》


Matlab中提供了函数句柄(function handle)的使用,定义方法为:

         h_function1 = @function1;

        或

         h_function1 = str2func(function1);

函数句柄包含了函数的路径、函数名、类型及可能存在的重载方法。

函数句柄有什么作用呢?

(1)函数的调用,范围更广、速度更快;

(2)使函数调用像使用变量一样方便简单;


例如在GUI设计中,

以函数句柄作为结构数组元素:

gui_State = struct('gui_Name',       mfilename, ...                   'gui_Singleton',  gui_Singleton, ...                   'gui_OpeningFcn', @guideTemp2_OpeningFcn, ...                   'gui_OutputFcn',  @guideTemp2_OutputFcn, ...                   'gui_LayoutFcn',  [] , ...                   'gui_Callback',   []);


点击按钮,使用函数句柄执行子函数 ------  K均值图像分类

function pushbutton1_Callback(hObject, eventdata, handles)[fname,pname] = uigetfile('*.*','Select Image');I_rgb = imread([pname,fname]);feval(@KmeansTemp1, I_rgb, handles );


GUI完整代码:

function varargout = guideTemp2(varargin)% Last Modified by GUIDE v2.5 15-Nov-2016 15:14:25% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name',       mfilename, ...                   'gui_Singleton',  gui_Singleton, ...                   'gui_OpeningFcn', @guideTemp2_OpeningFcn, ...                   'gui_OutputFcn',  @guideTemp2_OutputFcn, ...                   'gui_LayoutFcn',  [] , ...                   'gui_Callback',   []);if nargin && ischar(varargin{1})    gui_State.gui_Callback = str2func(varargin{1});endif 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 guideTemp2 is made visible.function guideTemp2_OpeningFcn(hObject, eventdata, handles, varargin)% Choose default command line output for guideTemp2handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes guideTemp2 wait for user response (see UIRESUME)% uiwait(handles.figure1);global axes1_h; axes1_h = handles.axes1;% --- Outputs from this function are returned to the command line.function varargout = guideTemp2_OutputFcn(hObject, eventdata, handles) % Get default command line output from handles structurevarargout{1} = handles.output;% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)[fname,pname] = uigetfile('*.*','Select Image');I_rgb = imread([pname,fname]);feval(@KmeansTemp1, I_rgb, handles );% --- Executes during object creation, after setting all properties.function axes1_CreateFcn(hObject, eventdata, handles)% Hint: place code in OpeningFcn to populate axes1


k均值图像分类函数代码:

function   KmeansTemp1( I_rgb, handles )% %   Image Classification by K-meansaxes(handles.axes1);  imshow(I_rgb);  hold on;  title('Origin Image');% RGB to lab C = makecform('srgb2lab');        I_lab = applycform(I_rgb, C); ab = double(I_lab(:,:,2:3));      nrows = size(ab,1);               ncols = size(ab,2);               ab = reshape(ab,nrows*ncols,2);   nColors = 4;                      [cluster_idx,cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean','Replicates',3); %使用k均值聚类算法得到的结果对图像进行标记pixel_labels = reshape(cluster_idx,nrows,ncols);  axes(handles.axes2);   imshow(pixel_labels,[]);    title('Cluster Image');%--------segmented_images------segmented_images = cell(1,4);               rgb_label = repmat(pixel_labels,[1 1 3]);    for k = 1:nColors    color = I_rgb;    color(rgb_label ~= k) = 0;    segmented_images{k} = color;endaxes(handles.axes3);ntemp = round(sqrt(nColors));for i=1:nColors    h(i)=subplot(ntemp,ntemp,i);     imshow(segmented_images{i});end%annotation(gcf,'textbox','String',{'Segmented Images'},'FontSize',15,...%           'Position',[0.38 0.92 0.36 0.06],'edgecolor',get(gcf,'color'));end
实验结果:

0 0