typename PatchExtractor<PValue>::ImageType::Pointer PatchExtractor<PValue>::ExtractPatch() { ContIndexType startIndex; for(unsigned int i = 0; i < 2; i++) { startIndex[i] = m_Center[i] - (m_Size[i] / 2.0); } startIndex[2] = 0; PointType newOrigin; m_Image->TransformContinuousIndexToPhysicalPoint(startIndex, newOrigin); typename ImageType::Pointer patch = ImageType::New(); patch->SetDirection(m_Image->GetDirection()); patch->SetOrigin(newOrigin); patch->SetSpacing(m_Image->GetSpacing()); typename ImageType::RegionType region; m_Size[2] = 1; region.SetSize(m_Size); patch->SetRegions(region); patch->Allocate(); typedef itk::NearestNeighborInterpolateImageFunction<ImageType, double> InterpolatorType; typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); interpolator->SetInputImage(m_Image); itk::ImageRegionIterator<ImageType> imageIt(patch, region); while(!imageIt.IsAtEnd()) { typename ImageType::IndexType index = imageIt.GetIndex(); PointType point; patch->TransformIndexToPhysicalPoint(index, point); if(interpolator->IsInsideBuffer(point)) imageIt.Set(interpolator->Evaluate(point)); else imageIt.Set(0); ++imageIt; } return patch; }
void WorkbenchUtils::addPaddingItk(itk::Image <PixelType, ImageDimension> *itkImage, Axis axis, bool append, int numberOfSlices, float pixelValue, Image::Pointer outImage) { // pixel type is templated. The input field for the value is set to float, so the user might enter some invalid values for the image type at hand. // since all primitive built-in types have well defined casting behaviour between each other, we'll just do a typecast. we will clip the entered // value at PixelTypes min/max to prevent an overflow. The possible loss of precision is ignored. float lower = itk::NumericTraits<PixelType>::min(); float upper = itk::NumericTraits<PixelType>::max(); float clippedPixelValue = std::max(lower, std::min(pixelValue, upper)); PixelType paddingPixelValue = (PixelType) clippedPixelValue; typedef itk::Image <PixelType, ImageDimension> ImageType; // gather all data typename ImageType::SizeType lowerBound; typename ImageType::SizeType upperBound; lowerBound.Fill(0); upperBound.Fill(0); unsigned int itkAxis = convertToItkAxis(axis); if (append) { upperBound[itkAxis] = numberOfSlices; } else { lowerBound[itkAxis] = numberOfSlices; } // setup the filter typedef itk::ConstantPadImageFilter <ImageType, ImageType> PadFilterType; typename PadFilterType::Pointer padFilter = PadFilterType::New(); padFilter->SetInput(itkImage); padFilter->SetConstant(paddingPixelValue); padFilter->SetPadLowerBound(lowerBound); padFilter->SetPadUpperBound(upperBound); padFilter->UpdateLargestPossibleRegion(); // Update the origin, since padding creates negative index that is lost when returned to MITK typename ImageType::Pointer paddedImage = padFilter->GetOutput(); typename ImageType::RegionType paddedImageRegion = paddedImage->GetLargestPossibleRegion(); typename ImageType::PointType origin; paddedImage->TransformIndexToPhysicalPoint(paddedImageRegion.GetIndex(), origin); paddedImage->SetOrigin(origin); // get the results and cast them back to mitk. return via out parameter. outImage->InitializeByItk(paddedImage.GetPointer()); CastToMitkImage(paddedImage, outImage); }