def extract_single_contour
      # Set up an array to keep track of the pixels belonging to the current contour being analyzed by the algorithm:
      current_indices = Array.new
      # Also keep track of the direction we 'arrived from' (in addition to the pixel position):
      contour_directions = Array.new
      # First step of contour algorithm: Identify a border pixel which will be our start pixel.
      # Traditionally this is achieved by scanning the image, row by row, left to right, until a foreground pixel is found.
      # Instead of scanning, this implementation will extract all foreground indices and chose the first index as our start pixel.
      indices = (@narray > 0).where
      if indices.length > 0
        current_indices << indices[0]
        # Generally we will store the direction we came from along with the pixel position - but not for the first pixel.
        # Specific indices for the first pixel:
        p_col, p_row = indices_general_to_specific(current_indices.first, columns)
        # Set up variables for the initial run of the contour algorithm loop:
        arrived_from = :west
        direction_from_corner = nil
        continue = true
        while continue do
          # Radially sweep the 8 pixels surrounding the start pixel, until a foreground pixel is found.
          # We do this by extracting a 3*3 array centered on our position. Based on the direction to the previous pixel,
          # we then extract the neighbour pixels in such a way the pixel where we came from in the previous step is first in our vector.
          local_pixels = @narray[(p_col-1)..(p_col+1), (p_row-1)..(p_row+1)].flatten
          local_indices = @narray_indices[(p_col-1)..(p_col+1), (p_row-1)..(p_row+1)].flatten
          neighbour_pixels = local_pixels[@reorder[arrived_from]]
          neighbour_indices = local_indices[@reorder[arrived_from]]
          neighbour_relative_indices = @relative_indices[@reorder[arrived_from]]
          # The next border pixel is then the first foreground pixel in the extracted vector.
          local_foreground = (neighbour_pixels > 0).where
          if local_foreground.length > 0
            # We identified another border pixel.
            first_foreground_index = local_foreground[0]
            current_pixel_index = neighbour_indices[first_foreground_index]
            # Stopping criterion: If current pixel equals the second pixel, and the previous pixel equals the first pixel,
            # then we can be absolutely sure that we have made a full contour, regardless of the connectivity of the border.
            if current_pixel_index == current_indices[1] and current_indices.last == current_indices.first
              # We have re-located the original start pixels. Remove the duplicate last element and abort the search.
              current_indices.pop
              #contour_directions.pop
              continue = false
            else
              # Extract x and y index as well as the direction we arrived from:
              p_col, p_row = indices_general_to_specific(current_pixel_index, columns)
              arrived_from = @arrived_from_directions[neighbour_relative_indices[first_foreground_index]]
              # Store pixel and continue the search, using the newly identified border pixel in the next step.
              current_indices << current_pixel_index
              contour_directions << arrived_from
            end
          else
            # No foreground pixel was found, search finished.
            continue = false
          end
        end
        # Before reducing to corner indices, make a copy of the full set of indices:
        all_indices = Selection.create_from_array(current_indices, self)
        # We only want corner points: Remove all the indices that are between corner points:
        img_contour_original = current_indices.dup
        current_indices = Array.new(1, img_contour_original.first)
        img_contour_original.delete_at(0)
        original_direction = contour_directions.first
        contour_directions.delete_at(0)
        img_contour_original.each_index do |i|
          if contour_directions[i] != original_direction
            # Store pixel and set new direction:
            current_indices << img_contour_original[i]
            original_direction = contour_directions[i]
          end
        end
        corner_indices = Selection.create_from_array(current_indices, self)
      end
      return corner_indices, all_indices
    end
def extract_single_contour
      # Set up an array to keep track of the pixels belonging to the current contour being analyzed by the algorithm:
      current_indices = Array.new
      # Also keep track of the direction we 'arrived from' (in addition to the pixel position):
      contour_directions = Array.new
      # First step of contour algorithm: Identify a border pixel which will be our start pixel.
      # Traditionally this is achieved by scanning the image, row by row, left to right, until a foreground pixel is found.
      # Instead of scanning, this implementation will extract all foreground indices and chose the first index as our start pixel.
      indices = (@narray > 0).where
      if indices.length > 0
        current_indices << indices[0]
        # Generally we will store the direction we came from along with the pixel position - but not for the first pixel.
        # Specific indices for the first pixel:
        p_col, p_row = indices_general_to_specific(current_indices.first, columns)
        # Set up variables for the initial run of the contour algorithm loop:
        arrived_from = :west
        direction_from_corner = nil
        continue = true
        while continue do
          # Radially sweep the 8 pixels surrounding the start pixel, until a foreground pixel is found.
          # We do this by extracting a 3*3 array centered on our position. Based on the direction to the previous pixel,
          # we then extract the neighbour pixels in such a way the pixel where we came from in the previous step is first in our vector.
          local_pixels = @narray[(p_col-1)..(p_col+1), (p_row-1)..(p_row+1)].flatten
          local_indices = @narray_indices[(p_col-1)..(p_col+1), (p_row-1)..(p_row+1)].flatten
          neighbour_pixels = local_pixels[@reorder[arrived_from]]
          neighbour_indices = local_indices[@reorder[arrived_from]]
          neighbour_relative_indices = @relative_indices[@reorder[arrived_from]]
          # The next border pixel is then the first foreground pixel in the extracted vector.
          local_foreground = (neighbour_pixels > 0).where
          if local_foreground.length > 0
            # We identified another border pixel.
            first_foreground_index = local_foreground[0]
            current_pixel_index = neighbour_indices[first_foreground_index]
            # Stopping criterion: If current pixel equals the second pixel, and the previous pixel equals the first pixel,
            # then we can be absolutely sure that we have made a full contour, regardless of the connectivity of the border.
            if current_pixel_index == current_indices[1] and current_indices.last == current_indices.first
              # We have re-located the original start pixels. Remove the duplicate last element and abort the search.
              current_indices.pop
              #contour_directions.pop
              continue = false
            else
              # Extract x and y index as well as the direction we arrived from:
              p_col, p_row = indices_general_to_specific(current_pixel_index, columns)
              arrived_from = @arrived_from_directions[neighbour_relative_indices[first_foreground_index]]
              # Store pixel and continue the search, using the newly identified border pixel in the next step.
              current_indices << current_pixel_index
              contour_directions << arrived_from
            end
          else
            # No foreground pixel was found, search finished.
            continue = false
          end
        end
        # Before reducing to corner indices, make a copy of the full set of indices:
        all_indices = Selection.create_from_array(current_indices, self)
        # We only want corner points: Remove all the indices that are between corner points:
        img_contour_original = current_indices.dup
        current_indices = Array.new(1, img_contour_original.first)
        img_contour_original.delete_at(0)
        original_direction = contour_directions.first
        contour_directions.delete_at(0)
        img_contour_original.each_index do |i|
          if contour_directions[i] != original_direction
            # Store pixel and set new direction:
            current_indices << img_contour_original[i]
            original_direction = contour_directions[i]
          end
        end
        corner_indices = Selection.create_from_array(current_indices, self)
      end
      return corner_indices, all_indices
    end
Gives the contour indices (Selection) from the first (if any) structure
 found in the binary image of this instance.

 FIXME: For now, a rather simple corner detection algorithm is integrated and used.
 At some stage this could be replaced/supplemented with a proper ('lossy') corner detection algorithm.

 @return [Array<Selection>] an array containing two sets of indices: corner indices as well as all contour indices