Computer Vision Fundamentals

Computer Vision Fundamentals - Lesson 3: Image Segmentation and Contour Detection

Overview:

This lesson delves into image segmentation and contour detection, two essential techniques in computer vision for identifying and analyzing distinct regions within an image. Students will learn how to segment images and detect contours, along with practical coding examples.

Objectives:

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

  • Understand the principles of image segmentation.
  • Apply different image segmentation techniques.
  • Detect and draw contours in images.
  • Implement practical applications using segmentation and contour detection.

Topics Covered:

  1. Introduction to Image Segmentation:

    • Definition and importance in computer vision.
    • Different types of segmentation: Threshold-based, region-based, and edge-based.
    • Practical applications of segmentation (e.g., in medical imaging, object detection).
  2. Threshold-based Segmentation:

    • Global thresholding using simple techniques like Otsu's method.
    • Adaptive thresholding for varying lighting conditions.

    Code Example:

    import cv2
    import numpy as np
    
    # Load an image in grayscale
    image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
    
    # Apply global thresholding
    _, binary_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
    
    # Apply adaptive thresholding
    adaptive_image = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                           cv2.THRESH_BINARY, 11, 2)
    
    # Display results
    cv2.imshow('Original Image', image)
    cv2.imshow('Binary Image', binary_image)
    cv2.imshow('Adaptive Thresholding', adaptive_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
  3. Region-based Segmentation:

    • Understanding region growing and region splitting techniques.
    • Watershed algorithm for image segmentation.

    Code Example:

    # Using the Watershed algorithm for segmentation
    # Load an image
    image = cv2.imread('image.jpg')
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Apply thresholding
    _, binary_image = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    
    # Remove noise with morphological operations
    kernel = np.ones((3, 3), np.uint8)
    opening = cv2.morphologyEx(binary_image, cv2.MORPH_OPEN, kernel, iterations=2)
    
    # Sure background area
    sure_bg = cv2.dilate(opening, kernel, iterations=3)
    
    # Sure foreground area
    dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
    _, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(), 255, 0)
    
    # Unknown region
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg, sure_fg)
    
    # Marker labeling
    _, markers = cv2.connectedComponents(sure_fg)
    markers = markers + 1
    markers[unknown == 255] = 0
    
    # Apply Watershed
    markers = cv2.watershed(image, markers)
    image[markers == -1] = [0, 0, 255]
    
    # Display results
    cv2.imshow('Watershed Result', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
  4. Contour Detection:

    • Understanding contours and their significance in image analysis.
    • Using OpenCV to find and draw contours.
    • Hierarchical contour detection and its applications.

    Code Example:

    # Load an image
    image = cv2.imread('image.jpg')
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Apply thresholding
    _, binary_image = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    
    # Find contours
    contours, _ = cv2.findContours(binary_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    # Draw contours on the original image
    cv2.drawContours(image, contours, -1, (0, 255, 0), 3)
    
    # Display the result
    cv2.imshow('Contours', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
  5. Practical Applications:

    • Combining segmentation and contour detection for object tracking.
    • Real-world example: Segmentation and contour detection in a traffic scene.
  6. Challenges in Segmentation and Contour Detection:

    • Dealing with noise, overlapping objects, and varying illumination.

Assignments:

  • Assignment 3: Create a project where you implement segmentation and contour detection on a dataset of your choice (e.g., segmenting and detecting objects in a set of outdoor images). Submit the code along with a report explaining the techniques used and the results obtained.

This lesson provides a deeper understanding of how to break down and analyze images, paving the way for more advanced computer vision tasks in the upcoming lessons.