Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Segmentation basics, recalled

~13 min

Before a vision system can recognize what is in an image, it often has to decide something else first: which pixels belong together. That could mean the object versus the background, or one region versus another. digital-image-processing built the classical answers to that question. Chapter 14 builds the deep learning answer on top of them. This section recalls the two classical ideas Chapter 14 keeps contrasting itself against. One splits an image into two groups by intensity. The other splits it into any number of groups by clustering.

Thresholding is the simplest way to answer "is this pixel object or background?" Pick one brightness cutoff. Darker than it means object; everything else means background — like a silhouette. Otsu's method takes the guesswork out of picking that cutoff. Instead of eyeballing it, look at the image's own histogram. Try every possible split, and score each one by how cleanly it separates the pixels into two tight, well-separated groups. The best-scoring split wins. It's still just one number applied everywhere, though. Otsu answers "what's the best possible single cutoff," not "does a single cutoff even make sense here."

K-means lets you ask for as many groups as actually make sense, not just two. Every pixel gets compared against k "typical" values, called centroids. Each pixel joins whichever centroid it's closest to. Then each centroid updates itself to the honest average of the pixels that just joined it, since its group just changed. Repeat those two steps — join the nearest centroid, then update it to a real average — and the centroids settle into place. Each one ends up standing for a genuinely distinct cluster of pixel values, however many clusters were asked for.

Global thresholding classifies every pixel against one fixed cutoff T: g(x,y)=255 (foreground) if f(x,y)\le T, else 0 (background). This is cheap, and exact whenever foreground and background genuinely occupy separate intensity bands everywhere in the frame. Otsu's method removes the guesswork in choosing T. It picks whichever candidate maximizes the between-class variance, \sigma_B^2(T) = w_0(T)\,w_1(T)\,\big(\mu_0(T)-\mu_1(T)\big)^2, where w_0,w_1 are the fraction of pixels in each class (v\le T and v>T), and \mu_0,\mu_1 their own mean intensities. That is the cutoff making the two resulting groups as internally tight, and as far apart from each other, as possible. It's computed directly from the image's own histogram, with no cutoff chosen by eye.

Both methods share one limitation: exactly two classes, foreground and background. K-means segmentation generalizes to any number k of classes, chosen up front. It alternates two steps until nothing changes. Assign: put every pixel with its nearest of k centroids (by intensity, or by a richer feature vector). Update: move each centroid to the mean of the pixels now assigned to it. Replace every pixel with its own cluster's centroid, and the image splits into k regions grouped by similarity, rather than by a single fixed cutoff.

Quick review

Practice from earlier coursework, pulled in for this topic.

From Digital Image Processing

This chapter's global thresholding rule classifies a pixel as foreground (255) when its own value is...

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

This chapter's convention flags the DARKER class as foreground, not the brighter one -- greater-than is backwards for this rule.

Foreground doesn't require an exact match -- the whole intensity range at or below T counts as foreground.

That's adaptive thresholding's own defining feature (Section 3) -- global thresholding shares one T everywhere.

Why

g(x,y)=255 if f(x,y)<=T else 0 -- one shared T, applied identically to every pixel.

From Digital Image Processing

Using the same T=100, what is g(x,y) for a pixel reading v=150?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

150<=100 is false, so g=0 (background).

Correct answer

Also accepted:

Why

150 is brighter than T=100 -- check whether that still satisfies v<=T.

Formula

Otsu's Method

also known as: between-class variance thresholding

Recalled from Digital Image Processing · Otsu's method — first taught there.

🔖

Otsu's method picks the threshold T that best separates a histogram into two classes, by maximizing the between-class variance \sigma_B^2(T) = w_0(T)\,w_1(T)\,\big(\mu_0(T)-\mu_1(T)\big)^2, where w_0,w_1 are the two classes' own population fractions (v\le T and v>T respectively) and \mu_0,\mu_1 their own mean intensities. Evaluating this at every candidate split and keeping the largest is exhaustive but cheap (at most 255 candidates for an 8-bit image), and requires nothing but the image's own histogram — no assumption about where the "right" cutoff should sit. Otsu's method still produces a single global threshold; it removes the guesswork from choosing T, not the uneven-lighting weakness of using one T for the whole image.

Computing Otsu's threshold from a small histogram

This 2\times3 image has values 30, 30, 70, 190, 230, 230. Every candidate split's own between-class variance is scored above -- confirm by eye that t=70 wins, then step through the pixels to see the resulting foreground/background mask.

Loading interactive…

Worked example

Scoring one Otsu candidate by hand

  1. 1

    The histogram above holds \{30,30,70,190,230,230\} (N=6). Score the candidate t=70: class A=\{30,30,70\}, class B=\{190,230,230\}.

  2. 2
    w_0=\tfrac36=0.5,\ \ \mu_0=\tfrac{30+30+70}{3}\approx43.33,\ \ w_1=0.5,\ \ \mu_1=\tfrac{190+230+230}{3}\approx216.67\sigma_B^2(70) = (0.5)(0.5)(43.33-216.67)^2 \approx (0.25)(30046.8)\approx7511.7

