I wanted to partition watershed segmentation on original image using spatial criteria and I got this error message: "Subscript indices must either be real positive integers or logicals".

1 view (last 30 days)
Hi pls can anyone help me. I am trying to partition a watershed segmentation image and merge it to the original image. But I got this error don't know how to resolve it. I will appreciate your solutions. This is the error message: "Subscript indices must either be real positive integers or logicals". I am applying spatial criteria for the partitioning so that I can reduce the number of partitions on the image.
NB: the error is in line: B(i,j)= mean(abs(ima(B(i)+B(i)-ima(B(j)+B(i)))));
Thank you in advance.
ima=dicomread('MRI');
LWS=bwlabel(WS); % initial partition
max=max(LWS(:)); % total number of partitions or size
N=max;
for i=1:N;
M(i)=mean(ima(LWS==i)); % calculate the mean intensity of each partition
end
for i=1:N;
j=1:N;
B(i,j)=(i(1,:)-j(1,:));
M(i,j)=abs(M(i)-M(j)); % difference in mean intensity partition i and j.
B(i,j)= mean(abs(ima(B(i)+B(i)-ima(B(j)+B(i)))));
% B(i,j)=mean(abs(ima(LWS==i))-(ima(LWS==j))); % difference intensity btw partition i & j.
p1=bwperim(i:N);
p2=bwperim(j:N);
H=mean(p1+p2);
ind=find(p1==1);
ind2=find(p2==1);
T=0.5;
indices=intersect(ind,ind2)<T; % merging partition
figure,imshow(p1,[]);
figure,imshow(H,[]);
end

Accepted Answer

Walter Roberson
Walter Roberson on 25 Sep 2013
Look at
B(i,j)=(i(1,:)-j(1,:));
You have "for i = 1:N" so "i" will be a scalar. For any given "i" as "i" is in 1:N, there will be a value of j (which is 1:N) which is at least as great, leading to B(i,j) being 0 or negative. Is that what you expect?
You then have
ima(B(j)+B(i))
as part of the expression that is leading to the error message. So you are indexing a two-dimensional array, B, with a single index. That is going to go down columns first, leading to some odd traversals between columns as you expect the first dimension of B as the "for i" loop proceeds (you do not pre-allocate B when you assign to B(i,j)).
Without bothering to work out exactly why the problem occurs, there is enough suspicious going on here that one can be pretty sure the code is broken.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!