How do you apply an image threshold and track a centroid in a video?
5 次查看(过去 30 天)
显示 更早的评论
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]
0 个评论
回答(1 个)
Kevin Holly
2022-11-28
编辑:Kevin Holly
2022-11-28
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Computer Vision with Simulink 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!