MATLAB Answers

How can I track the displacement of a moving object.

31 views (last 30 days)
Matthew Roper
Matthew Roper on 27 Nov 2016
Commented: Image Analyst on 27 Mar 2018
I am trying to track the displacement of a white cross from a video file.
I can convert each individual frame to grayscale but can't then calculate the displacement of the white cross (the object I am trying to track) and plot this on a graph vs time. Here is the code I am currently:
v = VideoReader('Test_4.wmv');
video = read(v);
imtool(video(:,:,:,50))
data=size(video)
numframes = data(4) %Convert to grayscale
for k = numframes-1:-1:1
g(:, :, k) = rgb2gray(video(:, :, :, k));
end
for k = numframes-1:-1:1 %Difference between frames
d(:, :, k) = imabsdiff(g(:, :, k), g(:, :, k+1));
end
imtool(d(:, :, 50), [])
thresh = graythresh(d)%Divides between foreground and background
bw = (d >= thresh * 255); imtool(bw(:, :, 50))
bw2 = bwareaopen(bw, 20, 8); %Removes objects that are too small
s = regionprops(bwlabel(bw2(:,:,1)), 'centroid'); %label each object and calc centre of mass
c = [s.Centroid]
subplot(2, 1, 1)
plot(t, centroids(:,1)), ylabel('x')
subplot(2, 1, 2)
plot(t, centroids(:, 2)), ylabel('y')
xlabel('time (s)')
Help would be much appreciated, thanks.

  0 Comments

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 28 Nov 2016
Well not having the video, or even a frame from it, it's hard to know what to suggest. But assuming you have one cross and you've segmented it so that it's the largest blob in the image, you might use bwareafilt(bw, 1) instead of bwareaopen(bw, 20, 8).

  7 Comments

Show 4 older comments
Matthew Roper
Matthew Roper on 29 Nov 2016
Those images are all of the same frame at each stage of my code, so I am a bit confused as to why it is not a cross shape throughout. The light is DC operated. Is there a way I can track a single point on the cross instead of calculating the centroid?
Abdullah CAKAN
Abdullah CAKAN on 27 Mar 2018

Dear Sir, I want to use simulink image processing and need to measure white object's displacement graph shown in figure. I attached my video.. How can do this? Anyone can help me ? If i success i will record a real object that moves faster in video..

Sign in to comment.


Abdullah CAKAN
Abdullah CAKAN on 27 Mar 2018
Dear Sir, I want to use simulink image processing and need to measure white object's displacement graph shown in figure. I attached my video.. How can do this? Anyone can help me ? If i success i will record a real object that moves faster in video..

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!