18 #include <vtkImageStencil.h> 19 #include <vtkPointData.h> 20 #include <vtkPolyData.h> 21 #include <vtkPolyDataToImageStencil.h> 34 if (
nullptr == slice ||
nullptr == contourIn3D)
38 projectedContour->Initialize(*contourIn3D);
41 auto numberOfTimesteps =
static_cast<int>(contourIn3D->
GetTimeSteps());
43 for (decltype(numberOfTimesteps) t = 0; t < numberOfTimesteps; ++t)
45 auto iter = contourIn3D->
Begin(t);
46 auto end = contourIn3D->
End(t);
50 const auto ¤tPointIn3D = (*iter)->Coordinates;
53 projectedPointIn2D.Fill(0.0);
55 sliceGeometry->WorldToIndex(currentPointIn3D, projectedPointIn2D);
57 projectedContour->AddVertex(projectedPointIn2D, t);
62 return projectedContour;
68 if (
nullptr == sliceGeometry ||
nullptr == contourIn2D)
72 worldContour->Initialize(*contourIn2D);
74 auto numberOfTimesteps =
static_cast<int>(contourIn2D->
GetTimeSteps());
76 for (decltype(numberOfTimesteps) t = 0; t < numberOfTimesteps; ++t)
78 auto iter = contourIn2D->
Begin(t);
79 auto end = contourIn2D->
End(t);
83 const auto ¤tPointIn2D = (*iter)->Coordinates;
86 worldPointIn3D.Fill(0.0);
88 sliceGeometry->
IndexToWorld(currentPointIn2D, worldPointIn3D);
90 worldContour->AddVertex(worldPointIn3D, t);
101 FillContourInSlice(projectedContour, 0, sliceImage, workingImage, paintingPixelValue);
108 contourModelFilter->SetInput(projectedContour);
109 contourModelFilter->Update();
112 surface = contourModelFilter->GetOutput();
114 if (
nullptr == surface->GetVtkPolyData(t))
116 MITK_WARN <<
"Could not create surface from contour model.";
120 auto surface2D = vtkSmartPointer<vtkPolyData>::New();
121 surface2D->SetPoints(surface->GetVtkPolyData(t)->GetPoints());
122 surface2D->SetLines(surface->GetVtkPolyData(t)->GetLines());
124 auto image = vtkSmartPointer<vtkImageData>::New();
127 const double FOREGROUND_VALUE = 255.0;
128 const double BACKGROUND_VALUE = 0.0;
130 vtkIdType count =
image->GetNumberOfPoints();
131 for (decltype(count) i = 0; i < count; ++i)
132 image->GetPointData()->GetScalars()->SetTuple1(i, FOREGROUND_VALUE);
134 auto polyDataToImageStencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
137 polyDataToImageStencil->SetTolerance(
mitk::eps);
138 polyDataToImageStencil->SetInputData(surface2D);
139 polyDataToImageStencil->Update();
141 auto imageStencil = vtkSmartPointer<vtkImageStencil>::New();
143 imageStencil->SetInputData(
image);
144 imageStencil->SetStencilConnection(polyDataToImageStencil->GetOutputPort());
145 imageStencil->ReverseStencilOff();
146 imageStencil->SetBackgroundValue(BACKGROUND_VALUE);
147 imageStencil->Update();
149 vtkSmartPointer<vtkImageData> filledImage = imageStencil->GetOutput();
150 vtkSmartPointer<vtkImageData> resultImage = sliceImage->
GetVtkImageData();
151 FillSliceInSlice(filledImage, resultImage, workingImage, paintingPixelValue);
153 sliceImage->
SetVolume(resultImage->GetScalarPointer());
157 vtkSmartPointer<vtkImageData> filledImage, vtkSmartPointer<vtkImageData> resultImage,
mitk::Image::Pointer image,
int paintingPixelValue)
159 auto labelImage =
dynamic_cast<LabelSetImage *
>(image.GetPointer());
160 auto numberOfPoints = filledImage->GetNumberOfPoints();
162 if (
nullptr == labelImage)
164 for (decltype(numberOfPoints) i = 0; i < numberOfPoints; ++i)
166 if (1 < filledImage->GetPointData()->GetScalars()->GetTuple1(i))
167 resultImage->GetPointData()->GetScalars()->SetTuple1(i, paintingPixelValue);
174 if (paintingPixelValue != backgroundValue)
176 for (decltype(numberOfPoints) i = 0; i < numberOfPoints; ++i)
178 if (1 < filledImage->GetPointData()->GetScalars()->GetTuple1(i))
180 auto existingValue = resultImage->GetPointData()->GetScalars()->GetTuple1(i);
182 if (!labelImage->GetLabel(existingValue, labelImage->GetActiveLayer())->GetLocked())
183 resultImage->GetPointData()->GetScalars()->SetTuple1(i, paintingPixelValue);
189 auto activePixelValue = labelImage->GetActiveLabel(labelImage->GetActiveLayer())->GetValue();
191 for (decltype(numberOfPoints) i = 0; i < numberOfPoints; ++i)
193 if (1 < filledImage->GetPointData()->GetScalars()->GetTuple1(i))
195 if (resultImage->GetPointData()->GetScalars()->GetTuple1(i) == activePixelValue)
196 resultImage->GetPointData()->GetScalars()->SetTuple1(i, paintingPixelValue);
205 if (
nullptr == contour)
209 resultContour->Expand(t + 1);
212 resultContour->AddVertex(vertex, t);
215 return resultContour;
static ContourModel::Pointer BackProjectContourFrom2DSlice(const BaseGeometry *sliceGeometry, ContourModel *contourIn2D, bool correctionForIpSegmentation=false)
Projects a slice index coordinates of a contour back into world coordinates.
ContourModel is a structure of linked vertices defining a contour in 3D space. The vertices are store...
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)...
static void FillContourInSlice(ContourModel *projectedContour, Image *sliceImage, mitk::Image::Pointer workingImage, int paintingPixelValue=1)
Fill a contour in a 2D slice with a specified pixel value at time step 0.
virtual vtkImageData * GetVtkImageData(int t=0, int n=0)
Get a volume at a specific time t of channel n as a vtkImageData.
mitk::Label * GetExteriorLabel()
Gets the mitk::Label which is used as default exterior label.
VertexIterator Begin(int timestep=0) const
Returns a const VertexIterator at the start element of the contour.
virtual bool SetVolume(const void *data, int t=0, int n=0)
Set data as volume at time t in channel n. It is in the responsibility of the caller to ensure that t...
Image class for storing images.
static ContourModel::Pointer ProjectContourTo2DSlice(Image *slice, ContourModel *contourIn3D, bool correctionForIpSegmentation, bool constrainToInside)
Projects a contour onto an image point by point. Converts from world to index coordinates.
mitk::Image::Pointer image
PixelType GetValue() const
VertexIterator End(int timestep=0) const
Returns a const VertexIterator at the end element of the contour.
LabelSetImage class for handling labels and layers in a segmentation session.
MITKCORE_EXPORT const ScalarType eps
unsigned int GetTimeSteps() const
Get the number of time steps from the TimeGeometry As the base data has not a data vector given by it...
static void FillSliceInSlice(vtkSmartPointer< vtkImageData > filledImage, vtkSmartPointer< vtkImageData > resultImage, mitk::Image::Pointer image, int paintingPixelValue)
Fills a image (filledImage) into another image (resultImage) by incorporating the rules of LabelSet-I...
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
static ContourModel::Pointer MoveZerothContourTimeStep(const ContourModel *contour, unsigned int timeStep)
Move the contour in time step 0 to to a new contour model at the given time step. ...
Represents a single vertex of contour.
BaseGeometry Describes the geometry of a data object.
~ContourModelUtils() override