21 #ifndef MIRTK_BaseImage_H 22 #define MIRTK_BaseImage_H 24 #include "mirtk/Object.h" 25 #include "mirtk/Voxel.h" 26 #include "mirtk/Point.h" 27 #include "mirtk/PointSet.h" 28 #include "mirtk/Vector.h" 29 #include "mirtk/Vector3.h" 30 #include "mirtk/Matrix.h" 31 #include "mirtk/ImageAttributes.h" 38 #include "mirtk/ImageConfig.h" 39 #if MIRTK_Image_WITH_VTK 40 class vtkStructuredPoints;
41 #endif // MIRTK_Image_WITH_VTK 84 Orientation_Unknown = 0,
177 virtual void Clear() = 0;
204 virtual int N()
const;
219 double XSize()
const;
222 double YSize()
const;
225 double ZSize()
const;
228 double TSize()
const;
252 void GetPixelSize(
double &,
double &,
double &,
double &)
const;
264 void GetOrigin(
double &,
double &,
double &)
const;
267 void GetOrigin(
double &,
double &,
double &,
double &)
const;
276 void PutOrigin(
double,
double,
double,
double);
320 void IndexToVoxel(
int,
int &,
int &,
int &,
int &)
const;
326 void IndexToWorld(
int,
double &,
double &,
double &)
const;
357 void ImageToWorld(WorldCoordsImage &i2w,
bool _3D =
true)
const;
401 bool IsInFOV(
double,
double,
double);
413 virtual double GetAsDouble(
int,
int,
int = 0,
int = 0)
const = 0;
425 virtual void PutAsDouble(
int,
int,
int,
int,
double) = 0;
455 void PutMask(BinaryImage *,
bool =
false);
458 BinaryImage *
GetMask(
bool =
false);
461 const BinaryImage *
GetMask()
const;
521 bool IsInside(
int,
int,
int,
int)
const;
533 bool IsOutside(
int,
int,
int,
int)
const;
584 void BoundingBox(
int &,
int &,
int &,
int &)
const;
587 void BoundingBox(
int &,
int &,
int &,
int &,
int &,
int &)
const;
590 void BoundingBox(
int &,
int &,
int &,
int &,
int &,
int &,
int &,
int &)
const;
624 int i2,
int j2,
int k2,
625 bool orthogonal =
true)
const;
658 virtual void GetRegion(
BaseImage *&,
int,
int,
int,
int,
int,
int,
int,
int)
const = 0;
685 virtual const void *
GetDataPointer(
int,
int,
int = 0,
int = 0)
const = 0;
702 virtual void ReflectX(
bool modify_axes =
false) = 0;
703 virtual void ReflectY(
bool modify_axes =
false) = 0;
704 virtual void ReflectZ(
bool modify_axes =
false) = 0;
705 virtual void ReflectT(
bool modify_axes =
false) = 0;
707 virtual void FlipXY(
bool modify_origin =
false) = 0;
708 virtual void FlipXZ(
bool modify_origin =
false) = 0;
709 virtual void FlipYZ(
bool modify_origin =
false) = 0;
710 virtual void FlipXT(
bool modify_origin =
false) = 0;
711 virtual void FlipYT(
bool modify_origin =
false) = 0;
712 virtual void FlipZT(
bool modify_origin =
false) = 0;
714 virtual void SwapXY(
bool modify_axes =
true) = 0;
715 virtual void SwapXZ(
bool modify_axes =
true) = 0;
716 virtual void SwapYZ(
bool modify_axes =
true) = 0;
717 virtual void SwapXT(
bool modify_axes =
true) = 0;
718 virtual void SwapYT(
bool modify_axes =
true) = 0;
719 virtual void SwapZT(
bool modify_axes =
true) = 0;
723 #if MIRTK_Image_WITH_VTK 728 int ImageToVTKScalarType()
const;
733 virtual void ImageToVTK(vtkStructuredPoints *)
const;
738 virtual void VTKToImage(vtkStructuredPoints *);
740 #endif // MIRTK_Image_WITH_VTK 745 virtual void Read(
const char *) = 0;
748 virtual void Write(
const char *)
const = 0;
771 typedef voxel_info<VoxelType>::ScalarType ScalarType;
772 typedef voxel_info<VoxelType>::RealType RealType;
773 typedef voxel_info<RealType>::ScalarType RealScalarType;
776 VoxelType
Get(
int)
const;
779 VoxelType
Get(
int,
int,
int = 0,
int = 0)
const;
796 void GetPixelSize(
double *,
double *,
double *,
double *)
const;
833 #endif // MIRTK_BaseImage_H 839 #ifndef MIRTK_BaseImage_HH 840 #define MIRTK_BaseImage_HH 845 #include "mirtk/GenericImage.h" 846 #include "mirtk/Math.h" 1090 const size_t n = 3 *
sizeof(double);
1105 const size_t n = 3 *
sizeof(double);
1193 x = a, y = b, z = c;
1208 v._x = a, v._y = b, v._z = c;
1225 x = a, y = b, z = c;
1240 v._x = a, v._y = b, v._z = c;
1277 return (-0.5 <= x && x <
_attr.
_x - 0.5) &&
1278 (-0.5 <= y && y <
_attr.
_y - 0.5) &&
1279 (-0.5 <= z && z <
_attr.
_z - 0.5);
1384 return _mask != NULL;
1435 return (
_mask->
Get(idx) == BinaryPixel(0));
1447 return (
_mask->
Get(i, j, k, l) == BinaryPixel(0));
1564 int i2,
int j2)
const 1566 if (i1 < 0 || i2 >=
_attr.
_x ||
1567 j1 < 0 || j2 >=
_attr.
_y)
return false;
1569 for (
int j = j1; j != j2; j++) {
1570 for (
int i = i1; i != i2; i++) {
1571 if (!
_mask->
Get(i, j))
return false;
1575 for (
int j = j1; j != j2; j++) {
1576 for (
int i = i1; i != i2; i++) {
1586 int i2,
int j2,
int k2)
const 1588 if (i1 < 0 || i2 >=
_attr.
_x ||
1590 k1 < 0 || k2 >=
_attr.
_z)
return false;
1592 for (
int k = k1; k != k2; k++) {
1593 for (
int j = j1; j != j2; j++) {
1594 for (
int i = i1; i != i2; i++) {
1595 if (!
_mask->
Get(i, j, k))
return false;
1600 for (
int k = k1; k != k2; k++) {
1601 for (
int j = j1; j != j2; j++) {
1602 for (
int i = i1; i != i2; i++) {
1613 int i2,
int j2,
int k2,
int l2)
const 1615 if (i1 < 0 || i2 >=
_attr.
_x ||
1618 l1 < 0 || l2 >=
_attr.
_t)
return false;
1621 for (
int k = k1; k != k2; k++) {
1622 for (
int j = j1; j != j2; j++) {
1623 for (
int i = i1; i != i2; i++) {
1624 if (!
_mask->
Get(i, j, k))
return false;
1629 for (
int l = l1; l != l2; l++) {
1630 for (
int k = k1; k != k2; k++) {
1631 for (
int j = j1; j != j2; j++) {
1632 for (
int i = i1; i != i2; i++) {
1633 if (!
_mask->
Get(i, j, k, l))
return false;
1641 for (
int l = l1; l != l2; l++) {
1642 for (
int k = k1; k != k2; k++) {
1643 for (
int j = j1; j != j2; j++) {
1644 for (
int i = i1; i != i2; i++) {
1658 for (
int nl = l - 1; nl <= l + 1; ++nl)
1659 for (
int nk = k - 1; nk <= k + 1; ++nk)
1660 for (
int nj = j - 1; nj <= j + 1; ++nj)
1661 for (
int ni = i - 1; ni <= i + 1; ++ni) {
1662 if (ni != 0 || nj != 0 || nk != 0 || nl != 0) {
1682 for (
int nl = l - 1; nl <= l + 1; ++nl)
1683 for (
int nk = k - 1; nk <= k + 1; ++nk)
1684 for (
int nj = j - 1; nj <= j + 1; ++nj)
1685 for (
int ni = i - 1; ni <= i + 1; ++ni) {
1686 if (ni != 0 || nj != 0 || nk != 0 || nl != 0) {
1808 #endif // MIRTK_BaseImage_HH virtual void PutAsVector(int, const Vector &)
Function for pixel put access.
double _dt
Voxel t-dimensions (in ms)
int GetScalarType() const
int _NumberOfVoxels
Total number of voxels.
bool IsOutsideForeground(int) const
Whether voxel is index is outside finite image domain or part of background region.
virtual int GetDataTypeSize() const =0
Function which returns size of pixel scalar type in bytes.
double _xaxis[3]
Direction of x-axis.
void InitializeMask(int t=-1, bool=false)
Initialize mask if not done yet or none is set.
bool HasBackgroundValue() const
Whether a background value has been set.
double GetBackgroundValueAsDouble() const
Get background value.
bool IsBoundingBoxInsideForeground(int, int, int, int) const
Whether all voxels within a 2D bounding region are inside foreground region.
bool IsBackground(int) const
Whether voxel is within background without index-out-of-bounds check.
void ClearBackgroundValue()
Clear background value.
const Matrix & GetAffineMatrix() const
BinaryImage * _mask
Foreground mask.
int VoxelToIndex(int, int, int=0, int=0) const
Function to convert pixel to index.
double GetScalarTypeMax() const
static BaseImage * New(const char *)
Read file and construct image.
virtual void FlipXY(bool modify_origin=false)=0
Flip x and y axis, always also swaps voxel size.
void PutBackgroundValueAsDouble(double)
Put background value.
virtual void FlipYT(bool modify_origin=false)=0
Flip y and t axis, always also swaps voxel size.
const ImageAttributes & Attributes() const
Gets the image attributes.
bool IsOutside(int) const
Whether voxel is index is outside finite image domain.
double GetYSize() const
Returns the size of a voxel in the y-direction.
void PutOrientation(double *, double *, double *=NULL)
Put image x- and y-axis and z-axis.
void PutAttributes(const ImageAttributes &)
Puts attributes of image.
bool IsEmpty() const
Whether image is uninitialized.
void WorldToImage(double &, double &) const
World to image coordinate conversion with two doubles.
Matrix _smat
Affine transformation matrix.
bool HasSpatialAttributesOf(const BaseImage *) const
Checks if this image shares the same spatial attributes with another image.
void PutMask(BinaryImage *, bool=false)
Set foreground mask.
void PutAffineMatrix(const Matrix &m, bool apply=false)
virtual double GetDataTypeMax() const =0
Function which returns the minimum value the pixel can hold without overflowing.
BaseImage()
Default constructor.
double _torigin
Image t-origin (in ms)
double _x
x coordinate of Point
int _y
Image y-dimension (in voxels)
virtual void GetAsVector(Vector &, int) const
Function for pixel get access as double.
MIRTKCU_API bool IsNaN(double x)
Check if floating point value is not a number (NaN)
double ZSize() const
Returns the size of a voxel in the z-direction.
bool HasBackground() const
Whether any voxel is within background.
double GetScalarTypeMin() const
virtual int GetDataType() const =0
Function which returns pixel scalar type.
const ImageAttributes & GetImageAttributes() const
virtual void FlipZT(bool modify_origin=false)=0
Flip z and t axis, always also swaps voxel size.
void ResetBackgroundValueAsDouble(double)
virtual void PutMinMaxAsDouble(double, double)
Minimum and maximum pixel values put accessor.
virtual void GetRegion(BaseImage *&, int, int) const =0
Get image consisting of specified 2D slice.
Matrix _matW2I
Transformation matrix from (transformed) world coordinates to image coordinates.
void GetPixelSize(double &, double &) const
Voxel dimensions get access.
virtual void SwapZT(bool modify_axes=true)=0
Swap z and t axis.
virtual BaseImage * Copy() const
Create copy of this image.
double _xorigin
Image x-origin (in mm)
double _bg
Background value - may also be NaN for floating point images.
Matrix & Ident()
Set to identity matrix.
bool IsForeground(int) const
Whether voxel is within foreground without index-out-of-bounds check.
void PutOrigin(const Point &)
Image origin put access.
virtual void Clear()=0
Clear image.
virtual void Read(const char *)=0
Read image from file.
virtual void SwapXT(bool modify_axes=true)=0
Swap x and t axis.
ImageAttributes _attr
Image attributes.
MIRTKCU_API bool AreEqualOrNaN(double a, double b, double tol=1e-12)
Determine equality of two floating point numbers including check if both are NaN. ...
void Orientation(OrientationCode &, OrientationCode &, OrientationCode &) const
Get orientation of axis relative to patient.
virtual void Initialize(const ImageAttributes &, int=-1)=0
Initialize image.
void IndexToWorld(int, double &, double &) const
Get world coordinates (in mm) of lattice point.
virtual void Write(const char *) const =0
Write image to file.
int GetX() const
Returns the number of voxels in the x-direction.
void PutAffineMatrix(const Matrix &, bool=false)
BaseImage & operator=(const BaseImage &)
Assignment operator.
void ImageToWorld(double &, double &) const
Image to world coordinate conversion with two doubles.
double GetTOrigin() const
Get temporal origin.
int _z
Image z-dimension (in voxels)
double _dz
Voxel z-dimensions (in mm)
virtual double GetAsDouble(int) const
Function for pixel get access as double.
const Matrix & GetWorldToImageMatrix() const
Return transformation matrix for world to image coordinates.
int _t
Image t-dimension (in voxels)
virtual void GetFrame(BaseImage *&, int, int=-1) const =0
Get time instance (i.e., frame) or channel of image.
void UpdateMatrix()
Update coordinate transformation.
int GetT() const
Returns the number of voxels in the t-direction.
double TimeToImage(double) const
Time to image coordinate conversion.
virtual void SwapXZ(bool modify_axes=true)=0
Swap x and z axis.
int GetY() const
Returns the number of voxels in the y-direction.
double _zorigin
Image z-origin (in mm)
virtual double GetDataTypeMin() const =0
Function which returns the minimum value the pixel can hold without overflowing.
double GetZSize() const
Returns the size of a voxel in the z-direction.
virtual void FlipXT(bool modify_origin=false)=0
Flip x and t axis, always also swaps voxel size.
virtual void * GetDataPointer(int=0)=0
Function for pixel access via pointers.
bool IsInsideForeground(int) const
Whether voxel is index is within finite image domain and part of foreground region.
bool IsInside(int) const
Whether voxel index is within finite image domain.
int Y() const
Returns the number of voxels in the y-direction.
virtual void SwapYZ(bool modify_axes=true)=0
Swap y and z axis.
int GetNumberOfVoxels() const
double ImageToTime(double) const
Image to time coordinate conversion.
virtual int N() const
Returns the number of vector components (i.e., 1 for scalar images)
int LatticeToIndex(int, int, int=0, int=0) const
Get Index from Lattice.
const BinaryImage * GetMask() const
Get foreground mask.
void * GetScalarPointer(int=0, int=0, int=0, int=0)
virtual ~BaseImage()
Destructor.
int X() const
Returns the number of voxels in the x-direction.
bool IsNextToBackground(int) const
Whether at least one neighboring voxel is outside the finite foreground region.
virtual void ReflectZ(bool modify_axes=false)=0
Reflect image along z.
Point GetOrigin() const
Image origin get access.
int NumberOfSpatialPoints() const
Number of spatial lattice points.
void IndexToWorld(int, double &, double &) const
Get world coordinates (in mm) of pixl.
double _zaxis[3]
Direction of z-axis.
virtual void Print(Indent=0) const
Print image information.
bool EqualInSpace(const ImageAttributes &attr) const
Whether spatial attributes are equal.
virtual void FlipYZ(bool modify_origin=false)=0
Flip y and z axis, always also swaps voxel size.
double _yorigin
Image y-origin (in mm)
void PutPixelSize(double, double, double)
Voxel dimensions put access.
double TSize() const
Returns the size of a voxel in the t-direction.
GenericImage< BinaryPixel > BinaryImage
Binary image as used for masks (0: off, otherwise: on)
double _dy
Voxel y-dimensions (in mm)
VoxelType Get(int) const
Get pixel value at voxel with given index.
void PutTSize(double)
Set temporal voxel size, i.e., to zero for vector field and non-zero for temporal sequence...
virtual void ReflectY(bool modify_axes=false)=0
Reflect image along y.
double GetXSize() const
Returns the size of a voxel in the x-direction.
double GetTSize() const
Returns the size of a voxel in the t-direction.
int T() const
Returns the number of voxels in the t-direction.
bool IsNextToForeground(int) const
Whether at least one neighboring voxel is inside the finite foreground region.
int NumberOfVoxels() const
Returns the total number of voxels.
virtual void PutAsDouble(int, double)
Function for pixel put access.
double XSize() const
Returns the size of a voxel in the x-direction.
VoxelType Get(int) const
Function for pixel get access.
double _z
z coordinate of Point
virtual void ReflectX(bool modify_axes=false)=0
Reflect image along x.
void IndexToLattice(int, int *, int *, int *=NULL, int *=NULL) const
Get Index from Lattice.
void IndexToVoxel(int, int &, int &) const
Function to convert index to pixel coordinates.
GenericImage< double > WorldCoordsImage
FIXME: Use double3 as voxel type instead.
int _x
Image x-dimension (in voxels)
bool _bgSet
Whether a background value was set.
bool IsInside(int) const
Whether voxel index is within finite image domain.
virtual void SwapYT(bool modify_axes=true)=0
Swap y and t axis.
Matrix _matI2W
Transformation matrix from image coordinates to (transformed) world coordinates.
virtual void GetMinMaxAsDouble(double &, double &) const
Minimum and maximum pixel values get accessor.
virtual void FlipXZ(bool modify_origin=false)=0
Flip x and z axis, always also swaps voxel size.
double _y
y coordinate of Point
void GetOrientation(double *, double *, double *=NULL) const
Get image x- and y-axis and z-axis.
int GetZ() const
Returns the number of voxels in the z-direction.
int CenterOfForeground(Point ¢er) const
bool IsInFOV(double, double, double)
Returns true if point is within the field of view of image.
bool IsBoundary(int) const
Whether voxel index is at boundary of finite image domain.
virtual void SwapXY(bool modify_axes=true)=0
Swap x and y axis.
virtual void ReflectT(bool modify_axes=false)=0
Reflect image along t.
bool IsBoundary(int) const
Whether voxel index is at boundary of finite image domain.
double _dx
Voxel x-dimensions (in mm)
int NumberOfSpatialVoxels() const
Returns the total number of spatial voxels.
ImageAttributes ForegroundDomain(int i1, int j1, int k1, int i2, int j2, int k2, bool orthogonal=true) const
BaseImage Image
Alternative/backwards compatible type name.
int GetScalarTypeSize() const
OrientationCode
Orientation codes (same as defined in nifti1_io.h, e.g., NIFTI_L2R)
void ClearMask(bool=false)
Clear mask upon n-th call after n preceeding InitializeMask calls.
int Z() const
Returns the number of voxels in the z-direction.
const Matrix & GetImageToWorldMatrix() const
Return transformation matrix for image to world coordinates.
double YSize() const
Returns the size of a voxel in the y-direction.
void PutTOrigin(double)
Put temporal origin.
double _yaxis[3]
Direction of y-axis.
void BoundingBox(int &, int &, int &, int &) const
Get 2D bounding box of image foreground.