How do you apply an image threshold and track a centroid in a video?

3 views (last 30 days)
I can sucessfully open an image and apply a mask to it and find the centroid. How do I do this for each frame of a video and have matlab give me launch angle and velocties in the x and y directions? I have the threshold code below.
clear
clc
close all
%%
%Find Image
RGB = imread('4.jpg');
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.129;
channel1Max = 0.215;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.307;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.618;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
% Creates binary matrix for masked values
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
%Show image
imshow(BW)
% Find Centroid and Area of object
[rows, columns] = find(BW);
topRow = min(rows);
bottomRow = max(rows);
leftColumn = min(columns);
rightColumn = max(columns);
ypos=(topRow+bottomRow)/2;
xpos=(leftColumn+rightColumn)/2;
Centroid = [xpos,ypos]

Answers (1)

Kevin Holly
Kevin Holly on 28 Nov 2022
Edited: Kevin Holly on 28 Nov 2022
You can use VideoReader. Here is an Example reading a video file.
Edit: As for calculating the velocity, you can make your Centriod variable a 3D matrix and then calculate the derivate using diff.
Centroid(:,:,frame) = [xpos,ypos]
You can get the fps from the video reader.
As for the angle, you could find perform a linear fit of the first few points and use trig to find the angle between this line and the surface. If surface is horizontal, use can you the slope (m) found in the the linear fit model y = mx+b to calculate the launching angle.
atand(m)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!