track a moving curve in image set in matlab

To track a moving curve in an image set in MATLAB, the following steps can be followed:

  1. Load the image set into MATLAB using the imageSet function.
main.m
imageSetDir = fullfile('path/to/image/set');
imgSet = imageSet(imageSetDir);
77 chars
3 lines
  1. Select a region of interest (ROI) around the curve in the first image of the set using the imrect function.
main.m
firstImg = read(imgSet, 1);
imshow(firstImg);
roi = imrect(gca, [x y w h]); % x,y = coordinates of the top-left corner, w,h = width and height of the ROI
154 chars
4 lines
  1. Extract features from the ROI using the detectSURFFeatures function.
main.m
grayImg = rgb2gray(firstImg);
features = detectSURFFeatures(grayImg, 'ROI', roi.getPosition());
96 chars
3 lines
  1. Track the curve in the remaining images of the set using the vision.PointTracker object.
main.m
pointTracker = vision.PointTracker('MaxBidirectionalError', 1, 'BlockSize', [21 21]); % adjust parameters as needed
initialize(pointTracker, features.Location, grayImg);

for i = 2:imgSet.Count
    currImg = read(imgSet, i);
    grayImg = rgb2gray(currImg);
    [points, isValid] = step(pointTracker, grayImg);
    
    if sum(isValid) < 10 % if less than 10 points tracked, re-initialize with features
        features = detectSURFFeatures(grayImg, 'ROI', roi.getPosition());
        initialize(pointTracker, features.Location, grayImg);
    end
end
551 chars
14 lines
  1. Plot the tracked points on the original images using the insertMarker function.
main.m
trackedPts = pointTracker.TrackedPoints;
for i = 1:imgSet.Count
    currImg = read(imgSet, i);
    markedImg = insertMarker(currImg, trackedPts(i).Location);
    imshow(markedImg);
end
185 chars
7 lines

gistlibby LogSnag