Computer Vision · Intermediate level

Intermediate level · Module 5: Image Segmentation · Lesson 1 of 3

Classical Segmentation Methods

Learners separate objects from background with thresholds and colour clustering, split touching objects with the watershed algorithm and extract foreground with GrabCut.

In this lesson you will

  • Apply classical segmentation methods
  • Choose a method for an image type

Key concepts

foreground and backgrounddistance transformwatershedGrabCutmarkers

The lesson

When objects differ clearly from the background, thresholding or k-means colour clustering gives a usable mask. Touching objects (seeds, fruit in a crate) merge into one region; the watershed algorithm uses a distance transform and markers to split them.

GrabCut (cv2.grabCut) separates a foreground object from its background using a rectangle drawn around the object as a hint, which is useful for cleaning product photos.

Classical methods need no training data and run fast on a CPU, but they need tuning for each kind of image and fail on complex scenes.

Step by step

  1. Segment touching beans with threshold, distance transform and watershed.
  2. Count the separated objects and compare with the Beginner counter.
  3. Extract a fruit from a cluttered background with GrabCut.
  4. Note where each method fails.

Practice activity

Segment and count touching objects and extract foreground objects for the produce dataset.

Expected result: Better counts than simple thresholding and clean foreground masks, with failures noted.

Check your understanding

  1. What problem does the watershed algorithm solve?

    Show answer

    Separating touching objects that form one region.

  2. What hint does GrabCut need?

    Show answer

    A rectangle around the object (or marked foreground and background).

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.