ITK/Examples/Images/ShrinkImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
Line 1: Line 1:
Image doesn't seem to be shrunk?
==ShrinkImageFilter.cxx==
==ShrinkImageFilter.cxx==
<source lang="cpp">
<source lang="cpp">
Line 7: Line 5:
#include "itkRescaleIntensityImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkShrinkImageFilter.h"
#include "itkShrinkImageFilter.h"
#include <itkImageToVTKImageFilter.h>
#include "vtkImageViewer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkImageActor.h"
#include "vtkInteractorStyleImage.h"
#include "vtkRenderer.h"


typedef itk::Image<unsigned char, 2>  ImageType;
typedef itk::Image<unsigned char, 2>  ImageType;
Line 26: Line 15:
   CreateImage(image);
   CreateImage(image);


  std::cout << "Original size: " << image->GetLargestPossibleRegion().GetSize() << std::endl;
 
   typedef itk::ShrinkImageFilter <ImageType, ImageType>
   typedef itk::ShrinkImageFilter <ImageType, ImageType>
           ShrinkImageFilterType;
           ShrinkImageFilterType;
Line 32: Line 23:
           = ShrinkImageFilterType::New();
           = ShrinkImageFilterType::New();
   shrinkFilter->SetInput(image);
   shrinkFilter->SetInput(image);
   shrinkFilter->SetShrinkFactor(0, 2);
   shrinkFilter->SetShrinkFactor(0, 2); // shrink the first dimension by a factor of 2 (i.e. 100 gets changed to 50)
  shrinkFilter->SetShrinkFactor(1, 3); // shrink the second dimension by a factor of 3 (i.e. 100 gets changed to 33)
   shrinkFilter->Update();
   shrinkFilter->Update();


   // Visualize first image
   std::cout << "New size: " << shrinkFilter->GetOutput()->GetLargestPossibleRegion().GetSize() << std::endl;
  typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;
  ConnectorType::Pointer originalConnector = ConnectorType::New();
  originalConnector->SetInput(image);
 
  vtkSmartPointer<vtkImageActor> originalActor =
    vtkSmartPointer<vtkImageActor>::New();
  originalActor->SetInput(originalConnector->GetOutput());
 
  // Visualize padded image
  ConnectorType::Pointer shrunkConnector = ConnectorType::New();
  shrunkConnector->SetInput(shrinkFilter->GetOutput());
 
  vtkSmartPointer<vtkImageActor> shrunkActor =
    vtkSmartPointer<vtkImageActor>::New();
  shrunkActor->SetInput(shrunkConnector->GetOutput());


  // There will be one render window
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->SetSize(600, 300);
  vtkSmartPointer<vtkRenderWindowInteractor> interactor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  interactor->SetRenderWindow(renderWindow);
  // Define viewport ranges
  // (xmin, ymin, xmax, ymax)
  double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
  double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
  // Setup both renderers
  vtkSmartPointer<vtkRenderer> leftRenderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderWindow->AddRenderer(leftRenderer);
  leftRenderer->SetViewport(leftViewport);
  leftRenderer->SetBackground(.6, .5, .4);
  vtkSmartPointer<vtkRenderer> rightRenderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderWindow->AddRenderer(rightRenderer);
  rightRenderer->SetViewport(rightViewport);
  rightRenderer->SetBackground(.4, .5, .6);
  // Add the sphere to the left and the cube to the right
  leftRenderer->AddActor(originalActor);
  rightRenderer->AddActor(shrunkActor);
  leftRenderer->ResetCamera();
  rightRenderer->ResetCamera();
  renderWindow->Render();
  vtkSmartPointer<vtkInteractorStyleImage> style =
    vtkSmartPointer<vtkInteractorStyleImage>::New();
  interactor->SetInteractorStyle(style);
  interactor->Start();


   return EXIT_SUCCESS;
   return EXIT_SUCCESS;
