ITK/Examples/Inspection/CheckerBoardImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
(Made the example a little more useful)
Line 3: Line 3:
<source lang="cpp">
<source lang="cpp">
#include "itkImage.h"
#include "itkImage.h"
#include "itkRGBPixel.h"
#include "itkCheckerBoardImageFilter.h"
#include "itkCheckerBoardImageFilter.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionIterator.h"
Line 8: Line 9:
#include "QuickView.h"
#include "QuickView.h"


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


static void CreateImage(ImageType::Pointer image, unsigned char color);
static void CreateImage(ImageType::Pointer image, PixelType);


int main(int argc, char *argv[])
int main(int argc, char *argv[])
{
{
   ImageType::Pointer image1 = ImageType::New();
   ImageType::Pointer image1 = ImageType::New();
   CreateImage(image1, 0);
  PixelType pixel1;
  pixel1.Fill(0);
  if (argc > 3)
    {
    pixel1.SetRed(atoi(argv[1]));
    pixel1.SetGreen(atoi(argv[2]));
    pixel1.SetBlue(atoi(argv[3]));
    }
   CreateImage(image1, pixel1);
   ImageType::Pointer image2 = ImageType::New();
   ImageType::Pointer image2 = ImageType::New();
   CreateImage(image2, 255);
 
  PixelType pixel2;
  pixel2.Fill(255);
  if (argc > 6)
    {
    pixel2.SetRed(atoi(argv[4]));
    pixel2.SetGreen(atoi(argv[5]));
    pixel2.SetBlue(atoi(argv[6]));
    }
   CreateImage(image2, pixel2);
    
    
   typedef itk::CheckerBoardImageFilter< ImageType > CheckerBoardFilterType;
   typedef itk::CheckerBoardImageFilter< ImageType > CheckerBoardFilterType;
Line 34: Line 53:
}
}


void CreateImage(ImageType::Pointer image, unsigned char color)
void CreateImage(ImageType::Pointer image, PixelType color)
{
{
   // Create an image with all pixels set to 'color'
   // Create an image with all pixels set to 'color'

Revision as of 18:46, 28 November 2012

ITK Examples Baseline Inspection TestCheckerBoardImageFilter.png

CheckerBoardImageFilter.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkRGBPixel.h"
  3. include "itkCheckerBoardImageFilter.h"
  4. include "itkImageRegionIterator.h"
  1. include "QuickView.h"

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

static void CreateImage(ImageType::Pointer image, PixelType);

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

 ImageType::Pointer image1 = ImageType::New();
 PixelType pixel1;
 pixel1.Fill(0);
 if (argc > 3)
   {
   pixel1.SetRed(atoi(argv[1]));
   pixel1.SetGreen(atoi(argv[2]));
   pixel1.SetBlue(atoi(argv[3]));
   }
 CreateImage(image1, pixel1);
 ImageType::Pointer image2 = ImageType::New();
 PixelType pixel2;
 pixel2.Fill(255);
 if (argc > 6)
   {
   pixel2.SetRed(atoi(argv[4]));
   pixel2.SetGreen(atoi(argv[5]));
   pixel2.SetBlue(atoi(argv[6]));
   }
 CreateImage(image2, pixel2);
 
 typedef itk::CheckerBoardImageFilter< ImageType > CheckerBoardFilterType;
 CheckerBoardFilterType::Pointer checkerBoardFilter = CheckerBoardFilterType::New();
 checkerBoardFilter->SetInput1(image1);
 checkerBoardFilter->SetInput2(image2);
 checkerBoardFilter->Update();
 
 QuickView viewer;
 viewer.AddImage<ImageType>(image1);
 viewer.AddImage<ImageType>(image2);
 viewer.AddImage<ImageType>(checkerBoardFilter->GetOutput());
 viewer.Visualize();

 return EXIT_SUCCESS;

}

void CreateImage(ImageType::Pointer image, PixelType color) {

 // Create an image with all pixels set to 'color'
 
 ImageType::IndexType start;
 start.Fill(0);
 
 ImageType::SizeType size;
 size.Fill(100);
 ImageType::RegionType region;
 region.SetSize(size);
 region.SetIndex(start);
 image->SetRegions(region);
 image->Allocate();
 
 itk::ImageRegionIterator<ImageType> imageIterator(image,region);
 while(!imageIterator.IsAtEnd())
   {
   imageIterator.Set(color);
   ++imageIterator;
   }

} </source>

CMakeLists.txt

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

project(CheckBoardImageFilter)

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(CheckBoardImageFilter MACOSX_BUNDLE CheckBoardImageFilter.cxx) target_link_libraries(CheckBoardImageFilter

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

</syntaxhighlight>

Download and Build CheckBoardImageFilter

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

cd CheckBoardImageFilter/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:

./CheckBoardImageFilter

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.