giant.image_processing.otsu

giant.image_processing.otsu(image, n)[source]

This function performs multilevel Otsu thresholding on a 2D array.

Otsu thresholding is a technique by which the optimal threshold is chosen so as to split a 2D array based on the peaks in its histogram. In multilevel thresholding, we choose multiple optimal thresholds so that multiple peaks are separated. This process is described in “Otsu N, A Threshold Selection Method from Gray-Level Histograms, IEEE Trans. Syst. Man Cybern. 1979;9:62-66.”

To use this function, simply input the image and the number of times you want to split the histogram. The function will then return the optimal threshold values used to bin the image (n-1 thresholds), and a labeled image where each bin has its own number (n labels). Note that the function will convert the image to a uint8 image if it is not already, and the thresholds will correspond to the uint8 image.

This function uses the opencv threhold function to perform the thresholding when n=2 and is based off of the MATLAB function otsu (https://www.mathworks.com/matlabcentral/fileexchange/26532-image-segmentation-using-otsu-thresholding?s_tid=prof_contriblnk) for when n>=3.

>>> import numpy
>>> from giant.image_processing import otsu
>>> from giant.point_spread_functions import Gaussian
>>> im = numpy.zeros((100, 100), dtype=numpy.float64)
>>> x, y = numpy.meshgrid(numpy.arange(10), numpy.arange(10))
>>> psf = Gaussian(sigma_x=1.5, sigma_y=0.7, amplitude=100, centroid_x=5, centroid_y=5)
>>> im[50:60, 50:60] = psf.evaluate(x, y)
>>> thresh, labeled_im = otsu(im, 3)
>>> print(thresh)
[0.24723526470339388, 2.235294117647059]
Parameters:
  • image (ndarray) – The grayscale image to be thresholded as a numpy array

  • n (int) – The number of times to bin the image (for a binary threshold n=2)

Returns:

The n-1 threshold values and the labeled image with the background being labeled 0 and each subsequent bin being labeled with the next integer (ie 1, 2, 3, …)

Return type:

Tuple[List[Real], ndarray]