How can I save the centroids and area of connected components of an image in a matrix?

1 次查看(过去 30 天)
I have a video,I am extracting its frames and I need to find the connected components of every frame of that video and also find the centroid of the connected components along with their corresponding pixel size.I am running a loop to do this.I want to save the centroids and pixel size in a matrix but I couldn't do that.How to do this?Below is the code-
vidobj=VideoReader('cloud.mp4');
Frame_No=vidobj.NumberOfFrames;
for i=1:NumberOfFrames
imwrite(read(vidobj,i),strcat('C:\Users\PRACKKZ\Documents\MATLAB\cloudframes\',num2str(i),'.png'))
end
for i=1:NoOfFrames
rgb=imread(strcat('C:\Users\PRACKKZ\Documents\MATLAB\cloudframes\',num2str(i),'.png'));
I=imresize(rgb,[512 512]);
bw = im2bw(I);
CC=bwconncomp(bw);
lab=labelmatrix(CC);
props = regionprops(lab, 'Centroid', 'Area');
After this I don't know how to save centroid and pixel size.

采纳的回答

Image Analyst
Image Analyst 2016-10-25
props is a structure array with two fields: Centroid and Area. For example, if you have 3 blobs, you'll have props(1).Centroid, props(1).Area, props(2).Centroid, props(2).Area, props(3).Centroid, and props(3).Area. Now Centroid is an vector of two doubles that are x and y centroid locations for that blob.
Now each frame could have a different number of blobs in it. Frame #1 might have 2 blobs but frame #73 might have 5 blobs in it. So since you'd need to save 2 centroids in one case and 5 centroids in the other case, you can either create a double array in advance with enough space to hold as many blobs as you ever expect to encounter, OR you can use a cell array. For the cell array approach, you might try something like this (untested):
for f = 1 : numFrames
% code to get props for this frame. Then...
theseCentroids = [props.Centroid];
% Get x and y for this frame only.
xCentroids = theseCentroids(1 : 2 : end);
yCentroids = theseCentroids(2 : 2 : end);
caCentroids{f} = [xCentroids; yCentroids];
end
Now caCentroids is a cell array where each cell is a 2 by N array of doubles where the first row is the x centroid values and the second row is the y centroids. Note that caCentroids{1} (for frame 1) might contain different sized arrays that caCentroids{2} if frame 1 and frame 2 don't both have the same number of blobs in them.
  4 个评论
Image Analyst
Image Analyst 2016-10-25
Same concept:
allAreas = [props.Area]; % All blob areas for this frame.
caAreas{f} = allAreas; % Store in the f'th cell of the cell array.
Prachi Sharma
Prachi Sharma 2016-10-25
Hey thank you so much.It is woking,it is giving area of both the blobs.Also I need to compare the overlap of each blob in first frame with every blob in the other frame and then save the corresponding pair including the blob to be compared with the blob that matched.I was reading your answer and link you provided in an answer in 2013(I have provided the link below to that answer of yours) but couldn't get much. Can you guide me with the overlap problem. https://in.mathworks.com/matlabcentral/answers/81925-finding-overlapping-points-of-objects-in-a-binary-image

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by