using regionprops in MATLAB
1 次查看(过去 30 天)
显示 更早的评论
I wrote code to convert the video frames to images then I make some analyses using regionprops function. Since I have many frames and I need to the analysis for many frames, I need to used for loop for that, but I have a problem storing the results of each iteration since the result is a structure. May I ask you please to help me in that? The code follows:
clc;
clear;
mov=aviread('Flying Car1.avi');
Leng = length (mov)
for i=1:Leng
x(:,:,:,i)= frame2im(mov(:,i));
end
for k=1:Leng-1
z(:,:,:,k)= imabsdiff (x(:,:,:,k+1),x(:,:,:,k));
bw(:,:,k)= im2bw (z(:,:,:,k),graythresh(z(:,:,:,k)));
L(:,:,k)= bwlabel (bw(:,:,k));
s = regionprops(L(:,:,k), 'centroid')
H(:,:,k)= s;
end
0 个评论
回答(3 个)
Image Analyst
2013-1-3
I think you can make an array of structures:
s(k) = regionprops(L(:,:,k), 'centroid')
I see no reason why z, bw, and L need to be multi-dimensional, depending on k. You can just re-use those over and over again. No need to store them all.
2 个评论
Image Analyst
2023-2-20
@mustafa alnasser whatever you did, you did it incorrectly. Here is a very well commented way that works:
% Optional initialization steps
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
% Read in video.
videoObject = VideoReader('rhinos.avi');
% Determine how many frames there are.
videoHeightRows = videoObject.Height;
videoWidthColumns = videoObject.Width;
fprintf('The movie has %d rows, %d columns.\n', videoHeightRows, videoWidthColumns);
numberOfFrames = videoObject.NumFrames
% Preallocation 4-D array for all frames
% (actually unnecessary but this is what the original poster thought he needed to do).
allFrames = zeros(videoHeightRows, videoWidthColumns, 3, numberOfFrames, 'uint8');
for k = 1 : numberOfFrames
% Read the kth frame from the video.
thisFrame = read(videoObject, k);
% Store it in the 4-D array in the last index.
allFrames(:,:,:, k) = thisFrame;
end
% Preallocate space for the measurements of each frame.
allProps = cell(numberOfFrames - 1, 1);
fullFrameCentroids = zeros(numberOfFrames - 1, 2); % (x, y) Each row is weighted centroid for one frame
% Make a mask of the full frame to get the centroid of the whole image instead of individual blobs.
fullFrameMask = true(videoHeightRows, videoWidthColumns);
for k = 1 : numberOfFrames - 1
% Get the difference between the (k+1)st blob and the kth blob.
diffImage = imabsdiff (allFrames(:,:,:,k+1), allFrames(:,:,:,k));
% Convert it to gray scale since we will need to threshold it in the next loop.
if size(diffImage, 3) == 3
diffImage = rgb2gray(diffImage);
end
% Display the image
imshow(diffImage, []);
drawnow; % Force immediate screen refresh.
% Binarize the image to find blobs.
% Note that there may be a different number of blobs in each frame.
binaryImage = imbinarize(diffImage);
[labeledImage, numBlobs] = bwlabel (binaryImage);
fprintf('Found %d blobs in frame #%3d of %3d.\n', numBlobs, k, numberOfFrames)
% First get results for just this one image into a structure,
theseProps = regionprops(labeledImage, 'Centroid');
% and then this image's measurements into a cell array.
allProps{k} = theseProps;
% Get the weighted centroid of the full frame of the difference image.
theseProps = regionprops(fullFrameMask, diffImage, 'WeightedCentroid');
fprintf(' Weighted Centroid at (%.1f, %.1f).\n', theseProps.WeightedCentroid(1), theseProps.WeightedCentroid(2))
fullFrameCentroids(k, :) = theseProps.WeightedCentroid;
end
Not sure if that's what you wanted, but it works. It gets both the number of blobs (which may vary) for each frame, and the centroids of all those blobs into one cell array called allProps. And it gets the weighted centroid of the whole frame of the difference image into one matrix called fullFrameCentroids.
mustafa alnasser
2013-1-6
编辑:DGM
2023-2-20
3 个评论
Image Analyst
2015-10-27
移动:DGM
2023-2-20
His code is above. And he was working on vehicle identification or tracking, not hand gestures.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!