code: def get_ridler_calvard_threshold(image, mask = None):
      
      cropped_image = np.array(image.flat) if mask is None else image[mask]
      if np.product(cropped_image.shape)<3:
          return 0
      if np.min(cropped_image) == np.max(cropped_image):
          return cropped_image[0]
      
      # We want to limit the dynamic range of the image to 256. Otherwise,
      # an image with almost all values near zero can give a bad result.
      min_val = np.max(cropped_image)/256;
      cropped_image[cropped_image < min_val] = min_val;
      im = np.log(cropped_image);
      min_val = np.min(im);
      max_val = np.max(im);
      im = (im - min_val)/(max_val - min_val);
      pre_thresh = 0;
      # This method needs an initial value to start iterating. Using
      # graythresh (Otsu's method) is probably not the best, because the
      # Ridler Calvard threshold ends up being too close to this one and in
      # most cases has the same exact value.
      new_thresh = otsu(im)
      delta = 0.00001;
      while abs(pre_thresh - new_thresh)>delta:
          pre_thresh = new_thresh;
          mean1 = np.mean(im[im<pre_thresh]);
          mean2 = np.mean(im[im>=pre_thresh]);
          new_thresh = np.mean([mean1,mean2]);
      return math.exp(min_val + (max_val-min_val)*new_thresh);
docs: Find a threshold using the method of Ridler and Calvard
      
      The reference for this method is:
      "Picture Thresholding Using an Iterative Selection Method" 
      by T. Ridler and S. Calvard, in IEEE Transactions on Systems, Man and
      Cybernetics, vol. 8, no. 8, August 1978.