Loading interactive…

Worked example

K-means: one assignment-and-update round

  1. 1

    Using the same six values \{30,30,70,190,230,230\}, run one round of k=2 k-means starting from centroids c_1=30, c_2=230. Assign each value to its nearer centroid, then update each centroid to its own cluster's mean. Compare each value's distance to 30 versus its distance to 230.

  2. 2

    Assignment: 30,30,70 are all closer to c_1=30; 190,230,230 are all closer to c_2=230. Update: c_1=\tfrac{30+30+70}{3}\approx43.33, c_2=\tfrac{190+230+230}{3}\approx216.67. These are the same two groups Otsu's between-class-variance search found on its own, and the same two class means computed above. K-means reached them by a completely different rule: nearest-centroid distance, instead of maximizing separation.

Loading interactive…

Where this goes next

Classical vs. learned segmentation

Chapter 14 opens by contrasting itself directly against this section. A learned segmentation network assigns every pixel a class. It uses features it discovered during training — not a fixed intensity cutoff or a hand-picked k. But it's judged against the exact same kind of question: "did this pixel get the right label?" Global thresholding and k-means already answer that question here. They just use far simpler classes ("foreground" or "background") instead of richer ones like car, road, or pedestrian.

Check your understanding (counts toward your progress)

Global thresholding classifies a pixel as foreground when...

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Requiring an EXACT value of 255 would classify almost no real pixel as foreground -- the rule uses a threshold comparison, not an exact match.

Position in the image plays no role in this rule at all -- only the pixel's own value matters.

Global thresholding looks only at one pixel's own value against T -- it never inspects the neighboring pixels.

Why

The standard convention this course uses treats a pixel as foreground exactly when its own value is at or below the chosen cutoff T.

In k-means segmentation, the "update" step recomputes each centroid as...

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Using only the single closest pixel would throw away information from every other pixel in that cluster.

The centroids are meant to UPDATE as the algorithm runs, tracking whichever pixels are currently assigned -- a fixed value never adapts.

The image's overall maximum has nothing to do with any one cluster's own actual membership.

Why

The update step recomputes each centroid as the honest average of exactly the pixels currently assigned to it, which is why it can shift between iterations.

What real-world problem does Otsu's method solve that plain global thresholding does not?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Otsu's method still produces exactly two classes, same as plain thresholding -- k-means is the method that generalizes beyond two.

A single global T is still used everywhere in the image -- Otsu doesn't make the threshold local, only automatic.

Otsu's method works identically on grayscale data -- it isn't limited to color images.

Why

Otsu's method still picks one global cutoff, but computes the BEST one directly from the image's own histogram instead of requiring a hand-picked guess.

For the values \{40,40,200,200\} split at candidate t=40 (class A = values \le 40, class B = values >40), what is w_0, the fraction of pixels in class A?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

Two of the four values (40 and 40) fall in class A, so w_0 = 2/4 = 0.5.

Correct answer

Also accepted:

Why

Unlike global thresholding or Otsu's method, k-means segmentation can produce...

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

A binary split is exactly what thresholding already does -- k-means's whole advantage is generalizing PAST two classes.

Centroids are the entire mechanism k-means is built around -- there is no k-means without them.

Different initial centroid placements can genuinely change the final result -- k-means is not guaranteed to be identical every run.

Why

K-means lets the user choose any k, generalizing past thresholding's fixed two-class limit.

Explain why k-means and Otsu's method can arrive at the same two-group split for a clearly bimodal dataset, despite using completely different rules to get there.

Practice: From one fixed cutoff to k clusters (4 problems · doesn’t affect your progress)

What real weakness does Otsu's method still share with plain global thresholding, despite computing T automatically?

For values \{20,20,80,180,220,220\} split at t=80 (class A \le80, class B >80), what is w_1, the fraction of values in class B?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

Three of the six values (180,220,220) fall in class B, so w_1=3/6=0.5.

Correct answer

Also accepted:

Why

0.5

If k=3 is chosen for an image with only two genuinely distinct intensity groups, what is the most likely outcome?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

K-means always produces exactly the number of clusters requested -- asking for more clusters than the data naturally has typically splits a genuine group into pieces rather than detecting that fewer would fit better.

k-means still finds 3 clusters, possibly splitting one real group into two

Running k=2 k-means on \{15,15,95,95\} starting from centroids c_1=15, c_2=95, what is the updated c_1 after one assignment-and-update round?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

Both 15s are already closest to c_1=15; their mean is 15 itself, so c_1 doesn't move -- the algorithm has already converged.

Correct answer

Also accepted:

Why

15

Feeling good about this section?

Take the section quiz

Every answer gets a full explanation at the end.

Graph settings

Calculator

Sign in to save highlights

Insert an equation

  • No matches.