Question

I feel dumb even having to ask this, it really should be dead simple, but being new to MatLab I'd like to know how a more experienced person would do it.

Simple problem; I need to find some regions in multiple images, correlate them by position, save those regions of interest, and use them later. One way to do that would be to store the regions in a vector.

%% pseudo code
regions = [];
for i = some_vector_of_images
  % segment, get mask
  % find regions
  cc = bwconncomp(mask);
  stats = regionprops(cc, 'all');
  % correlate against known x/y
  % save for later
  regions[index++] = stats;
end
% use 'regions'

However, the array declaration is problematic. Its default type is double, so that won't work (can't assign a struct to an element). I've tried struct.empty, but the array is not resizable. I've tried a cell array, but I get a similar error (Conversion to cell from struct is not possible.)

Really, I just need a way to have some collection declared prior to the loop that will hold instances of these structures. Again, pretty noobish question and slightly embarrassed here... please take pity.

Was it helpful?

Solution

See if using struct2cell helps you with this. Give this pseudo-code a try -

regions = cell(num_of_images,1) %// This will be before the loop starts
...

regions[index++] = {struct2cell(stats)} %// Inside the loop

Please not that this a pseudo-code, so square brackets and ++ won't work.

Thus, the complete version of pseudo-code would be -

%%// ---  pseudo code

%// Without this pre-allocation you would get the error - 
%%// "Conversion to cell from struct is not possible"
regions = cell(numel(some_vector_of_images),1) 

for i = some_vector_of_images
  % segment, get mask
  % find regions
  cc = bwconncomp(mask);
  stats = regionprops(cc, 'all');
  % correlate against known x/y
  % save for later
  regions(i) = {struct2cell(stats)}
end

OTHER TIPS

You can cast your empty array to a structure array by appending a structure. Replace regions[index++] = stats; with

regions = [regions, stats];

This line will continue to build the array within the loop. This idiom is generally frowned on in MATLAB because a new array needs to be created each loop.

Another method is to preallocate the array with a template structure, using repmat.

stats = some_operations_on(some_vector_of_images(1));
regions = repmat(stats, numel(some_vector_of_images), 1);

and within the loop, assign with

regions(i) = stats;

In this scenario, typically I just don't preallocate at all, or use a cell-cat pattern.

Not initializing

This one doesn't initialize the struct array, but works fine. Make sure i is an index of each element in this case.

for i = 1:numel(some_vector_of_images)
  % mask = outcome of some_vector_of_images(i)
  cc = bwconncomp(mask);
  regions(i) = regionprops(cc, 'all');
end

cell-cat pattern

This one catches results in a cell array, and concatenates all elements at the end.

regions = cell(numel(some_vector_of_images), 1);
index = 1;
for i = some_vector_of_images
  % mask = outcome of i
  cc = bwconncomp(mask);
  regions{index} = regionprops(cc, 'all');
  index = index + 1;
end
regions = cat(1, regions{:});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top