필터 지우기
필터 지우기

Regionprops table for first and the last frame of the video

조회 수: 1 (최근 30일)
Roshni
Roshni 2024년 3월 7일
답변: Aishwarya 2024년 3월 19일
Hi,
I am working on a code that should track the cylindrical particle and calculate its velocity over 1000 frames.
Using stats on region props, the table gives me the final position of the centroid. But I would like to extract the first and the final position and then use it to calculate the distance and velocity involved. I have attached the function file for finding centroid.
function [xCent,yCent, thetaOrient, ellX, ellY, initialPositon, finalPosition] = find_cylinder_info(bFrame)
%
% Find centroid and orientation of cylinder in cropped image
%
% Roshni, A Alessio, 2/2/2024
mxVal = max(bFrame(:));
bFrameMask = bFrame > (mxVal*.50);
bFrame(~bFrameMask) = min(bFrame(:));
% Convert to binary
binaryImage = imbinarize(bFrame);
[labeledImage,numberOfBlobs] = bwlabel(binaryImage);
% Find centroid
bMeas = regionprops(labeledImage,'area');
[sortedAreas, sortIndexes] = sort(bMeas.Area,'descend');
biggest = ismember(labeledImage,sortIndexes(1));
binaryImage1Blob =biggest>0;
props = regionprops(binaryImage1Blob,'centroid','orientation','MajorAxisLength','MinorAxisLength');
xCent=props.Centroid(1);
yCent=props.Centroid(2);
thetaOrient = props.Orientation;
%props = regionprops(mask, 'Centroid', 'MajorAxisLength', 'MinorAxisLength', 'Orientation');
centroid = props.Centroid;
majorAxis = props.MajorAxisLength / 2;
minorAxis = props.MinorAxisLength / 2;
orient = deg2rad(-props.Orientation);
angle = linspace(0, 2 * pi, 1000);
ellX = centroid(1) + majorAxis * cos(angle) * cos(orient) - minorAxis * sin(angle) * sin(orient);
ellY = centroid(2) + majorAxis * cos(angle) * sin(orient) + minorAxis * sin(angle) * cos(orient);
stats = regionprops("table",binaryImage1Blob,"centroid","orientation",...
"MajorAxisLength","MinorAxisLength")
I am new to matlab and image analysis. Any help is appreciated
  댓글 수: 1
Matt J
Matt J 2024년 3월 7일
If you already have a function that extracts the centroids for a given frame, can't you just call it on your first and last frame? What exactly is the difficulty?

댓글을 달려면 로그인하십시오.

답변 (1개)

Aishwarya
Aishwarya 2024년 3월 19일
Hi Roshni,
After reviewing the provided function to calculate the centroid of a frame, as @Matt J mentioned, calling this function at initial and final frames should give the required information to calculate the distance and velocity.
I would suggest modifying the function so that initial and final positions are calculated outside the function. Then the output variables in the function namely “initialPositon” and “finalPosition” can be removed. The function definition now becomes:
function [xCent, yCent, thetaOrient, ellX, ellY] = find_cylinder_info(bFrame)
%
% Function code
%
end
To illustrate the application of this updated function, here's an example script that calculates the distance and velocity, assuming a uniform time interval of 1 unit between frames. This example considers the "frames" variable as a 3D array, with each slice along the third dimension representing a distinct frame.
% Find the initial position:
[xCent,yCent, thetaOrient, ellX, ellY] = find_cylinder_info(frames(:,:,1));
initialPosition = [xCent, yCent];
% Find the final position:
[xCent,yCent, thetaOrient, ellX, ellY] = find_cylinder_info(frames(:,:,end));
finalPosition = [xCent, yCent];
% Calculate distance and velocity
distance = sqrt((finalPosition(1) - initialPosition(1))^2 + (finalPosition(2) - initialPosition(2))^2);
time = numFrames; % Assuming 1 time unit between frames, numFrames is total number of frames
velocity = distance / time;
% Display the results
fprintf('Initial Position: (%f, %f)\n', initialPosition(1), initialPosition(2));
fprintf('Final Position: (%f, %f)\n', finalPosition(1), finalPosition(2));
fprintf('Distance: %f units\n', distance);
fprintf('Velocity: %f units/time\n', velocity);
This script first calculates the initial position by processing the first frame through the function. Similarly, it determines the final position using the last frame. These positions are then used to calculate the distance and velocity. Adjust the "time" variable accordingly if your frames are not spaced evenly in time.
For more information about the “sqrt” function used, you can refer to the following documentation: https://www.mathworks.com/help/matlab/ref/sqrt.html
I hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by