Image Segmentation Exercise: Comparing Classification Methods#

Exercise Overview#

In this hands-on exercise, you will compare three different image segmentation approaches:

  1. Fixed Thresholding - A simple rule-based method

  2. Naive Bayes Classifier - A probabilistic approach with Gaussian assumptions

  3. k-Nearest Neighbors (k-NN) - A distance-based instance learning method

Learning Objectives:

  • Understand the practical differences between simple and machine learning-based segmentation

  • Experience the impact of training sample selection on segmentation quality

  • Compare parametric vs non-parametric classification methods

  • Develop intuition for when each method performs best

Prerequisites#

You should be familiar with the concepts from our lecture on image segmentation methods. The code implementations for Naive Bayes and k-NN segmentation are provided in our course materials.

Exercise Tasks#

Part 1: Image Acquisition and Preparation#

  1. Capture Your Own Image

    • Take a photo of a scene with clear foreground and background elements

    • Examples: book on a table, object in your hand, plant against a wall

    • Save the image in JPEG or PNG format

  2. Preprocessing

    • Convert your image to grayscale

    • Ensure the image size is reasonable (recommended: 400-800 pixels width/height)

Part 2: Interactive Training Sample Selection#

You will write an interactive Python program to select training samples using mouse clicks.

Implementation Requirements:

  • Display your image

  • Use LEFT mouse click to select BACKGROUND samples (blue markers)

  • Use RIGHT mouse click to select FOREGROUND samples (orange markers)

  • Press ENTER key when finished selecting samples

  • Store the coordinates and labels of selected points

# Basic structure for your interactive selector
import matplotlib.pyplot as plt
import numpy as np

class TrainingSampleSelector:
    def __init__(self, image):
        self.image = image
        self.background_samples = []  # Store (y,x) coordinates
        self.foreground_samples = []  # Store (y,x) coordinates
        
    def setup_interaction(self):
        # Display image and set up mouse click handlers
        # LEFT click → add to background_samples, plot blue circle
        # RIGHT click → add to foreground_samples, plot orange square
        # ENTER key → finish selection
        pass
        
    def get_training_data(self):
        # Return combined coordinates and labels
        coords = self.background_samples + self.foreground_samples
        labels = [0]*len(self.background_samples) + [1]*len(self.foreground_samples)
        return np.array(coords), np.array(labels)

Sample Selection Guidelines:

  • Select 10-15 samples for each class (background/foreground)

  • Choose representative samples from different regions of each class

  • Include some challenging areas near boundaries

Part 3: Run Segmentation Methods#

Use the provided segmentation functions to compare all three methods on your image:

# Use these functions from our course materials
from segmentation_methods import (
    fixed_threshold_segmentation,
    naive_bayes_segmentation, 
    knn_segmentation,
    compare_all_segmentation_methods
)

# Run comparison
results = compare_all_segmentation_methods(
    your_image, 
    your_ground_truth,  # Optional: if you have manual segmentation
    train_coords, 
    train_labels
)

Part 4: Self-Reflection Questions#

Consider these questions for your own understanding (not required for submission):

  1. Which method gave the best results on your image and why?

  2. How did the training sample selection affect the results?

  3. What are the practical advantages and limitations of each method?

  4. Based on your image characteristics, which method would be most suitable for real applications?

Deliverables#

Submit a ZIP archive containing your input image and the Jupyter Notebook file through the university’s virtual learning system.

Your Jupyter Notebook should contain:

  1. Your original image (embedded in the notebook)

  2. Your interactive selection code with working implementation

  3. Complete execution output showing:

    • Original image with training samples marked

    • Segmentation results from all three methods

    • All visualizations and comparisons generated by the code

  4. Brief comments explaining your observations

Submission Instructions#

  1. Complete all work in a single Jupyter Notebook file

  2. Ensure all code cells execute properly and show outputs

  3. Name your file as: StudentID_ImageSegmentation.ipynb

  4. Compress your notebook file as a ZIP archive

  5. Submit through the assignment link above

Tips for Success#

  • Start with a simple, high-contrast image for easier analysis

  • Make sure to select diverse training samples from different image regions

  • Test your interactive selector thoroughly before final submission

  • Ensure all code cells run sequentially without errors

  • Save your work frequently and backup your notebook file


Note: The base segmentation code is available in our course materials. Focus on implementing the interactive selector and understanding the method behaviors through practical experimentation.