2011-11-07 84 views
1

我有以下函數可以計算GLCM,然後計算給定的統計參數。我想將這個函數傳遞給NLFILTER來完成整個圖像的計算(在小窗口中,例如卷積)。我已經那麼NLFILTER設置使用並行計算工具箱來運行,所以我真的很想功能轉換我有如下:用於NLFILTER的MATLAB匿名函數句柄

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 

我真的停留在如何將其轉換爲適合於功能手柄與NLFILTER一起使用。有任何想法嗎?謝謝。

回答

2

當您創建一個匿名函數,你可以在函數定義傳遞附加的,靜態的參數:

%# 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) 
+0

正是我一直在尋找,謝謝! – MBL