if ~isempty(bbox)
% Find corner points inside the detected region.
points = detectMinEigenFeatures(videoFrameGray, 'ROI', bbox(1, :));
xyPoints = points.Location;
numPts = size(xyPoints,1);
release(pointTracker);
initialize(pointTracker, xyPoints, videoFrameGray);
oldPoints = xyPoints;
bboxPoints = bbox2points(bbox(1, :));
bboxPolygon = reshape(bboxPoints', 1, []);
videoFrame = insertShape(videoFrame, 'Polygon', bboxPolygon, 'LineWidth', 3);
videoFrame = insertShape(videoFrame, 'circle', [(bbox(1)+bbox(3))/2 (bbox(2)+bbox(4))/2 10], 'LineWidth', 3);
videoFrame = insertMarker(videoFrame, xyPoints, '+', 'Color', 'white');
videoFrame = insertMarker(videoFrame, [mean(bboxPoints(2:2:8)) mean(bboxPoints(1:2:7))], 'o', 'Color', 'red');
end
else
% Tracking mode.
[xyPoints, isFound] = step(pointTracker, videoFrameGray);
visiblePoints = xyPoints(isFound, :);
oldInliers = oldPoints(isFound, :);
numPts = size(visiblePoints, 1);
if numPts >= 10
[xform, oldInliers, visiblePoints] = estimateGeometricTransform(...
oldInliers, visiblePoints, 'similarity', 'MaxDistance', 4);
% Apply the transformation to the bounding box.
bboxPoints = transformPointsForward(xform, bboxPoints);
% Convert the box corners into the [x1 y1 x2 y2 x3 y3 x4 y4]
% format required by insertShape.
bboxPolygon = reshape(bboxPoints', 1, []);
% Display a bounding box around the face being tracked.
videoFrame = insertShape(videoFrame, 'Polygon', bboxPolygon, 'LineWidth', 3);
% Display tracked points.
videoFrame = insertMarker(videoFrame, visiblePoints, '+', 'Color', 'white');
videoFrame = insertMarker(videoFrame, [mean(bboxPoints(2:2:8)) mean(bboxPoints(1:2:7))], 'o', 'Color', 'red');
% Reset the points.
oldPoints = visiblePoints;
setPoints(pointTracker, oldPoints);
end
end