Skip to content
DeepForgeby svx
All problems
HardComputer Visioncv-034

Otsu Threshold

Problem

Compute the Otsu threshold of a grayscale image.

Build a 256-bin histogram and choose the threshold t that maximizes the between-class variance w_b * w_f * (mean_b - mean_f)^2, where the classes are pixels <= t and pixels > t. Return the smallest such t; if the image is constant, return 0.

Hint

Sweep t from 0 to 255 while tracking the running sum of the low class.

Test cases

case 1
in: [[[0,0],[255,255]]]
expected: 0
case 2
in: [[[1,2],[3,4]]]
expected: 2
case 3
in: [[[0,1],[8,9]]]
expected: 1
case 4
in: [[[7,7],[7,7]]]
expected: 0
Starter codePython
def otsu_threshold(image):
    # Your code here
    pass

4 test cases · run your code against them in the editor