This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Track a Face in Scene

Create System objects for reading and displaying video and for drawing a bounding box of the object.

videoFileReader = vision.VideoFileReader('visionface.avi');
videoPlayer = vision.VideoPlayer('Position',[100,100,680,520]);

Read the first video frame, which contains the object, define the region.

objectFrame = videoFileReader();
objectRegion = [264,122,93,93];

As an alternative, you can use the following commands to select the object region using a mouse. The object must occupy the majority of the region:

figure; imshow(objectFrame);


Show initial frame with a red bounding box.

objectImage = insertShape(objectFrame,'Rectangle',objectRegion,'Color','red');
title('Red box shows object region');

Detect interest points in the object region.

points = detectMinEigenFeatures(rgb2gray(objectFrame),'ROI',objectRegion);

Display the detected points.

pointImage = insertMarker(objectFrame,points.Location,'+','Color','white');
title('Detected interest points');

Create a tracker object.

tracker = vision.PointTracker('MaxBidirectionalError',1);

Initialize the tracker.


Read, track, display points, and results in each video frame.

while ~isDone(videoFileReader)
      frame = videoFileReader();
      [points,validity] = tracker(frame);
      out = insertMarker(frame,points(validity, :),'+');

Release the video reader and player.