19 #include <itkAntiAliasBinaryImageFilter.h> 20 #include <itkAutoCropLabelMapFilter.h> 21 #include <itkBinaryThresholdImageFilter.h> 22 #include <itkLabelImageToLabelMapFilter.h> 23 #include <itkLabelMap.h> 24 #include <itkLabelMapToLabelImageFilter.h> 25 #include <itkLabelObject.h> 26 #include <itkNumericTraits.h> 27 #include <itkSmoothingRecursiveGaussianImageFilter.h> 30 #include <vtkCleanPolyData.h> 31 #include <vtkImageChangeInformation.h> 32 #include <vtkImageData.h> 33 #include <vtkLinearTransform.h> 34 #include <vtkMarchingCubes.h> 35 #include <vtkSmartPointer.h> 38 : m_GenerateAllLabels(false), m_RequestedLabel(1), m_BackgroundLabel(0), m_UseSmoothing(0), m_Sigma(0.1)
49 this->ProcessObject::SetNthInput(0, const_cast<mitk::Image *>(image));
59 if (this->GetNumberOfInputs() < 1)
64 return static_cast<const mitk::Image *
>(this->ProcessObject::GetInput(0));
69 itkDebugMacro(<<
"GenerateOutputInformation()");
75 if (inputImage.IsNull())
85 template <
typename TPixel,
unsigned int VDimension>
89 typedef itk::Image<TPixel, VDimension>
ImageType;
91 typedef itk::BinaryThresholdImageFilter<ImageType, ImageType> BinaryThresholdFilterType;
92 typedef itk::LabelObject<TPixel, VDimension> LabelObjectType;
94 typedef itk::LabelImageToLabelMapFilter<ImageType, LabelMapType> Image2LabelMapType;
95 typedef itk::AutoCropLabelMapFilter<LabelMapType> AutoCropType;
96 typedef itk::LabelMapToLabelImageFilter<LabelMapType, ImageType> LabelMap2ImageType;
98 typedef itk::Image<float, VDimension> RealImageType;
100 typedef itk::AntiAliasBinaryImageFilter<ImageType, RealImageType> AntiAliasFilterType;
101 typedef itk::SmoothingRecursiveGaussianImageFilter<RealImageType, RealImageType> GaussianFilterType;
103 typename BinaryThresholdFilterType::Pointer thresholdFilter = BinaryThresholdFilterType::New();
104 thresholdFilter->SetInput(input);
107 thresholdFilter->SetOutsideValue(0);
108 thresholdFilter->SetInsideValue(1);
110 thresholdFilter->Update();
112 typename Image2LabelMapType::Pointer image2label = Image2LabelMapType::New();
113 image2label->SetInput(thresholdFilter->GetOutput());
115 typename AutoCropType::SizeType border;
120 typename AutoCropType::Pointer autoCropFilter = AutoCropType::New();
121 autoCropFilter->SetInput(image2label->GetOutput());
122 autoCropFilter->SetCropBorder(border);
123 autoCropFilter->InPlaceOn();
125 typename LabelMap2ImageType::Pointer label2image = LabelMap2ImageType::New();
126 label2image->SetInput(autoCropFilter->GetOutput());
128 label2image->Update();
130 typename AntiAliasFilterType::Pointer antiAliasFilter = AntiAliasFilterType::New();
131 antiAliasFilter->SetInput(label2image->GetOutput());
132 antiAliasFilter->SetMaximumRMSError(0.001);
133 antiAliasFilter->SetNumberOfLayers(3);
134 antiAliasFilter->SetUseImageSpacing(
false);
135 antiAliasFilter->SetNumberOfIterations(40);
137 antiAliasFilter->Update();
139 typename RealImageType::Pointer result;
143 typename GaussianFilterType::Pointer gaussianFilter = GaussianFilterType::New();
144 gaussianFilter->SetSigma(
m_Sigma);
145 gaussianFilter->SetInput(antiAliasFilter->GetOutput());
146 gaussianFilter->Update();
147 result = gaussianFilter->GetOutput();
151 result = antiAliasFilter->GetOutput();
154 result->DisconnectPipeline();
156 typename ImageType::RegionType cropRegion;
157 cropRegion = autoCropFilter->GetOutput()->GetLargestPossibleRegion();
159 const typename ImageType::IndexType &cropIndex = cropRegion.GetIndex();
172 vtkSmartPointer<vtkImageChangeInformation> indexCoordinatesImageFilter =
173 vtkSmartPointer<vtkImageChangeInformation>::New();
174 indexCoordinatesImageFilter->SetInputData(vtkimage);
175 indexCoordinatesImageFilter->SetOutputOrigin(0.0, 0.0, 0.0);
177 vtkSmartPointer<vtkMarchingCubes> marching = vtkSmartPointer<vtkMarchingCubes>::New();
178 marching->ComputeScalarsOff();
179 marching->ComputeNormalsOn();
180 marching->ComputeGradientsOn();
181 marching->SetInputConnection(indexCoordinatesImageFilter->GetOutputPort());
182 marching->SetValue(0, 0.0);
186 vtkPolyData *polydata = marching->GetOutput();
188 if ((!polydata) || (!polydata->GetNumberOfPoints()))
189 throw itk::ExceptionObject(__FILE__, __LINE__,
"marching cubes has failed.");
193 vtkPoints *points = polydata->GetPoints();
194 vtkMatrix4x4 *vtkmatrix = vtkMatrix4x4::New();
196 double(*matrix)[4] = vtkmatrix->Element;
198 for (
int i = 0; i < 3; ++i)
199 for (
int j = 0; j < 3; ++j)
200 matrix[i][j] /= spacing[j];
202 unsigned int n = points->GetNumberOfPoints();
205 for (
unsigned int i = 0; i < n; i++)
207 points->GetPoint(i, point);
209 points->SetPoint(i, point);
213 vtkSmartPointer<vtkCleanPolyData> cleanPolyDataFilter = vtkSmartPointer<vtkCleanPolyData>::New();
214 cleanPolyDataFilter->SetInputData(polydata);
215 cleanPolyDataFilter->PieceInvariantOff();
216 cleanPolyDataFilter->ConvertLinesToPointsOff();
217 cleanPolyDataFilter->ConvertPolysToLinesOff();
218 cleanPolyDataFilter->ConvertStripsToPolysOff();
219 cleanPolyDataFilter->PointMergingOn();
220 cleanPolyDataFilter->Update();
223 output->SetVtkPolyData(cleanPolyDataFilter->GetOutput(), 0);
const mitk::Image * GetInput(void)
Class for storing surfaces (vtkPolyData).
~LabelSetImageToSurfaceFilter() override
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)...
itk::Image< unsigned char, 3 > ImageType
vtkLinearTransform * GetVtkTransform() const
Get the m_IndexToWorldTransform as a vtkLinearTransform.
void InternalProcessing(const itk::Image< TPixel, VImageDimension > *input, mitk::Surface *surface)
void GenerateData() override
std::map< LabelType, unsigned long > LabelMapType
void mitkVtkLinearTransformPoint(T1 matrix[4][4], T2 in[3], T3 out[3])
void SetOrigin(const Point3D &origin)
Set the origin, i.e. the upper-left corner of the plane.
Image class for storing images.
void vtk2itk(const Tin &in, Tout &out)
LabelSetImageToSurfaceFilter()
mitk::Image::Pointer m_ResultImage
mitk::Image::Pointer image
void GenerateOutputInformation() override
#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.
const mitk::Vector3D GetSpacing() const
Get the spacing (size of a pixel).
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
virtual void SetInput(const mitk::Image *image)
BaseGeometry Describes the geometry of a data object.