parameters: (imin,  step,  d_weight, filter_ori)
return_statement: return imout,  im_grad,  im_minima_val
docstring: Compute m-waterpixels, i.e. superpixels based on the watershed transformation. 
      Flooding starts form the best minimum of each cell of a regular grid. 
      The gradient used to be flooded is regularized using the distance function to these minima.
      Cells of the grid are chosen here to be squares.
      
      Input :
      imin (image UINT8): original image, to be segmented into superpixels
      step (UINT8) : grid step, i.e. distance between two cell centers of the grid
      d_weight (UINT8) : constant to be multiplied with function distance before addition to gradient image.
          If d_weight <=0, then only the gradient is taken into account.
      filter_ori (BOOLEAN) : do we filter the input image?
  
      Output:
      image (UINT16) : labelled superpixels
      image (UINT8) : distance weighted gradient function
      image (UINT16) : minima used in the computation
docstring_summary: Compute m-waterpixels, i.e. superpixels based on the watershed transformation. 
      Flooding starts form the best minimum of each cell of a regular grid. 
      The gradient used to be flooded is regularized using the distance function to these minima.
      Cells of the grid are chosen here to be squares.
      
      Input :
      imin (image UINT8): original image, to be segmented into superpixels
      step (UINT8) : grid step, i.e. distance between two cell centers of the grid
      d_weight (UINT8) : constant to be multiplied with function distance before addition to gradient image.
          If d_weight <=0, then only the gradient is taken into account.
      filter_ori (BOOLEAN) : do we filter the input image?
docstring_tokens: Compute m - waterpixels i . e . superpixels based on the watershed transformation . Flooding starts form the best minimum of each cell of a regular grid . The gradient used to be flooded is regularized using the distance function to these minima . Cells of the grid are chosen here to be squares . Input : imin ( image UINT8 ) : original image to be segmented into superpixels step ( UINT8 ) : grid step i . e . distance between two cell centers of the grid d_weight ( UINT8 ) : constant to be multiplied with function distance before addition to gradient image . If d_weight < = 0 then only the gradient is taken into account . filter_ori ( BOOLEAN ) : do we filter the input image?
function: def demo_m_waterpixels(imin,  step,  d_weight, filter_ori):
      """ 
      Compute m-waterpixels, i.e. superpixels based on the watershed transformation. 
      Flooding starts form the best minimum of each cell of a regular grid. 
      The gradient used to be flooded is regularized using the distance function to these minima.
      Cells of the grid are chosen here to be squares.
      
      Input :
      imin (image UINT8): original image, to be segmented into superpixels
      step (UINT8) : grid step, i.e. distance between two cell centers of the grid
      d_weight (UINT8) : constant to be multiplied with function distance before addition to gradient image.
          If d_weight <=0, then only the gradient is taken into account.
      filter_ori (BOOLEAN) : do we filter the input image?
  
      Output:
      image (UINT16) : labelled superpixels
      image (UINT8) : distance weighted gradient function
      image (UINT16) : minima used in the computation
      """
      ##-----------------------------------------------------------------------------------------
      ##-----------------------------------------------------------------------------------------   
      # Connexity:
      basicse = sp.CrossSE()
      gridse = sp.SquSE()
  
      # Ori filtering
      if filter_ori is True:
          my_area_filtering(imin, step*step/16)
  
      # Gradient computation
      im_grad = my_gradient(imin, basicse,  True)
  
      ## Pool of working images:
      imwrk0 = sp.Image(im_grad)
      imwrk1 = sp.Image(im_grad,  "UINT16")
      imwrk2 = sp.Image(im_grad,  "UINT16")
  
      # Compute cell centers and cells
      size = imin.getSize()
      im_markers, im_cells = im_labelled_square_grid_points(size, step, step/6) 
  
      ##-----------------------------------------------------------------------------------------
      ##-----------------------------------------------------------------------------------------
      ## Choice of the markers : one per grid cell
      ##-----------------------------------------------------------------------------------------
      ##-----------------------------------------------------------------------------------------
  
      # Step 1 : Computation of the minima of the gradient
      im_minima = sp.Image(im_grad)
      sp.minima(im_grad, im_minima, basicse)
      #Step 2 : Imposing minimum distance between minima (= Removing minima candidates which fall on cell margins )
      sp.test(im_cells, im_minima, 0, imwrk0)
      sp.label(imwrk0, imwrk1, basicse)
      #Step 3 : Evaluation of the importance of minima ( = computation of their surfacic extinction)
      im_minima_val = sp.Image(imwrk1)
      sp.watershedExtinction( im_grad, imwrk1, im_minima_val, "a", basicse)
      # Step 4 : Taking back at value 2 the minima of null-volumic extinction 
      sp.test( imwrk0,  2,  0,  imwrk2)
      sp.test( im_minima_val,  im_minima_val, imwrk2, im_minima_val )
      # Step 5 : Coping with the potential absence of minimum in cells (= choosing the minimum value inside this cell as its marker if necessary)
      imwrk00=sp.Image(imwrk0)
      blobs = sp.computeBlobs(im_cells)
      sp.test(im_cells, im_grad, 0, imwrk00)
      minVals = sp.measMinVals(imwrk00, blobs)
      sp.applyLookup(im_cells, minVals, imwrk0)
      sp.test(imwrk00==imwrk0, 1, 0, imwrk1)
      maxVals = sp.measMaxVals(im_minima_val, blobs)
      sp.applyLookup(im_cells, maxVals, imwrk2)
      sp.test(imwrk2, im_minima_val, imwrk1, im_minima_val)
      # Step 6 : Selection of one marker per cell
      one_min_per_grid_cell(im_cells, blobs, im_minima_val, basicse)
  
      ##-----------------------------------------------------------------------------------------
      ##-----------------------------------------------------------------------------------------
      ## Spatial regularization of the gradient
      ##-----------------------------------------------------------------------------------------
      ##-----------------------------------------------------------------------------------------  
      
      #Step 1 : Computation of the distance function to the markers
      immask = sp.Image(im_markers, "UINT8")
      sp.test(im_minima_val, 0, 1, immask)
      imdist = sp.Image(immask, "UINT8")
      sp.dist(immask, imdist, gridse)
      #Step 2 : Adding the distance function to the gradient
      if d_weight > 0:
          weight = d_weight * float(2)/step
          sp.mul(imdist, weight, imdist)
          sp.add(imdist, im_grad, im_grad)
  
      ##-----------------------------------------------------------------------------------------
      ##-----------------------------------------------------------------------------------------
      ## Superpixel segmentation : watershed transformation, flooding from selected minima on the regularized gradient
      ##-----------------------------------------------------------------------------------------
      ##----------------------------------------------------------------------------------------- 
      sp.copy(im_minima_val, imwrk1)
      sp.label(imwrk1,  im_minima_val, basicse)
      imout = sp.Image(im_minima_val)
      sp.basins(im_grad, im_minima_val, imout, basicse) 
      ##-----------------------------------------------------------------------------------------
      ##-----------------------------------------------------------------------------------------  
      return imout,  im_grad,  im_minima_val
