Computer Vision Fundamentals

Computer Vision Fundamentals - Lesson 7: Object Tracking and Motion Analysis

Overview:

This lesson covers object tracking and motion analysis, essential techniques for monitoring and analyzing objects as they move through a sequence of images or video frames. Students will learn about various tracking methods and how to analyze motion to extract meaningful information from video data.

Objectives:

By the end of this lesson, students should be able to:

  • Understand the principles and techniques of object tracking.
  • Implement various tracking algorithms to follow objects in video sequences.
  • Analyze motion and extract useful information from video data.

Topics Covered:

  1. Introduction to Object Tracking:

    • Definition and importance of object tracking.
    • Applications in video surveillance, autonomous vehicles, and human-computer interaction.
  2. Tracking Algorithms:

    • Kalman Filter:

      • Overview of the Kalman Filter and its use in tracking.
      • Implementation for tracking an object in a video sequence.

      Code Example:

      import cv2
      import numpy as np
      
      # Create a Kalman Filter object
      kalman = cv2.KalmanFilter(4, 2)
      kalman.measurementMatrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0]], dtype=np.float32)
      kalman.transitionMatrix = np.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]], dtype=np.float32)
      kalman.processNoiseCov = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 100, 0], [0, 0, 0, 100]], dtype=np.float32)
      kalman.measurementNoiseCov = np.array([[1, 0], [0, 1]], dtype=np.float32)
      kalman.errorCovPost = np.eye(4, dtype=np.float32)
      
      # Load video
      cap = cv2.VideoCapture('video.mp4')
      ret, frame = cap.read()
      
      # Initialize tracking
      bbox = (200, 200, 50, 50)  # Initial bounding box (x, y, w, h)
      tracker = cv2.TrackerKCF_create()
      tracker.init(frame, bbox)
      
      while True:
          ret, frame = cap.read()
          if not ret:
              break
      
          # Track object
          ret, bbox = tracker.update(frame)
          if ret:
              p1 = (int(bbox[0]), int(bbox[1]))
              p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
              cv2.rectangle(frame, p1, p2, (0, 255, 0), 2, 1)
      
          # Display the result
          cv2.imshow('Tracking', frame)
          if cv2.waitKey(1) & 0xFF == 27:  # ESC key to exit
              break
      
      cap.release()
      cv2.destroyAllWindows()
      
    • Mean Shift and CAMShift:

      • Explanation of Mean Shift and CAMShift algorithms.
      • Code implementation and example.

      Code Example:

      import cv2
      import numpy as np
      
      # Load video
      cap = cv2.VideoCapture('video.mp4')
      ret, frame = cap.read()
      
      # Define the region of interest (ROI)
      roi = cv2.selectROI(frame)
      hsv_roi = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
      roi_hist = cv2.calcHist([hsv_roi], [0, 1], None, [16, 16], [0, 180, 0, 256])
      roi_hist = cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
      
      # Set up the termination criteria
      term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
      
      while True:
          ret, frame = cap.read()
          if not ret:
              break
      
          hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
          dst = cv2.calcBackProject([hsv], [0, 1], roi_hist, [0, 180, 0, 256], 1)
          ret, track_window = cv2.CamShift(dst, roi, term_crit)
          pts = cv2.boxPoints(ret)
          pts = np.int0(pts)
          cv2.polylines(frame, [pts], True, 255, 2)
      
          # Display the result
          cv2.imshow('Tracking', frame)
          if cv2.waitKey(1) & 0xFF == 27:  # ESC key to exit
              break
      
      cap.release()
      cv2.destroyAllWindows()
      
    • Deep Learning-Based Tracking:

      • Overview of deep learning methods for tracking (e.g., SORT, DeepSORT).
      • Implementation using a pre-trained deep learning model.
  3. Motion Analysis:

    • Optical Flow:

      • Concept of optical flow and its applications.
      • Implementing optical flow using the Lucas-Kanade method.

      Code Example:

      import cv2
      import numpy as np
      
      # Load video
      cap = cv2.VideoCapture('video.mp4')
      ret, old_frame = cap.read()
      old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
      p0 = cv2.goodFeaturesToTrack(old_gray, mask=None, **dict(maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7))
      
      # Create a mask for drawing
      mask = np.zeros_like(old_frame)
      
      while True:
          ret, frame = cap.read()
          if not ret:
              break
      
          frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
          p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None)
      
          good_new = p1[st == 1]
          good_old = p0[st == 1]
      
          for i, (new, old) in enumerate(zip(good_new, good_old)):
              a, b = new.ravel()
              c, d = old.ravel()
              mask = cv2.line(mask, (a, b), (c, d), color[i].tolist(), 2)
              frame = cv2.circle(frame, (a, b), 5, color[i].tolist(), -1)
      
          img = cv2.add(frame, mask)
          cv2.imshow('Optical Flow', img)
      
          if cv2.waitKey(30) & 0xFF == 27:  # ESC key to exit
              break
      
          old_gray = frame_gray.copy()
          p0 = good_new.reshape(-1, 1, 2)
      
      cap.release()
      cv2.destroyAllWindows()
      
    • Background Subtraction:

      • Techniques for background subtraction (e.g., MOG2, KNN).
      • Application in video surveillance for motion detection.

      Code Example:

      import cv2
      
      # Load video
      cap = cv2.VideoCapture('video.mp4')
      backSub = cv2.createBackgroundSubtractorMOG2()
      
      while True:
          ret, frame = cap.read()
          if not ret:
              break
      
          # Apply background subtraction
          fg_mask = backSub.apply(frame)
          cv2.imshow('Foreground Mask', fg_mask)
          cv2.imshow('Original Video', frame)
      
          if cv2.waitKey(30) & 0xFF == 27:  # ESC key to exit
              break
      
      cap.release()
      cv2.destroyAllWindows()
      
  4. Applications of Object Tracking and Motion Analysis:

    • Video surveillance and security.
    • Traffic monitoring and analysis.
    • Sports analytics and motion tracking.
  5. Challenges in Object Tracking and Motion Analysis:

    • Handling occlusions, abrupt movements, and variable lighting conditions.
    • Dealing with complex backgrounds and multiple objects.

Activities and Quizzes:

  • Activity: Implement an object tracking system using one of the tracking algorithms discussed. Test it on a video sequence and analyze its performance.
  • Quiz: Multiple-choice questions on tracking algorithms, motion analysis techniques, and their applications.

Assignments:

  • Assignment 7: Develop a project where you track and analyze the motion of objects in a video dataset (e.g., tracking people in a crowded scene or vehicles on a road). Submit the code along with a report detailing the tracking method used, performance metrics, and insights gained from the motion analysis.

This lesson provides students with practical skills in object tracking and motion analysis, preparing them for real-world applications and advanced topics in computer vision.