Line 100: Line 36:
{
{
   // Create an image with 2 connected components
   // Create an image with 2 connected components
  ImageType::RegionType region;
   ImageType::IndexType start;
   ImageType::IndexType start;
   start[0] = 0;
   start.Fill(0);
  start[1] = 0;


   ImageType::SizeType size;
   ImageType::SizeType size;
  unsigned int NumRows = 200;
   size.Fill(100);
  unsigned int NumCols = 300;
   size[0] = NumRows;
  size[1] = NumCols;
 
  region.SetSize(size);
  region.SetIndex(start);


  ImageType::RegionType region(start, size);
   image->SetRegions(region);
   image->SetRegions(region);
   image->Allocate();
   image->Allocate();
  image->FillBuffer(0);


   // Make a square
   // Make a white square
   for(unsigned int r = 20; r < 80; r++)
   for(unsigned int r = 20; r < 80; r++)
  {
    {
      for(unsigned int c = 20; c < 30; c++)
    for(unsigned int c = 20; c < 30; c++)
       {
       {
          ImageType::IndexType pixelIndex;
      ImageType::IndexType pixelIndex;
          pixelIndex[0] = r;
      pixelIndex[0] = r;
          pixelIndex[1] = c;
      pixelIndex[1] = c;


          image->SetPixel(pixelIndex, 200);
      image->SetPixel(pixelIndex, 255);
       }
       }
  }
    }
}
}


Line 135: Line 65:
==CMakeLists.txt==
==CMakeLists.txt==
<source lang="cmake">
<source lang="cmake">
cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 2.6)


PROJECT(ShrinkImageFilter)
PROJECT(ShrinkImageFilter)
include_directories(/home/doriad/src/ITK/Wrapping/WrapITK/ExternalProjects/ItkVtkGlue/src/)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})


FIND_PACKAGE(ITK REQUIRED)
FIND_PACKAGE(ITK REQUIRED)
Line 149: Line 75:
ADD_EXECUTABLE(ShrinkImageFilter ShrinkImageFilter.cxx)
ADD_EXECUTABLE(ShrinkImageFilter ShrinkImageFilter.cxx)
TARGET_LINK_LIBRARIES(ShrinkImageFilter
TARGET_LINK_LIBRARIES(ShrinkImageFilter
vtkHybrid
ITKBasicFilters ITKCommon ITKIO)
ITKBasicFilters ITKCommon ITKIO)


</source>
</source>

Revision as of 22:35, 21 February 2011

ShrinkImageFilter.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkImageFileWriter.h"
  3. include "itkRescaleIntensityImageFilter.h"
  4. include "itkShrinkImageFilter.h"

typedef itk::Image<unsigned char, 2> ImageType;

void CreateImage(ImageType::Pointer image);

int main(int, char *[]) {

 ImageType::Pointer image = ImageType::New();
 CreateImage(image);
 std::cout << "Original size: " << image->GetLargestPossibleRegion().GetSize() << std::endl;
 
 typedef itk::ShrinkImageFilter <ImageType, ImageType>
         ShrinkImageFilterType;
 ShrinkImageFilterType::Pointer shrinkFilter
         = ShrinkImageFilterType::New();
 shrinkFilter->SetInput(image);
 shrinkFilter->SetShrinkFactor(0, 2); // shrink the first dimension by a factor of 2 (i.e. 100 gets changed to 50)
 shrinkFilter->SetShrinkFactor(1, 3); // shrink the second dimension by a factor of 3 (i.e. 100 gets changed to 33)
 shrinkFilter->Update();
 std::cout << "New size: " << shrinkFilter->GetOutput()->GetLargestPossibleRegion().GetSize() << std::endl;


 return EXIT_SUCCESS;

}

void CreateImage(ImageType::Pointer image) {

 // Create an image with 2 connected components
 ImageType::IndexType start;
 start.Fill(0);
 ImageType::SizeType size;
 size.Fill(100);
 ImageType::RegionType region(start, size);
 image->SetRegions(region);
 image->Allocate();
 image->FillBuffer(0);
 // Make a white square
 for(unsigned int r = 20; r < 80; r++)
   {
   for(unsigned int c = 20; c < 30; c++)
     {
     ImageType::IndexType pixelIndex;
     pixelIndex[0] = r;
     pixelIndex[1] = c;
     image->SetPixel(pixelIndex, 255);
     }
   }

}

</source>

CMakeLists.txt

<source lang="cmake">

cmake_minimum_required(VERSION 2.6)

PROJECT(ShrinkImageFilter)

FIND_PACKAGE(ITK REQUIRED) INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE(ShrinkImageFilter ShrinkImageFilter.cxx) TARGET_LINK_LIBRARIES(ShrinkImageFilter ITKBasicFilters ITKCommon ITKIO)

</source>