How to store different values of regionprops inside a structure from a 3D matrix

3 views (last 30 days)
Hello,
I have a 3D matrix containing 16 binary images. Each of these 16 images are containing multiple regions (of different size and shape) inside them. I want to be able to calculate regionprops of each of those 16 images and their regions inside them. I was able to calculate Centroids and BoundingBox for each region separately of each image and store them in a 1x16 structure containing these two fields ( Centroids and BoundingBox ).
But, I am not being able to calculate PixelValues for each of those regions inside each of those 16 images and store them also inside my 1x16 struct and adding the third field (that would be PixelValues ).
Here is the piece of my code where I'm having trouble:
numImages - is a number of images inside my 3D matrix (16 of them)
for i=1:numImages
j = regionprops(logical(bw(:,:,i)), M2(:,:,i), {'Centroid','PixelValues','BoundingBox'});
jj(i).Centroids = [j.Centroid];
jj(i).Centroids=reshape(jj(i).Centroids, 2, []);
jj(i).Centroids=transpose(jj(i).Centroids); %%Here I reshaped these values in order to put them in Nx2 matrix
jj(i).PixelValues = [j.PixelValues]; %%Here lies the problem andI do not know how solve it
jj(i).BoundingBox = [j.BoundingBox];
jj(i).BoundingBox=reshape(jj(i).BoundingBox, 4, []);
jj(i).BoundingBox=transpose(jj(i).BoundingBox);%%Here is the same thing, except it was Nx4 matrix
end
The problem that I have with my code (regarding PixelValues ) is that each region contains different number of pixels in them and that is the problem that is causing me to get this error:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
It was easier to separate those values for Centroids and BoundingBox since each Centroid contains only two values(x and y coord.) and BoundingBox containing 4 values (which define the box).
But with the PixelValues each region contains different number of those values. Here is the example for only one image containing 21 regions:
region1: 15840x1,
region2: 28x1,
region3: 131;131;131;131;,
region4: 37x1,
and so on...
To elaborate a little bit more I have attached the image of my structure.
Thanks in advance!

Accepted Answer

Image Analyst
Image Analyst on 6 Apr 2015
Put them into a cell array
  3 Comments
Mario
Mario on 6 Apr 2015
I managed to get it to work
I used
jj(i).PixelValues = {j().PixelValues}.';
and was able to get PixelValues of all regions as a separate cell inside my 1x16 structure.
Thanks for the hint.
Image Analyst
Image Analyst on 6 Apr 2015
Edited: Image Analyst on 6 Apr 2015
Maybe this (untested)
for k = 1 : length(j) % for every blob...
% Put this blob's array into the kth cell.
ca{k} = j(k).PixelValues;
end
% Make the cell array a field of the i'th
% element of jj structure array.
jj(i).PixelValues = ca;

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!