MATLAB错误:没有为类'struct'的值定义函数'subsindex'

Alo*_*iel 7 matlab

我试过这些命令:

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;
Run Code Online (Sandbox Code Playgroud)

(这里我有另一个命令行,但我猜问题不是因为它们.)

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

我收到此错误消息:

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

Error in ==> test2 at 129
numberOfBlobs = size(blobMeasurements, 1);
Run Code Online (Sandbox Code Playgroud)

出了什么问题?

gno*_*ice 18

你得到了这个错误,因为你创建了一个名为"size" 的变量,它隐藏了内置函数 SIZE.MATLAB不是调用函数来计算numberOfBlobs,而是尝试使用结构作为索引来索引变量blobMeasurements(这不起作用,如错误消息所示).

通常,您不应该为变量或函数指定已存在函数的名称(除非您知道自己在做什么).只需将代码中变量的名称更改为"size"以外的其他名称,发出命令clear size以清除工作区中的旧大小变量,然后重新运行代码.