ITK/Examples/Morphology/ForegroundHasAccurateArea

From KitwarePublic
< ITK‎ | Examples
Revision as of 14:58, 23 October 2013 by Dirkpadfield (talk | contribs)
Jump to navigationJump to search
Ball with mode off

Ball with mode on

Annulus with mode off
Annulus with mode on
A cartoon centipede... detailed description.
The Wikipede edits Myriapoda.

When using the FlatStructuringElement, the "ball" and "annulus" structuring elements have an optional flag called "foregroundHasAccurateArea". Setting this flag to true will generate structuring elements with more accurate areas, which can be especially important when morphological operations are intended to remove or retain objects of particular sizes.

This mode was introduced because the original ball and annulus structuring elements have a systematic bias in the radius of +0.5 voxels in each dimension relative to the analytic definition of the radius. For example, a 3D ball of radius 5 should have an area of 523. With this mode turned on, the number of "on" pixels is 515 (error 1.6%), but with it turned off, the area is 739 (error 41%). For a 3D annulus of radius 5 and thickness 2, the area should be 410. With this mode turned on, the area is 392 (error 4.5%), but when turned off it is 560 (error 36%). This same trend holds for balls and annuli of any radius or dimension.

For more detailed experiments with this mode, please refer to the results of the test itkFlatStructuringElementTest.cxx.

We recommend using this mode for more accurate structuring elements. However, this mode is turned off by default for backward compatibility.

ForegroundHasAccurateArea.cxx

<source lang="cpp">

  1. include "itkFlatStructuringElement.h"

// Helper function template< class SEType> bool ComputeAreaError(SEType k, unsigned int thickness = 0);

int main(int, char *[]) {

 int scalarRadius = 5;
 int scalarThickness = 2;
 bool foregroundHasAccurateArea = true;
 typedef itk::FlatStructuringElement< 2 > SE2Type;
 SE2Type::RadiusType r2;
 r2.Fill( scalarRadius );
 SE2Type k2;
 std::cout << "2D ball of radius " << scalarRadius
 << " with foregroundHasAccurateArea mode off:" << std::endl;
 k2 = SE2Type::Ball( r2 );
 ComputeAreaError(k2);
 // Test the foregroundHasAccurateArea mode.
 std::cout << "2D ball of radius " << scalarRadius
 << " with foregroundHasAccurateArea mode on:" << std::endl;
 k2 = SE2Type::Ball(r2, foregroundHasAccurateArea);
 ComputeAreaError(k2);
 std::cout << "2D annulus of radius " << scalarRadius
 << " and thickness " <<  scalarThickness
 << " with foregroundHasAccurateArea mode off:" << std::endl;
 k2 = SE2Type::Annulus(r2,scalarThickness,false);
 ComputeAreaError(k2,scalarThickness);
 // Test the foregroundHasAccurateArea mode.
 std::cout << "2D annulus of radius " << scalarRadius
 << " and thickness " <<  scalarThickness
 << " with foregroundHasAccurateArea mode on:" << std::endl;
 k2 = SE2Type::Annulus(r2,scalarThickness,false,foregroundHasAccurateArea);
 ComputeAreaError(k2,scalarThickness);
 typedef itk::FlatStructuringElement< 3 > SE3Type;
 SE3Type::RadiusType r3;
 r3.Fill( scalarRadius );
 SE3Type k3;
 std::cout << "3D ball of radius " << scalarRadius
 << " with foregroundHasAccurateArea mode off:" << std::endl;
 k3 = SE3Type::Ball( r3 );
 ComputeAreaError(k3);
 // Test the foregroundHasAccurateArea mode.
 std::cout << "3D ball of radius " << scalarRadius
 << " with foregroundHasAccurateArea mode on:" << std::endl;
 k3 = SE3Type::Ball(r3, foregroundHasAccurateArea);
 ComputeAreaError(k3);
 std::cout << "3D annulus of radius " << scalarRadius
 << " and thickness " <<  scalarThickness
 << " with foregroundHasAccurateArea mode off:" << std::endl;
 k3 = SE3Type::Annulus(r3,scalarThickness,false);
 ComputeAreaError(k3,scalarThickness);
 // Test the foregroundHasAccurateArea mode.
 std::cout << "3D annulus of radius " << scalarRadius
 << " and thickness " <<  scalarThickness
 << " with foregroundHasAccurateArea mode on:" << std::endl;
 k3 = SE3Type::Annulus(r3,scalarThickness,false,foregroundHasAccurateArea);
 ComputeAreaError(k3,scalarThickness);
 typedef itk::FlatStructuringElement< 4 > SE4Type;
 SE4Type::RadiusType r4;
 r4.Fill( scalarRadius );
 SE4Type k4;
 std::cout << "4D ball of radius " << scalarRadius
 << " with foregroundHasAccurateArea mode off:" << std::endl;
 k4 = SE4Type::Ball( r4 );
 ComputeAreaError(k4);
 // Test the foregroundHasAccurateArea mode.
 std::cout << "4D ball of radius " << scalarRadius
 << " with foregroundHasAccurateArea mode on:" << std::endl;
 k4 = SE4Type::Ball(r4, foregroundHasAccurateArea);
 ComputeAreaError(k4);
 std::cout << "4D annulus of radius " << scalarRadius
 << " and thickness " <<  scalarThickness
 << " with foregroundHasAccurateArea mode off:" << std::endl;
 k4 = SE4Type::Annulus(r4,scalarThickness,false);
 ComputeAreaError(k4,scalarThickness);
 // Test the foregroundHasAccurateArea mode.
 std::cout << "4D annulus of radius " << scalarRadius
 << " and thickness " <<  scalarThickness
 << " with foregroundHasAccurateArea mode on:" << std::endl;
 k4 = SE4Type::Annulus(r4,scalarThickness,false,foregroundHasAccurateArea);
 ComputeAreaError(k4,scalarThickness);
 return EXIT_SUCCESS;

}

