27 #include "vtkMatrix4x4.h"
28 #include "vtkSmartPointer.h"
29 #include "vtkTransform.h"
31 #include "itkImageRegionIteratorWithIndex.h"
32 #include <itkImageIOBase.h>
33 #include <itkImageRegionConstIterator.h>
34 #include <itkRGBAPixel.h>
35 #include <itkRGBPixel.h>
40 : m_Geometry(nullptr),
42 m_UseCropTimeStepOnly(false),
44 m_UseWholeInputRegion(false),
48 this->SetNumberOfIndexedInputs(2);
49 this->SetNumberOfRequiredInputs(2);
54 template <
typename TPixel,
unsigned int VImageDimension>
57 MITK_INFO <<
"Scalar Pixeltype" << std::endl;
59 typedef TPixel TOutputPixel;
60 typedef itk::Image<TPixel, VImageDimension> ItkInputImageType;
61 typedef itk::Image<TOutputPixel, VImageDimension> ItkOutputImageType;
62 typedef typename itk::ImageBase<VImageDimension>::RegionType ItkRegionType;
63 typedef itk::ImageRegionIteratorWithIndex<ItkInputImageType> ItkInputImageIteratorType;
64 typedef itk::ImageRegionIteratorWithIndex<ItkOutputImageType> ItkOutputImageIteratorType;
70 if (this->m_Geometry.IsNull())
73 if (inputItkImage ==
nullptr)
76 "An internal error occurred. Can't convert Image. Please report to bugs@mitk.org");
77 std::cout <<
" image is NULL...returning" << std::endl;
82 typename ItkRegionType::IndexType::IndexValueType tmpIndex[3];
83 itk2vtk(this->m_InputRequestedRegion.GetIndex(), tmpIndex);
84 typename ItkRegionType::IndexType index;
85 index.SetIndex(tmpIndex);
88 typename ItkRegionType::SizeType::SizeValueType tmpSize[3];
89 itk2vtk(this->m_InputRequestedRegion.GetSize(), tmpSize);
90 typename ItkRegionType::SizeType size;
91 size.SetSize(tmpSize);
94 ItkRegionType inputRegionOfInterest(index, size);
99 outputimagetoitk->SetInput(this->m_OutputTimeSelector->GetOutput());
100 outputimagetoitk->Update();
104 ItkInputImageIteratorType inputIt(inputItkImage, inputRegionOfInterest);
105 ItkOutputImageIteratorType outputIt(outputItkImage, outputItkImage->GetLargestPossibleRegion());
114 vtkSmartPointer<vtkMatrix4x4> imageTransform = this->m_Geometry->GetGeometry()->GetVtkTransform()->GetMatrix();
115 Point3D center = this->m_Geometry->GetGeometry()->GetCenter();
117 translation->Translate(center[0] - imageTransform->GetElement(0, 3),
118 center[1] - imageTransform->GetElement(1, 3),
119 center[2] - imageTransform->GetElement(2, 3));
121 transform->SetMatrix(imageTransform);
122 transform->PostMultiply();
123 transform->Concatenate(translation);
127 for (
unsigned int i = 0; i < 3; ++i)
128 extent[i] = (this->m_Geometry->GetGeometry()->GetExtent(i));
130 for (inputIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd(); ++inputIt, ++outputIt)
132 vtk2itk(inputIt.GetIndex(), p);
140 transform->GetInverse()->TransformPoint(p2, p2);
142 bool isInside = (p2[0] >= (-extent[0] / 2.0)) && (p2[0] <= (extent[0] / 2.0)) && (p2[1] >= (-extent[1] / 2.0)) &&
143 (p2[1] <= (extent[1] / 2.0)) && (p2[2] >= (-extent[2] / 2.0)) && (p2[2] <= (extent[2] / 2.0));
145 if ((!this->m_UseCropTimeStepOnly && isInside) ||
146 (this->m_UseCropTimeStepOnly && timeStep == this->m_CurrentTimeStep && isInside))
148 outputIt.Set((TOutputPixel)inputIt.Value());
152 outputIt.Set(outsideValue);
161 this->ProcessObject::SetNthInput(1, const_cast<mitk::GeometryData *>(geometry));
173 if ((output->
IsInitialized() ==
false) || (m_Geometry.IsNull()) ||
174 (m_Geometry->GetTimeGeometry()->CountTimeSteps() == 0))
184 if ((output->IsInitialized()) && (output->GetPipelineMTime() <= m_TimeOfHeaderInitialization.GetMTime()))
191 mitkThrow() <<
"Input is not a mitk::Image";
193 itkDebugMacro(<<
"GenerateOutputInformation()");
195 unsigned int dimension = input->GetDimension();
198 mitkThrow() <<
"ImageCropper cannot handle 1D or 2D Objects.";
201 if ((m_Geometry.IsNull()) || (m_Geometry->GetTimeGeometry()->CountTimeSteps() == 0))
212 m_InputRequestedRegion = input->GetLargestPossibleRegion();
219 for (
unsigned int i = 0; i < dimension; i++)
221 index[i] = (mitk::SlicedData::IndexType::IndexValueType)(std::ceil(min[i]));
222 size[i] = (mitk::SlicedData::SizeType::SizeValueType)(std::ceil(max[i]) - index[i]);
226 if (m_UseWholeInputRegion ==
false)
229 if (m_InputRequestedRegion.Crop(bsRegion) ==
false)
233 m_InputRequestedRegion.SetSize(size);
234 bsRegion.SetSize(size);
235 mitkThrow() <<
"No overlap of the image and the cropping object.";
240 auto dimensions =
new unsigned int[dimension];
242 memcpy(dimensions + 3, input->GetDimensions() + 3, (dimension - 3) *
sizeof(
unsigned int));
246 itk2vtk(m_InputRequestedRegion.GetSize(), dimensions);
252 mitk::BoundingShapeCropper::RegionType outputRegion = output->GetRequestedRegion();
254 m_TimeOfHeaderInitialization.Modified();
262 MITK_ERROR <<
"Filter cannot handle dimensions less than 2 and greater than 4" << std::endl;
263 itkExceptionMacro(
"Filter cannot handle dimensions less than 2 and greater than 4");
272 MITK_INFO <<
"Generate Data" << std::endl;
279 if ((output->IsInitialized() ==
false) || (m_Geometry.IsNull()) ||
280 (m_Geometry->GetTimeGeometry()->CountTimeSteps() == 0))
283 m_InputTimeSelector->SetInput(input);
284 m_OutputTimeSelector->SetInput(this->
GetOutput());
286 mitk::BoundingShapeCropper::RegionType outputRegion = output->GetRequestedRegion();
291 int tstart = outputRegion.GetIndex(3);
292 int tmax = tstart + outputRegion.GetSize(3);
294 if (this->m_UseCropTimeStepOnly)
298 indexToWorldTransform->SetParameters(input->GetSlicedGeometry(tstart)->GetIndexToWorldTransform()->GetParameters());
305 m_InputTimeSelector->SetTimeNr(m_CurrentTimeStep);
306 m_InputTimeSelector->UpdateLargestPossibleRegion();
307 m_OutputTimeSelector->SetTimeNr(tstart);
308 m_OutputTimeSelector->UpdateLargestPossibleRegion();
309 ComputeData(m_InputTimeSelector->GetOutput(), m_CurrentTimeStep);
314 for (t = tstart; t < tmax; ++t)
318 indexToWorldTransform->SetParameters(input->GetSlicedGeometry(t)->GetIndexToWorldTransform()->GetParameters());
325 m_InputTimeSelector->SetTimeNr(t);
326 m_InputTimeSelector->UpdateLargestPossibleRegion();
327 m_OutputTimeSelector->SetTimeNr(t);
328 m_OutputTimeSelector->UpdateLargestPossibleRegion();
332 m_InputTimeSelector->SetInput(
nullptr);
333 m_OutputTimeSelector->SetInput(
nullptr);
334 m_TimeOfHeaderInitialization.Modified();
itk::SmartPointer< Self > Pointer
virtual ScalarType GetOutsideValue()
void SetIndexToWorldTransform(mitk::AffineTransform3D *transform)
virtual void GenerateOutputInformation() override
DataCollection - Class to facilitate loading/accessing structured data.
virtual void GenerateData() override
A version of GenerateData() specific for image processing filters.
#define AccessByItk_1(mitkImage, itkImageTypeFunction, arg1)
itk::Size< RegionDimension > SizeType
void DisplayErrorText(const char *t)
virtual bool GetUseCropTimeStepOnly()
itk::ImageRegion< RegionDimension > RegionType
void SetOrigin(const Point3D &origin)
Set the origin, i.e. the upper-left corner of the plane.
Image class for storing images.
void CutImage(itk::Image< TPixel, VImageDimension > *inputItkImage, int timeStep)
Template Function for cropping and masking images with scalar pixel type.
Provides access to a volume at a specific time of the input image.
void vtk2itk(const Tin &in, Tout &out)
virtual void ComputeData(mitk::Image *input3D, int boTimeStep)
Process the image and create the output.
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
Data class only having a BaseGeometry but not containing any specific data.
virtual void GenerateInputRequestedRegion() override
Reimplemented from ImageToImageFilter.
virtual ~BoundingShapeCropper()
mitk::BoundingBox::Pointer CalculateBoundingBoxRelativeToTransform(const mitk::AffineTransform3D *transform) const
Calculates a bounding-box around the geometry relative to a coordinate system defined by a transform...
Describes the geometry of a data object consisting of slices.
void GenerateTimeInInputRegion(const mitk::TimeGeometry *outputTimeGeometry, const TOutputRegion &outputRegion, const mitk::TimeGeometry *inputTimeGeometry, TInputRegion &inputRegion)
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)
void IndexToWorld(const mitk::Vector3D &vec_units, mitk::Vector3D &vec_mm) const
Convert (continuous or discrete) index coordinates of a vector vec_units to world coordinates (in mm)...
void SetGeometry(const mitk::GeometryData *geometry)
Set geometry of the bounding object.
virtual const PixelType GetOutputPixelType()
virtual bool IsInitialized() const
Check whether the data has been initialized, i.e., at least the Geometry and other header data has be...
OutputType * GetOutput()
Get the output data of this image source object.
unsigned int GetDimension() const
Get dimension of the image.
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
BaseGeometry Describes the geometry of a data object.
Class for defining the data type of pixels.
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.