21 #include "itkCastImageFilter.h"
27 #include "vtkImageData.h"
28 #include "vtkImageLogic.h"
29 #include "vtkImageStencil.h"
30 #include "vtkPointData.h"
31 #include "vtkPolyData.h"
32 #include "vtkPolyDataToImageStencil.h"
33 #include "vtkSmartPointer.h"
46 Image *slice,
ContourModel *contourIn3D,
bool itkNotUsed(correctionForIpSegmentation),
bool constrainToInside)
48 if (!slice || !contourIn3D)
52 projectedContour->Initialize(*contourIn3D);
56 int numberOfTimesteps = contourIn3D->
GetTimeGeometry()->CountTimeSteps();
58 for (
int currentTimestep = 0; currentTimestep < numberOfTimesteps; currentTimestep++)
60 auto iter = contourIn3D->
Begin(currentTimestep);
61 auto end = contourIn3D->
End(currentTimestep);
65 Point3D currentPointIn3D = (*iter)->Coordinates;
68 projectedPointIn2D.Fill(0.0);
69 sliceGeometry->
WorldToIndex(currentPointIn3D, projectedPointIn2D);
72 if (!sliceGeometry->
IsIndexInside(projectedPointIn2D) && constrainToInside)
74 MITK_DEBUG <<
"**" << currentPointIn3D <<
" is " << projectedPointIn2D <<
" --> correct it (TODO)" << std::endl;
77 projectedContour->AddVertex(projectedPointIn2D, currentTimestep);
82 return projectedContour;
88 if (!sliceGeometry || !contourIn2D)
92 worldContour->Initialize(*contourIn2D);
94 int numberOfTimesteps = contourIn2D->
GetTimeGeometry()->CountTimeSteps();
96 for (
int currentTimestep = 0; currentTimestep < numberOfTimesteps; currentTimestep++)
98 auto iter = contourIn2D->
Begin(currentTimestep);
99 auto end = contourIn2D->
End(currentTimestep);
103 Point3D currentPointIn2D = (*iter)->Coordinates;
106 worldPointIn3D.Fill(0.0);
107 sliceGeometry->
IndexToWorld(currentPointIn2D, worldPointIn3D);
110 worldContour->AddVertex(worldPointIn3D, currentTimestep);
121 int paintingPixelValue)
127 unsigned int timeStep,
130 int paintingPixelValue)
135 contourModelFilter->SetInput(projectedContour);
136 contourModelFilter->Update();
137 surface = contourModelFilter->GetOutput();
141 if (surface->GetVtkPolyData(timeStep) ==
nullptr)
143 MITK_WARN <<
"No surface has been created from contour model. Add more points to fill contour in slice.";
146 surface2D->SetPoints(surface->GetVtkPolyData(timeStep)->GetPoints());
147 surface2D->SetLines(surface->GetVtkPolyData(timeStep)->GetLines());
148 surface2D->Modified();
156 unsigned char inval = 255;
157 unsigned char outval = 0;
158 vtkIdType count = whiteImage->GetNumberOfPoints();
159 for (vtkIdType i = 0; i < count; ++i)
161 whiteImage->GetPointData()->GetScalars()->SetTuple1(i, inval);
168 pol2stenc->SetInputData(surface2D);
174 imgstenc->SetInputData(whiteImage);
175 imgstenc->SetStencilConnection(pol2stenc->GetOutputPort());
176 imgstenc->ReverseStencilOff();
177 imgstenc->SetBackgroundValue(outval);
181 vtkSmartPointer<vtkImageData> filledImage = imgstenc->GetOutput();
182 vtkSmartPointer<vtkImageData> resultImage = sliceImage->
GetVtkImageData();
183 FillSliceInSlice(filledImage, resultImage, workingImage, paintingPixelValue);
185 sliceImage->
SetVolume(resultImage->GetScalarPointer());
189 vtkSmartPointer<vtkImageData> resultImage,
191 int paintingPixelValue)
194 labelImage =
dynamic_cast<LabelSetImage *
>(image.GetPointer());
195 int count = filledImage->GetNumberOfPoints();
200 for (vtkIdType i = 0; i < count; ++i)
202 if (filledImage->GetPointData()->GetScalars()->GetTuple1(i) > 1)
204 resultImage->GetPointData()->GetScalars()->SetTuple1(i, paintingPixelValue);
215 if (paintingPixelValue != backgroundValue)
217 for (vtkIdType i = 0; i < count; ++i)
219 if (filledImage->GetPointData()->GetScalars()->GetTuple1(i) > 1)
221 auto existingValue = resultImage->GetPointData()->GetScalars()->GetTuple1(i);
224 resultImage->GetPointData()->GetScalars()->SetTuple1(i, paintingPixelValue);
235 for (vtkIdType i = 0; i < count; ++i)
237 if (filledImage->GetPointData()->GetScalars()->GetTuple1(i) > 1)
239 if (resultImage->GetPointData()->GetScalars()->GetTuple1(i) == activePixelValue)
241 resultImage->GetPointData()->GetScalars()->SetTuple1(i, paintingPixelValue);
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...
VertexIterator End(int timestep=0) const
Returns a const VertexIterator at the end element of the contour.
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.
bool IsIndexInside(const mitk::Point3D &index) const
Test whether the point p ((continous!)index coordinates in units) is inside the bounding box...
mitk::Label * GetLabel(PixelType pixelValue, unsigned int layer=0) const
Returns the mitk::Label with the given pixelValue and for the given layer.
virtual vtkImageData * GetVtkImageData(int t=0, int n=0)
Get a volume at a specific time t of channel n as a vtkImageData.
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
mitk::Label * GetExteriorLabel()
Gets the mitk::Label which is used as default exterior label.
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::Label * GetActiveLabel(unsigned int layer=0)
Returns the active label of a specific layer.
virtual ~ContourModelUtils()
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)...
LabelSetImage class for handling labels and layers in a segmentation session.
VertexIterator Begin(int timestep=0) const
Returns a const VertexIterator at the start element of the contour.
MITKCORE_EXPORT const ScalarType eps
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...
unsigned int GetActiveLayer() const
Gets the ID of the currently active layer.
PixelType GetValue() const
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.
void WorldToIndex(const mitk::Point3D &pt_mm, mitk::Point3D &pt_units) const
Convert world coordinates (in mm) of a point to (continuous!) index coordinates.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.