Exemplo n.º 1
0
  def __init__( self, scalarName, volume, neighbors=0 ):
    super( FyRadiusMapAction, self ).__init__( scalarName, volume )

    self._startLabels = {}
    self._endLabels = {}
    self._neighbors = neighbors
    self._paddedImage = ap.pad( self._image, neighbors, 'constant', constant_values=( 0 ) )
Exemplo n.º 2
0
def pad_images_and_equalize_sizes(images, padding=(0, 0, 0), mode="reflect", **kwargs):
    images_padded = []
    max = np.max([im.shape for im in images], axis=0)
    for im in images:
        pad_width = [(p, p+e) for p, e in zip(padding, (max - im.shape))]
        im_padded = pad(im, pad_width, mode=str(mode), **kwargs)
        images_padded.append(im_padded)
    return np.array(images_padded)
Exemplo n.º 3
0
def pad_images_and_equalize_sizes_swapped(images, padding=(0, 0), mode="reflect", **kwargs):
    images_padded = []
    max = np.max([im.shape for im in images], axis=0)[1:]
    for image in images:
        im_padded = []
        for im in image: # divide by channels
            pad_width = [(p, p+e) for p, e in zip(padding, (max - im.shape))]
            im_padded.append(pad(im, pad_width, mode=str(mode), **kwargs)[np.newaxis,:,:])
        image_padded = np.vstack(tuple(im_padded))
        images_padded.append(image_padded)
    return np.array(images_padded)
Exemplo n.º 4
0
def validateMapping( volumefile, trkfile, radius=0, map_intermediate=True ):
  '''
  Check if a trk file has correctly mapped scalar values from a volume file.
  
  If radius is > 0 take it into account by looking for the most common value in a sphere
  around the original point. This only happens on start and end points so.
  
  If map_intermediate is active, also the points between end points are validated but never
  using the radius.
  
  Returns TRUE if everything is fine, FALSE if there were errors.
  '''
  # load the mapped trk file
  s = io.loadTrk( trkfile )

  volume = io.readImage( volumefile )
  imageHeader = volume.header
  image_size = volume.shape[:3]

  # grab the tracks
  tracks = s[0]

  # pad the image with zeros
  image = ap.pad( volume, radius, 'constant', constant_values=( 0 ) )

  # any errors?
  any_errors = False

  # incorporate spacing
  spacing = imageHeader.get_zooms()[:3]

  # .. and loop through them
  for t in tracks:

    points = t[0] # the points of this fiber track
    scalars = t[1] # the mapped scalars

    for index, p in enumerate( points ):

      current_point = [ int( a / b ) for a, b in zip( [p[0], p[1], p[2]], spacing )]

      #print 'ORIG', volume[current_point[0], current_point[1], current_point[2]]

      is_first_point = ( index == 0 )
      is_last_point = ( index == len( points ) - 1 )

      # if this is 
      if not map_intermediate and not is_first_point and not is_last_point:
        real_scalar = 0.0
      else:

        # here we check for the neighborhood if radius > 0
        if radius > 0 and ( is_first_point or is_last_point ):

          # neighborhood search!
          r = radius
          a, b, c = current_point

          # crop the image according to the neighborhood look-up
          # since we zero-padded the image, we don't need boundary checks here
          min_x = a - r
          max_x = a + r + 1
          min_y = b - r
          max_y = b + r + 1
          min_z = c - r
          max_z = c + r + 1
          cropped_image = numpy.asarray( image[min_x + r:max_x + r, min_y + r:max_y + r, min_z + r:max_z + r] )


          # create a sphere mask
          x, y, z = numpy.ogrid[0:2 * r + 1, 0:2 * r + 1, 0:2 * r + 1]
          mask = ( x - r ) ** 2 + ( y - r ) ** 2 + ( z - r ) ** 2 <= r * r # 3d sphere mask

          # apply the mask
          masked_container = cropped_image[mask]

          # throw away all zeros (0)
          masked_container = masked_container[numpy.nonzero( masked_container )]

          # find the most frequent label in the masked container
          from collections import Counter

          # by default, we use the original one
          mostFrequentLabel = volume[a, b, c]

          if len( masked_container ) != 0:
            counter = Counter( masked_container )
            all_labels = counter.most_common()
            best_match_label = counter.most_common( 1 )

            original_pos = [i for i, v in enumerate( all_labels ) if v[0] == mostFrequentLabel]

            if not original_pos or all_labels[original_pos[0]][1] != best_match_label[0][1]:
              # the original label appears less often as the new best_match_label
              # in this case, we use the new best matched label
              mostFrequentLabel = best_match_label[0][0]
              # we don't need an else here since the original label is already set

          real_scalar = mostFrequentLabel

        else:

          # simple mapping without radius incorporation and make sure we are inside the volume
          real_scalar = volume[min( current_point[0], image_size[0] - 1 ), min( current_point[1], image_size[1] - 1 ) , min( current_point[2], image_size[2] - 1 )]

      if type( scalars ) is types.NoneType:
        mapped_scalar = -1
      else:
        mapped_scalar = scalars[index][0]

      # now check if the mapped scalar from the trk file matches the real scalar
      compare = ( mapped_scalar == real_scalar )
      if compare:
        compare = Colors.GREEN + 'OK'
      else:
        compare = Colors.RED + 'WRONG!!!'
        any_errors = True

      print Colors.PURPLE + 'Probing ' + Colors.CYAN + str( current_point ) + Colors.PURPLE + ' for scalar.. SHOULD BE: ' + Colors.CYAN + str( real_scalar ) + Colors.PURPLE + ' WAS: ' + Colors.CYAN + str( mapped_scalar ) + Colors.PURPLE + ' ... ' + str( compare ) + Colors._CLEAR

  # return TRUE if everything went fine and FALSE if there were errors
  return not any_errors