ITK/Examples/Morphology/FlatStructuringElementRadiusIsParametric: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Created page with "{| class="wikitable" |- ! 200px|Ball with mode off !! 200px|Ball with mode on !! [[File:Annulus_ModeOff.png|200px|Annulus with ...")
 
(Deprecated content that is moved to sphinx)
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{| class="wikitable"
{{warning|1=The media wiki content on this page is no longer maintainedThe examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releasesIn many cases, the examples on this page no longer conform to the best practices for modern ITK versions.}}
|-
! [[File:Ball_ModeOff.png|200px|Ball with mode off]] !! [[File:Ball_ModeOn.png|200px|Ball with mode on]] !! [[File:Annulus_ModeOff.png|200px|Annulus with mode off]] !! [[File:Annulus_ModeOn.png|200px|Annulus with mode on]]
|-
| Ball with mode off || Ball with mode on || Annulus with mode off || Annulus with mode on
|}
 
 
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 sizesThis 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. 
 
We recommend using this mode for more accurate structuring elements.  However, this mode is turned off by default for backward compatibility.
 
Here is the output of the test in the code below:<br>
 
2D ball of radius 5 with foregroundHasAccurateArea mode off:<br>
Expected foreground area: 78.5398<br>
Computed foreground area: 97<br>
Foreground area error: 23.5042%<br>
 
2D ball of radius 5 with foregroundHasAccurateArea mode on:<br>
Expected foreground area: 78.5398<br>
Computed foreground area: 81<br>
Foreground area error: 3.1324%<br>
 
2D annulus of radius 5 and thickness 2 with foregroundHasAccurateArea mode off:<br>
Expected foreground area: 50.2655<br>
Computed foreground area: 60<br>
Foreground area error: 19.3662%<br>
 
Writing out image Annulus_ModeOff.png
2D annulus of radius 5 and thickness 2 with foregroundHasAccurateArea mode on:
Expected foreground area: 50.2655
Computed foreground area: 52
Foreground area error: 3.45071%
 
Writing out image Annulus_ModeOn.png
3D ball of radius 5 with foregroundHasAccurateArea mode off:
Expected foreground area: 523.599
Computed foreground area: 739
Foreground area error: 41.1386%
 
3D ball of radius 5 with foregroundHasAccurateArea mode on:
Expected foreground area: 523.599
Computed foreground area: 515
Foreground area error: 1.64224%
 
3D annulus of radius 5 and thickness 2 with foregroundHasAccurateArea mode off:
Expected foreground area: 410.501
Computed foreground area: 560
Foreground area error: 36.4185%
 
3D annulus of radius 5 and thickness 2 with foregroundHasAccurateArea mode on:
Expected foreground area: 410.501
Computed foreground area: 392
Foreground area error: 4.50703%
 
4D ball of radius 5 with foregroundHasAccurateArea mode off:
Expected foreground area: 3084.25
Computed foreground area: 4785
Foreground area error: 55.143%
 
4D ball of radius 5 with foregroundHasAccurateArea mode on:
Expected foreground area: 3084.25
Computed foreground area: 2929
Foreground area error: 5.03368%
 
4D annulus of radius 5 and thickness 2 with foregroundHasAccurateArea mode off:
Expected foreground area: 2684.53
Computed foreground area: 4024
Foreground area error: 49.8957%
 
4D annulus of radius 5 and thickness 2 with foregroundHasAccurateArea mode on:
Expected foreground area: 2684.53
Computed foreground area: 2504
Foreground area error: 6.72491%
 
 
==FlatStructuringElementForegroundHasAccurateArea.cxx==
<source lang="cpp">
 
#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>
 
 
{{ITKCMakeLists|{{SUBPAGENAME}}}}

Latest revision as of 19:08, 6 June 2019

Warning: The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releases. In many cases, the examples on this page no longer conform to the best practices for modern ITK versions.