How to save X ,Y coordinates in a structure?

1 view (last 30 days)
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

Geoff Hayes
Geoff Hayes on 11 Feb 2015
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
Geoff Hayes
Geoff Hayes on 12 Feb 2015
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?
Mario
Mario on 12 Feb 2015
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)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!