2012-04-08 382 views
6

我試過值定義:MATLAB錯誤:函數「subsindex」沒有對這些命令類「結構」

im=imread('untitled_test1.jpg'); 
im1=rgb2gray(im); 
im1=medfilt2(im1,[15 15]); 
BW = edge(im1,'sobel'); 

msk=[0 0 0 0 0; 
0 1 1 1 0; 
0 1 1 1 0; 
0 1 1 1 0; 
0 0 0 0 0;]; 
B=conv2(double(BW),double(msk)); 

Ibw = im2bw(B); 
CC = bwconncomp(Ibw); %Ibw is my binary image 
stats = regionprops(CC,'pixellist'); 

% pass all over the stats 
for i=1:length(stats), 
size = length(stats(i).PixelList); 
% check only the relevant stats (the black ellipses) 
if size >150 && size < 600 
    % fill the black pixel by white  

    x = round(mean(stats(i).PixelList(:,2))); 
    y = round(mean(stats(i).PixelList(:,1))); 
    Ibw = imfill(Ibw, [x, y]); 

else 
    Ibw([CC.PixelIdxList{i}]) = false; 
end; 
end; 

(在這裏我還有一個命令行,但我想這個問題是因爲他們的不。)

labeledImage = bwlabel(binaryImage, 8);  % Label each blob so we can make measurements of it 
blobMeasurements = regionprops(labeledImage, Ibw, 'all'); 
numberOfBlobs = size(blobMeasurements, 1); 

我得到這個錯誤信息:

??? Error using ==> subsindex 
Function 'subsindex' is not defined for values of class 'struct'. 

Error in ==> test2 at 129 
numberOfBlobs = size(blobMeasurements, 1); 

什麼錯?

回答

17

你收到這個錯誤,因爲你已經創建了一個變量所謂的「大小」的陰影內置功能SIZE。 MATLAB不是調用函數來計算numberOfBlobs,而是試圖用索引來代替使用結構blobMeasurements作爲索引的變量(不起作用,因爲錯誤消息顯示)。

一般來說,您不應該給變量或函數一個已經存在的函數的名稱(除非您是know what you're doing)。只需將代碼中的變量名稱更改爲「size」以外的其他名稱,發出命令clear size即可從工作區中清除舊的大小變量,然後重新運行代碼。

1

您的錯誤消息告訴您錯誤在numberOfBlobs = size(blobMeasurements, 1);subsindex最有可能在size(..., 1)中用於訪問這些元素。

我假設blobMeasurements是一個結構體(或單個結構體)的數組,對於這個結構體,該操作沒有完全定義。

爲什麼不像以前那樣使用length命令?這在你的代碼中早一點。

相關問題