MATLAB Anonymous function handle to use with NLFILTER

一笑奈何 提交于 2019-12-11 06:58:27

问题


I have the following function which calculates a GLCM and then a given statistic parameter. I would like to pass this function to NLFILTER to do the calculation for a whole image (in small windows, e.g. convolution). I already have then NLFILTER set up to run using the parallel computing toolbox, so I would really like to convert the function I have below:

function [s]=glcm(img,meth)
%GLCM calculates a Gray Level Co-occurence matrix & stats for a given sub
% image.
% Input: Sub Image (subI) and a method (meth)...
%        'Contrast','Correlation','Energy','Homogeneity'
%

subI=uint8(img);
m=graycomatrix(img,'Offset',[0 1],'NumLevels',8,'Symmetric',true);

if meth(1:3)=='con'
    s=graycoprops(m,'Contrast');
    s=s.Contrast;
elseif meth(1:3)=='cor'
    s=graycoprops(m,'Correlation');
    s=s.Correlation;
elseif meth(1:3)=='ene'
    s=graycoprops(m,'Energy');
    s=s.Energy;        
elseif meth(1:3)=='hom'
    s=graycoprops(m,'Homogeneity');
    s=s.Homogeneity;
else
    error('No method selected.')
end

I am really stuck on how to convert this to a function handle suitable for use with NLFILTER. Any ideas? Thanks.


回答1:


When you create an anonymous function, you can pass additional, static arguments in the function definition:

%# define the method
method = 'ene';

%# create an anonymous function that takes one input argument
%# and that passes the `method` defined above
%# as an argument to glcm
anonFcn = @(x)glcm(x,method);

%# apply to your image with whatever window size you're interested in
out = nlfilter(yourImage,windowSize,anonFcn)


来源:https://stackoverflow.com/questions/8035360/matlab-anonymous-function-handle-to-use-with-nlfilter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!