22 #include <itkImageRegionConstIterator.h> 23 #include <itkRegionOfInterestImageFilter.h> 27 : m_BackgroundValue(0), m_MarginFactor(1.0), m_TimeSelector(nullptr), m_OverrideCroppingRegion(false)
35 template <
typename TPixel,
unsigned int VImageDimension>
37 unsigned int timestep)
39 if (inputItkImage ==
nullptr)
42 "An internal error occurred. Can't convert Image. Please report to bugs@mitk.org");
43 MITK_ERROR <<
"image is nullptr...returning" << std::endl;
48 typedef typename InternalImageType::Pointer InternalImagePointer;
50 typedef itk::RegionOfInterestImageFilter<InternalImageType, InternalImageType> ROIFilterType;
51 typedef typename itk::RegionOfInterestImageFilter<InternalImageType, InternalImageType>::Pointer ROIFilterPointer;
53 InternalImagePointer outputItk = InternalImageType::New();
55 ROIFilterPointer roiFilter = ROIFilterType::New();
56 roiFilter->SetInput(0, inputItkImage);
59 outputItk = roiFilter->GetOutput();
60 outputItk->DisconnectPipeline();
64 MITK_INFO <<
"Crop-Output dimension: " << (newMitkImage->GetDimension() == 3)
65 <<
" Filter-Output dimension: " << this->
GetOutput()->GetDimension() <<
" Timestep: " << timestep;
76 if (input->GetDimension() <= 2)
78 MITK_ERROR <<
"Only 3D any 4D images are supported." << std::endl;
87 itkDebugMacro(<<
"GenerateOutputInformation()");
130 unsigned int dimension = input->GetDimension();
131 auto dimensions =
new unsigned int[dimension];
134 memcpy(dimensions + 3, input->GetDimensions() + 3, (dimension - 3) *
sizeof(
unsigned int));
142 itk::ScalableAffineTransform<mitk::ScalarType, 3>::Pointer cloneTransform =
143 itk::ScalableAffineTransform<mitk::ScalarType, 3>::New();
144 cloneTransform->Compose(input->GetGeometry()->GetIndexToWorldTransform());
145 output->GetGeometry()->SetIndexToWorldTransform(cloneTransform.GetPointer());
153 input->GetSlicedGeometry()->IndexToWorld(origin, origin);
158 dynamic_cast<mitk::PlaneGeometry *
>(inputGeometry->GetPlaneGeometry(0)->Clone().GetPointer());
170 plane->InitializeStandardPlane(dimX, dimY, right, down, &spacing);
172 plane->SetOrigin(origin);
177 plane, inputGeometry->GetSpacing()[2], output->GetSlicedGeometry()->GetSlices());
181 propTimeGeometry->
Initialize(slicedGeometry, output->GetDimension(3));
185 output->SetPropertyList(input->GetPropertyList()->Clone());
196 if (input->GetDimension() <= 2)
198 MITK_ERROR <<
"Only 3D and 4D images supported";
202 if ((output->IsInitialized() ==
false))
212 int tstart = outputRegion.GetIndex(3);
213 int tmax = tstart + outputRegion.GetSize(3);
215 for (
int timestep = tstart; timestep < tmax; ++timestep)
234 for (
unsigned int i = 0; i < 3; ++i)
239 if (
m_RegionIndex[i] >= static_cast<RegionType::IndexValueType>(inputMitk->GetDimension(i)))
241 itkExceptionMacro(
"Cropping index is not inside the image. " << std::endl
257 for (
unsigned int i = 0; i < 3; ++i)
266 unsigned int timeSteps = 1;
267 if (inputMitk->GetDimension() == 4)
268 timeSteps = inputMitk->GetDimension(3);
270 ImageType::IndexType minima, maxima;
272 if (inputMitk->GetDimension() == 4)
286 ImageType::RegionType origRegion = inputItk->GetLargestPossibleRegion();
289 maxima = inputItk->GetLargestPossibleRegion().GetIndex();
290 minima[0] = inputItk->GetLargestPossibleRegion().GetSize()[0];
291 minima[1] = inputItk->GetLargestPossibleRegion().GetSize()[1];
292 minima[2] = inputItk->GetLargestPossibleRegion().GetSize()[2];
294 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
296 for (
unsigned int idx = 0; idx < timeSteps; ++idx)
307 ConstIteratorType inIt(inputItk, origRegion);
309 for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
311 float pix_val = inIt.Get();
314 for (
int i = 0; i < 3; i++)
316 minima[i] = vnl_math_min((
int)minima[i], (
int)(inIt.GetIndex()[i]));
317 maxima[i] = vnl_math_max((
int)maxima[i], (
int)(inIt.GetIndex()[i]));
323 typedef ImageType::RegionType::SizeType::SizeValueType SizeValueType;
335 origRegion.Crop(cropRegion);
RegionType m_CroppingRegion
mitk::SlicedData::RegionType m_InputRequestedRegion
bool m_OverrideCroppingRegion
~AutoCropImageFilter() override
RegionType::SizeType m_RegionSize
virtual const PixelType GetOutputPixelType()
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
itk::TimeStamp m_TimeOfHeaderInitialization
RegionType::IndexType m_RegionIndex
void ComputeNewImageBounds()
virtual void InitializeEvenlySpaced(mitk::PlaneGeometry *geometry2D, unsigned int slices)
Completely initialize this instance as evenly-spaced with slices parallel to the provided PlaneGeomet...
itk::Size< RegionDimension > SizeType
void DisplayErrorText(const char *t)
void GenerateInputRequestedRegion() override
void GenerateOutputInformation() override
void SetCroppingRegion(RegionType overrideRegion)
itk::ImageRegion< RegionDimension > RegionType
void SetOrigin(const Point3D &origin)
Set the origin, i.e. the upper-left corner of the plane.
void GenerateData() override
Image class for storing images.
void vtk2itk(const Tin &in, Tout &out)
Describes the geometry of a data object consisting of slices.
static StatusBar * GetInstance()
static method to get the GUI dependent StatusBar-instance so the methods DisplayText, etc. can be called No reference counting, cause of decentral static use!
void itk2vtk(const Tin &in, Tout &out)
InputImageType * GetInput(void)
#define AccessFixedDimensionByItk_1(mitkImage, itkImageTypeFunction, dimension, arg1)
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::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.
MITKCORE_EXPORT const ScalarType eps
void ITKCrop3DImage(itk::Image< TPixel, VImageDimension > *inputItkImage, unsigned int timestep)
OutputType * GetOutput()
Get the output data of this image source object.
Describes a two-dimensional, rectangular plane.
itk::Image< mitk::ScalarType, 3 > InternalImageType
virtual RegionType GetCroppingRegion()
ImageReadAccessor class to get locked read access for a particular image part.
ImageType::Pointer ImagePointer
void Initialize() override
Initilizes a new object with one time steps which contains an empty geometry.
const void * GetData() const
Gives const access to the data.
mitk::ImageTimeSelector::Pointer m_TimeSelector
Class for defining the data type of pixels.
itk::ImageRegion< 3 > RegionType