function_tokens: def demo_m_waterpixels ( imin , step , d_weight , filter_ori ) : ##----------------------------------------------------------------------------------------- ##-----------------------------------------------------------------------------------------    # Connexity: basicse = sp . CrossSE ( ) gridse = sp . SquSE ( ) # Ori filtering if filter_ori is True : my_area_filtering ( imin , step * step / 16 ) # Gradient computation im_grad = my_gradient ( imin , basicse , True ) ## Pool of working images: imwrk0 = sp . Image ( im_grad ) imwrk1 = sp . Image ( im_grad , "UINT16" ) imwrk2 = sp . Image ( im_grad , "UINT16" ) # Compute cell centers and cells size = imin . getSize ( ) im_markers , im_cells = im_labelled_square_grid_points ( size , step , step / 6 ) ##----------------------------------------------------------------------------------------- ##----------------------------------------------------------------------------------------- ## Choice of the markers : one per grid cell ##----------------------------------------------------------------------------------------- ##----------------------------------------------------------------------------------------- # Step 1 : Computation of the minima of the gradient im_minima = sp . Image ( im_grad ) sp . minima ( im_grad , im_minima , basicse ) #Step 2 : Imposing minimum distance between minima (= Removing minima candidates which fall on cell margins ) sp . test ( im_cells , im_minima , 0 , imwrk0 ) sp . label ( imwrk0 , imwrk1 , basicse ) #Step 3 : Evaluation of the importance of minima ( = computation of their surfacic extinction) im_minima_val = sp . Image ( imwrk1 ) sp . watershedExtinction ( im_grad , imwrk1 , im_minima_val , "a" , basicse ) # Step 4 : Taking back at value 2 the minima of null-volumic extinction  sp . test ( imwrk0 , 2 , 0 , imwrk2 ) sp . test ( im_minima_val , im_minima_val , imwrk2 , im_minima_val ) # Step 5 : Coping with the potential absence of minimum in cells (= choosing the minimum value inside this cell as its marker if necessary) imwrk00 = sp . Image ( imwrk0 ) blobs = sp . computeBlobs ( im_cells ) sp . test ( im_cells , im_grad , 0 , imwrk00 ) minVals = sp . measMinVals ( imwrk00 , blobs ) sp . applyLookup ( im_cells , minVals , imwrk0 ) sp . test ( imwrk00 == imwrk0 , 1 , 0 , imwrk1 ) maxVals = sp . measMaxVals ( im_minima_val , blobs ) sp . applyLookup ( im_cells , maxVals , imwrk2 ) sp . test ( imwrk2 , im_minima_val , imwrk1 , im_minima_val ) # Step 6 : Selection of one marker per cell one_min_per_grid_cell ( im_cells , blobs , im_minima_val , basicse ) ##----------------------------------------------------------------------------------------- ##----------------------------------------------------------------------------------------- ## Spatial regularization of the gradient ##----------------------------------------------------------------------------------------- ##-----------------------------------------------------------------------------------------   #Step 1 : Computation of the distance function to the markers immask = sp . Image ( im_markers , "UINT8" ) sp . test ( im_minima_val , 0 , 1 , immask ) imdist = sp . Image ( immask , "UINT8" ) sp . dist ( immask , imdist , gridse ) #Step 2 : Adding the distance function to the gradient if d_weight > 0 : weight = d_weight * float ( 2 ) / step sp . mul ( imdist , weight , imdist ) sp . add ( imdist , im_grad , im_grad ) ##----------------------------------------------------------------------------------------- ##----------------------------------------------------------------------------------------- ## Superpixel segmentation : watershed transformation, flooding from selected minima on the regularized gradient ##----------------------------------------------------------------------------------------- ##-----------------------------------------------------------------------------------------  sp . copy ( im_minima_val , imwrk1 ) sp . label ( imwrk1 , im_minima_val , basicse ) imout = sp . Image ( im_minima_val ) sp . basins ( im_grad , im_minima_val , imout , basicse ) ##----------------------------------------------------------------------------------------- ##-----------------------------------------------------------------------------------------   return imout , im_grad , im_minima_val