ITK/Examples/SimpleOperations/RegionOverlap: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
No edit summary
Line 1: Line 1:
[[File:ABC.png]]
<div class="floatcenter">[[File:ITK_Examples_Baseline_SimpleOperations_TestSimpleOperations.png]]</div>


==RegionOverlap.cxx==
==RegionOverlap.cxx==

Revision as of 16:38, 18 February 2016

ITK Examples Baseline SimpleOperations TestSimpleOperations.png

RegionOverlap.cxx

<source lang="cpp">

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

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

 if ( argc < 2)
   {
   std::cerr << argv[0] << ": Please provide an output image name." << std::endl;
   return EXIT_FAILURE;
   }
 const char * out_file_name = argv[1];
 const unsigned int Dimension = 2;
 typedef unsigned char                            ComponentType;
 typedef itk::RGBPixel < ComponentType >          RGBPixelType;
 typedef itk::Image< RGBPixelType, Dimension >    RGBImageType;
 typedef RGBImageType::RegionType                 RegionType;
 typedef RGBImageType::IndexType                  IndexType;
 typedef RGBImageType::SizeType                   SizeType; 
 typedef itk::ImageRegionIterator< RGBImageType > IteratorType;

 // Define the region of the entire image. 
 IndexType index = Template:0, 0;
 SizeType size = Template:100, 100;
 RegionType region(index,size);
 // Now define two overlapping regions within that image.
 IndexType indexA = Template:9, 9;
 SizeType sizeA = Template:50, 50;
 RegionType regionA(indexA,sizeA);
 IndexType indexB = Template:39, 39;
 SizeType sizeB = Template:50, 50;
 RegionType regionB(indexB,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(0.0); // Initialize to (0,0,0)
 RGBPixelType pix_red(0.0);
 pix_red.SetRed( 255 );
 RGBPixelType pix_green(0.0);
 pix_green.SetGreen( 255 );
 RGBPixelType pix_blue(0.0);
 pix_blue.SetBlue( 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 );
 for (itA.GoToBegin(); !itA.IsAtEnd(); ++itA)
   {
   itA.Value() += pix_red;
   }
 // Paint region B green.
 IteratorType itB( image, regionB );
 for (itB.GoToBegin(); !itB.IsAtEnd(); ++itB)
   {
   itB.Value() += pix_green;
   }
 // Paint region C blue.
 IteratorType itC( image, regionC );
 for (itC.GoToBegin(); !itC.IsAtEnd(); ++itC)
   {
   itC.Value() += pix_blue;
   }
 QuickView viewer;
 viewer.AddImage(
   image.GetPointer(),true);
 viewer.Visualize();
 // Writer
 typedef itk::ImageFileWriter< RGBImageType > FileWriterType;
 FileWriterType::Pointer writer = FileWriterType::New();
 writer->SetFileName( out_file_name );
 writer->SetInput( image );
 try
   {
   writer->Update();
   std::cout << "Created " << argv[1] << std::endl;
   }
 catch( itk::ExceptionObject & excep )
   {
   std::cerr << "Exception caught !" << std::endl;
   std::cerr << excep << std::endl;
   return EXIT_FAILURE;
   }
 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})

else()

 find_package(ItkVtkGlue REQUIRED)
 include(${ItkVtkGlue_USE_FILE})
 set(Glue ItkVtkGlue)

endif()

add_executable(RegionOverlap MACOSX_BUNDLE RegionOverlap.cxx) target_link_libraries(RegionOverlap

 ${Glue}  ${VTK_LIBRARIES} ${ITK_LIBRARIES})

</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 VTK and ITK bin directories to your path. This will resolve the VTK and 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.