def find_boundaries(label_img, connectivity=1, mode='thick', background=0):
 if (label_img.dtype == 'bool'):
  label_img = label_img.astype(np.uint8)
 ndim = label_img.ndim
 selem = ndi.generate_binary_structure(ndim, connectivity)
 if (mode != 'subpixel'):
  boundaries = (dilation(label_img, selem) != erosion(label_img, selem))
  if (mode == 'inner'):
   foreground_image = (label_img != background)
   boundaries &= foreground_image
  elif (mode == 'outer'):
   max_label = np.iinfo(label_img.dtype).max
   background_image = (label_img == background)
   selem = ndi.generate_binary_structure(ndim, ndim)
   inverted_background = np.array(label_img, copy=True)
   inverted_background[background_image] = max_label
   adjacent_objects = ((dilation(label_img, selem) != erosion(inverted_background, selem)) & (~ background_image))
   boundaries &= (background_image | adjacent_objects)
  return boundaries
 else:
  boundaries = _find_boundaries_subpixel(label_img)
  return boundaries

def find_boundaries ( label_img , connectivity = 1 , mode = 'thick' , background = 0 ) : if ( label_img . dtype == 'bool' ) : label_img = label_img . astype ( np . uint8 ) ndim = label_img . ndim selem = ndi . generate_binary_structure ( ndim , connectivity ) if ( mode != 'subpixel' ) : boundaries = ( dilation ( label_img , selem ) != erosion ( label_img , selem ) ) if ( mode == 'inner' ) : foreground_image = ( label_img != background ) boundaries &= foreground_image elif ( mode == 'outer' ) : max_label = np . iinfo ( label_img . dtype ) . max background_image = ( label_img == background ) selem = ndi . generate_binary_structure ( ndim , ndim ) inverted_background = np . array ( label_img , copy = True ) inverted_background [ background_image ] = max_label adjacent_objects = ( ( dilation ( label_img , selem ) != erosion ( inverted_background , selem ) ) & ( ~ background_image ) ) boundaries &= ( background_image | adjacent_objects ) return boundaries else : boundaries = _find_boundaries_subpixel ( label_img ) return boundaries
return bool array where boundaries between labeled regions are true .