using regionprops in MATLAB

1 visualizzazione (ultimi 30 giorni)
mustafa alnasser
mustafa alnasser il 3 Gen 2013
Commentato: Image Analyst il 20 Feb 2023
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

Risposte (3)

Walter Roberson
Walter Roberson il 3 Gen 2013
H(:,:,k)= s.centroid;
  2 Commenti
mustafa alnasser
mustafa alnasser il 4 Gen 2013
Thank for your answer but it does not work and i got the following error:
??? Illegal right hand side in assignment. Too many elements.
Error in ==> main at 22 H(:,:,k)= s.Centroid;
Walter Roberson
Walter Roberson il 4 Gen 2013
H(:,:,k)= {s.centroid};

Accedi per commentare.


Image Analyst
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
mustafa alnasser
mustafa alnasser il 4 Gen 2013
Thank for your answer but it does not work and i got the following error:
??? Subscripted assignment dimension mismatch.
Error in ==> main at 21 s(k) = regionprops(L(:,:,k), 'centroid');
why i need to store all of them because as you notice i need to make the analysis on the difference between frames.
Image Analyst
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.

Accedi per commentare.


mustafa alnasser
mustafa alnasser il 6 Gen 2013
Modificato: DGM il 20 Feb 2023
I am able to find the answer, the code as follow:
clc; clear;
mov = aviread('car.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)));
bw1(:,:,k) = imfill(bw(:,:,k),'holes');
L(:,:,k) = bwlabel(bw1(:,:,k));
s = regionprops(L(:,:,k),'centroid');
H1 = struct2cell(s);
[m1,n1] = size(H1);
for j = 1:n1
y1(k,j) = H1(1,j);
end
m = regionprops(L(:,:,k), 'ConvexHull');
H2 = struct2cell(m);
[m2,n2] = size(H2);
for j = 1:n2
y2(k,j) = H2(1,j);
end
w = regionprops(L(:,:,k), 'BoundingBox');
H3 = struct2cell(w);
[m3,n3] = size(H3);
for j = 1:n3
y3(k,j) = H3(1,j);
end
end
  3 Commenti
emy th
emy th il 27 Ott 2015
Spostato: DGM il 20 Feb 2023
Hi, I am also working on hand gesture, Mustafa Alnasser could you send me your code since I am working on detect and segment the hand region. thank you
Image Analyst
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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by