How to save X ,Y coordinates in a structure?

Helo, I am looking for help.
I have an image I that has several different regions on it.
I calculated the weighted centroids and standard deviation of those regions. Then I want to extract X and Y coordinates of a region on a image that has the maximum standard deviation in that structure, but I am having troubles doing that.
I have done this so far
I calculated the Std:
s = regionprops(BW, I, {'WeightedCentroid','PixelValues');
for k = 1 : numObj
s(k).StandardDeviation = std(double(s(k).PixelValues)); #I calculated the std of these regions
end
Then I tried to get the max value od std in structure but no luck.
This is how I did it:
sStd = [s.StandardDeviation];
MaxStd = find(max(sStd));
imshow(I);
hold on;
for k = 1 : length(MaxStd)
rectangle('Position', s(MaxStd(k)).BoundingBox, ...
'EdgeColor','g');
end
hold off;
I hope that I was clear with the explanation. This maybe seems somehow trivial, but I just can not find the solution. Can anyone help me? Thanks in advance!

 Accepted Answer

Mario - if s(k).PixelValues) is a one dimensional array, then the standard deviation of this array will be a single element. Is this the case? If true, then
sStd = [s.StandardDeviation];
will be a one dimensional array, and the maximums standard deviation calculated as
MaxStd = max(sStd); % no need for the find
will be a scalar value. If you want to find which region has this maximum standard deviation, then just do instead
[MaxStd,RegionIdx] = max(sStd);
with
rectangle('Position', s(RegionIdx).BoundingBox, ...
'EdgeColor','g');
Try the above and see what happens!
Note that your code is assuming that MaxStd is an array, and then you try to access s with MaxStd(k) which is (most likely) a floating point number which will lead to the Subscript indices must either be real positive integers or logicals.

3 Comments

Mario's answer moved here
Firs of all, thank you for your reply.
As far as,
s(k).PixelValues
it has mutiple values (eg. somewhere is one dimension, and on others is a structure or for eg. [134;131;128;131]), so I'm а bit confused there.
And the standard deviation of all regions is a single value. I have 41 regions on an image total and the region no.9 has the maximum standard deviation (I can see that from a struct(s) also).
I tried with the modifications that you gave me and I was able to extract the no. of region with the maximum standard deviation with RegionIdx, and it's deviation value with MaxStd. And I was able to locate the rectangle (a big one) with the maximum standard deviation on an image which is an improvement compared to my previous atempts.
I'm trying to extract Centroids (X,Y coordinates) of the region with the maximum standard deviation as a separate elements in order to use them further in my code.
In a struct(s) I have values of a Centroid, BoundingBox , PixelValues and StandardDeviation for all of 41 regions in my image.
Do you know maybe how could I extract Centroids from the region that has the highest standard deviation in a struct(s)? How can I get those X,Y coordinates out of a structure as a separate elements?
Thanks.
Mario - perhaps save the structure s to a mat file so that I can see what you about the PixelValues having different dimensions or are structures (?). Also, if you are now able to find the region with the largest standard deviation, given by the index RegionIdx, can't you use that to get the centroid of that region?
I managed to get the desired coordinates in a separate variables as I wanted.
To do that I had to reduce the structure s to only two arrays (centroids and standard deviation), then I converted that structure to table with struct2table and then I was able to find the mamximum standard deviation inside that table and extract the coordinates of that region with the max. stand. dev.
Anyway, Geoff Hayes your comments were very helpfull indeed so thank you for your help and time.

Sign in to comment.

More Answers (0)

Asked:

on 11 Feb 2015

Commented:

on 12 Feb 2015

Community Treasure Hunt

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

Start Hunting!