Class: RTKIT::Selection
- Inherits:
-
Object
- Object
- RTKIT::Selection
- Defined in:
- lib/rtkit/selection.rb
Overview
Contains DICOM data and methods related to a Selection of pixels (indices) from the binary 2D NArray of a BinImage instance.
Relations
-
The Selection belongs to a BinImage.
-
A Contour has many Coordinates.
Instance Attribute Summary collapse
-
#bin_image ⇒ Object
readonly
The BinImage that the Selection belongs to.
-
#indices ⇒ Object
readonly
An array of (general) indices.
Class Method Summary collapse
-
.create_from_array(indices, bin_image) ⇒ Object
Creates a new Selection instance from an Array (or NArray) of (general) indices.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Returns true if the argument is an instance with attributes equal to self.
-
#add_indices(indices) ⇒ Object
Adds an array of (general) indices to this Selection.
-
#columns ⇒ Object
Returns an array of column indices.
-
#hash ⇒ Object
Generates a Fixnum hash value for this instance.
-
#initialize(bin_image) ⇒ Selection
constructor
Creates a new Selection instance.
-
#length ⇒ Object
Returns the length (number of indices) of this selection.
-
#rows ⇒ Object
Returns an array of row indices.
-
#shift(delta_col, delta_row) ⇒ Object
Shifts the indices of this selection by the specified number of columns and rows.
-
#shift_and_crop(delta_col, delta_row) ⇒ Object
Shifts the indices of this selection by the specified number of columns and rows, virtually ‘crops’ the original image by 2*columns and 2*rows, and adapts the indices to this virtually cropped image.
-
#shift_columns(delta) ⇒ Object
Shifts the indices of this selection by the specified number of columns.
-
#shift_rows(delta) ⇒ Object
Shifts the indices of this selection by the specified number of rows.
-
#to_selection ⇒ Object
Returns self.
Constructor Details
#initialize(bin_image) ⇒ Selection
Creates a new Selection instance.
Parameters
-
bin_image
– The BinImage instance that this Selection belongs to.
45 46 47 48 49 |
# File 'lib/rtkit/selection.rb', line 45 def initialize(bin_image) raise ArgumentError, "Invalid argument 'bin_image'. Expected BinImage, got #{bin_image.class}." unless bin_image.is_a?(BinImage) @bin_image = bin_image @indices = Array.new end |
Instance Attribute Details
#bin_image ⇒ Object (readonly)
The BinImage that the Selection belongs to.
14 15 16 |
# File 'lib/rtkit/selection.rb', line 14 def bin_image @bin_image end |
#indices ⇒ Object (readonly)
An array of (general) indices.
16 17 18 |
# File 'lib/rtkit/selection.rb', line 16 def indices @indices end |
Class Method Details
.create_from_array(indices, bin_image) ⇒ Object
Creates a new Selection instance from an Array (or NArray) of (general) indices. Returns the Selection instance.
Parameters
-
arr
– An Array/NArray of general indices (Integers). -
slice
– The BinImage instance that this Selection belongs to.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rtkit/selection.rb', line 28 def self.create_from_array(indices, bin_image) raise ArgumentError, "Invalid argument 'indices'. Expected Array/NArray, got #{indices.class}." unless [NArray, Array].include?(indices.class) raise ArgumentError, "Invalid argument 'bin_image'. Expected BinImage, got #{bin_image.class}." unless bin_image.is_a?(BinImage) raise ArgumentError, "Invalid argument 'indices'. Expected Array to contain only integers, got #{indices.collect{|i| i.class}.uniq}." if indices.is_a?(Array) and not indices.collect{|i| i.class}.uniq == [Fixnum] # Create the Selection: s = self.new(bin_image) # Set the indices: s.add_indices(indices) return s end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Returns true if the argument is an instance with attributes equal to self.
53 54 55 56 57 |
# File 'lib/rtkit/selection.rb', line 53 def ==(other) if other.respond_to?(:to_selection) other.send(:state) == state end end |
#add_indices(indices) ⇒ Object
Adds an array of (general) indices to this Selection.
63 64 65 66 67 68 |
# File 'lib/rtkit/selection.rb', line 63 def add_indices(indices) raise ArgumentError, "Invalid argument 'indices'. Expected Array/NArray, got #{indices.class}." unless [NArray, Array].include?(indices.class) raise ArgumentError, "Invalid argument 'indices'. Expected Array to contain only integers, got #{indices.collect{|i| i.class}.uniq}." if indices.is_a?(Array) and not indices.collect{|i| i.class}.uniq == [Fixnum] indices = indices.to_a if indices.is_a?(NArray) @indices += indices end |
#columns ⇒ Object
Returns an array of column indices. Returns an empty array if the instance contains no indices.
73 74 75 |
# File 'lib/rtkit/selection.rb', line 73 def columns return @indices.collect {|index| index % @bin_image.columns} end |
#hash ⇒ Object
Generates a Fixnum hash value for this instance.
79 80 81 |
# File 'lib/rtkit/selection.rb', line 79 def hash state.hash end |
#length ⇒ Object
Returns the length (number of indices) of this selection.
85 86 87 |
# File 'lib/rtkit/selection.rb', line 85 def length return @indices.length end |
#rows ⇒ Object
Returns an array of row indices. Returns an empty array if the instance contains no indices.
92 93 94 |
# File 'lib/rtkit/selection.rb', line 92 def rows return @indices.collect {|index| index / @bin_image.columns} end |
#shift(delta_col, delta_row) ⇒ Object
Shifts the indices of this selection by the specified number of columns and rows. Positive arguments increases the column and row indices.
Restrictions
NB! No out of bounds check is performed for indices that are shifted past the image boundary.
104 105 106 107 108 109 110 111 |
# File 'lib/rtkit/selection.rb', line 104 def shift(delta_col, delta_row) raise ArgumentError, "Invalid argument 'delta_col'. Expected Integer, got #{delta_col.class}." unless delta_col.is_a?(Integer) raise ArgumentError, "Invalid argument 'delta_row'. Expected Integer, got #{delta_row.class}." unless delta_row.is_a?(Integer) new_columns = @indices.collect {|index| index % @bin_image.columns + delta_col} new_rows = @indices.collect {|index| index / @bin_image.columns + delta_row} # Set new indices: @indices = Array.new(new_rows.length) {|i| new_columns[i] + new_rows[i] * @bin_image.columns} end |
#shift_and_crop(delta_col, delta_row) ⇒ Object
Shifts the indices of this selection by the specified number of columns and rows, virtually ‘crops’ the original image by 2*columns and 2*rows, and adapts the indices to this virtually cropped image.
Notes
Negative arguments decreases the column and row indices and crops at the end of the columns and rows.
Positive arguments increases the column and row indices and crops at the start of the columns and rows.
Restrictions
NB! No out of bounds check is performed for indices that are shifted past the image boundary.
130 131 132 133 134 135 136 137 |
# File 'lib/rtkit/selection.rb', line 130 def shift_and_crop(delta_col, delta_row) raise ArgumentError, "Invalid argument 'delta_col'. Expected Integer, got #{delta_col.class}." unless delta_col.is_a?(Integer) raise ArgumentError, "Invalid argument 'delta_row'. Expected Integer, got #{delta_row.class}." unless delta_row.is_a?(Integer) new_columns = @indices.collect {|index| index % @bin_image.columns - delta_col.abs} new_rows = @indices.collect {|index| index / @bin_image.columns - delta_row.abs} # Set new indices: @indices = Array.new(new_rows.length) {|i| new_columns[i] + new_rows[i] * (@bin_image.columns - delta_col.abs * 2)} end |
#shift_columns(delta) ⇒ Object
Shifts the indices of this selection by the specified number of columns. A positive argument increases the column indices.
Restrictions
NB! No out of bounds check is performed for indices that are shifted past the image boundary.
147 148 149 150 151 152 153 |
# File 'lib/rtkit/selection.rb', line 147 def shift_columns(delta) raise ArgumentError, "Invalid argument 'delta'. Expected Integer, got #{delta.class}." unless delta.is_a?(Integer) new_columns = @indices.collect {|index| index % @bin_image.columns + delta} new_rows = rows # Set new indices: @indices = Array.new(new_columns.length) {|i| new_columns[i] + new_rows[i] * @bin_image.columns} end |
#shift_rows(delta) ⇒ Object
Shifts the indices of this selection by the specified number of rows. A positive argument increases the row indices.
Restrictions
NB! No out of bounds check is performed for indices that are shifted past the image boundary.
163 164 165 166 167 168 169 |
# File 'lib/rtkit/selection.rb', line 163 def shift_rows(delta) raise ArgumentError, "Invalid argument 'delta'. Expected Integer, got #{delta.class}." unless delta.is_a?(Integer) new_columns = columns new_rows = @indices.collect {|index| index / @bin_image.columns + delta} # Set new indices: @indices = Array.new(new_rows.length) {|i| new_columns[i] + new_rows[i] * @bin_image.columns} end |
#to_selection ⇒ Object
Returns self.
173 174 175 |
# File 'lib/rtkit/selection.rb', line 173 def to_selection self end |