using regionprops in MATLAB
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (3)
Image Analyst
il 3 Gen 2013
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 Commenti
Image Analyst
il 20 Feb 2023
@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
il 6 Gen 2013
Modificato: DGM
il 20 Feb 2023
3 Commenti
Image Analyst
il 27 Ott 2015
Spostato: DGM
il 20 Feb 2023
His code is above. And he was working on vehicle identification or tracking, not hand gestures.
Vedere anche
Categorie
Scopri di più su Image Segmentation and Analysis in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!