MATLAB: Using a loop, how to access/use values from a structure array that is within a cell array

cell arraysfor loopimage processingImage Processing Toolbox

I have a 1×26 cell that contains the regionprops measurements for 26 images (images with shapes), and within each image measurement cell is a structure array containing its measurements. I am trying to take the area and perimeter values for each image and put it in the following equation: SF = (4 * pi * Area) / (Perimeter^2), and save these values into another array. However, some of the images have multiple labeled objects, and so multiple Area/Perimeter measurements. I can get an individual value by using:
if true
% S = (4 * pi * measurements{i}(k).Area) / ((measurements{i}(k).Perimeter) ^ 2);
%

D = dir;
Image = cell(1, numel(D));
labeled = cell(1, numel(D));
for i = i : numel(D)
Image{i} = imread(D(i).name); %cell array with 26 images
labeled{i} = bwlabel(Image{i}, 8);
measurements{i} = regionprops(labeled{i}, Image{i}, 'all');
end
%
AREA = cell(1, numel(D));
PERIMETER = cell(1, numel(D));
a = cell(1, numel(D));
for k = 1 : numel(D)
a{k} = struct2cell(measurements{k});
numOfmeasurements{k} = length(a{k});
for i = 1: numOfmeasurements{k}
AREA{i} = measurements{k}(i).Area;
PERIMETER{i} = measurements{k}(i).Perimeter;
end
end
%Cal
Any help would be greatly appreciated.

Best Answer

  • Get rid of all that stuff and try this:
    allAreas = [measurements.Area];
    allPerimeters = [measurements.Perimeter];
    circularities = 4*pi*allAreas ./ allPerimeters .^ 2;
    No need for two for loops or cell arrays if you just compute and use the measurements inside the loop over filenames.
    Be sure to look at my Image Segmentation Tutorial where I go over all of this: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862