Tracking and plotting position of test subjects from pre-recorded video
Show older comments
Hi all,
I'm pretty new to MATLAB and I'm working on a research project where I need to create a position plot (Y vs. X) for the motion of a ferret recorded during experimental trials. The video essentially shows a ferret running around in an enclosed arena. I have been able to successfully use optical flow for motion tracking in the video (a green box appears around the head of the ferret as it moves), but I don't know how to use this to make a position plot. I know that there are a lot of variables stored with data and matrices, but I have no idea how to use them to make a position plot or even which ones I would need to use in order to do this. Can anyone assist me? Here's the optical flow code:
filename = 'test_video.avi'; hVidReader = vision.VideoFileReader(filename, 'ImageColorSpace', 'RGB',... 'VideoOutputDataType', 'single');
hOpticalFlow = vision.OpticalFlow( ... 'OutputValue', 'Horizontal and vertical components in complex form', ... 'ReferenceFrameDelay', 3);
hMean1 = vision.Mean; hMean2 = vision.Mean('RunningMean', true);
hMedianFilt = vision.MedianFilter;
hclose = vision.MorphologicalClose('Neighborhood', strel('line',5,45));
hblob = vision.BlobAnalysis(... 'CentroidOutputPort', false, 'AreaOutputPort', true, ... 'BoundingBoxOutputPort', true, 'OutputDataType', 'double', ... 'MinimumBlobArea', 250, 'MaximumBlobArea', 3600, 'MaximumCount', 1);
herode = vision.MorphologicalErode('Neighborhood', strel('square', 2));
hshapeins1 = vision.ShapeInserter('BorderColor', 'Custom', ... 'CustomBorderColor', [0 1 0]); hshapeins2 = vision.ShapeInserter( 'Shape','Lines', ... 'BorderColor', 'Custom', ... 'CustomBorderColor', [255 255 0]);
htextins = vision.TextInserter('Text', '%4d', 'Location', [1 1], ... 'Color', [1 1 1], 'FontSize', 12);
sz = get(0,'ScreenSize'); pos = [20 sz(4)-300 200 200]; hVideo1 = vision.VideoPlayer('Name','Original Video','Position',pos); pos(1) = pos(1)+220; hVideo2 = vision.VideoPlayer('Name','Motion Vector','Position',pos); pos(1) = pos(1)+220; hVideo3 = vision.VideoPlayer('Name','Thresholded Video','Position',pos); pos(1) = pos(1)+220; hVideo4 = vision.VideoPlayer('Name','Results','Position',pos);
lineRow = 22; firstTime = true; motionVecGain = 20; borderOffset = 5; decimFactorRow = 5; decimFactorCol = 5;
while ~isDone(hVidReader) frame = step(hVidReader); grayFrame = rgb2gray(frame); ofVectors = step(hOpticalFlow, grayFrame);
y1 = ofVectors .* conj(ofVectors);
vel_th = 0.5 * step(hMean2, step(hMean1, y1));
segmentedObjects = step(hMedianFilt, y1 >= vel_th);
segmentedObjects = step(hclose, step(herode, segmentedObjects));
[area, bbox] = step(hblob, segmentedObjects);
Idx = bbox(:,1) > lineRow;
ratio = zeros(length(Idx), 1);
ratio(Idx) = single(area(Idx,1))./single(bbox(Idx,3).*bbox(Idx,4));
ratiob = ratio > 0.4;
count = int32(sum(ratiob));
bbox(~ratiob, :) = int32(-1);
y2 = step(hshapeins1, frame, bbox);
y2(22:23,:,:) = 1;
y2(1:15,1:30,:) = 0;
result = step(htextins, y2, count);
if firstTime
[R C] = size(ofVectors);
RV = borderOffset:decimFactorRow:(R-borderOffset);
CV = borderOffset:decimFactorCol:(C-borderOffset);
[Y X] = meshgrid(CV,RV);
firstTime = false;
end
tmp = ofVectors(RV,CV) .* motionVecGain;
lines = [Y(:), X(:), Y(:) + real(tmp(:)), X(:) + imag(tmp(:))];
motionVectors = step(hshapeins2, frame, lines);
step(hVideo1, frame);
step(hVideo2, motionVectors);
step(hVideo3, segmentedObjects);
step(hVideo4, result);
end
release(hVidReader);
The results from this code have been the best I've come up with so far. Ideally, I would like to have a program that plots the position of the ferret as the video goes on, so you would essentially see the plot on the video and it would trace in real time behind the ferret. However, I obviously have no idea how to do this or if it is even possible. Any help is much appreciated. Thanks!
Answers (0)
Categories
Find more on Track Objects and Estimate Motion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!