2012-04-08 375 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命令?这在你的代码中早一点。

相关问题