ITK/Examples/SimpleOperations/RegionOverlap: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
No edit summary
Line 139: Line 139:
</source>
</source>


{{ITKCMakeLists|RegionOverlay}}
{{ITKCMakeLists|{{SUBPAGENAME}}}}

Revision as of 17:14, 25 December 2012

ABC.png

RegionOverlap.cxx

<source lang="cpp">

  1. include <stdlib.h>
  2. include <stdio.h>
  1. include "itkRGBPixel.h"
  1. include "itkImageRegionIterator.h"
  1. include "itkImageFileWriter.h"


int main( int argc, char *argv[] ) {

 const char * out_file_name = argv[1];
 const unsigned int Dimension = 2;
 typedef itk::RGBPixel <unsigned char >           RGBPixelType;
 typedef itk::Image< RGBPixelType, Dimension >    RGBImageType;
 typedef RGBImageType::RegionType                 RegionType;
 typedef RGBImageType::IndexType                  IndexType;
 typedef RGBImageType::SizeType                   SizeType; 
 typedef itk::ImageRegionIterator< RGBImageType > IteratorType;
 
 IndexType index;
 index[0] = 0;
 index[1] = 0;
 SizeType size;
 size[0] = 100;
 size[1] = 100;
 RegionType region;
 region.SetIndex( index );
 region.SetSize( size );
 IndexType indexA;
 indexA[0] = 9;
 indexA[1] = 9;
 SizeType sizeA;
 sizeA[0] = 50;
 sizeA[1] = 50;
 RegionType regionA;
 regionA.SetIndex( indexA );
 regionA.SetSize( sizeA );
 IndexType indexB;
 indexB[0] = 39;
 indexB[1] = 39;
 SizeType sizeB;
 sizeB[0] = 50;
 sizeB[1] = 50;
 RegionType regionB;
 regionB.SetIndex( indexB );
 regionB.SetSize( sizeB );
 // Region C is the intersection of A and B
 // padded by 10 pixels.
 RegionType regionC = regionA;
 regionC.Crop( regionB );
 regionC.PadByRadius( 10 );
 RGBPixelType pix_black;
 pix_black.Fill( 0 );
 RGBPixelType pix_red;
 pix_red.Fill( 0 );
 pix_red[0] = 255;
 RGBPixelType pix_green;
 pix_green.Fill( 0 );
 pix_green[1] = 255;
 RGBPixelType pix_blue;
 pix_blue.Fill( 0 );
 pix_blue[2] = 255;

 // A black canvas
 RGBImageType::Pointer image = RGBImageType::New();
 image->SetRegions( region );
 image->Allocate();
 image->FillBuffer( pix_black );
 // Paint region A red.
 IteratorType itA( image, regionA );
 itA.GoToBegin();
 while ( !itA.IsAtEnd() )
   {
   itA.Set( itA.Get() + pix_red );
   ++itA;
   }
 // Paint region B green.
 IteratorType itB( image, regionB );
 itB.GoToBegin();
 while ( !itB.IsAtEnd() )
   {
   itB.Set( itB.Get() + pix_green );
   ++itB;
   }
 // Paint region C blue.
 IteratorType itC( image, regionC );
 itC.GoToBegin();
 while ( !itC.IsAtEnd() )
   {
   itC.Set( itC.Get() + pix_blue );
   ++itC;
   }
 // Writer
 typedef itk::ImageFileWriter< RGBImageType > FileWriterType;
 FileWriterType::Pointer writer = FileWriterType::New();
 writer->SetFileName( out_file_name );
 writer->SetInput( image );


 try
   {
   writer->Update();
   }
 catch( itk::ExceptionObject & excep )
   {
   std::cerr << "Exception caught !" << std::endl;
   std::cerr << excep << std::endl;
   }


 return EXIT_SUCCESS;

} </source>

CMakeLists.txt

<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)

project(RegionOverlap)

find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)

 find_package(VTK REQUIRED)
 include(${VTK_USE_FILE})

endif()

add_executable(RegionOverlap MACOSX_BUNDLE RegionOverlap.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(RegionOverlap ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(RegionOverlap ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build RegionOverlap

Click here to download RegionOverlap and its CMakeLists.txt file. Once the tarball RegionOverlap.tar has been downloaded and extracted,

cd RegionOverlap/build
  • If ITK is installed:
cmake ..
  • If ITK is not installed but compiled on your system, you will need to specify the path to your ITK build:
cmake -DITK_DIR:PATH=/home/me/itk_build ..

Build the project:

make

and run it:

./RegionOverlap

WINDOWS USERS PLEASE NOTE: Be sure to add the ITK bin directory to your path. This will resolve the ITK dll's at run time.

Building All of the Examples

Many of the examples in the ITK Wiki Examples Collection require VTK. You can build all of the the examples by following these instructions. If you are a new VTK user, you may want to try the Superbuild which will build a proper ITK and VTK.

ItkVtkGlue

ITK >= 4

For examples that use QuickView (which depends on VTK), you must have built ITK with Module_ITKVtkGlue=ON.

ITK < 4

Some of the ITK Examples require VTK to display the images. If you download the entire ITK Wiki Examples Collection, the ItkVtkGlue directory will be included and configured. If you wish to just build a few examples, then you will need to download ItkVtkGlue and build it. When you run cmake it will ask you to specify the location of the ItkVtkGlue binary directory.