27 #include <itkCommand.h>
29 #include <itkImageSliceConstIteratorWithIndex.h>
49 : m_WorkingImage(NULL), m_ReferenceImage(NULL)
56 for (InterpolatorMapType::iterator iter = s_InterpolatorForImage.begin(); iter != s_InterpolatorForImage.end();
59 if (iter->second ==
this)
61 s_InterpolatorForImage.erase(iter);
69 m_LabelCountInSlice.clear();
70 int numberOfLabels = m_WorkingImage->GetNumberOfLabels();
71 m_LabelCountInSlice.resize(m_WorkingImage->GetTimeSteps());
73 for (
unsigned int timeStep = 0; timeStep < m_WorkingImage->GetTimeSteps(); ++timeStep)
75 m_LabelCountInSlice[timeStep].resize(3);
76 for (
unsigned int dim = 0; dim < 3; ++dim)
78 m_LabelCountInSlice[timeStep][dim].clear();
79 m_LabelCountInSlice[timeStep][dim].resize(m_WorkingImage->GetDimension(dim));
80 for (
unsigned int slice = 0; slice < m_WorkingImage->GetDimension(dim); ++slice)
82 m_LabelCountInSlice[timeStep][dim][slice].clear();
83 m_LabelCountInSlice[timeStep][dim][slice].resize(numberOfLabels);
84 m_LabelCountInSlice[timeStep][dim][slice].assign(numberOfLabels, 0);
92 if (m_WorkingImage != newImage)
95 InterpolatorMapType::iterator iter = s_InterpolatorForImage.find(m_WorkingImage);
96 if (iter != s_InterpolatorForImage.end())
98 s_InterpolatorForImage.erase(iter);
101 m_WorkingImage = newImage;
103 s_InterpolatorForImage.insert(std::make_pair(m_WorkingImage,
this));
106 this->ResetLabelCount();
130 m_ReferenceImage = newImage;
133 if (m_WorkingImage.IsNull() || m_ReferenceImage->GetDimension() != m_WorkingImage->GetDimension() ||
134 m_ReferenceImage->GetPixelType().GetNumberOfComponents() != 1 ||
135 m_WorkingImage->GetPixelType().GetNumberOfComponents() != 1)
137 MITK_WARN <<
"Segmentation image has different image characteristics than reference image." << std::endl;
138 m_ReferenceImage = NULL;
142 for (
unsigned int dim = 0; dim < m_WorkingImage->GetDimension(); ++dim)
144 if (m_ReferenceImage->GetDimension(dim) != m_WorkingImage->GetDimension(dim))
146 MITK_WARN <<
"original patient image does not match segmentation (different extent in dimension " << dim
147 <<
"), ignoring patient image" << std::endl;
148 m_ReferenceImage = NULL;
155 unsigned int sliceDimension,
156 unsigned int sliceIndex,
157 unsigned int timeStep)
163 if (sliceDimension > 2)
165 if (m_WorkingImage.IsNull())
169 int numberOfLabels = m_WorkingImage->GetNumberOfLabels();
170 if (m_LabelCountInSlice[0][0][0].size() != numberOfLabels)
173 unsigned int dim0(0);
174 unsigned int dim1(1);
177 switch (sliceDimension)
195 slice, ScanSliceITKProcessing, 2,
SetChangedSliceOptions(sliceDimension, sliceIndex, dim0, dim1, timeStep));
200 template <
typename PixelType>
204 unsigned int timeStep = options.
timeStep;
208 if (sliceDimension > 2)
210 if (sliceIndex >= m_LabelCountInSlice[timeStep][sliceDimension].size())
213 unsigned int dim0(options.
dim0);
214 unsigned int dim1(options.
dim1);
216 std::vector<int> numberOfPixels;
217 int numberOfLabels = m_WorkingImage->GetNumberOfLabels();
218 numberOfPixels.resize(numberOfLabels);
220 typedef itk::Image<PixelType, 2>
ImageType;
221 typedef itk::ImageRegionConstIteratorWithIndex<ImageType> IteratorType;
223 IteratorType iter(input, input->GetLargestPossibleRegion());
226 typename IteratorType::IndexType index;
228 while (!iter.IsAtEnd())
230 index = iter.GetIndex();
231 int value =
static_cast<int>(iter.Get());
232 ++m_LabelCountInSlice[timeStep][dim0][index[0]][value];
233 ++m_LabelCountInSlice[timeStep][dim1][index[1]][value];
234 ++numberOfPixels[value];
238 for (
unsigned int label = 0; label < numberOfLabels; ++label)
240 m_LabelCountInSlice[timeStep][sliceDimension][sliceIndex][label] = numberOfPixels[label];
244 template <
typename TPixel,
unsigned int VImageDimension>
246 unsigned int timeStep)
248 typedef itk::ImageSliceConstIteratorWithIndex<itk::Image<TPixel, VImageDimension>> IteratorType;
250 IteratorType iter(input, input->GetLargestPossibleRegion());
251 iter.SetFirstDirection(0);
252 iter.SetSecondDirection(1);
254 typename IteratorType::IndexType index;
259 std::vector<int> numberOfPixels;
260 int numberOfLabels = m_WorkingImage->GetNumberOfLabels();
261 numberOfPixels.resize(numberOfLabels);
264 while (!iter.IsAtEnd())
266 while (!iter.IsAtEndOfSlice())
268 while (!iter.IsAtEndOfLine())
270 index = iter.GetIndex();
275 int value =
static_cast<unsigned int>(iter.Get());
276 ++m_LabelCountInSlice[timeStep][0][x][value];
277 ++m_LabelCountInSlice[timeStep][1][y][value];
278 ++numberOfPixels[value];
285 for (
unsigned int label = 0; label < numberOfLabels; ++label)
286 m_LabelCountInSlice[timeStep][2][z][label] += numberOfPixels[label];
289 numberOfPixels.assign(numberOfLabels, 0);
295 unsigned int sliceIndex,
297 unsigned int timeStep)
299 if (m_WorkingImage.IsNull())
303 if (timeStep >= m_LabelCountInSlice.size())
305 if (sliceDimension > 2)
307 int upperLimit = m_LabelCountInSlice[timeStep][sliceDimension].size();
308 if (sliceIndex >= upperLimit - 1)
313 int pixelValue = m_WorkingImage->GetActiveLabel()->GetValue();
316 if (m_LabelCountInSlice[timeStep][sliceDimension][sliceIndex][pixelValue] > 0)
323 for (lowerBound = sliceIndex - 1; ; --lowerBound)
325 if (m_LabelCountInSlice[timeStep][sliceDimension][lowerBound][pixelValue] > 0)
339 for (upperBound = sliceIndex + 1; upperBound < upperLimit; ++upperBound)
341 if (m_LabelCountInSlice[timeStep][sliceDimension][upperBound][pixelValue] > 0)
355 extractor->SetInput(m_WorkingImage);
356 extractor->SetTimeStep(timeStep);
357 extractor->SetResliceTransformByGeometry(m_WorkingImage->GetTimeGeometry()->GetGeometryForTimeStep(timeStep));
358 extractor->SetVtkOutputRequest(
false);
361 extractor->SetWorldGeometry(currentPlane);
362 extractor->Modified();
368 catch (
const std::exception &e)
370 MITK_ERROR <<
"Error in 2D interpolation: " << e.what();
375 resultImage->DisconnectPipeline();
382 m_WorkingImage->GetSlicedGeometry()->WorldToIndex(origin, origin);
383 origin[sliceDimension] = lowerBound;
384 m_WorkingImage->GetSlicedGeometry()->IndexToWorld(origin, origin);
385 reslicePlane->SetOrigin(origin);
388 extractor->SetWorldGeometry(reslicePlane);
389 extractor->Modified();
395 catch (
const std::exception &e)
397 MITK_ERROR <<
"Error in 2D interpolation: " << e.what();
402 lowerMITKSlice->DisconnectPipeline();
405 m_WorkingImage->GetSlicedGeometry()->WorldToIndex(origin, origin);
406 origin[sliceDimension] = upperBound;
407 m_WorkingImage->GetSlicedGeometry()->IndexToWorld(origin, origin);
408 reslicePlane->SetOrigin(origin);
411 extractor->SetWorldGeometry(reslicePlane);
412 extractor->Modified();
418 catch (
const std::exception &e)
420 MITK_ERROR <<
"Error in 2D interpolation: " << e.what();
425 upperMITKSlice->DisconnectPipeline();
427 if (lowerMITKSlice.IsNull() || upperMITKSlice.IsNull())
442 algorithm->Interpolate(
443 lowerMITKSlice.GetPointer(), lowerBound, upperMITKSlice.GetPointer(), upperBound, sliceIndex, 0, resultImage);
const Point3D GetOrigin() const
Get the origin, e.g. the upper-left corner of the plane.
void ResetLabelCount()
Initializes the internal container with the number of voxels per label.
void SetWorkingImage(LabelSetImage *image)
Initialize with a whole volume.
itk::SmartPointer< Self > Pointer
Image::Pointer Interpolate(unsigned int sliceDimension, unsigned int sliceIndex, const mitk::PlaneGeometry *currentPlane, unsigned int timeStep)
Update after changing the whole working image.
static SliceBasedInterpolationController * InterpolatorForImage(const Image *)
Find interpolator for a given image.
void SetChangedSlice(const Image *image, unsigned int sliceDimension, unsigned int sliceIndex, unsigned int timeStep)
Update after changing a single slice in the working image.
unsigned int sliceDimension
Generates interpolations of 2D slices.
std::map< const Image *, SliceBasedInterpolationController * > InterpolatorMapType
static InterpolatorMapType s_InterpolatorForImage
map::core::discrete::Elements< 3 >::InternalImageType ImageType
SliceBasedInterpolationController()
Image class for storing images.
virtual ~SliceBasedInterpolationController()
void ScanImageITKProcessing(itk::Image< TPixel, VImageDimension > *, unsigned int timeStep)
internal scan of the whole image
void ScanSliceITKProcessing(const itk::Image< PixelType, 2 > *, const SetChangedSliceOptions &options)
internal scan of a single slice
#define AccessFixedDimensionByItk_1(mitkImage, itkImageTypeFunction, dimension, arg1)
LabelSetImage class for handling labels and layers in a segmentation session.
Describes a two-dimensional, rectangular plane.
unsigned int GetDimension() const
Get dimension of the image.
void SetReferenceImage(Image *image)
Set a reference image (original patient image) - optional.
Protected class of mitk::SliceBasedInterpolationController. Don't use (you shouldn't be able to do so...