21 #include "mirtk/BaseImage.h" 25 #ifndef MIRTK_GenericImage_H 26 #define MIRTK_GenericImage_H 28 #include "mirtk/VoxelCast.h" 41 template <
class TVoxel>
42 class GenericImage :
public BaseImage
56 typedef typename voxel_info<VoxelType>::RealType
RealType;
59 typedef typename voxel_info<VoxelType>::ScalarType
ScalarType;
96 explicit GenericImage(
int,
int,
int = 1,
int = 1, VoxelType *data = NULL);
99 explicit GenericImage(
int,
int,
int,
int,
int, VoxelType *data = NULL);
114 template <
class TVoxel2>
139 void Initialize(
int,
int,
int,
int,
int, VoxelType *data = NULL);
142 void Initialize(
int,
int,
int = 1,
int = 1, VoxelType *data = NULL);
163 template <
class TVoxel2>
167 operator bool()
const;
182 VoxelType
Get(
int)
const;
185 VoxelType
Get(
int,
int,
int = 0,
int = 0)
const;
188 void Put(
int, VoxelType);
191 void Put(
int,
int, VoxelType);
194 void Put(
int,
int,
int, VoxelType);
197 void Put(
int,
int,
int,
int, VoxelType);
206 VoxelType&
operator()(
int,
int,
int = 0,
int = 0);
209 const VoxelType&
operator()(
int,
int,
int = 0,
int = 0)
const;
218 virtual double GetAsDouble(
int,
int,
int = 0,
int = 0)
const;
230 virtual void PutAsDouble(
int,
int,
int,
int,
double);
260 VoxelType *
Data(
int = 0);
263 VoxelType *
Data(
int,
int,
int = 0,
int = 0);
266 const VoxelType *
Data(
int = 0)
const;
269 const VoxelType *
Data(
int,
int,
int = 0,
int = 0)
const;
281 virtual const void *
GetDataPointer(
int,
int,
int = 0,
int = 0)
const;
309 int,
int,
int)
const;
313 int,
int,
int)
const;
317 int,
int,
int)
const;
321 int,
int,
int,
int)
const;
325 int,
int,
int,
int)
const;
329 int,
int,
int,
int)
const;
346 template <
class TVoxel2>
391 void GetMinMax(VoxelType &, VoxelType &)
const;
394 void GetMinMax(VoxelType &, VoxelType &, VoxelType)
const;
402 RealType
Mean(
bool fg =
true)
const;
409 RealType
GetSD(
int = 1)
const;
420 virtual void ReflectX(
bool modify_axes =
false);
421 virtual void ReflectY(
bool modify_axes =
false);
422 virtual void ReflectZ(
bool modify_axes =
false);
423 virtual void ReflectT(
bool modify_axes =
false);
425 virtual void FlipXY(
bool modify_origin =
false);
426 virtual void FlipXZ(
bool modify_origin =
false);
427 virtual void FlipYZ(
bool modify_origin =
false);
428 virtual void FlipXT(
bool modify_origin =
false);
429 virtual void FlipYT(
bool modify_origin =
false);
430 virtual void FlipZT(
bool modify_origin =
false);
432 virtual void SwapXY(
bool modify_axes =
true);
433 virtual void SwapXZ(
bool modify_axes =
true);
434 virtual void SwapYZ(
bool modify_axes =
true);
435 virtual void SwapXT(
bool modify_axes =
true);
436 virtual void SwapYT(
bool modify_axes =
true);
437 virtual void SwapZT(
bool modify_axes =
true);
443 #if MIRTK_Image_WITH_VTK 448 virtual void ImageToVTK(vtkStructuredPoints *)
const;
453 virtual void VTKToImage(vtkStructuredPoints *);
455 #endif // MIRTK_Image_WITH_VTK 460 virtual void Read(
const char *);
463 virtual void Write(
const char *)
const;
470 void GetMinMax(VoxelType *, VoxelType *)
const;
474 void GetMinMax(VoxelType *, VoxelType *, VoxelType)
const;
478 void GetMinMaxPad(VoxelType *, VoxelType *, VoxelType)
const;
499 template <
class VoxelType>
template <
class VoxelType2>
504 const VoxelType2 *ptr2 = image.GetPointerToVoxels();
506 ptr1[idx] = voxel_cast<
VoxelType>(ptr2[idx]);
508 if (image.OwnsMask()) {
515 if (image.HasBackgroundValue()) {
522 template <
class VoxelType>
533 template <
class VoxelType>
536 return voxel_info<VoxelType>::vector_size();
544 template <
class VoxelType>
551 template <
class VoxelType>
558 template <
class VoxelType>
565 template <
class VoxelType>
572 template <
class VoxelType>
579 template <
class VoxelType>
586 template <
class VoxelType>
593 template <
class VoxelType>
600 template <
class VoxelType>
607 template <
class VoxelType>
618 template <
class VoxelType>
template <
class VoxelType2>
623 const VoxelType2 *ptr2 = image.GetPointerToVoxels();
624 for (
int idx = 0; idx < image; ++idx) {
625 if (
IsForeground(idx) && image.IsForeground(idx) && ptr1[idx] != ptr2[idx]) {
637 template <
class VoxelType>
644 template <
class VoxelType>
651 template <
class VoxelType>
658 template <
class VoxelType>
665 template <
class VoxelType>
668 return voxel_cast<
double>(
_data[index]);
672 template <
class VoxelType>
675 return voxel_cast<
double>(
_matrix[t][z][y][x]);
679 template <
class VoxelType>
686 template <
class VoxelType>
693 template <
class VoxelType>
700 template <
class VoxelType>
707 template <
class VoxelType>
714 template <
class VoxelType>
721 template <
class VoxelType>
728 template <
class VoxelType>
739 template <
class VoxelType>
746 template <
class VoxelType>
753 template <
class VoxelType>
760 template <
class VoxelType>
767 template <
class VoxelType>
774 template <
class VoxelType>
781 template <
class VoxelType>
788 template <
class VoxelType>
795 template <
class VoxelType>
798 return voxel_info<VoxelType>::type();
802 template <
class VoxelType>
805 return static_cast<int>(
sizeof(
VoxelType));
809 template <
class VoxelType>
812 return voxel_limits<VoxelType>::min();
816 template <
class VoxelType>
819 return voxel_limits<VoxelType>::max();
827 template <
class VoxelType>
834 template <
class VoxelType>
841 template <
class VoxelType>
848 template <
class VoxelType>
855 template <
class VoxelType>
870 Throw(ERR_InvalidArgument, __FUNCTION__,
"Input image must be 3D without temporal components");
872 if (out.Attributes().EqualInSpace(in.Attributes()) && out.T() == 3) {
875 out.Initialize(in.Attributes(), 3);
878 T *x = out.Data(0, 0, 0, 0);
879 T *y = out.Data(0, 0, 0, 1);
880 T *z = out.Data(0, 0, 0, 2);
881 const int nvox = in.NumberOfVoxels();
882 for (
int idx = 0; idx < nvox; ++idx, ++x, ++y, ++z, ++v) {
894 Throw(ERR_InvalidArgument, __FUNCTION__,
"Input image must be 3D without temporal components");
896 if (out.Attributes().EqualInSpace(in.Attributes()) && out.T() == 4) {
899 out.Initialize(in.Attributes(), 4);
902 T *x = out.Data(0, 0, 0, 0);
903 T *y = out.Data(0, 0, 0, 1);
904 T *z = out.Data(0, 0, 0, 2);
905 T *t = out.Data(0, 0, 0, 3);
906 const int nvox = in.NumberOfVoxels();
907 for (
int idx = 0; idx < nvox; ++idx, ++x, ++y, ++z, ++t, ++v) {
916 template <
int N,
class T>
920 Throw(ERR_InvalidArgument, __FUNCTION__,
"Input image must be 3D without temporal components");
922 if (out.Attributes().EqualInSpace(in.Attributes()) && out.T() ==
N) {
925 out.Initialize(in.Attributes(),
N);
928 const int nvox = in.NumberOfVoxels();
929 for (
int idx = 0; idx < nvox; ++idx, ++v) {
931 T *p = out.Data(idx);
932 for (
int i = 0; i <
N; ++i, p += nvox, ++c) {
943 Throw(ERR_InvalidArgument, __FUNCTION__,
"Input image must have 3 components in the temporal dimension");
945 if (!out.Attributes().EqualInSpace(in.Attributes()) || out.T() != 1) {
946 out.Initialize(in.Attributes(), 1);
948 const T *x = in.Data(0, 0, 0, 0);
949 const T *y = in.Data(0, 0, 0, 1);
950 const T *z = in.Data(0, 0, 0, 2);
952 for (
int idx = 0; idx < out.NumberOfVoxels(); ++idx, ++x, ++y, ++z, ++v) {
964 Throw(ERR_InvalidArgument, __FUNCTION__,
"Input image must have 4 components in the temporal dimension");
966 if (!out.Attributes().EqualInSpace(in.Attributes()) || out.T() != 1) {
967 out.Initialize(in.Attributes(), 1);
969 const T *x = in.Data(0, 0, 0, 0);
970 const T *y = in.Data(0, 0, 0, 1);
971 const T *z = in.Data(0, 0, 0, 2);
972 const T *t = in.Data(0, 0, 0, 3);
974 for (
int idx = 0; idx < out.NumberOfVoxels(); ++idx, ++x, ++y, ++z, ++t, ++v) {
983 template <
int N,
class T>
987 Throw(ERR_InvalidArgument, __FUNCTION__,
"Input image must have ",
N,
" components in the temporal dimension");
989 if (!out.Attributes().EqualInSpace(in.Attributes()) || out.T() != 1) {
990 out.Initialize(in.Attributes(), 1);
993 const int nvox = out.NumberOfVoxels();
994 for (
int idx = 0; idx < nvox; ++idx, ++v) {
995 const T *p = in.Data(idx);
997 for (
int i = 0; i <
N; ++i, p += nvox, ++c) {
1014 #endif // MIRTK_GenericImage_H int _NumberOfVoxels
Total number of voxels.
bool operator==(const GenericImage< TVoxel2 > &) const
virtual void ReflectX(bool modify_axes=false)
Reflect image along x.
VoxelType * Data(int=0)
Get raw pointer to contiguous image data.
virtual void FlipZT(bool modify_origin=false)
Flip z and t axis, always also swaps voxel size.
GenericImage & operator=(VoxelType)
Assign constant value to each voxel.
BinaryImage * _mask
Foreground mask.
voxel_info< VoxelType >::RealType RealType
void PutBackgroundValueAsDouble(double)
Put background value.
GenericImage operator*(const GenericImage &) const
Multiply images voxel-wise.
GenericImage & operator-=(const GenericImage &)
Subtract image.
void Clear()
Clear an image.
virtual void FlipXY(bool modify_origin=false)
Flip x and y axis, always also swaps voxel size.
void GetMinMax(VoxelType &, VoxelType &) const
Minimum and maximum pixel values get accessor.
RealType GetSD(int=1) const
Standard Deviation of the pixels.
void Put(int, VoxelType)
Function for pixel put access.
VoxelType * _data
Pointer to image data.
virtual void FlipYT(bool modify_origin=false)
Flip y and t axis, always also swaps voxel size.
GenericImage & operator/=(const GenericImage &)
Divide voxels.
virtual void ReflectZ(bool modify_axes=false)
Reflect image along z.
void GetMaxPosition(Point &, int=1, int=0) const
Get Max Intensity position around the point.
GenericImage operator>(VoxelType) const
Clamp image given upper threshold.
RealType GetAverage(int=1) const
void CopyFrom(const VoxelType *)
Copy image data from 1D array.
GenericImage operator<(VoxelType) const
Clamp image given lower threshold.
const ImageAttributes & GetImageAttributes() const
virtual void SwapXY(bool modify_axes=true)
Swap x and y axis.
RealType Mean(bool fg=true) const
bool IsForeground(int) const
Whether voxel is within foreground without index-out-of-bounds check.
virtual void * GetDataPointer(int=0)
Get raw pointer to contiguous image data.
virtual BaseImage * Copy() const
Create copy of this image.
int N() const
Number of vector components per voxel.
ImageAttributes _attr
Image attributes.
void AllocateImage(VoxelType *=NULL)
Allocate image memory.
GenericImage & operator>=(VoxelType)
Clamp image given upper threshold.
virtual void FlipXT(bool modify_origin=false)
Flip x and t axis, always also swaps voxel size.
VoxelType * GetPointerToVoxels(int=0, int=0, int=0, int=0)
virtual void SwapXZ(bool modify_axes=true)
Swap x and z axis.
T _v[N]
Vector components.
virtual void PutAsDouble(int, double)
Function for pixel put access.
virtual void FlipYZ(bool modify_origin=false)
Flip y and z axis, always also swaps voxel size.
virtual int GetDataType() const
Get enumeration value corresponding to voxel type.
void PutMinMax(VoxelType, VoxelType)
Linearly rescale intensities.
virtual void Read(const char *)
Read image from file.
voxel_info< VoxelType >::ScalarType ScalarType
Scalar type corresponding to voxel type.
voxel_info< ScalarType >::RealType RealScalarType
Floating point type corresponding to scalar type of voxel type.
GenericImage operator-(const GenericImage &) const
Subtract images.
~GenericImage()
Destructor.
virtual void SwapYT(bool modify_axes=true)
Swap y and t axis.
virtual void ReflectY(bool modify_axes=false)
Reflect image along y.
bool CropPad(int margin=0)
Crop/pad image background.
virtual double GetDataTypeMax() const
Maximum value a pixel can hold without overflowing.
VoxelType & operator()(int)
Function for pixel access from via operators.
GenericImage GetRegion(int, int) const
Get image consisting of specified 2D slice.
virtual void SwapXT(bool modify_axes=true)
Swap x and t axis.
virtual double GetAsDouble(int) const
Function for pixel get access as double.
void GetMinMaxPad(VoxelType *, VoxelType *, VoxelType) const
virtual void PutAsVector(int, const Vector &)
Function for pixel put access.
virtual void FlipXZ(bool modify_origin=false)
Flip x and z axis, always also swaps voxel size.
void Throw(ErrorType err, const char *func, Args... args) const
bool _dataOwner
Whether image data memory itself is owned by this instance.
GenericImage & operator*=(const GenericImage &)
Multipy voxels.
GenericImage< BinaryPixel > BinaryImage
Binary image as used for masks (0: off, otherwise: on)
GenericImage & operator<=(VoxelType)
Clamp image given lower threshold.
virtual void GetAsVector(Vector &, int) const
Function for pixel get access as double.
virtual void PutBackgroundValueAsDouble(double, bool)
Put background value.
virtual void Write(const char *) const
Write image to file.
virtual int GetDataTypeSize() const
Get size of each voxel in bytes.
int T() const
Returns the number of voxels in the t-direction.
void GravityCenter(Point &, int=1, int=0) const
Get Gravity center position of a given window.
virtual void SwapYZ(bool modify_axes=true)
Swap y and z axis.
VoxelType Get(int) const
Function for pixel get access.
virtual void Initialize()
Initialize a previously allocated image.
GenericImage()
Default constructor.
BinaryImage operator!=(VoxelType) const
Get binary mask for voxels which are not equal the scalar.
virtual void ReflectT(bool modify_axes=false)
Reflect image along t.
GenericImage operator/(const GenericImage &) const
Divide images voxel-wise.
virtual double GetDataTypeMin() const
Minimum value a pixel can hold without overflowing.
virtual void SwapZT(bool modify_axes=true)
Swap z and t axis.
GenericImage GetFrame(int, int=-1) const
Get time instance (i.e., frame) or channel of image.
GenericImage operator+(const GenericImage &) const
Add images.
TVoxel VoxelType
Voxel type.
GenericImage & operator+=(const GenericImage &)
Add image.