def threshold_isodata(image, nbins=256, return_all=False):
 (hist, bin_centers) = histogram(image.ravel(), nbins)
 if (len(bin_centers) == 1):
  if return_all:
   return bin_centers
  else:
   return bin_centers[0]
 hist = hist.astype(np.float32)
 csuml = np.cumsum(hist)
 csumh = (np.cumsum(hist[::(-1)])[::(-1)] - hist)
 intensity_sum = (hist * bin_centers)
 csumh[(-1)] = 1
 l = (np.cumsum(intensity_sum) / csuml)
 h = ((np.cumsum(intensity_sum[::(-1)])[::(-1)] - intensity_sum) / csumh)
 all_mean = ((l + h) / 2.0)
 bin_width = (bin_centers[1] - bin_centers[0])
 distances = (all_mean - bin_centers)
 thresholds = bin_centers[((distances >= 0) & (distances < bin_width))]
 if return_all:
  return thresholds
 else:
  return thresholds[0]

def threshold_isodata ( image , nbins = 256 , return_all = False ) : ( hist , bin_centers ) = histogram ( image . ravel ( ) , nbins ) if ( len ( bin_centers ) == 1 ) : if return_all : return bin_centers else : return bin_centers [ 0 ] hist = hist . astype ( np . float32 ) csuml = np . cumsum ( hist ) csumh = ( np . cumsum ( hist [ : : ( - 1 ) ] ) [ : : ( - 1 ) ] - hist ) intensity_sum = ( hist * bin_centers ) csumh [ ( - 1 ) ] = 1 l = ( np . cumsum ( intensity_sum ) / csuml ) h = ( ( np . cumsum ( intensity_sum [ : : ( - 1 ) ] ) [ : : ( - 1 ) ] - intensity_sum ) / csumh ) all_mean = ( ( l + h ) / 2.0 ) bin_width = ( bin_centers [ 1 ] - bin_centers [ 0 ] ) distances = ( all_mean - bin_centers ) thresholds = bin_centers [ ( ( distances >= 0 ) & ( distances < bin_width ) ) ] if return_all : return thresholds else : return thresholds [ 0 ]
return threshold value(s) based on isodata method .