motion tracking multiple objects

11 views (last 30 days)
C.G. on 13 Mar 2020
i am trying to motion track multiple mini footballs. I generated a code to track 1 object using youtube, but im struggling to see how to scale this up to track all 17 of the balls in my video. The code below works and will track 1 of the balls.
%% Get video in, make sure it works, get all frames as individual images
%Input video using videoreader and name it 'obj'
obj = VideoReader('footballs.mp4')
%Tell it to read the frames in the video
vidFrames = read(obj);
%Tell it to get the number of individual frames in the video
numFrames = get(obj, 'numberOfFrames');
%Get the individual frames
%Colormap is a table of colors used for index color videos is an image sequence matrix
%How many times you repeat a function in a loop is defined by 'k'. k = the
%range of values the for loop will run through before ending.
for k = 1 : numFrames
mov(k).cdata = vidFrames(:,:,:,k);
mov(k).colormap = [];
%Watch the video
figure(1), movie(mov, 1, obj.FrameRate), title('Original movie');
%Show all frames in a montaged figure
figure(2), montage(vidFrames(:,:,:,1:80)),title('Montage of frames 1 to 80');
%% Change from colormap to grayscale
%For the total number of frames backwards (L31), turn the frames in the video
%from colour to grayscale
for k = numFrames:-1:1
grmov(:, :, k) = rgb2gray(mov(k).cdata);
%Show all greyscale frames in a montaged figure
figure(3), montage(grmov),title('Montage of greyscale frames 1 to 80')
%% Computing differences in frames
%For the total number of frames backwards, see how the object in the video
%has moved by subtracting frames from each other
%imabsdiff = absolute difference between two images
for k = numFrames-1:-1:1
framediff(:, :, k) = imabsdiff(grmov(:,:,k), grmov(:,:,k+1));
%Examine one of the difference images, see the change between frames
figure(4), imshow(framediff(:,:,100), [])
%% Threshold the sequence - ISSUES
%We can now convert the image to binary image using a threshold
%For the total number of frames backwards, turn each frame from grayscale
%to black and white, using a threshold 'greythresh' and save as variable
for k = numFrames-1:-1:1
bwdiffs(:, :, k)= im2bw(framediff(:, :, k),graythresh(framediff(:, :, k)));
%Show all black and white frames in a montaged figure
figure(5), montage(bwdiffs)
%% Label each region and compute centre of mass
% %For the total number of frames backwards, calculate centroids for
% %connected components in the frames. Store the x and y coordinates of the
% %centroids
for k = numFrames-1:-1:1
s = regionprops(bwlabel(bwdiffs(:,:,k)), 'centroid');
centroids = [s.Centroid];
xavg = mean(centroids(:,1:2:end));
yavg = mean(centroids(:,2:2:end));
position(:,k) = [xavg,yavg];
%% Overlay detected ball position on original video
%Yellow rectangle will be drawn over the detected ball location through
%time in the orignal video clip
for k = 1:length(position)
I = mov(k).cdata;
xpos = int32(position(1,k));
ypos = int32(position(2,k));
I(ypos-5:ypos+5,xpos-5:xpos+5,1:2) = 255;
mov(k).cdata = I;
%Play new clip
figure(6), movie(mov, 1), title('tracking v1')

Answers (1)

Harsha Priya Daggubati
Harsha Priya Daggubati on 16 Mar 2020
I guess use of Kalman Filter available in MATLAB might help you. Refer to the following documentation link for more details:
Hope this helps!

Community Treasure Hunt

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

Start Hunting!