concatenate structures inside structures

1 view (last 30 days)
Dear all,
I am working on a project in with I should store the position of objects in a video. In each frame I have different number of objects and each object fills different number of pixels. The good part is that I am able to find the rows and columns that each object fills and then I store rowNumber and columnNumber in variable r and c.
I use a loop on the number of video frames (lets say 1000 frames). I want to concatenate object positions over Video frames. I need to store the position of objects such that I can refer to each video frame and read them individually. However, when I use position=[position D], it concatenates structures after the other and I can not recognize which frame and object it is.
For example in frame1 I have 2380 objects so structure D is 1x2380 with 2 fields r and c. and the second frame I have 1100 objects, so D becomes 1x1100 with 2 fields of r and c. But, after using position=[position D], it gives me 1x 3480 structure with 2 fields of r and c. The problem is that I do not want to combine objects over frames. (I need the position of each object in each frame). So I need to concatenate structures (with different sizes) vertically.
maybe this sketch helps you understand better:
It describes that in the first frame of the video I have 2380 objects, then within this structure , the first object fills 131 rows and 131 column (131x1 double), the second object fills 11 rows and 11 columns, the third object fills 231 rows and column,....
Then in the second frame of the video we have different 1100 objects,....
This is a part of my code:
for k=1:numberFrames
Image = imread(ImageFile, k); %read frame k of new frame
bw = BinarizeImage( Image); %binarize it using my BinarizeImage function
C = bwconncomp(bw, 8); % find objects
labelMap = labelmatrix(C); %label current objects
Object = regionprops('table', labelMap, 'Area', 'Centroid', 'MajorAxisLength', 'MinorAxisLength', 'Eccentricity');
for m = 1:size(Object,1) % This loop reads the row and column of each object in the image
[D(m).r, D(m).c] = find(labelMap==m); % r is for the row number and c is for column number
position=[position D];
end
end

Accepted Answer

Dorsa Elmi
Dorsa Elmi on 10 Mar 2022
Thank you. cell was a great idea. I did it differently with cells and got what I wanted. Exciting!
position={}; % this is a cell
for k=1:numberFrames
Image = imread(ImageFile, k); %read frame k of new frame
bw = BinarizeImage( Image); %binarize it using my BinarizeImage function
C = bwconncomp(bw, 8); % find objects
labelMap = labelmatrix(C); %label current objects
Object = regionprops('table', labelMap, 'Area', 'Centroid', 'MajorAxisLength', 'MinorAxisLength', 'Eccentricity');
D=[];
for m = 1:size(Object,1) % This loop reads the row and column of each object in the image
[D(m).r, D(m).c] = find(labelMap==m); % r is for the row number and c is for column number
end
position =[position ;D]; % collect all positions in this cell
end

More Answers (1)

AndresVar
AndresVar on 10 Mar 2022
Edited: AndresVar on 10 Mar 2022
since each frame has different sized structure array maybe you can use a cell for each frame
clear;
% example positions for 2 elements
positions(1).r = zeros(131,1);
positions(1).c = zeros(131,1);
positions(2).r = zeros(11,1);
positions(2).c = zeros(11,1);
% example frame 1 with 2 elements
frames(1) = {positions};
% frame 2.. for example with 4 elements (reusing positions for brevity)
frames(2) = {[positions positions]};
frames
frames = 1×2 cell array
{1×2 struct} {1×4 struct}
% look at element 4 in frame 2
positionrc = frames{2}(4)
positionrc = struct with fields:
r: [11×1 double] c: [11×1 double]

Community Treasure Hunt

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

Start Hunting!

Translated by