Method: RTKIT::Image#binary_image

Defined in:
lib/rtkit/image.rb

#binary_image(coords_x, coords_y, coords_z) ⇒ Object

Creates and returns a filled, binary NArray image (a ‘segmented’ image) based on the provided contour coordinates.

Parameters

  • coords_x – An Array/NArray of a contour’s X coordinates. Must have at least 3 elements.

  • coords_y – An Array/NArray of a contour’s Y coordinates. Must have at least 3 elements.

  • coords_z – An Array/NArray of a contour’s Z coordinates. Must have at least 3 elements.

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rtkit/image.rb', line 94

def binary_image(coords_x, coords_y, coords_z)
  raise ArgumentError, "Invalid argument 'coords_x'. Expected at least 3 elements, got #{coords_x.length}" unless coords_x.length >= 3
  raise ArgumentError, "Invalid argument 'coords_y'. Expected at least 3 elements, got #{coords_y.length}" unless coords_y.length >= 3
  raise ArgumentError, "Invalid argument 'coords_z'. Expected at least 3 elements, got #{coords_z.length}" unless coords_z.length >= 3
  # Values that will be used for image geometry:

  empty_value = 0
  line_value = 1
  fill_value = 2
  # Convert physical coordinates to image indices:

  column_indices, row_indices = coordinates_to_indices(NArray.to_na(coords_x), NArray.to_na(coords_y), NArray.to_na(coords_z))
  # Create an empty array and fill in the gathered points:

  empty_array = NArray.byte(@columns, @rows)
  delineated_array = draw_lines(column_indices.to_a, row_indices.to_a, empty_array, line_value)
  # Establish starting point indices for the coming flood fill algorithm:

  # (Using a rather simple approach by finding the average column and row index among the selection of indices)

  start_col = column_indices.mean
  start_row = row_indices.mean
  # Perform a flood fill to enable us to extract all pixels contained in a specific ROI:

  filled_array = flood_fill(start_col, start_row, delineated_array, fill_value)
  # Extract the indices of 'ROI pixels':

  if filled_array[0,0] != fill_value
    # ROI has been filled as expected. Extract indices of value line_value and fill_value:

    filled_array[(filled_array.eq line_value).where] = fill_value
    indices = (filled_array.eq fill_value).where
  else
    # An inversion has occured. The entire image except our ROI has been filled. Extract indices of value line_value and empty_value:

    filled_array[(filled_array.eq line_value).where] = empty_value
    indices = (filled_array.eq empty_value).where
  end
  # Create binary image:

  bin_image = NArray.byte(@columns, @rows)
  bin_image[indices] = 1
  return bin_image
end