16 #include <itkeigen/Eigen/Dense>
18 #include <itkImageRegionIterator.h>
25 #include <itkConnectedComponentImageFilter.h>
76 static void CountVoxel(mitk::Image::Pointer image, std::map<unsigned int, unsigned int> &
map);
85 static void CountVoxel(mitk::Image::Pointer image,
unsigned int label,
unsigned int & count);
93 static void CountVoxel(mitk::Image::Pointer image,
unsigned int & count);
103 static void SumVoxelForLabel(mitk::Image::Pointer image,
const mitk::Image::Pointer & source ,
unsigned int label,
double & val );
113 static void SqSumVoxelForLabel(mitk::Image::Pointer image,
const mitk::Image::Pointer & source,
unsigned int label,
double & val );
122 static void LogicalAndImages(
const Image::Pointer &image1,
const Image::Pointer &image2, Image::Pointer &outimage);
131 static void GaussianFilter(mitk::Image::Pointer image, mitk::Image::Pointer & smoothed ,
double sigma);
173 static void LocalHistogram(mitk::Image::Pointer image, std::vector<mitk::Image::Pointer> &out,
int Bins,
int NeighbourhoodSize);
185 template<
typename TMatrixElementType>
186 static mitk::Image::Pointer
Transform(
const Eigen::Matrix<TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic> & matrix,
const mitk::Image::Pointer & mask)
188 itk::Image<unsigned int, 3>::Pointer itkMask;
191 typename itk::Image<TMatrixElementType, 3>::Pointer itk_img = itk::Image<TMatrixElementType, 3>::New();
192 itk_img->SetRegions(itkMask->GetLargestPossibleRegion());
193 itk_img->SetOrigin(itkMask->GetOrigin());
194 itk_img->SetSpacing(itkMask->GetSpacing());
195 itk_img->SetDirection(itkMask->GetDirection());
199 unsigned int n_numSamples = 0;
202 if(n_numSamples != matrix.rows())
203 MITK_ERROR <<
"Number of samples in matrix and number of points under the masks is not the same!";
205 auto mit = itk::ImageRegionConstIterator<itk::Image<unsigned int, 3> >(itkMask, itkMask->GetLargestPossibleRegion());
206 auto oit = itk::ImageRegionIterator<itk::Image<TMatrixElementType, 3> >(itk_img, itk_img->GetLargestPossibleRegion());
208 unsigned int current_row = 0;
209 while(!mit.IsAtEnd())
212 oit.Set(matrix(current_row++,0));
234 template<
typename TMatrixElementType>
235 static Eigen::Matrix<TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic>
Transform(
const mitk::Image::Pointer & img,
const mitk::Image::Pointer & mask)
237 itk::Image<unsigned int, 3>::Pointer current_mask;
240 unsigned int n_numSamples = 0;
243 typename itk::Image<TMatrixElementType, 3>::Pointer current_img;
246 Eigen::Matrix<TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic> out_matrix(n_numSamples,1);
248 auto mit = itk::ImageRegionConstIterator<itk::Image<unsigned int, 3> >(current_mask, current_mask->GetLargestPossibleRegion());
249 auto iit = itk::ImageRegionConstIterator<itk::Image<TMatrixElementType, 3> >(current_img,current_img->GetLargestPossibleRegion());
250 unsigned int current_row = 0;
251 while (!mit.IsAtEnd()) {
253 out_matrix(current_row++) = iit.Value();
293 static void MergeLabels(mitk::Image::Pointer & img,
const std::map<unsigned int, unsigned int> &
map);
302 static void ConnectedComponentsImage(mitk::Image::Pointer & image, mitk::Image::Pointer& mask, mitk::Image::Pointer &outimage,
unsigned int& num_components);
310 static void GrabLabel(mitk::Image::Pointer & image, mitk::Image::Pointer & outimage,
unsigned int label);
318 static void InsertLabel(mitk::Image::Pointer & image, mitk::Image::Pointer & maskImage,
unsigned int label);
352 static void ProbabilityMap(
const mitk::Image::Pointer& sourceImage,
double mean,
double std_dev, mitk::Image::Pointer& resultImage);
354 template<
class TImageType>
357 auto it = itk::ImageRegionIterator< TImageType >(image,image->GetLargestPossibleRegion());
360 if(
map.find(it.Value()) ==
map.end())
367 template <
class TImageType>
368 static void itkCountVoxel(TImageType* image,
typename TImageType::PixelType label,
unsigned int & count )
370 itk::ImageRegionConstIterator<TImageType> inputIter(image, image->GetLargestPossibleRegion());
371 while(!inputIter.IsAtEnd())
373 if(inputIter.Value() == label) ++count;
378 template<
typename TImageType>
379 static inline void itkCountVoxel(TImageType * mask,
unsigned int & n_numSamples)
381 auto mit = itk::ImageRegionConstIterator<TImageType>(mask, mask->GetLargestPossibleRegion());
382 while (!mit.IsAtEnd())
390 template <
class TImageType1,
class TImageType2>
391 static void itkSampleLabel(TImageType1* image, TImageType2* output,
double acceptrate,
unsigned int label)
393 std::srand (time(
nullptr));
395 itk::ImageRegionConstIterator< TImageType1 > inputIter(image, image->GetLargestPossibleRegion());
396 itk::ImageRegionIterator< TImageType2 > outputIter(output, output->GetLargestPossibleRegion());
398 while (!inputIter.IsAtEnd())
400 double r = (double)(rand()) / RAND_MAX;
401 if(inputIter.Get() == label && r < acceptrate)
402 outputIter.Set(label);
409 template <
class TImageType>
410 static void itkSampleLabel(TImageType* image, mitk::Image::Pointer & output,
unsigned int n_samples_drawn)
412 std::srand (time(
nullptr));
414 typename TImageType::Pointer itk_out = TImageType::New();
415 itk_out->SetRegions(image->GetLargestPossibleRegion());
416 itk_out->SetDirection(image->GetDirection());
417 itk_out->SetOrigin(image->GetOrigin());
418 itk_out->SetSpacing(image->GetSpacing());
420 itk_out->FillBuffer(0);
422 itk::ImageRegionConstIterator< TImageType > inputIter(image, image->GetLargestPossibleRegion());
423 itk::ImageRegionIterator< TImageType > outputIter(itk_out, itk_out->GetLargestPossibleRegion());
425 for(
unsigned int i = 0 ; i < n_samples_drawn ;)
427 double r = (double)(rand()) / RAND_MAX;
428 if(inputIter.Value() != 0 && r < 0.01 && outputIter.Value() == 0)
430 outputIter.Set(inputIter.Value());
436 if(inputIter.IsAtEnd())
438 inputIter.GoToBegin();
439 outputIter.GoToBegin();
449 template<
class TImageType>
452 template<
class TImageType>
455 template<
class TImageType>
456 static void itkFillHoleGrayscale(TImageType * image, mitk::Image::Pointer & outimage);
458 template<
typename TImageType >
459 static void itkInsertLabel(TImageType * maskImage, mitk::Image::Pointer & outimage,
unsigned int label)
462 typename TImageType::Pointer itk_out;
464 if(outimage.IsNull())
467 itk_out = TImageType::New();
468 itk_out->SetSpacing(maskImage->GetSpacing());
469 itk_out->SetDirection(maskImage->GetDirection());
470 itk_out->SetOrigin(maskImage->GetOrigin());
471 itk_out->SetRegions(maskImage->GetLargestPossibleRegion());
473 itk_out->FillBuffer(0);
479 itk::ImageRegionIterator<TImageType> oit(itk_out,itk_out->GetLargestPossibleRegion());
480 itk::ImageRegionConstIterator<TImageType> mit(maskImage,maskImage->GetLargestPossibleRegion());
482 while(!mit.IsAtEnd())
496 template<
typename TImageType >
497 static void itkGrabLabel(TImageType * image, mitk::Image::Pointer & outimage,
unsigned int label)
499 typedef itk::Image<unsigned short, 3> TOutType;
500 TOutType::Pointer itk_out = TOutType::New();
501 itk_out->SetRegions(image->GetLargestPossibleRegion());
502 itk_out->SetDirection(image->GetDirection());
503 itk_out->SetOrigin(image->GetOrigin());
504 itk_out->SetSpacing(image->GetSpacing());
507 itk::ImageRegionConstIterator<TImageType> iit(image, image->GetLargestPossibleRegion());
508 itk::ImageRegionIterator<TOutType> oit(itk_out,itk_out->GetLargestPossibleRegion());
510 while(!iit.IsAtEnd())
512 if(iit.Value() ==
static_cast<typename TImageType::PixelType
>(label))
524 template<
class TImagetype>
525 static void itkMergeLabels(TImagetype * img,
const std::map<unsigned int, unsigned int> &
map)
528 auto it = itk::ImageRegionIterator<TImagetype>(img,img->GetLargestPossibleRegion());
532 if(
map.find(it.Value())!=
map.end())
533 it.Set(
map.at(it.Value()) );
539 template<
typename TImageType>
540 static void itkConnectedComponentsImage(TImageType * image, mitk::Image::Pointer& mask, mitk::Image::Pointer &outimage,
unsigned int& num_components)
542 typedef itk::Image<unsigned short, 3> MaskImageType;
543 MaskImageType::Pointer itk_mask;
546 itk_mask = MaskImageType::New();
547 itk_mask->SetRegions(image->GetLargestPossibleRegion());
548 itk_mask->SetDirection(image->GetDirection());
549 itk_mask->SetOrigin(image->GetOrigin());
550 itk_mask->SetSpacing(image->GetSpacing());
551 itk_mask->Allocate();
552 itk_mask->FillBuffer(1);
557 typedef itk::ConnectedComponentImageFilter<TImageType, MaskImageType, MaskImageType > FilterType;
558 typename FilterType::Pointer cc_filter = FilterType::New();
559 cc_filter->SetMaskImage(itk_mask.GetPointer());
560 cc_filter->SetInput(image);
561 cc_filter->SetBackgroundValue(0);
564 num_components = cc_filter->GetObjectCount();
568 template<
typename TImageType >
569 static void itkCreateCheckerboardMask(TImageType * image, mitk::Image::Pointer & outimage);
571 template<
typename TImageType >
572 static void itkInterpolateCheckerboardPrediction(TImageType * checkerboard_prediction, mitk::Image::Pointer & checkerboard_mask, mitk::Image::Pointer & outimage);
574 template <
class TImageType>
575 static void itkSumVoxelForLabel(TImageType* image,
const mitk::Image::Pointer & source ,
typename TImageType::PixelType label,
double & val );
577 template <
class TImageType>
578 static void itkSqSumVoxelForLabel(TImageType* image,
const mitk::Image::Pointer & source,
typename TImageType::PixelType label,
double & val );
580 template<
typename TStructuringElement>
581 static void itkFitStructuringElement(TStructuringElement & se, MorphologicalDimensions d,
int radius);
583 template<
typename TImageType>
584 static void itkDilateBinary(TImageType * sourceImage, mitk::Image::Pointer& resultImage,
int radius , MorphologicalDimensions d);
586 template<
typename TImageType>
587 static void itkErodeBinary(TImageType * sourceImage, mitk::Image::Pointer& resultImage,
int radius, MorphologicalDimensions d);
589 template<
typename TImageType>
590 static void itkClosingBinary(TImageType * sourceImage, mitk::Image::Pointer& resultImage,
int radius, MorphologicalDimensions d);
592 template<
typename TPixel,
unsigned int VDimension>
593 static void itkFillHolesBinary(itk::Image<TPixel, VDimension>* sourceImage, mitk::Image::Pointer& resultImage);
595 template<
typename TImageType>
596 static void itkLogicalAndImages(
const TImageType * image1,
const mitk::Image::Pointer & image2, mitk::Image::Pointer & outimage);
598 template<
class TImageType>
599 static void itkGaussianFilter(TImageType * image, mitk::Image::Pointer & smoothed ,
double sigma);
601 template<
class TImageType>
602 static void itkDifferenceOfGaussianFilter(TImageType * image, mitk::Image::Pointer & smoothed,
double sigma1,
double sigma2);
604 template<
typename TImageType>
605 static void itkProbabilityMap(
const TImageType * sourceImage,
double mean,
double std_dev, mitk::Image::Pointer& resultImage);
607 template<
typename TPixel,
unsigned int VImageDimension>
608 static void itkHessianOfGaussianFilter(itk::Image<TPixel, VImageDimension>* itkImage,
double variance, std::vector<mitk::Image::Pointer> &out);
610 template<
typename TPixel,
unsigned int VImageDimension>
611 static void itkLaplacianOfGaussianFilter(itk::Image<TPixel, VImageDimension>* itkImage,
double variance, mitk::Image::Pointer &output);
613 template<
typename TPixel,
unsigned int VImageDimension>
614 static void itkLocalHistograms(itk::Image<TPixel, VImageDimension>* itkImage, std::vector<mitk::Image::Pointer> &out,
int size,
int bins);
#define MITKCLUTILITIES_EXPORT
Collection of static utility functions for classification and image processing.
static void HessianOfGaussianFilter(mitk::Image::Pointer image, std::vector< mitk::Image::Pointer > &out, double sigma)
Compute the Hessian of Gaussian for an image.
static void ProbabilityMap(const mitk::Image::Pointer &sourceImage, double mean, double std_dev, mitk::Image::Pointer &resultImage)
Create a probability map based on Gaussian distribution parameters.
MorphologicalDimensions
Specifies the anatomical plane for morphological operations.
@ Sagittal
Sagittal plane.
@ Axial
Axial (transverse) plane.
@ Coronal
Coronal (frontal) plane.
static void CreateCheckerboardMask(mitk::Image::Pointer image, mitk::Image::Pointer &outimage)
Create a checkerboard mask from the given image.
static void InterpolateCheckerboardPrediction(mitk::Image::Pointer checkerboard_prediction, mitk::Image::Pointer &checkerboard_mask, mitk::Image::Pointer &outimage)
Interpolate a prediction using a checkerboard pattern.
static void LogicalAndImages(const Image::Pointer &image1, const Image::Pointer &image2, Image::Pointer &outimage)
Compute the logical AND of two binary images.
static void GaussianFilter(mitk::Image::Pointer image, mitk::Image::Pointer &smoothed, double sigma)
Apply a Gaussian smoothing filter to an image.
static void SumVoxelForLabel(mitk::Image::Pointer image, const mitk::Image::Pointer &source, unsigned int label, double &val)
Sum intensity values from a source image for voxels matching a given label.
static void GrabLabel(mitk::Image::Pointer &image, mitk::Image::Pointer &outimage, unsigned int label)
Extract a single label from a label image into a binary mask.
static void FillHoleGrayscale(mitk::Image::Pointer &image, mitk::Image::Pointer &outimage)
Fill holes in a grayscale image.
static void itkSampleLabel(TImageType1 *image, TImageType2 *output, double acceptrate, unsigned int label)
static void itkCountVoxel(TImageType *image, typename TImageType::PixelType label, unsigned int &count)
static void MergeLabels(mitk::Image::Pointer &img, const std::map< unsigned int, unsigned int > &map)
Merge labels in a label image according to a mapping.
static void InsertLabel(mitk::Image::Pointer &image, mitk::Image::Pointer &maskImage, unsigned int label)
Insert a label value into an output image at positions defined by a mask.
static void ClosingBinary(mitk::Image::Pointer &sourceImage, mitk::Image::Pointer &resultImage, int radius, MorphologicalDimensions d)
Perform morphological closing on a binary image.
static void itkCountVoxel(TImageType *mask, unsigned int &n_numSamples)
static void itkSampleLabel(TImageType *image, mitk::Image::Pointer &output, unsigned int n_samples_drawn)
static void DifferenceOfGaussianFilter(mitk::Image::Pointer image, mitk::Image::Pointer &smoothed, double sigma1, double sigma2)
Compute the Difference of Gaussians (DoG) of an image.
static void LocalHistogram(mitk::Image::Pointer image, std::vector< mitk::Image::Pointer > &out, int Bins, int NeighbourhoodSize)
Compute local histograms for each voxel in an image.
static void SqSumVoxelForLabel(mitk::Image::Pointer image, const mitk::Image::Pointer &source, unsigned int label, double &val)
Sum squared intensity values from a source image for voxels matching a given label.
static void itkCountVoxel(TImageType *image, std::map< unsigned int, unsigned int > &map)
static void ErodeBinary(mitk::Image::Pointer &sourceImage, mitk::Image::Pointer &resultImage, int radius, MorphologicalDimensions d)
Erode a binary image using a ball structuring element.
static void CountVoxel(mitk::Image::Pointer image, unsigned int label, unsigned int &count)
Count the number of voxels with a specific label value.
static void ErodeGrayscale(mitk::Image::Pointer &image, unsigned int radius, mitk::CLUtil::MorphologicalDimensions d, mitk::Image::Pointer &outimage)
Perform grayscale erosion using a ball structuring element.
static void DilateBinary(mitk::Image::Pointer &sourceImage, mitk::Image::Pointer &resultImage, int radius, MorphologicalDimensions d)
Dilate a binary image using a ball structuring element.
static void CountVoxel(mitk::Image::Pointer image, std::map< unsigned int, unsigned int > &map)
Count voxels per label in an image.
static mitk::Image::Pointer Transform(const Eigen::Matrix< TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic > &matrix, const mitk::Image::Pointer &mask)
Transform an Eigen matrix back to an MITK image using a mask.
static void CountVoxel(mitk::Image::Pointer image, unsigned int &count)
Count the total number of non-zero voxels.
static void DilateGrayscale(mitk::Image::Pointer &image, unsigned int radius, mitk::CLUtil::MorphologicalDimensions d, mitk::Image::Pointer &outimage)
Perform grayscale dilation using a ball structuring element.
static void ConnectedComponentsImage(mitk::Image::Pointer &image, mitk::Image::Pointer &mask, mitk::Image::Pointer &outimage, unsigned int &num_components)
Compute connected components of a label image.
static Eigen::Matrix< TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic > Transform(const mitk::Image::Pointer &img, const mitk::Image::Pointer &mask)
Transform an MITK image into an Eigen column matrix using a mask.
static void LaplacianOfGaussianFilter(mitk::Image::Pointer image, mitk::Image::Pointer &smoothed, double sigma1)
Compute the Laplacian of Gaussian (LoG) of an image.
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grab the memory of an itk::Image (with a specific type) and put it into an mitk::Image.
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
#define MITK_INFO
Log an informational message.
#define MITK_ERROR
Log an error message.
Find image slices visible on a given plane.
@ All
Return all nodes in the DataStorage.