Class ObjectMask
BoundingBox
, with a corresponding mask sized
to match the bounding-box.
This represents the concept of an object residing within the image.
Each voxel in the mask must be one of two states, either on or off. The object is specified by all voxels that are on.
The on voxels need not be contiguous i.e. they need not form a single connected-component.
The interfaces for assigning, extracting voxels etc. all expect global coordinates, expressed relative to the image's coordinates as a whole.
The voxels in the mask are mutable.
A point is informally referred to as belonging on or in in the object-mask, or being contained by the object-mask if it fulfills both the two conditions of:
- The point exists inside the bounding-box.
- The corresponding voxel in
voxels()
has an on state.
-
Constructor Summary
ConstructorsConstructorDescriptionObjectMask
(BinaryVoxels<UnsignedByteBuffer> voxels) Creates from aBinaryVoxels
to be located at the origin.ObjectMask
(BoundedVoxels<UnsignedByteBuffer> voxels) Creates from aBoundedVoxels
withUnsignedByteBuffer
.ObjectMask
(BoundedVoxels<UnsignedByteBuffer> voxels, BinaryValuesInt binaryValues) Creates fromBoundedVoxels
and correspondingBinaryValuesInt
.ObjectMask
(Voxels<UnsignedByteBuffer> voxels) Creates from aVoxels
mask that is cornered at the origin.ObjectMask
(BoundingBox box) Creates as a bounding-box with all corresponding mask voxels set to off.ObjectMask
(BoundingBox box, BinaryVoxels<UnsignedByteBuffer> voxels) Creates from aBinaryVoxels
and a corresponding bounding-box.ObjectMask
(BoundingBox box, Voxels<UnsignedByteBuffer> voxels) ObjectMask
(BoundingBox box, Voxels<UnsignedByteBuffer> voxels, BinaryValuesByte binaryValues) LikeObjectMask(BoundingBox, Voxels, BinaryValuesInt)
but specifies the binary-values as bytes.ObjectMask
(BoundingBox box, Voxels<UnsignedByteBuffer> voxels, BinaryValuesInt binaryValues) -
Method Summary
Modifier and TypeMethodDescriptionAssigns the off value to voxels, expecting global coordinates.assignOn()
Assigns the on value to voxels, expecting global coordinates.What values constitute an on and off state invoxels
- as ints.What values constitute an on and off state invoxels
- as bytes.The underlying voxel memory buffers for the object-mask, exposed via aBinaryVoxels
.The underlying voxel memory buffers for the object-mask, exposed viaBoundedVoxels
.The bounding-box which gives a location for the object-mask on an image.Calculates center-of-gravity across all axes.double
centerOfGravity
(Axis axis) Calculates center-of-gravity for one specific axis only.boolean
Determines if an object-mask is connected.Makes sure an object fits inside an extent, removing any parts which do not.boolean
Whether a particular point exists within the object-mask?int
Counts the number of intersecting-voxels between two object-masks.Creates a list of all on voxels as points, using local coordinates i.e.Creates a deep-copy of the current object-mask.boolean
equalsDeep
(ObjectMask other) A deep equality check with anotherObjectMask
.extent()
The size of the object-mask's bounding-box across three dimensions.extract()
Provides methods to read/copy/duplicate regions of voxels.extractSlice
(int sliceIndex, boolean keepIndex) Extracts one particular slice as an object-maskFinds any arbitrary on voxel on the object.flattenZ()
A maximum-intensity projection.growBuffer
(Point3i growthNegative, Point3i growthPositive, Optional<Extent> clipRegion) Grows the object-mask's voxel buffers in the positive and negative directions by a certain amount.growToZ
(int sizeZ) Grows a single z-slicedObjectMask
by duplicating the slice across the z-dimensionsizeZ
number of times.boolean
hasIntersectingVoxels
(ObjectMask other) Determines whether there are any intersecting voxels on two object-masks.intersect
(ObjectMask other, Extent extent) Intersects this object-mask with anotherinvert()
Produces a new object-mask that uses the same voxel-buffer but switches the off and on mapping.mapBoundingBoxChangeExtent
(BoundingBox boxToAssign) Applies a function to map the bounding-box to a new-value (whose extent is expected to change in value).mapBoundingBoxPreserveExtent
(UnaryOperator<BoundingBox> mapOperation) Applies a function to map the bounding-box to a new-value (whose extent should be unchanged in value).int
The number of on voxels on the object-mask.int
offsetGlobal
(int x, int y) Calculates an offset for locating a voxel inside the buffer, with global encoding of coordinates.int
offsetRelative
(int x, int y) Calculates an offset for locating a voxel inside the buffer, with local encoding of coordinates.region
(BoundingBox box, boolean reuseIfPossible) A (sub-)region of the object-mask.Creates an object-mask covering the a bounding-box (that is required to intersect at least partially)regionZ
(int zMin, int zMax) Creates an object-mask with a subrange of the slices.Creates a new object-mask with coordinates changed to be relative to another box.replaceVoxels
(Voxels<UnsignedByteBuffer> voxelsToAssign) Replaces the voxels in the object-mask.scale
(ScaleFactor factor) Produces a scaled-version of an object-mask.scale
(ScaleFactor factor, Optional<Extent> clipTo) Produces a scaled-version of an object-mask.shiftBackBy
(ReadableTuple3i shift) Shifts the object-mask by moving its bounding-box backwards.shiftBy
(ReadableTuple3i shift) Shifts the object-mask by moving its bounding-box forwards.Creates a new object-mask with identical voxels but with the bounding-box beginning at the origin (0,0,0).sliceBufferGlobal
(int sliceIndexGlobal) A slice buffer with global coordinates.sliceBufferLocal
(int sliceIndexRelative) A slice buffer with local coordinates.toString()
A string representation of the object-mask showing: the center-of-gravity.voxels()
The underlying voxel memory buffers for the object-mask, exposed viaVoxels
.Provides aVoxelsPredicate
that finds or counts all off voxels in the object-mask.voxelsOn()
Provides aVoxelsPredicate
that finds or counts all on voxels in the object-mask.
-
Constructor Details
-
ObjectMask
Creates from aVoxels
mask that is cornered at the origin.i.e. the bounding box corner is set as
0,0,0
.Default
BinaryValuesInt
of (off=0
,on=255
) are used to interpretVoxels
as a mask.The
Voxels
are reused internally in memory without duplication.- Parameters:
voxels
- voxels to be used in the object-mask.
-
ObjectMask
Creates as a bounding-box with all corresponding mask voxels set to off.Default
BinaryValuesInt
of (off=0, on=255) are used for the mask.- Parameters:
box
- bounding-box.
-
ObjectMask
Creates from aBoundedVoxels
withUnsignedByteBuffer
.The voxels are reused without duplication.
Default
BinaryValuesInt
of (off=0, on=255) are assumed, andvoxels
should only contain these values.- Parameters:
voxels
- voxels to be used in the object-mask.
-
ObjectMask
Creates from aBinaryVoxels
to be located at the origin.The voxels are reused without duplication.
- Parameters:
voxels
- the voxels.
-
ObjectMask
Creates fromVoxels
and aBoundingBox
with defaultBinaryValuesInt
.The voxels are reused without duplication.
Default
BinaryValuesInt
of (off=0, on=255) are assumed, andvoxels
should only contain these values.- Parameters:
box
- the bounding-box.voxels
- the voxels, which must be the same size asbox
.
-
ObjectMask
Creates fromVoxels
and a correspondingBoundingBox
andBinaryValuesInt
.The voxels are reused without duplication.
- Parameters:
box
- the bounding-box.voxels
- the voxels, which must be the same size asbox
, and should only contain values inbinaryValues
.binaryValues
- the binary-values to use (asint
s).
-
ObjectMask
Creates from aBinaryVoxels
and a corresponding bounding-box.The voxels are reused without duplication.
- Parameters:
box
- the bounding-box.voxels
- the voxels, which must be the same size asbox
.
-
ObjectMask
Creates fromBoundedVoxels
and correspondingBinaryValuesInt
.The voxels are reused without duplication.
- Parameters:
voxels
- the voxels.binaryValues
- the binary-values to use.
-
ObjectMask
public ObjectMask(BoundingBox box, Voxels<UnsignedByteBuffer> voxels, BinaryValuesByte binaryValues) LikeObjectMask(BoundingBox, Voxels, BinaryValuesInt)
but specifies the binary-values as bytes.- Parameters:
box
- the bounding-box.voxels
- the voxels, which must be the same size asbox
, and should only contain values inbinaryValues
.binaryValues
- the binary-values to use (asbyte
s).
-
-
Method Details
-
duplicate
Creates a deep-copy of the current object-mask.- Returns:
- a newly created mask that is a duplicate, including duplicating the voxel memory buffers.
-
replaceVoxels
Replaces the voxels in the object-mask.This is an immutable operation, and a new object-mask is created.
- Parameters:
voxelsToAssign
- voxels to be assigned.- Returns:
- a new object with the replacement voxels but identical in other respects.
-
growToZ
Grows a single z-slicedObjectMask
by duplicating the slice across the z-dimensionsizeZ
number of times.- Parameters:
sizeZ
- the size in the z-dimension to grow to i.e. the number of duplicated sizes.- Returns:
- a new
BoundedVoxels
with an identical corner, but with a 3D bounding-box (and duplicated slices) instead of the previous 2D. - Throws:
OperationFailedException
- if the existing voxels aren't 2D (a single slice).
-
growBuffer
public ObjectMask growBuffer(Point3i growthNegative, Point3i growthPositive, Optional<Extent> clipRegion) throws OperationFailedException Grows the object-mask's voxel buffers in the positive and negative directions by a certain amount.This operation is immutable.
- Parameters:
growthNegative
- how much to grow in the negative direction (i.e. downards direction on an axis).growthPositive
- how much to grow in the positive direction (i.e. upwards direction on an axis).clipRegion
- if defined, clips the buffer to this region.- Returns:
- the grown object-mask with newly-created buffers.
- Throws:
OperationFailedException
- if the voxels are located outside the clipping region.
-
equalsDeep
A deep equality check with anotherObjectMask
.Each voxel must be identical, as well as identical binary-values and bounding-box.
- Parameters:
other
- the otherObjectMask
to compare against.- Returns:
- true iff the two object-masks are identical.
-
countIntersectingVoxels
Counts the number of intersecting-voxels between two object-masks.This is an immutable operation.
- Parameters:
other
- the other object-mask to consider.- Returns:
- number of on-voxels the two object-masks have in common.
-
hasIntersectingVoxels
Determines whether there are any intersecting voxels on two object-masks.This is an immutable operation.
The algorithm exits as soon as an intersecting voxel is encountered i.e. as early as possible.
- Parameters:
other
- the other object-mask to consider.- Returns:
- true if at least one voxel exists that is on in both object-masks.
-
scale
Produces a scaled-version of an object-mask.This is an immutable operation.
- Parameters:
factor
- scale-factor.- Returns:
- a scaled object-mask.
-
invert
Produces a new object-mask that uses the same voxel-buffer but switches the off and on mapping.- Returns:
- a newly created object-mask (reusing the same buffer).
-
scale
Produces a scaled-version of an object-mask.This is an immutable operation.
- Parameters:
factor
- scale-factor.clipTo
- an extent which the object-masks should always fit inside after scaling (to catch any rounding errors that push the bounding box outside the scene-boundary).- Returns:
- a scaled object-mask.
-
clampTo
Makes sure an object fits inside an extent, removing any parts which do not.This is not an immutable operation, returning the current object unchanged if it already fits inside.
- Parameters:
extent
- the extent an object must fit in- Returns:
- either the current object-mask unchanged (if it already fits inside) or a new object-mask clipped to fit inside
-
centerOfGravity
Calculates center-of-gravity across all axes.This is the mean of the position coordinates in each dimension.
- Returns:
- the center-of-gravity or
(NaN, NaN, NaN)
if there are no on voxels.
-
centerOfGravity
Calculates center-of-gravity for one specific axis only.- Parameters:
axis
- the specific axis.- Returns:
- a point on the specific axis that is the center-of-gravity, or
NaN
if there are no on voxels.
-
checkIfConnected
public boolean checkIfConnected()Determines if an object-mask is connected.Adjacent is defined with a big neighborhood definition i.e. with 8 or 26 connectivity.
- Returns:
- true if all on voxels in the mask are spatially adjacent to at least one other on voxel with.
-
voxelsOn
Provides aVoxelsPredicate
that finds or counts all on voxels in the object-mask.- Returns:
- the predicate.
-
voxelsOff
Provides aVoxelsPredicate
that finds or counts all off voxels in the object-mask.- Returns:
- the predicate.
-
numberVoxelsOn
public int numberVoxelsOn()The number of on voxels on the object-mask.- Returns:
- the number of voxels that are on.
-
intersect
Intersects this object-mask with anotherThis is an immutable operation (the existing two masks are not modified).
- Parameters:
other
- the other object-mask to intersect withextent
- extent to constrain any intersection- Returns:
- a new object of the intersecting region iff it exists
-
contains
Whether a particular point exists within the object-mask?For this to occur, two condition needs to be fulfilled for the
point
:- It exists inside the bounding-box.
- The corresponding voxel in
#voxels()
has an on state.
- Parameters:
point
- the point to query.- Returns:
- true iff both conditions as true, as per above.
-
flattenZ
A maximum-intensity projection.This flattens across z-dimension, setting a voxel to on if it is on in any one slice.
This is an immutable operation.
- Returns:
- a new object-mask flattened in Z dimension.
-
boundingBox
The bounding-box which gives a location for the object-mask on an image.- Returns:
- the bounding-box.
-
binaryVoxels
The underlying voxel memory buffers for the object-mask, exposed via aBinaryVoxels
.- Returns:
- the voxels.
-
voxels
The underlying voxel memory buffers for the object-mask, exposed viaVoxels
.- Returns:
- the voxels.
-
boundedVoxels
The underlying voxel memory buffers for the object-mask, exposed viaBoundedVoxels
.- Returns:
- the voxels.
-
extent
The size of the object-mask's bounding-box across three dimensions.- Returns:
- the size.
-
offsetRelative
public int offsetRelative(int x, int y) Calculates an offset for locating a voxel inside the buffer, with local encoding of coordinates.- Parameters:
x
- the X-dimension value for the voxel, relative to the object-mask's bounding-box.y
- the Y-dimension value for the voxel, relative to the object-mask's bounding-box.- Returns:
- the offset to use in the buffer specifying the location of this specific voxel.
-
offsetGlobal
public int offsetGlobal(int x, int y) Calculates an offset for locating a voxel inside the buffer, with global encoding of coordinates.- Parameters:
x
- the X-dimension value for the voxel, relative to the entire image scene.y
- the Y-dimension value for the voxel, relative to the entire image scene.- Returns:
- the offset to use in the buffer specifying the location of this specific voxel.
-
extractSlice
Extracts one particular slice as an object-mask- Parameters:
sliceIndex
- the z-slice in global coordinateskeepIndex
- iff true the z coordinate is reused for the slice, otherwise 0 is set- Returns:
- a newly-created object reusing the slice's buffers
-
regionZ
Creates an object-mask with a subrange of the slices.This will always reuse the existing
Voxels
.- Parameters:
zMin
- minimum z-slice index, inclusive.zMax
- maximum z-slice index, inclusive.- Returns:
- a newly created object-mask for the slice-range requested.
- Throws:
CreateException
-
region
A (sub-)region of the object-mask.The region may some smaller portion of the voxels, or the voxels as a whole.
It should never be larger than the voxels.
See
VoxelsExtracter.region(org.anchoranalysis.spatial.box.BoundingBox, boolean)
for more details.- Parameters:
box
- bounding-box in absolute coordinates.reuseIfPossible
- if true the existing object will be reused if possible, otherwise a new object is always created.- Returns:
- an object-mask corresponding to the requested region, either newly-created or reused
- Throws:
CreateException
-
regionIntersecting
Creates an object-mask covering the a bounding-box (that is required to intersect at least partially)The region outputted will have the same size and coordinates as the bounding-box not the existing
ObjectMask
.It will contain the correct object-mask values for the intersecting region, and off values for the rest.
A new voxel-buffer is always created for this operation i.e. the existing box is never reused like sometimes in
region(org.anchoranalysis.spatial.box.BoundingBox, boolean)
.- Parameters:
box
- bounding-box in absolute coordinates, that must at least partially intersect with the current object-mask bounds.- Returns:
- a newly created object-mask containing partially some parts of the existing object-mask as well as off voxels for any other region.
- Throws:
CreateException
- if the boxes do not intersect.
-
findArbitraryOnVoxel
Finds any arbitrary on voxel on the object.First it tries the center-of-gravity voxel, and if that's not on, it iterates through the box until it finds an on voxel.
This is a DETERMINISTIC operation, so one can rely on the same voxel being found for a given object.
- Returns:
- the location (in absolute coordinates) of an arbitrary on voxel on the object, if it exists.
-
sliceBufferLocal
A slice buffer with local coordinates.i.e. with coordinates relative to the bounding-box corner.
- Parameters:
sliceIndexRelative
- sliceIndex (z) relative to the bounding-box of the object-mask.- Returns:
- the buffer.
-
sliceBufferGlobal
A slice buffer with global coordinates.- Parameters:
sliceIndexGlobal
- sliceIndex (z) in global coordinates.- Returns:
- the buffer.
-
shiftToOrigin
Creates a new object-mask with identical voxels but with the bounding-box beginning at the origin (0,0,0).This is an immutable operation: but beware the existing voxel-buffers are reused in the new object.
- Returns:
- a new object-mask reusing the existing voxel-buffers.
-
shiftBy
Shifts the object-mask by moving its bounding-box forwards.i.e. by adding
shift
from its corner.This is an immutable operation: but beware the existing voxel-buffers are reused in the new object.
- Parameters:
shift
- what to add from the corner position- Returns:
- a new object-mask reusing the existing voxel-buffers.
-
shiftBackBy
Shifts the object-mask by moving its bounding-box backwards.i.e. by subtracting
shift
from its corner.This is an immutable operation: but beware the existing voxel-buffers are reused in the new object.
- Parameters:
shift
- what to subtract from the corner position.- Returns:
- a new object-mask reusing the existing voxel-buffers.
-
mapBoundingBoxPreserveExtent
Applies a function to map the bounding-box to a new-value (whose extent should be unchanged in value).This is an immutable operation: but beware the existing voxel-buffers are reused in the new object.
- Parameters:
mapOperation
- map function to perform mapping of bounding-box.- Returns:
- a new object-mask with the updated bounding box (but unchanged voxels).
-
mapBoundingBoxChangeExtent
Applies a function to map the bounding-box to a new-value (whose extent is expected to change in value).This is a almost immutable operation, and NEW voxel-buffers are usually created for the new object, but not if the bounding-box or its extent need no change.
Precondition: the new bounding-box's extent must be greater than or equal to the existing extent in all dimensions.
- Parameters:
boxToAssign
- bounding-box to assign.- Returns:
- a new object-mask with the updated bounding box (and changed voxels).
-
relativeMaskTo
Creates a new object-mask with coordinates changed to be relative to another box.This is an immutable operation.
- Parameters:
box
- box used as a reference point, against which new relative coordinates are calculated.- Returns:
- a newly created object-mask with updated coordinates.
-
assignOn
Assigns the on value to voxels, expecting global coordinates.- Returns:
- the assigner.
-
assignOff
Assigns the off value to voxels, expecting global coordinates.- Returns:
- the assigner.
-
derivePointsLocal
Creates a list of all on voxels as points, using local coordinates i.e. relative to the bounding-box corner.- Returns:
- a newly created list with newly created points.
-
toString
A string representation of the object-mask showing:- the center-of-gravity.
- the number of on voxels on the object.
-
binaryValues
What values constitute an on and off state invoxels
- as ints. -
binaryValuesByte
What values constitute an on and off state invoxels
- as bytes. -
extract
Provides methods to read/copy/duplicate regions of voxels.
-