19 #include <itkCastImageFilter.h>
20 #include <itkGradientMagnitudeImageFilter.h>
21 #include <itkImageRegionIterator.h>
28 this->SetNumberOfRequiredInputs(1);
29 this->SetNumberOfIndexedOutputs(1);
30 this->SetNthOutput(0, output.GetPointer());
44 return Superclass::GetOutput();
49 this->SetInput(0, input);
55 if (idx + 1 > this->GetNumberOfInputs())
57 this->SetNumberOfRequiredInputs(idx + 1);
59 if (input != static_cast<InputType *>(this->ProcessObject::GetInput(idx)))
61 this->ProcessObject::SetNthInput(idx, const_cast<InputType *>(input));
70 if (this->GetNumberOfInputs() < 1)
78 if (this->GetNumberOfInputs() < 1)
90 itkExceptionMacro(
"mitk::ImageToLiveWireContourFilter: No input available. Please set the input!");
94 if (input->GetDimension() != 2)
96 MITK_ERROR <<
"Filter is only working on 2D images.";
97 itkExceptionMacro(
"mitk::ImageToLiveWireContourFilter: Filter is only working on 2D images.. Please make sure that "
102 input->GetGeometry()->WorldToIndex(m_StartPoint, m_StartPointInIndex);
103 input->GetGeometry()->WorldToIndex(m_EndPoint, m_EndPointInIndex);
106 if (input->GetGeometry()->IsIndexInside(this->m_StartPointInIndex) &&
107 input->GetGeometry()->IsIndexInside(this->m_EndPointInIndex))
111 this->UpdateLiveWire();
113 catch (itk::ExceptionObject &e)
115 MITK_INFO <<
"Exception caught during live wiring calculation: " << e;
121 template <
typename TPixel,
unsigned int VImageDimension>
125 typedef itk::CastImageFilter<InputImageType, InternalImageType> CastFilterType;
128 castFilter->SetInput(inputImage);
129 castFilter->Update();
130 m_InternalImage = castFilter->GetOutput();
131 m_CostFunction->SetImage(m_InternalImage);
132 m_ShortestPathFilter->SetInput(m_InternalImage);
137 m_CostFunction->ClearRepulsivePoints();
142 m_CostFunction->AddRepulsivePoint(idx);
148 mask->InitializeByItk(this->m_CostFunction->GetMaskImage());
149 mask->SetVolume(this->m_CostFunction->GetMaskImage()->GetBufferPointer());
155 m_CostFunction->RemoveRepulsivePoint(idx);
160 m_CostFunction->ClearRepulsivePoints();
162 ShortestPathType::const_iterator iter = points.begin();
163 for (; iter != points.end(); iter++)
165 m_CostFunction->AddRepulsivePoint((*iter));
172 InternalImageType::IndexType startPoint, endPoint;
174 startPoint[0] = m_StartPointInIndex[0];
175 startPoint[1] = m_StartPointInIndex[1];
177 endPoint[0] = m_EndPointInIndex[0];
178 endPoint[1] = m_EndPointInIndex[1];
181 InternalImageType::IndexType startRegion;
182 startRegion[0] = startPoint[0] < endPoint[0] ? startPoint[0] : endPoint[0];
183 startRegion[1] = startPoint[1] < endPoint[1] ? startPoint[1] : endPoint[1];
186 InternalImageType::SizeType size;
187 size[0] = abs(startPoint[0] - endPoint[0]) + 1;
188 size[1] = abs(startPoint[1] - endPoint[1]) + 1;
191 region.SetSize(size);
192 region.SetIndex(startRegion);
198 m_CostFunction->SetStartIndex(startPoint);
199 m_CostFunction->SetEndIndex(endPoint);
200 m_CostFunction->SetRequestedRegion(region);
201 m_CostFunction->SetUseCostMap(m_UseDynamicCostMap);
204 m_ShortestPathFilter->SetFullNeighborsMode(
true);
206 m_ShortestPathFilter->SetMakeOutputImage(
false);
209 m_ShortestPathFilter->SetStartIndex(startPoint);
210 m_ShortestPathFilter->SetEndIndex(endPoint);
212 m_ShortestPathFilter->Update();
220 this->SetNthOutput(0, output.GetPointer());
223 output->Expand(m_TimeStep + 1);
229 ShortestPathType::const_iterator pathIterator = shortestPath.begin();
231 while (pathIterator != shortestPath.end())
236 currentPoint[2] = 0.0;
238 input->GetGeometry()->IndexToWorld(currentPoint, currentPoint);
239 output->AddVertex(currentPoint,
false, m_TimeStep);
255 catch (itk::ExceptionObject &e)
257 MITK_INFO <<
"Exception caught during dynamic cost map alculation: " << e;
264 template <
typename TPixel,
unsigned int VImageDimension>
278 std::vector<itk::Index<VImageDimension>> shortestPath;
285 while (it != output->IteratorEnd())
289 input->GetGeometry()->WorldToIndex(c, c);
293 shortestPath.push_back(cur);
304 input->GetGeometry()->WorldToIndex(c, c);
308 shortestPath.push_back(cur);
314 typedef itk::GradientMagnitudeImageFilter<itk::Image<TPixel, VImageDimension>, itk::Image<TPixel, VImageDimension>>
315 GradientMagnitudeFilterType;
317 gradientFilter->SetInput(inputImage);
318 gradientFilter->Update();
324 typename std::vector<itk::Index<VImageDimension>>::iterator pathIterator = shortestPath.begin();
326 std::map<int, int> histogram;
329 while (pathIterator != shortestPath.end())
333 histogram[
static_cast<int>(gradientMagnImage->GetPixel((*pathIterator)) *
341 if (!histogram.empty())
343 std::map<int, int>::iterator itMAX;
346 int currentMaxValue = 0;
347 std::map<int, int>::iterator it = histogram.begin();
348 while (it != histogram.end())
350 if ((*it).second > currentMaxValue)
353 currentMaxValue = (*it).second;
358 std::map<int, int>::key_type keyOfMax = itMAX->first;
361 std::map<int, int>::iterator end = histogram.end();
362 std::map<int, int>::iterator last = --(histogram.end());
364 std::map<int, int>::iterator left2;
365 std::map<int, int>::iterator left1;
366 std::map<int, int>::iterator right1;
367 std::map<int, int>::iterator right2;
371 if (right1 == end || right1 == last)
377 std::map<int, int>::iterator temp = right1;
382 if (right1 == histogram.begin())
387 else if (right1 == (++(histogram.begin())))
389 std::map<int, int>::iterator temp = right1;
396 std::map<int, int>::iterator temp = right1;
402 double partRight1, partRight2, partLeft1, partLeft2;
403 partRight1 = partRight2 = partLeft1 = partLeft2 = 0.0;
435 max = (partRight1 + partRight2 + partLeft1 + partLeft2);
438 this->m_CostFunction->SetDynamicCostMap(histogram);
439 this->m_CostFunction->SetCostMapMaximum(max);
ImageLiveWireContourModelFilter()
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
ContourModel is a structure of linked vertices defining a contour in 3D space. The vertices are store...
itk::SmartPointer< Self > Pointer
const InputType * GetInput(void)
itk::ImageRegion< 2 > RegionType
std::vector< itk::Index< 2 > > ShortestPathType
void ClearRepulsivePoints()
Clear all repulsive points used in the cost function.
#define AccessFixedDimensionByItk(mitkImage, itkImageTypeFunction, dimension)
Access a mitk-image with known dimension by an itk-image.
virtual ~ImageLiveWireContourModelFilter()
CostFunctionType::Pointer m_CostFunction
The cost function to compute costs between two pixels.
void CreateDynamicCostMapByITK(const itk::Image< TPixel, VImageDimension > *inputImage, mitk::ContourModel *path=NULL)
mitk::ContourElement::VertexIterator VertexIterator
void AddRepulsivePoint(const itk::Index< 2 > &idx)
Add a single repulsive point to the cost function.
void SetRepulsivePoints(const ShortestPathType &points)
Set a vector with repulsive points to use in the cost function.
virtual OutputType * GetOutput()
VertexIterator IteratorBegin(int timestep=0) const
Returns a const VertexIterator at the start element of the contour.
static double Gaussian(double x, double xOfGaussian, double yOfGaussian)
Returns the y value of gaussian with given offset and amplitude.
bool CreateDynamicCostMap(mitk::ContourModel *path=NULL)
Create dynamic cost tranfer map - on the fly training.
virtual mitkBaseDataSourceGetOutputDeclarations itk::DataObject::Pointer MakeOutput(DataObjectPointerArraySizeType idx) override
itk::Image< double, 3 > InputImageType
ShortestPathImageFilterType::Pointer m_ShortestPathFilter
Shortest path filter according to cost function m_CostFunction.
void GenerateData() override
Image class for storing images.
bool m_UseDynamicCostMap
Flag to use a dynmic cost map or not.
virtual void DumpMaskImage()
#define AccessFixedDimensionByItk_1(mitkImage, itkImageTypeFunction, dimension, arg1)
void ItkPreProcessImage(const itk::Image< TPixel, VImageDimension > *inputImage)
virtual void SetInput(const InputType *input)
VertexIterator IteratorEnd(int timestep=0) const
Returns a const VertexIterator at the end element of the contour.
void RemoveRepulsivePoint(const itk::Index< 2 > &idx)
Remove a single repulsive point from the cost function.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.