Main Content

initializeObject

Set object to track

Description

initializeObject(hbtracker,I,R) sets the object to track by extracting it from the [x y width height] region R located in the 2-D input image, I. The input image, I, can be any 2-D feature map that distinguishes the object from the background. For example, the image can be a hue channel of the HSV color space. Typically, I will be the first frame in which the object appears. The region, R, is also used for the initial search window, in the next call to the object. For best results, the object must occupy the majority of the region, R.

example

initializeObject(hbtracker,I,R,N) additionally, lets you specify N, the number of histogram bins.

Examples

collapse all

Track and display a face in each frame of an input video.

Create System objects for reading and displaying video.

videoReader = VideoReader("vipcolorsegmentation.avi");
videoPlayer = vision.VideoPlayer();

Read the first video frame, which contains the object. Convert the image to HSV color space. Then define and display the object region.

objectFrame = im2single(readFrame(videoReader));
objectHSV = rgb2hsv(objectFrame);
objectRegion = [40, 45, 25, 25];
objectImage = insertShape(objectFrame,"rectangle",objectRegion,Color=[1 0 0]);

figure
imshow(objectImage)
title("Red box shows object region")

(Optionally, you can select the object region using your mouse. The object must occupy the majority of the region. Use the following command.)

figure; imshow(objectFrame); objectRegion=round(getPosition(imrect))

Set the object, based on the hue channel of the first video frame.

tracker = vision.HistogramBasedTracker;
initializeObject(tracker, objectHSV(:,:,1) , objectRegion);

Track and display the object in each video frame. The while loop reads each image frame, converts the image to HSV color space, then tracks the object in the hue channel where it is distinct from the background. Finally, the example draws a box around the object and displays the results.

while hasFrame(videoReader)
  frame = im2single(readFrame(videoReader));
  hsv = rgb2hsv(frame);
  bbox = tracker(hsv(:,:,1));
  out = insertShape(frame,"rectangle",bbox,Color=[1 0 0]);
  videoPlayer(out);
end

Release the video player.

release(videoPlayer);

Input Arguments

collapse all

Histogram based tracker, specified as a vision.HistogramBasedTracker object.

Video frame, specified as grayscale or truecolor (RGB).

Initial search window, specified in the format [x y width height].

Number of histogram bins, specified as an integer. Increasing the number of bins enhances the ability of the tracker to discriminate the object. However, this approach also narrows the range of changes to the object's visual characteristics that the tracker can accommodate. Consequently, this narrow range increases the likelihood of losing track.

Version History

Introduced in R2012a