CrImage::Mask
Inherits CrImage::Map < Reference < Object
Mask is a wrapper around BitArray, where each flag represents a boolean bit of information about a pixel from an image. This can include whether a particular pixel has a value within certain conditions, OR if that pixel should be zeroed out or not.
See []= methods below for examples of how to manually construct masks.
(x,y) - coordinates. Represent these positions in a Mask of size 10x10:
[
(0,0), (0,1), (0,2), (0,3), (0,4), (0,5), (0,6), (0,7), (0,8), (0,9),
(1,0), (1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9),
(2,0), (2,1), (2,2), (2,3), (2,4), (2,5), (2,6), (2,7), (2,8), (2,9),
(3,0), (3,1), (3,2), (3,3), (3,4), (3,5), (3,6), (3,7), (3,8), (3,9),
(4,0), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6), (4,7), (4,8), (4,9),
(5,0), (5,1), (5,2), (5,3), (5,4), (5,5), (5,6), (5,7), (5,8), (5,9),
(6,0), (6,1), (6,2), (6,3), (6,4), (6,5), (6,6), (6,7), (6,8), (6,9),
(7,0), (7,1), (7,2), (7,3), (7,4), (7,5), (7,6), (7,7), (7,8), (7,9),
(8,0), (8,1), (8,2), (8,3), (8,4), (8,5), (8,6), (8,7), (8,8), (8,9),
(9,0), (9,1), (9,2), (9,3), (9,4), (9,5), (9,6), (9,7), (9,8), (9,9),
]
And every position is a Bool value.
Different ways to refer to coordinates:
mask.at(0, 0) # => (0,0)
mask[0, 0] # => (0,0), same as .at(0, 0)
mask[0..1, 4] # => (4,0), (4,1)
mask[3, 3..5] # => (3,3), (3,4), (3,5)
mask[2..3, 4..5] # => (2,4), (2,5), (3,4), (3,5)
See Operation::Crop and Operation::MaskApply for how this can be useful
Constructors
Construct a new Mask of width x height, preset to initial
Construct a new Mask from an integer (useful for testing or small mask construction)
Construct a new Mask from the dimensions of passed in image with an initial bit
Construct a new Mask of width x height using &block to determine if a bit should be true or not (passed in x and y coordinates)
Construct a new Mask from an array of BitArray. See #[](xs : Range, ys : Range) : Array(BitArray)
This assumes other_bits[0] corresponds to x == 0 in the mask, and the corresponding
BitArray represents all bits for that row. All BitArrays must be of the same size in
other_bits.
Instance methods
Return an Array(BitArray) for the partial box (of partial rows and partial columns) of this mask.
Can be used to construct another mask from.
Return a new BitArray corresponding to the partial row specified
Return a new BitArray corresponding to the partial column specified
Return an Array(BitArray) for the partial box (of partial rows and partial columns) of this mask.
Can be used to construct another mask from.
Set the bit for coordinate x and y
mask = CrImage::Mask.new(50, 50, false)
mask[20, 20] = true
mask.to_gray.save("mask_point.jpg")
Set the bits for partial row xs at column y
mask = CrImage::Mask.new(50, 50, false)
mask[20..40, 20] = true
mask.to_gray.save("mask_partial_row.jpg")
Set the bits for row x and partial columns ys
mask = CrImage::Mask.new(50, 50, false)
mask[20..40, 20] = true
mask.to_gray.save("mask_partial_column.jpg")
Set the bits for partial rows xs and partial columns ys
mask = CrImage::Mask.new(50, 50, false)
mask[20..40, 20..40] = true
mask.to_gray.save("mask_partial_column.jpg")
Apply this mask to the provided image with Operation::MaskApply#apply
Apply this mask to the provided image with Operation::MaskApply#apply
Returns the bounding box of the mask where all true bits are contained. Any pixels outside of the region are false
mask = CrImage::Mask.new(50, 50, false)
mask[20..40, 20] = true
mask[20, 20..40] = true
mask.region # => Region(x: 20, y: 20, width: 20, height: 20)
Return an array of Masks, each one corresponding to an area of contiguous true bits (identified from flood fills).
May specify diagonal: false for only 4-way (up, down, left, right) flood fill instead of default 8-way.
Starting with sample mask:
mask = CrImage::Mask.new(50, 50, false)
mask[5..45, 5..45] = true
mask[15..35, 15..35] = false
mask[21..25, 21..25] = true
mask[26..30, 26..30] = true
Its segments look like:
mask.segments.each_with_index do |segment, i|
segment.to_gray.save("mask_8-way_segments_example_#{i}.jpg")
end
Yields the images:
Using diagonal: false yields:
mask.segments(diagonal: false).each_with_index do |segment, i|
segment.to_gray.save("mask_4-way_segments_example_#{i}.jpg")
end
Yields the images:
Convert this Mask to a GrayscaleImage, with false bits becoming 0, and true bits becoming 255