template< class SEType > bool ComputeAreaError(SEType k, unsigned int thickness) {

 float expectedOuterForegroundArea = 1;
 float expectedInnerForegroundArea;
 if( thickness == 0 )
 {
   // Circle/Ellipse has no inner area to subract.
   expectedInnerForegroundArea = 0;
 }
 else
 {
   // Annulus does have inner area to subract.
   expectedInnerForegroundArea = 1;
 }
 if( SEType::NeighborhoodDimension == 2)
 {
   expectedOuterForegroundArea *= vnl_math::pi;
   expectedInnerForegroundArea *= vnl_math::pi;
 }
 else if( SEType::NeighborhoodDimension == 3 )
 {
   expectedOuterForegroundArea *= 4.0/3.0 * vnl_math::pi;
   expectedInnerForegroundArea *= 4.0/3.0 * vnl_math::pi;
 }
 else if ( SEType::NeighborhoodDimension == 4 )
 {
   expectedOuterForegroundArea *= 0.5 * vnl_math::pi * vnl_math::pi;
   expectedInnerForegroundArea *= 0.5 * vnl_math::pi * vnl_math::pi;
 }
 else
 {
   return EXIT_FAILURE;
 }
 for( unsigned int i = 0; i < SEType::NeighborhoodDimension; i++ )
 {
   expectedOuterForegroundArea *= k.GetRadius()[i];
   expectedInnerForegroundArea *= (k.GetRadius()[i] - thickness);
 }
 float expectedForegroundArea = expectedOuterForegroundArea - expectedInnerForegroundArea;
 // Show the neighborhood if it is 2D.
 typename SEType::Iterator SEIt;
 if( SEType::NeighborhoodDimension == 2 )
 {
   for( SEIt = k.Begin(); SEIt != k.End(); ++SEIt )
   {
     std::cout << *SEIt << "\t";
     if( (SEIt - k.Begin()+1) % k.GetSize()[0] == 0 )
     {
       std::cout << std::endl;
     }
   }
 }
 // Compute the area/volume.
 float computedForegroundArea = 0;
 for( SEIt = k.Begin(); SEIt != k.End(); ++SEIt )
 {
   if( *SEIt )
   {
     computedForegroundArea++;
   }
 }
 std::cout << "Expected foreground area: " << expectedForegroundArea << std::endl;
 std::cout << "Computed foreground area: " << computedForegroundArea << std::endl;
 std::cout << "Foreground area error: "
 << 100 * vnl_math_abs(expectedForegroundArea-computedForegroundArea)/expectedForegroundArea
 << "%" << "\n\n";
 
 return EXIT_FAILURE;

} </source>


CMakeLists.txt

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

project(ForegroundHasAccurateArea)

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

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

endif()

add_executable(ForegroundHasAccurateArea MACOSX_BUNDLE ForegroundHasAccurateArea.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(ForegroundHasAccurateArea ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(ForegroundHasAccurateArea ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build ForegroundHasAccurateArea

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

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

./ForegroundHasAccurateArea

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.