]> Creatis software - clitk.git/commitdiff
Padding tool
authorRomulo Pinho <pinho@lyon.fnclcc.fr>
Thu, 7 Jul 2011 11:27:07 +0000 (13:27 +0200)
committerRomulo Pinho <pinho@lyon.fnclcc.fr>
Thu, 7 Jul 2011 11:27:07 +0000 (13:27 +0200)
tools/clitkPadImage.cxx [new file with mode: 0644]
tools/clitkPadImage.ggo [new file with mode: 0644]

diff --git a/tools/clitkPadImage.cxx b/tools/clitkPadImage.cxx
new file mode 100644 (file)
index 0000000..8b6570b
--- /dev/null
@@ -0,0 +1,107 @@
+#include "itkImage.h"
+#include "itkImageFileReader.h"
+#include "itkImageFileWriter.h"
+#include "itkConstantPadImageFilter.h"
+#include "clitkCommon.h"
+#include "clitkPadImage_ggo.h"
+#include <string>
+
+enum
+{
+  ERR_SUCCESS = 0,
+  ERR_NOT_SAME_SPACING = -1,
+  ERR_NOT_LIKE_LARGER = -2
+};
+
+typedef unsigned int DimType;
+
+template <class ImageType, class PadBoundType, DimType dim>
+int pad_like(typename ImageType::Pointer input, const std::string& likeFile, PadBoundType* pad_lower, PadBoundType* pad_upper)
+{
+  typedef typename ImageType::SpacingType SpacingType;
+  typedef typename ImageType::RegionType RegionType;
+  typedef typename ImageType::SizeType SizeType;
+  typedef typename ImageType::IndexType IndexType;
+  typedef typename ImageType::PointType PointType;
+
+  typedef itk::ImageFileReader<ImageType> ImageReaderType;
+  typename ImageReaderType::Pointer reader = ImageReaderType::New();
+  reader->SetFileName(likeFile);
+  reader->Update();
+  
+  typename ImageType::Pointer like_image = reader->GetOutput();
+
+  SpacingType spacing = input->GetSpacing(), like_spacing = like_image->GetSpacing(); 
+  if (spacing != like_spacing) {
+    std::cerr << spacing << " " << like_spacing << std::endl;
+    return ERR_NOT_SAME_SPACING;
+  }
+  
+  SizeType size = input->GetLargestPossibleRegion().GetSize(), like_size = like_image->GetLargestPossibleRegion().GetSize();
+  PointType origin = input->GetOrigin(), like_origin = like_image->GetOrigin();
+  IndexType lower_bound, like_lower_bound;
+  SizeType upper_bound, like_upper_bound;
+  for (DimType i = 0; i < dim; i++) {
+    lower_bound[i] = origin[i];
+    like_lower_bound[i] = like_origin[i];
+    pad_lower[i] = (PadBoundType)(abs(like_lower_bound[i] - lower_bound[i])/spacing[i]);
+    
+    upper_bound[i] = (lower_bound[i] + size[i]*spacing[i]);
+    like_upper_bound[i] = (like_lower_bound[i] + like_size[i]*spacing[i]);
+    pad_upper[i] = (PadBoundType)((like_upper_bound[i] - upper_bound[i])/spacing[i]);
+    if (pad_upper[i] < 0)
+      pad_upper[i] = 0;
+  }
+
+   
+  return ERR_SUCCESS;
+}
+
+int main(int argc, char** argv)
+{
+  const DimType dim = 3;
+  typedef short PixelType;
+  typedef itk::Image<PixelType, dim> ImageType;
+  typedef itk::ImageFileReader<ImageType> ImageReaderType;
+  typedef itk::ImageFileWriter<ImageType> ImageWriterType;
+  typedef itk::ConstantPadImageFilter<ImageType, ImageType> PadFilterType;
+  typedef typename PadFilterType::SizeValueType PadBoundType;
+  
+  GGO(clitkPadImage, args_info);
+  
+  typename ImageReaderType::Pointer reader = ImageReaderType::New();
+  reader->SetFileName(args_info.input_arg);
+  reader->Update();
+  
+  PadBoundType pad_lower[dim], pad_upper[dim];
+  typename ImageType::Pointer input = reader->GetOutput();
+  if (args_info.like_given) {
+    int err = pad_like<ImageType, PadBoundType, dim>(input, args_info.like_arg, pad_lower, pad_upper);
+    if (err) {
+      std::cerr << "Error processing like image." << std::endl;
+      return err;
+    }
+  }
+  else {
+    for (DimType i = 0; i < dim; i++) {
+      pad_lower[i] = args_info.lower_arg[i];
+      pad_upper[i] = args_info.upper_arg[i];
+    }
+  }
+  
+  typename PadFilterType::Pointer filter = PadFilterType::New();
+  filter->SetPadLowerBound(pad_lower);
+  filter->SetPadUpperBound(pad_upper);
+  filter->SetInput(input);
+  filter->SetConstant(args_info.value_arg);
+  filter->Update();
+  
+  typename ImageType::Pointer output = filter->GetOutput();
+  typename ImageWriterType::Pointer writer = ImageWriterType::New();
+  writer->SetInput(output);
+  writer->SetFileName(args_info.output_arg);
+  writer->Update();
+  
+  return ERR_SUCCESS;
+}
+
diff --git a/tools/clitkPadImage.ggo b/tools/clitkPadImage.ggo
new file mode 100644 (file)
index 0000000..59c1f98
--- /dev/null
@@ -0,0 +1,26 @@
+#File clitkCropImage.ggo
+package "clitkCropImage"
+version "1.0"
+purpose "Crop an image according to a given extends or AutoCrop with a background value or like another image"
+
+option "config"                   -  "Config file"                 string  no
+option "verbose"           v  "Verbose"                            flag    off
+
+section "I/O"
+
+option "input"         i       "Input image filename"            string        yes
+option "output"        o       "Output image filename"           string        yes
+
+section "Used determined crop"
+option "lower"         l       "Size of the lower crop region (multiple values)"         int   no  multiple
+option "upper"         u       "Size of the upper crop region (multiple values)"         int   no  multiple
+
+section "Crop like another image"
+option "like"          -       "Crop like this image (must have the same spacing"   string no
+
+section "Extra parameters"
+option "value"    - "Value to be set in padded aread" float default="0" no  
+
+
+
+