code: def shape_based_slice_interpolation(img, dim, nslices):
      
      # check arguments
      if not 0 == nslices % 2:
          raise ValueError('nslices must be an even number')
      
      out = None
      slicer = [slice(None)] * img.ndim
      chunk_full_shape = list(img.shape)
      chunk_full_shape[dim] = nslices + 2
       
      for sl1, sl2 in zip(numpy.rollaxis(img, dim)[:-1], numpy.rollaxis(img, dim)[1:]):
          if 0 == numpy.count_nonzero(sl1) and 0 == numpy.count_nonzero(sl2):
              chunk = numpy.zeros(chunk_full_shape, dtype=numpy.bool)
          else:
              chunk = shape_based_slice_insertation_object_wise(sl1, sl2, dim, nslices)
          if out is None:
              out = numpy.delete(chunk, -1, dim)
          else:
              out = numpy.concatenate((out, numpy.delete(chunk, -1, dim)), dim)
      
      slicer[dim] = numpy.newaxis    
      out = numpy.concatenate((out, sl2[slicer]), dim)
      
      slicer[dim] = slice(0, 1)
      for _ in range(nslices // 2):
          out = numpy.concatenate((img[slicer], out), dim)
      slicer[dim] = slice(-1, None)
      for _ in range(nslices // 2):
          out = numpy.concatenate((out, img[slicer]), dim)
          
      return out
docs: Adds `nslices` slices between all slices of the binary image `img` along dimension
      `dim` respecting the original slice values to be situated in the middle of each
      slice. Extrapolation situations are handled by simple repeating.
      
      Interpolation of new slices is performed using shape based interpolation.
      
      Parameters
      ----------
      img : array_like
          A n-dimensional image.
      dim : int
          The dimension along which to add slices.
      nslices : int
          The number of slices to add. Must be an even number.
      
      Returns
      -------
      out : ndarray
          The re-sampled image.