??? Subscript indices must either be real positive integers or logicals.

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

Show inline the pertinent (small) section of code--don't expect others to debug a whole app.
The problem is that you've used Z(ii) as an index to an array and the value is either <=0 if integer or illegal if floating point non-integer.
Fix your code logic to eliminate that problem.
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
Thank you for your answers, I am having trouble, I do not know that the resulting cells to portray the image back,

Sign in to comment.

Answers (1)

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

Thank you for your advice. I know where my error. I do not know to change the code to work properly. Therefore, I upload code,if anyone knows advice to change the code.
A bit hard since I don't understand that language, but I did run it. Is there any reason why Z is a cell array instead of a regular numerical array (double)? You can do it but you're just making it more complicated than it needs to be. ANyway, Z{ii} is an array of 24597 doubles with fractional numbers, NOT integers! I did
K>> zIndex = Z{1}
1.0162
1.0200
1.0238
1.0277
0.9738
0.9777
0.9815
0.9854
0.9892
0.9931
0.9969
1.0008
and so on. I don't know how to fix it because I don't know what you were thinking when you tried to set the (1.0162)th index of the array called "cluster" to ii. Do you know that there is no index between 1 and 2? If you need something there you'll have to interpolate. But I don't think you want that, I think you just need to figure out better what you want to do, and I can't help much because I don't understand any of few comments that are there.
Ok ,anyway thank you for looking at it I'll try to rewrite,if you have time, so it can look Thanks
I attach my code in English main function is clustering.
...clustering grayscale image,so that the pixels in a similar intensities were in one cluster.
You might want to look at histc, then, to bin them.
Clearly your using Z as an array index isn't what you intended; perhaps it's the bin values you've identified? If so, use it as the EDGES vector in histc
I have to clustering grayscale image, thus, the coordinates and the intensity will be at the same level. To me it clustering only to the distance coordinates of the object, but ignores color/intensities pixeln in grayscale image
I don't know what the above means, precisely, sorry, so can't make other suggestions other than clearly whatever you intend the array cluster to hold you can't index into it with non-integer values.
I think it's time for you to attach your image so we know what you're wanting to cluster because what you've said does not make sense, to me at least.
my input image, i convert to grayscale image
I want to clusterin black rings under intesity graycolor
What do you want to find? The number of gray spots, which would be 12? Or the number of gray clusters, like you'd see from examining the histogram, which would be 2 since there are apparently only 2 gray levels? Or the centroid of each gray spot?
But you're passing in the coordinates and ignoring the gray level. If you told it that there should be 12 clusters then kmeans will probably give you back a list saying which spot number each pixel is in. You could do this in one line of code if you had the Image Processing Toolbox, and you don't even need to tell it how many spots there are like you do with kmeans:
labeledImage = bwlabel(grayImage);
That single line would replace your whole huge chunk of code where you're using kmeans.
I understand you only have to use kmeans, I must clustering gray levels in image. How do I get all the gray levels of the image, so I had to input matrix to kmeans ?
Thanks
Tell me where you want to hold this conversation: here or in this duplicate question because I'm not going to do it in two places.
To get all the gray levels of the image, you do grayImage(:). But don't understand why you ask for that when you said that you are going to ignore gray level and only consider position (location). I think you need to get a better English speaker involved because when you say "I have to clustering grayscale image, thus, the coordinates and the intensity will be at the same level. To me it clustering only to the distance coordinates of the object, but ignores color/intensities pixeln in grayscale image" it makes no sense at all. You're confusing the heck out of dpb and me. What does it mean that the coordinates and intensity are at the same level??? I have no idea. Coordinates are two numbers, row and column, whereas intensity is a gray level. They're even different units. Look at my questions 2 comments back and answer what you want to find. Please give the expected answer for this particular demo image.
i want to find points on the image, I do not care background, the thing is ,when the points are near yourself but have different intesity not to be in single cluster,points that are far from yourself a have similar intesity were in one cluster.
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

Sign in to comment.

Asked:

on 27 Apr 2014

Edited:

on 28 Apr 2014

Community Treasure Hunt

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

Start Hunting!