??? Subscript indices must either be real positive integers or logicals.
1 view (last 30 days)
Show older comments
Hello, i need help. I will be grateful for any advice.
I have to clustering grayscale image.
I enclose my code The main function is kmnsImage.
error in :
??? Subscript indices must either be real positive integers
or logicals.
Error in ==> KMNSimage at 542
cluster(Z{ii}) = ii;
I must to clustering grayscale image,so that the pixels in a similar intensities were in one cluster.
Please help me.
Thank you for your help
3 Comments
Geoff Hayes
on 27 Apr 2014
Edited: Geoff Hayes
on 27 Apr 2014
Hi Tomas,
Try putting in some breakpoints in and around line 542 and check to see what your value of ii is being set to. Given the error, it sounds like your ii is being set to zero or some rational (or fraction of a) number.
If there are several iterations of the loop (over ii) then it may take some time to step through all iterations until the failure occurs. Sometimes, what I do, is to wrap the trouble spot in a try catch block and then just put the breakpoint in the catch. So when the error occurs, the code pauses in the catch:
try
cluster(X{ii}) = ii;
catch
fprintf('error!');
end
The breakpoint will be on the line of the fprintf, and you can debug here to see what the value of ii is.
Geoff
Answers (1)
Image Analyst
on 27 Apr 2014
Then try this:
try
whos X
whos ii
fprintf('X{%d} = %d\n', ii, X{ii});
index = X{ii}
cluster(index) = ii;
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
15 Comments
Image Analyst
on 28 Apr 2014
Edited: Image Analyst
on 28 Apr 2014
So like I said, you want to label the image. All pixels in spot #1 should be labeled 1, and all pixels in spot #2 should be labeled #2, and so on. This is precisely what bwlabel does in one line, and what the kmeans code that I gave over in your duplicate question does, reproduced below here:
classifiedImage = zeros(size(I), 'int32');
for p = 1 : length(IDX)
row = P(p, 1);
column = P(p, 2);
% Set this pixel of the classified image
% to the class it identified for that pixel.
classifiedImage(row, column) = IDX(p);
end
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!