Authors belong to:
- University of LYON http://www.universite-lyon.fr/
- - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
+ - Léon Bérard cancer center http://www.centreleonberard.fr
- CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
This software is distributed WITHOUT ANY WARRANTY; without even
- BSD See included LICENSE.txt file
- CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
- ======================================================================-====*/
+ ===========================================================================**/
#ifndef CLITKCROPLIKEIMAGEFILTER_TXX
#define CLITKCROPLIKEIMAGEFILTER_TXX
// clitk
#include "clitkCommon.h"
-
-// itk
-#include "itkRegionOfInterestImageFilter.h"
-
+#include "clitkPasteImageFilter.h"
//--------------------------------------------------------------------
template <class ImageType>
this->SetNumberOfRequiredInputs(1);
m_LikeImage = NULL;
m_LikeFilenameIsGiven = false;
+ this->SetBackgroundValue(typename PixelTraits<typename ImageType::PixelType>::ValueType(0));
m_CropAlongThisDimension.resize(ImageType::ImageDimension);
for(uint i=0; i<ImageType::ImageDimension; i++)
m_CropAlongThisDimension[i] = true;
template <class ImageType>
void
clitk::CropLikeImageFilter<ImageType>::
-SetInput(const ImageType * image) {
- // Process object is not const-correct so the const casting is required.
- this->SetNthInput(0, const_cast<ImageType *>( image ));
+GenerateInputRequestedRegion() {
+ // Needed because output region can be larger than input
+ ImagePointer input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
+ input->SetRequestedRegion(input->GetLargestPossibleRegion());
}
//--------------------------------------------------------------------
-
+
//--------------------------------------------------------------------
template <class ImageType>
void
clitk::CropLikeImageFilter<ImageType>::
-GenerateOutputInformation() {
+GenerateOutputInformation() {
// Get input pointers
ImageConstPointer input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
// Get output pointer
ImagePointer output = this->GetOutput(0);
-
+
// Get input info
typename ImageType::SizeType likeSize;
typename ImageType::IndexType likeStart;
typename ImageType::PointType likeOrigin;
typename ImageType::SpacingType likeSpacing;
- if (m_LikeImage) {
+ if (m_LikeImage) {
likeSize = m_LikeImage->GetLargestPossibleRegion().GetSize();
likeStart = m_LikeImage->GetLargestPossibleRegion().GetIndex();
likeOrigin = m_LikeImage->GetOrigin();
likeSpacing = m_LikeImage->GetSpacing();
+ output->CopyInformation(m_LikeImage);
}
else {
+ // Only load the header (allows to use 'like' with any image type)
if (m_LikeFilenameIsGiven) {
itk::ImageIOBase::Pointer header = readImageHeader(m_LikeFilename);
for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
- likeSize[i] = header->GetIORegion().GetSize()[i]; //GetDimensions(i);
- likeStart[i] = header->GetIORegion().GetIndex()[i];
+ likeSize[i] = header->GetDimensions(i);
+ likeStart[i] = 0;//header->GetIORegion().GetIndex()[i];
likeOrigin[i] = header->GetOrigin(i);
likeSpacing[i] = header->GetSpacing(i);
}
}
}
- // Compute region
- typename ImageType::SizeType size;
- typename ImageType::IndexType start;
+ // Check spacing
for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
- double ol;
- if (m_CropAlongThisDimension[i]) {
- size[i] = likeSize[i];
- ol = likeOrigin[i];
+ if (likeSpacing[i] != input->GetSpacing()[i]) {
+ clitkExceptionMacro("Images must have the same spacing, but input's spacing(" << i
+ <<") is " << input->GetSpacing()[i] << " while the spacing(" << i
+ << ") of 'like' is " << likeSpacing[i] << ".");
+ }
+ }
+
+ // Check that we must crop along each dimension. If not, we use the
+ // size of the input image
+ for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
+ if (m_CropAlongThisDimension[i] == false) {
+ likeStart[i] = input->GetLargestPossibleRegion().GetIndex()[i];
+ likeSize[i] = input->GetLargestPossibleRegion().GetSize()[i];
+ }
+ }
+
+ // Define output region
+ m_OutputRegion.SetIndex(likeStart);
+ m_OutputRegion.SetSize(likeSize);
+ output->SetRegions(m_OutputRegion);
+ output->SetRequestedRegion(m_OutputRegion);
+ output->SetBufferedRegion(m_OutputRegion);
+ output->SetSpacing(likeSpacing);
+ output->SetOrigin(likeOrigin);
+ output->Allocate(); // Needed ?
+
+ // get startpoint source/dest
+ // for each dim
+ // if source < dest -> start from dest, compute in source
+ // if source > dest -> start from source, compute in dest
+ m_StartDestIndex = output->GetLargestPossibleRegion().GetIndex();
+ m_StartSourceIndex = input->GetLargestPossibleRegion().GetIndex();
+ PointType m_StartPointInSource;
+ PointType m_StartPointInDest;
+ m_StartSourceIndex = input->GetLargestPossibleRegion().GetIndex();
+ input->TransformIndexToPhysicalPoint(m_StartSourceIndex, m_StartPointInSource);
+ m_StartDestIndex = output->GetLargestPossibleRegion().GetIndex();
+ output->TransformIndexToPhysicalPoint(m_StartDestIndex, m_StartPointInDest);
+ IndexType startDestInSource;
+ IndexType startSourceInDest;
+ input->TransformPhysicalPointToIndex(m_StartPointInDest, startDestInSource);
+ output->TransformPhysicalPointToIndex(m_StartPointInSource, startSourceInDest);
+ for(int i=0; i<ImageType::ImageDimension; i++) {
+ if (m_StartPointInSource[i] < m_StartPointInDest[i]) {
+ m_StartSourceIndex[i] = startDestInSource[i];
}
else {
- size[i] = input->GetLargestPossibleRegion().GetSize()[i];
- ol = input->GetOrigin()[i];
+ m_StartDestIndex[i] = startSourceInDest[i];
}
- double oi = input->GetOrigin()[i];
- start[i] = lrint((ol-oi)/input->GetSpacing()[i]);
- m_Origin[i] = likeOrigin[i];
- if (likeSpacing[i] != input->GetSpacing()[i]) {
- clitkExceptionMacro("Images must have the same spacing, but input's spacing(" << i
- <<") is " << input->GetSpacing()[i] << " while like's spacing(" << i
- << ") is " << likeSpacing[i] << ".");
+ }
+ m_Region.SetIndex(m_StartSourceIndex);
+
+ // Stop index
+ m_StopSourceIndex = input->GetLargestPossibleRegion().GetIndex()+
+ input->GetLargestPossibleRegion().GetSize();
+ m_StopDestIndex = output->GetLargestPossibleRegion().GetIndex()+
+ output->GetLargestPossibleRegion().GetSize();
+ PointType m_StopPointInSource;
+ PointType m_StopPointInDest;
+ input->TransformIndexToPhysicalPoint(m_StopSourceIndex, m_StopPointInSource);
+ output->TransformIndexToPhysicalPoint(m_StopDestIndex, m_StopPointInDest);
+ IndexType stopDestInSource;
+ IndexType stopSourceInDest;
+ input->TransformPhysicalPointToIndex(m_StopPointInDest, stopDestInSource);
+ output->TransformPhysicalPointToIndex(m_StopPointInSource, stopSourceInDest);
+
+ for(int i=0; i<ImageType::ImageDimension; i++) {
+ if (m_StopPointInSource[i] > m_StopPointInDest[i]) {
+ m_StopSourceIndex[i] = stopDestInSource[i];
+ }
+ else {
+ m_StopDestIndex[i] = stopSourceInDest[i];
}
}
- m_Region.SetSize(size);
- m_Region.SetIndex(start);
- output->SetRegions(m_Region);
- output->SetSpacing(input->GetSpacing());
+ // Set size to the region we want to paste
+ SizeType s;
+ for(int i=0; i<ImageType::ImageDimension; i++)
+ s[i] = m_StopSourceIndex[i]-m_StartSourceIndex[i];
+ m_Region.SetSize(s);
+
}
//--------------------------------------------------------------------
GenerateData() {
// Get input pointers
ImageConstPointer input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
+
+ // Get output pointer, fill with Background
+ ImagePointer output = this->GetOutput(0);
+
+ output->FillBuffer(GetBackgroundValue());
- typedef itk::RegionOfInterestImageFilter<ImageType, ImageType> CropFilterType;
+ // Paste image inside
+ typedef clitk::PasteImageFilter<ImageType,ImageType> PasteFilterType;
+ typename PasteFilterType::Pointer pasteFilter = PasteFilterType::New();
+ //pasteFilter->ReleaseDataFlagOn(); // change nothing ?
+ // pasteFilter->InPlaceOn(); // makt it seg fault
+ pasteFilter->SetSourceImage(input);
+ pasteFilter->SetDestinationImage(output);
+ pasteFilter->SetDestinationIndex(m_StartDestIndex);
+ pasteFilter->SetSourceRegion(m_Region);
+ pasteFilter->Update();
+
+ // Get (graft) output (SetNthOutput does not fit here because of Origin).
+ this->GraftOutput(pasteFilter->GetOutput());
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ImageType>
+typename ImageType::Pointer
+clitk::ResizeImageLike(const ImageType * input,
+ const itk::ImageBase<ImageType::ImageDimension> * like,
+ typename ImageType::PixelType backgroundValue)
+{
+ typedef clitk::CropLikeImageFilter<ImageType> CropFilterType;
typename CropFilterType::Pointer cropFilter = CropFilterType::New();
cropFilter->SetInput(input);
- cropFilter->SetReleaseDataFlag(this->GetReleaseDataFlag());
- cropFilter->SetRegionOfInterest(m_Region);
+ cropFilter->SetCropLikeImage(like);
+ cropFilter->SetBackgroundValue(backgroundValue);
cropFilter->Update();
+ return cropFilter->GetOutput();
+}
+//--------------------------------------------------------------------
- // Get (graft) output (SetNthOutput does not fit here because of Origin).
- this->GraftOutput(cropFilter->GetOutput());
+
+//--------------------------------------------------------------------
+template<class ImageType>
+typename ImageType::Pointer
+clitk::ResizeImageLike(const ImageType * input,
+ typename itk::ImageBase<ImageType::ImageDimension>::RegionType * region,
+ typename ImageType::PixelType backgroundValue)
+{
+ typename ImageType::Pointer output = ImageType::New();
+ output->CopyInformation(input);
+ typename ImageType::RegionType reg;
+ reg.SetIndex(region->GetIndex());
+ reg.SetSize(region->GetSize());
+ output->SetRegions(reg);
+ output->Allocate();
+ return clitk::ResizeImageLike<ImageType>(input, output, backgroundValue);
}
//--------------------------------------------------------------------
-
-
-#endif //#define CLITKAUTOCROPFILTER
+
+
+//--------------------------------------------------------------------
+template<class ImageType>
+typename ImageType::Pointer
+clitk::ResizeImageLike(const ImageType * input,
+ typename itk::BoundingBox<unsigned long, ImageType::ImageDimension>::Pointer bb,
+ typename ImageType::PixelType BG)
+{
+ typename ImageType::RegionType region;
+ clitk::ComputeRegionFromBB<ImageType>(input, bb, region);
+ typename ImageType::Pointer output = ImageType::New();
+ output->CopyInformation(input);
+ output->SetRegions(region);
+ output->Allocate();
+ return clitk::ResizeImageLike<ImageType>(input, output, BG);
+}
+//--------------------------------------------------------------------
+
+#endif //#define CLITKCROPLIKEIMAGEFILTER_TXX