16 #include <itkeigen/Eigen/Dense>
18 #include <itkImageRegionIterator.h>
25 #include <itkConnectedComponentImageFilter.h>
135 static void HessianOfGaussianFilter(
mitk::Image::Pointer image, std::vector<mitk::Image::Pointer> &out,
double sigma);
144 static void LocalHistogram(
mitk::Image::Pointer image, std::vector<mitk::Image::Pointer> &out,
int Bins,
int NeighbourhoodSize);
151 template<
typename TMatrixElementType>
154 itk::Image<unsigned int, 3>::Pointer itkMask;
157 typename itk::Image<TMatrixElementType, 3>::Pointer itk_img = itk::Image<TMatrixElementType, 3>::New();
158 itk_img->SetRegions(itkMask->GetLargestPossibleRegion());
159 itk_img->SetOrigin(itkMask->GetOrigin());
160 itk_img->SetSpacing(itkMask->GetSpacing());
161 itk_img->SetDirection(itkMask->GetDirection());
165 unsigned int n_numSamples = 0;
168 if(n_numSamples != matrix.rows())
169 MITK_ERROR <<
"Number of samples in matrix and number of points under the masks is not the same!";
171 auto mit = itk::ImageRegionConstIterator<itk::Image<unsigned int, 3> >(itkMask, itkMask->GetLargestPossibleRegion());
172 auto oit = itk::ImageRegionIterator<itk::Image<TMatrixElementType, 3> >(itk_img, itk_img->GetLargestPossibleRegion());
174 unsigned int current_row = 0;
175 while(!mit.IsAtEnd())
178 oit.Set(matrix(current_row++,0));
194 template<
typename TMatrixElementType>
197 itk::Image<unsigned int, 3>::Pointer current_mask;
200 unsigned int n_numSamples = 0;
203 typename itk::Image<TMatrixElementType, 3>::Pointer current_img;
206 Eigen::Matrix<TMatrixElementType, Eigen::Dynamic, Eigen::Dynamic> out_matrix(n_numSamples,1);
208 auto mit = itk::ImageRegionConstIterator<itk::Image<unsigned int, 3> >(current_mask, current_mask->GetLargestPossibleRegion());
209 auto iit = itk::ImageRegionConstIterator<itk::Image<TMatrixElementType, 3> >(current_img,current_img->GetLargestPossibleRegion());
210 unsigned int current_row = 0;
211 while (!mit.IsAtEnd()) {
213 out_matrix(current_row++) = iit.Value();
316 template<
class TImageType>
319 auto it = itk::ImageRegionIterator< TImageType >(image,image->GetLargestPossibleRegion());
322 if(
map.find(it.Value()) ==
map.end())
329 template <
class TImageType>
330 static void itkCountVoxel(TImageType* image,
typename TImageType::PixelType label,
unsigned int & count )
332 itk::ImageRegionConstIterator<TImageType> inputIter(image, image->GetLargestPossibleRegion());
333 while(!inputIter.IsAtEnd())
335 if(inputIter.Value() == label) ++count;
340 template<
typename TImageType>
341 static inline void itkCountVoxel(TImageType * mask,
unsigned int & n_numSamples)
343 auto mit = itk::ImageRegionConstIterator<TImageType>(mask, mask->GetLargestPossibleRegion());
344 while (!mit.IsAtEnd())
352 template <
class TImageType1,
class TImageType2>
353 static void itkSampleLabel(TImageType1* image, TImageType2* output,
double acceptrate,
unsigned int label)
355 std::srand (time(
nullptr));
357 itk::ImageRegionConstIterator< TImageType1 > inputIter(image, image->GetLargestPossibleRegion());
358 itk::ImageRegionIterator< TImageType2 > outputIter(output, output->GetLargestPossibleRegion());
360 while (!inputIter.IsAtEnd())
362 double r = (double)(rand()) / RAND_MAX;
363 if(inputIter.Get() == label && r < acceptrate)
364 outputIter.Set(label);
371 template <
class TImageType>
374 std::srand (time(
nullptr));
376 typename TImageType::Pointer itk_out = TImageType::New();
377 itk_out->SetRegions(image->GetLargestPossibleRegion());
378 itk_out->SetDirection(image->GetDirection());
379 itk_out->SetOrigin(image->GetOrigin());
380 itk_out->SetSpacing(image->GetSpacing());
382 itk_out->FillBuffer(0);
384 itk::ImageRegionConstIterator< TImageType > inputIter(image, image->GetLargestPossibleRegion());
385 itk::ImageRegionIterator< TImageType > outputIter(itk_out, itk_out->GetLargestPossibleRegion());
387 for(
unsigned int i = 0 ; i < n_samples_drawn ;)
389 double r = (double)(rand()) / RAND_MAX;
390 if(inputIter.Value() != 0 && r < 0.01 && outputIter.Value() == 0)
392 outputIter.Set(inputIter.Value());
398 if(inputIter.IsAtEnd())
400 inputIter.GoToBegin();
401 outputIter.GoToBegin();
411 template<
class TImageType>
414 template<
class TImageType>
417 template<
class TImageType>
420 template<
typename TImageType >
421 static void itkInsertLabel(TImageType * maskImage,
mitk::Image::Pointer & outimage,
unsigned int label)
424 typename TImageType::Pointer itk_out;
426 if(outimage.IsNull())
429 itk_out = TImageType::New();
430 itk_out->SetSpacing(maskImage->GetSpacing());
431 itk_out->SetDirection(maskImage->GetDirection());
432 itk_out->SetOrigin(maskImage->GetOrigin());
433 itk_out->SetRegions(maskImage->GetLargestPossibleRegion());
435 itk_out->FillBuffer(0);
441 itk::ImageRegionIterator<TImageType> oit(itk_out,itk_out->GetLargestPossibleRegion());
442 itk::ImageRegionConstIterator<TImageType> mit(maskImage,maskImage->GetLargestPossibleRegion());
444 while(!mit.IsAtEnd())
458 template<
typename TImageType >
459 static void itkGrabLabel(TImageType * image,
mitk::Image::Pointer & outimage,
unsigned int label)
461 typedef itk::Image<unsigned short, 3> TOutType;
462 TOutType::Pointer itk_out = TOutType::New();
463 itk_out->SetRegions(image->GetLargestPossibleRegion());
464 itk_out->SetDirection(image->GetDirection());
465 itk_out->SetOrigin(image->GetOrigin());
466 itk_out->SetSpacing(image->GetSpacing());
469 itk::ImageRegionConstIterator<TImageType> iit(image, image->GetLargestPossibleRegion());
470 itk::ImageRegionIterator<TOutType> oit(itk_out,itk_out->GetLargestPossibleRegion());
472 while(!iit.IsAtEnd())
474 if(iit.Value() ==
static_cast<typename TImageType::PixelType
>(label))
486 template<
class TImagetype>
487 static void itkMergeLabels(TImagetype * img,
const std::map<unsigned int, unsigned int> &
map)
490 auto it = itk::ImageRegionIterator<TImagetype>(img,img->GetLargestPossibleRegion());
494 if(
map.find(it.Value())!=
map.end())
495 it.Set(
map.at(it.Value()) );
501 template<
typename TImageType>
504 typedef itk::Image<unsigned short, 3> MaskImageType;
505 MaskImageType::Pointer itk_mask;
508 itk_mask = MaskImageType::New();
509 itk_mask->SetRegions(image->GetLargestPossibleRegion());
510 itk_mask->SetDirection(image->GetDirection());
511 itk_mask->SetOrigin(image->GetOrigin());
512 itk_mask->SetSpacing(image->GetSpacing());
513 itk_mask->Allocate();
514 itk_mask->FillBuffer(1);
519 typedef itk::ConnectedComponentImageFilter<TImageType, MaskImageType, MaskImageType > FilterType;
520 typename FilterType::Pointer cc_filter = FilterType::New();
521 cc_filter->SetMaskImage(itk_mask.GetPointer());
522 cc_filter->SetInput(image);
523 cc_filter->SetBackgroundValue(0);
526 num_components = cc_filter->GetObjectCount();
530 template<
typename TImageType >
533 template<
typename TImageType >
536 template <
class TImageType>
537 static void itkSumVoxelForLabel(TImageType* image,
const mitk::Image::Pointer & source ,
typename TImageType::PixelType label,
double & val );
539 template <
class TImageType>
540 static void itkSqSumVoxelForLabel(TImageType* image,
const mitk::Image::Pointer & source,
typename TImageType::PixelType label,
double & val );
542 template<
typename TStructuringElement>
543 static void itkFitStructuringElement(TStructuringElement & se, MorphologicalDimensions d,
int radius);
545 template<
typename TImageType>
546 static void itkDilateBinary(TImageType * sourceImage,
mitk::Image::Pointer& resultImage,
int radius , MorphologicalDimensions d);
548 template<
typename TImageType>
549 static void itkErodeBinary(TImageType * sourceImage,
mitk::Image::Pointer& resultImage,
int radius, MorphologicalDimensions d);
551 template<
typename TImageType>
552 static void itkClosingBinary(TImageType * sourceImage,
mitk::Image::Pointer& resultImage,
int radius, MorphologicalDimensions d);
554 template<
typename TPixel,
unsigned int VDimension>
555 static void itkFillHolesBinary(itk::Image<TPixel, VDimension>* sourceImage,
mitk::Image::Pointer& resultImage);
557 template<
typename TImageType>
560 template<
class TImageType>
561 static void itkGaussianFilter(TImageType * image,
mitk::Image::Pointer & smoothed ,
double sigma);
563 template<
class TImageType>
564 static void itkDifferenceOfGaussianFilter(TImageType * image,
mitk::Image::Pointer & smoothed,
double sigma1,
double sigma2);
566 template<
typename TImageType>
567 static void itkProbabilityMap(
const TImageType * sourceImage,
double mean,
double std_dev,
mitk::Image::Pointer& resultImage);
569 template<
typename TPixel,
unsigned int VImageDimension>
570 static void itkHessianOfGaussianFilter(itk::Image<TPixel, VImageDimension>* itkImage,
double variance, std::vector<mitk::Image::Pointer> &out);
572 template<
typename TPixel,
unsigned int VImageDimension>
573 static void itkLaplacianOfGaussianFilter(itk::Image<TPixel, VImageDimension>* itkImage,
double variance,
mitk::Image::Pointer &output);
575 template<
typename TPixel,
unsigned int VImageDimension>
576 static void itkLocalHistograms(itk::Image<TPixel, VImageDimension>* itkImage, std::vector<mitk::Image::Pointer> &out,
int size,
int bins);