Loading...
Implement Otsu's method to find the optimal binarization threshold that maximizes between-class variance.
Given a list of pixel intensities (values 0-255), find the threshold t that best separates the pixels into two classes (foreground and background) by maximizing the between-class variance σB2.
Algorithm:
For each candidate threshold t from 0 to 255:
Class probabilities:
Class means:
Between-class variance: σB2(t)=w0(t)⋅w1(t)⋅(μ0(t)−μ1(t))2
The optimal threshold maximizes σB2(t).
where p(i) is the probability of intensity i (histogram normalized by total pixel count).
Return the threshold value (integer) that maximizes σB2.
pixels = [0, 0, 0, 0, 0, 255, 255, 255, 255, 255] num_bins = 256
0
pixels is used to calculate the probability of each intensity i, which is p(i)=total number of pixelsnumber of pixels with intensity i. For the given input, p(0)=105=0.5 and p(255)=105=0.5.