ITK/Examples/ImageProcessing/ShapeAttributes

From KitwarePublic
< ITK‎ | Examples
Revision as of 16:31, 18 December 2013 by Lorensen (talk | contribs) (Created page with "==ShapeAttributes.cxx== <source lang="cpp"> #include "itkImage.h" #include "itkImageFileReader.h" #include "itkConnectedComponentImageFilter.h" #include "itkLabelImageToShapeLabe...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

ShapeAttributes.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkImageFileReader.h"
  3. include "itkConnectedComponentImageFilter.h"
  4. include "itkLabelImageToShapeLabelMapFilter.h"

template <typename TImage> static void CreateImage(TImage* const image);

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

 const unsigned int Dimension = 2;
 typedef unsigned char                                 PixelType;
 typedef unsigned short                                LabelType;
 typedef itk::Image<PixelType, Dimension>              InputImageType;
 typedef itk::Image< LabelType, Dimension >            OutputImageType;
 typedef itk::ShapeLabelObject< LabelType, Dimension > ShapeLabelObjectType;
 typedef itk::LabelMap< ShapeLabelObjectType >         LabelMapType;
 std::string fileName;
 InputImageType::Pointer image;
 if( argc < 2 )
   {
   image = InputImageType::New();
   CreateImage(image.GetPointer());
   fileName = "Generated image";
   }
 else
   {
   fileName = argv[1];
   typedef itk::ImageFileReader<InputImageType> ReaderType;
   ReaderType::Pointer reader = ReaderType::New();
   reader->SetFileName(fileName);
   reader->Update();
   image = reader->GetOutput();
   }


 typedef itk::ConnectedComponentImageFilter <InputImageType, OutputImageType >
   ConnectedComponentImageFilterType;
 typedef itk::LabelImageToShapeLabelMapFilter< OutputImageType, LabelMapType>
   I2LType;
 ConnectedComponentImageFilterType::Pointer connected =
   ConnectedComponentImageFilterType::New ();
 connected->SetInput(image);
 connected->Update();
 typedef itk::LabelImageToShapeLabelMapFilter< OutputImageType, LabelMapType> I2LType;
 I2LType::Pointer i2l = I2LType::New();
 i2l->SetInput( connected->GetOutput() );
 i2l->SetComputePerimeter(true);
 i2l->Update();
 LabelMapType *labelMap = i2l->GetOutput();
 std::cout << "File " << "\"" << fileName << "\""
           << " has " << labelMap->GetNumberOfLabelObjects() << " labels." << std::endl;
 // Retrieve all attributes
 for (unsigned int n = 0; n < labelMap->GetNumberOfLabelObjects(); ++n)
   {
   ShapeLabelObjectType *labelObject = labelMap->GetNthLabelObject(n);
   std::cout << "Label: "
             << itk::NumericTraits<LabelMapType::LabelType>::PrintType(labelObject->GetLabel()) << std::endl;
   std::cout << "    BoundingBox: "
             << labelObject->GetBoundingBox() << std::endl;
   std::cout << "    NumberOfPixels: "
             << labelObject->GetNumberOfPixels() << std::endl;
   std::cout << "    PhysicalSize: "
             << labelObject->GetPhysicalSize() << std::endl;
   std::cout << "    Centroid: "
             << labelObject->GetCentroid() << std::endl;
   std::cout << "    NumberOfPixelsOnBorder: "
             << labelObject->GetNumberOfPixelsOnBorder() << std::endl;
   std::cout << "    PerimeterOnBorder: "
             << labelObject->GetPerimeterOnBorder() << std::endl;
   std::cout << "    FeretDiameter: "
             << labelObject->GetFeretDiameter() << std::endl;
   std::cout << "    PrincipalMoments: "
             << labelObject->GetPrincipalMoments() << std::endl;
   std::cout << "    PrincipalAxes: "
             << labelObject->GetPrincipalAxes() << std::endl;
   std::cout << "    Elongation: "
             << labelObject->GetElongation() << std::endl;
   std::cout << "    Perimeter: "
             << labelObject->GetPerimeter() << std::endl;
   std::cout << "    Roundness: "
             << labelObject->GetRoundness() << std::endl;
   std::cout << "    EquivalentSphericalRadius: "
             << labelObject->GetEquivalentSphericalRadius() << std::endl;
   std::cout << "    EquivalentSphericalPerimeter: "
             << labelObject->GetEquivalentSphericalPerimeter() << std::endl;
   std::cout << "    EquivalentEllipsoidDiameter: "
             << labelObject->GetEquivalentEllipsoidDiameter() << std::endl;
   std::cout << "    Flatness: "
             << labelObject->GetFlatness() << std::endl;
   std::cout << "    PerimeterOnBorderRatio: "
             << labelObject->GetPerimeterOnBorderRatio() << std::endl;
   }
 return EXIT_SUCCESS;

}

template <typename TImage> void CreateImage(TImage* const image) {

 // Create an image with 2 objects
 typename TImage::IndexType start = Template:0,0;
 start[0] = 0;
 start[1] = 0;
 typename TImage::SizeType size;
 unsigned int NumRows = 200;
 unsigned int NumCols = 300;
 size[0] = NumRows;
 size[1] = NumCols;
 typename TImage::RegionType region(start, size);
 image->SetRegions(region);
 image->Allocate();
 // Make a square
 for(typename TImage::IndexValueType r = 20; r < 80; ++r)
   {
   for(typename TImage::IndexValueType c = 30; c < 100; ++c)
     {
     typename TImage::IndexType pixelIndex = Template:R,c;
     image->SetPixel(pixelIndex, 255);
     }
   }
 // Make another square
 for(typename TImage::IndexValueType r = 100; r < 130; ++r)
   {
   for(typename TImage::IndexValueType c = 115; c < 160; ++c)
     {
     typename TImage::IndexType pixelIndex = Template:R,c;
     image->SetPixel(pixelIndex, 255);
     }
   }

} </source>

CMakeLists.txt

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

project(ShapeAttributes)

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

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

endif()

add_executable(ShapeAttributes MACOSX_BUNDLE ShapeAttributes.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(ShapeAttributes ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(ShapeAttributes ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build ShapeAttributes

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

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

./ShapeAttributes

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.