def convex_hull_object(image, neighbors=8):
 if (image.ndim > 2):
  raise ValueError('Input must be a 2D image')
 if ((neighbors != 4) and (neighbors != 8)):
  raise ValueError('Neighbors must be either 4 or 8.')
 labeled_im = label(image, neighbors, background=0)
 convex_obj = np.zeros(image.shape, dtype=bool)
 convex_img = np.zeros(image.shape, dtype=bool)
 for i in range(1, (labeled_im.max() + 1)):
  convex_obj = convex_hull_image((labeled_im == i))
  convex_img = np.logical_or(convex_img, convex_obj)
 return convex_img

def convex_hull_object ( image , neighbors = 8 ) : if ( image . ndim > 2 ) : raise ValueError ( 'Input must be a 2D image' ) if ( ( neighbors != 4 ) and ( neighbors != 8 ) ) : raise ValueError ( 'Neighbors must be either 4 or 8.' ) labeled_im = label ( image , neighbors , background = 0 ) convex_obj = np . zeros ( image . shape , dtype = bool ) convex_img = np . zeros ( image . shape , dtype = bool ) for i in range ( 1 , ( labeled_im . max ( ) + 1 ) ) : convex_obj = convex_hull_image ( ( labeled_im == i ) ) convex_img = np . logical_or ( convex_img , convex_obj ) return convex_img
compute the convex hull image of individual objects in a binary image .