作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。
🍎个人主页:Matlab科研工作室
🍊个人信条:格物致知。
更多Matlab仿真内容点击👇
⛄ 内容介绍
基于计算机视觉的运动跟踪和检测是计算机视觉领域的重要课题。对视频序列中的运动目标进行检测、跟踪,获得目标的位置、速度等运动参数,是进行更高级的运动目标行为分析的基础。快速实时地检测出运动目标,对运动目标进行跟踪,并实现目标的运动数据分析,在体育领域可以得到广泛的应用。例如,对一个田径运动员的训练视频进行处理,提取出其瞬时运动速度、手臂摆动幅度、频率等信息,由于视频序列每秒钟有多幅图像,含有大量的人眼捕捉不到的有价值的信息,可用于分析运动员技术上的不足,提高运动技能。一种基于YOLO和改进模板匹配的杠铃识别与跟踪控制方法,具体包括以下步骤:S1,采集多种杠铃的侧视图作为训练数据,对训练数据制定标签,划分为训练集和测试集;S2,对训练集中的图片进行标准化处理,并使用YOLO算法通过调整超参数的方式训练杠铃识别模型;S3,获取原始杠铃视频,通过杠铃识别模型对杠铃视频的第一帧图像进行检测并定位杠铃的位置;S4,将从第一帧图像中得到的杠铃位置和原始杠铃视频输入到改进的模板匹配算法中,计算得到绘制有杠铃轨迹的杠铃追踪视频.与现有技术相比,本发明具有提升杠铃识别的准确率并能够识别出不同样式的杠铃,提高帧率和准确率等优点.
⛄ 部分代码
classdef liftVid < handle
%liftVid Read and analyze a weightlifting video frame by frame.
% vid = liftVid("file.mp4");
%
% This is the main class definition for running BarTrace.
properties
vObj = 0; % video object
frameSize = [0 0] % frame size
numFrames = 0; % number of frames
radius = 0; % avg radius of the plate in pixels
pos = zeros([],3); % center of plate and radius in each frame
model = load("TrainedNNB.mat"); % trained neural net
end
methods
function obj = liftVid(fileName)
%liftVid Construct an instance and open the video file.
% obj = liftVid(fileName)
if nargin == 0
[fileName, path] = uigetfile("*.mp4","Select video file");
fileName = string(path) + fileName;
elseif nargin > 1
error('Expected 0 or 1 inputs');
end
obj.vObj = VideoReader(fileName);
obj.frameSize = [obj.vObj.Height obj.vObj.Width];
end
function frame = processNextFrame(obj)
%processNextFrame
if isa(obj.vObj,"VideoReader") && hasFrame(obj.vObj)
% read frame
obj.numFrames = obj.numFrames + 1;
cdata = readFrame(obj.vObj);
% find plate
trainedSize = obj.model.detector.TrainingImageSize;
imSmall = imresize(cdata,trainedSize);
[bboxes,scores] = detect(obj.model.detector,imSmall);
if ~isempty(bboxes)
[~,idx] = max(scores);
box = bboxes(idx,1:4);
x = box(1) * obj.frameSize(2)/trainedSize(2);
y = box(2) * obj.frameSize(1)/trainedSize(1);
w = box(3) * obj.frameSize(2)/trainedSize(2);
h = box(4) * obj.frameSize(1)/trainedSize(1);
position = [(x+w/2) (y+h/2) ((w+h)/4)]; %[x y r]
obj.pos(obj.numFrames,1:3) = position; % store position
obj.radius = median(obj.pos(1:obj.numFrames,3)); % median of all past radii
else
% plate wasn't found
position = [1 1 10];
obj.pos(obj.numFrames,1:3) = position; % store position
end
% annotate
frame = insertShape(cdata,'circle',position,'LineWidth',5,'Color',"red");
trace = zeros(obj.numFrames,4); %add lines and dots to bar trace
for k = 2:obj.numFrames % concatonate a list of all past centers
trace(k-1,1:4) = [obj.pos(k-1,1) obj.pos(k-1,2) obj.pos(k,1) obj.pos(k,2)];
end
if ~isempty(trace)
frame = insertShape(frame,'Line',trace,'LineWidth',2,'Color',"blue");
frame = insertShape(frame,"Circle",[trace(1:size(trace,1),[3 4]) 2*ones(size(trace,1),1)],"LineWidth",1,"Color",{"Yellow"});
end
else
disp("no more frames")
end
end
function tracePlate(obj,playit,saveit,saveName)
% tracePlate Finds the plate in each frame
% tracePlate(obj,playit,saveit)
% playit = true will play the video
% saveit = true will save the video
if isa(obj.vObj,"VideoReader") && hasFrame(obj.vObj)
if playit
figure;
f = gca;
end
if saveit
v = VideoWriter(saveName);
open(v);
end
else
disp("no more frames to process - try reloading")
playit = false;
saveit = false;
end
while hasFrame(obj.vObj)
frame = obj.processNextFrame;
if playit
imshow(frame, 'Parent', f);
drawnow;
end
if saveit
writeVideo(v,frame);
end
end
if saveit
close(v)
end
end
end % end of methods
end % end of classdef
⛄ 运行结果
⛄ 参考文献
[1]李文举, 王子杰. 基于YOLO和改进模板匹配的杠铃识别与跟踪控制方法:, CN114743125A[P]. 2022.
[2]王群. 基于计算机视觉的人体跟踪与运动参数测量[D]. 大连海事大学.
⛄ 完整代码
❤️部分理论引用网络文献,若有侵权联系博主删除
❤️ 关注我领取海量matlab电子书和数学建模资料