code: def _find_all_segments(pixels, segment_threshold, min_segment_size):
      """
      Finds all the regions within an image pixel array, and returns a list of the regions.
  
      Note: Slightly different segmentations are produced when using pillow version 6 vs. >=7, due to a change in
      rounding in the greyscale conversion.
      :param pixels: A numpy array of the pixel brightnesses.
      :param segment_threshold: The brightness threshold to use when differentiating between hills and valleys.
      :param min_segment_size: The minimum number of pixels for a segment.
      """
      img_width, img_height = pixels.shape
      # threshold pixels
      threshold_pixels = pixels > segment_threshold
      unassigned_pixels = numpy.full(pixels.shape, True, dtype=bool)
  
      segments = []
      already_segmented = set()
  
      # Add all the pixels around the border outside the image:
      already_segmented.update([(-1, z) for z in range(img_height)])
      already_segmented.update([(z, -1) for z in range(img_width)])
      already_segmented.update(
          [(img_width, z) for z in range(img_height)]
      )
      already_segmented.update(
          [(z, img_height) for z in range(img_width)]
      )
  
      # Find all the "hill" regions
      while numpy.bitwise_and(
          threshold_pixels, unassigned_pixels
      ).any():
          remaining_pixels = numpy.bitwise_and(
              threshold_pixels, unassigned_pixels
          )
          segment = _find_region(remaining_pixels, already_segmented)
          # Apply segment
          if len(segment) > min_segment_size:
              segments.append(segment)
          for pix in segment:
              unassigned_pixels[pix] = False
  
      # Invert the threshold matrix, and find "valleys"
      threshold_pixels_i = numpy.invert(threshold_pixels)
      while len(already_segmented) < img_width * img_height:
          remaining_pixels = numpy.bitwise_and(
              threshold_pixels_i, unassigned_pixels
          )
          segment = _find_region(remaining_pixels, already_segmented)
          # Apply segment
          if len(segment) > min_segment_size:
              segments.append(segment)
          for pix in segment:
              unassigned_pixels[pix] = False
  
      return segments
  
imports: from __future__ import absolute_import, division, print_function
  import sys
  import numpy
  from PIL import Image, ImageFilter
label_desc: Use image node_id 14 for calling a global function with example usage: _find_all_segments(pixels, segment_threshold, min_segment_size) and returns: segments