Public Member Functions | Protected Member Functions | Protected Attributes

mitk::LevelWindow Class Reference
[Data Management Classes]

Class to store level/window values. More...

#include <mitkLevelWindow.h>

List of all members.

Public Member Functions

 LevelWindow (ScalarType level=127.5, ScalarType window=255.0)
 LevelWindow (const mitk::LevelWindow &levWin)
virtual ~LevelWindow ()
ScalarType GetLevel () const
 method that returns the level value, i.e. the center of the current grey value interval
ScalarType GetWindow () const
 returns the current window size, i.e the range size of the current grey value interval
ScalarType GetDefaultLevel () const
 method returns the default level value for the image
ScalarType GetDefaultWindow () const
 returns the default window size for the image
void ResetDefaultLevelWindow ()
 Resets the level and the window value to the default values.
ScalarType GetLowerWindowBound () const
ScalarType GetUpperWindowBound () const
void SetLevelWindow (ScalarType level, ScalarType window)
void SetWindowBounds (ScalarType lowerBound, ScalarType upperBound)
void SetToMaxWindowSize ()
void SetRangeMinMax (ScalarType min, ScalarType max)
ScalarType GetRangeMin () const
ScalarType GetRangeMax () const
ScalarType GetDefaultLowerBound () const
ScalarType GetDefaultUpperBound () const
void ResetDefaultRangeMinMax ()
 the default min and max range for image will be reset
ScalarType GetRange () const
 returns the size of the grey value range
void SetDefaultLevelWindow (ScalarType level, ScalarType window)
void SetDefaultBoundaries (ScalarType low, ScalarType up)
void SetAuto (const mitk::Image *image, bool tryPicTags=true, bool guessByCentralSlice=true)
 sets level/window to the min/max greyvalues of the given Image
void SetFixed (bool fixed)
bool GetFixed () const
bool IsFixed () const
virtual bool operator== (const LevelWindow &levWin) const
 equality operator implementation that allows to compare two level windows
virtual bool operator!= (const LevelWindow &levWin) const
 non equality operator implementation that allows to compare two level windows
virtual LevelWindowoperator= (const LevelWindow &levWin)
 implementation necessary because operator made private in itk::Object

Protected Member Functions

bool SetAutoByPicTags (const mitkIpPicDescriptor *pic)
 sets level/window according to the tags in the given mitkIpPicDescriptor
void EnsureConsistency ()

Protected Attributes

ScalarType m_LowerWindowBound
ScalarType m_UpperWindowBound
ScalarType m_RangeMin
ScalarType m_RangeMax
ScalarType m_DefaultLowerBound
ScalarType m_DefaultUpperBound
bool m_Fixed

Detailed Description

Class to store level/window values.

Current min and max value are stored in m_LowerWindowBound and m_UpperWindowBound. The maximum and minimum of valid value range is stored in m_RangeMin and m_RangeMax. m_DefaultLevel amd m_DefaultWindow store the initial Level/Window values for the image. m_DefaultRangeMin and m_DefaultRangeMax store the initial minrange and maxrange for the image.

See documentation of SetAuto for information on how the level window is initialized from an image.

Examples:

Step6RegionGrowing.txx.

Definition at line 44 of file mitkLevelWindow.h.


Constructor & Destructor Documentation

mitk::LevelWindow::LevelWindow ( ScalarType  level = 127.5,
ScalarType  window = 255.0 
)
mitk::LevelWindow::LevelWindow ( const mitk::LevelWindow levWin )
mitk::LevelWindow::~LevelWindow (  ) [virtual]

Definition at line 76 of file mitkLevelWindow.cpp.

{
}

Member Function Documentation

void mitk::LevelWindow::EnsureConsistency (  ) [inline, protected]

confidence tests

if m_LowerWindowBound > m_UpperWindowBound, then the values for m_LowerWindowBound and m_UpperWindowBound will be exchanged

if m_LowerWindowBound < m_RangeMin, m_LowerWindowBound will be set to m_RangeMin. m_UpperWindowBound will be decreased the same as m_LowerWindowBound will be increased, but minimum value for m_UpperWindowBound is also m_RangeMin.

if m_UpperWindowBound > m_RangeMax, m_UpperWindowBound will be set to m_RangeMax. m_LowerWindowBound will be increased the same as m_UpperWindowBound will be decreased, but maximum value for m_LowerWindowBound is also m_RangeMax.

Definition at line 26 of file mitkLevelWindow.cpp.

References m_LowerWindowBound, m_RangeMax, m_RangeMin, and m_UpperWindowBound.

mitk::ScalarType mitk::LevelWindow::GetDefaultLevel (  ) const

method returns the default level value for the image

Definition at line 90 of file mitkLevelWindow.cpp.

Referenced by mitkLevelWindowTest(), and mitk::LevelWindowPropertySerializer::Serialize().

mitk::ScalarType mitk::LevelWindow::GetDefaultLowerBound (  ) const

Get the default range minimum value

Definition at line 182 of file mitkLevelWindow.cpp.

Referenced by mitkLevelWindowTest(), operator=(), and operator==().

{
  return m_DefaultLowerBound;
}
mitk::ScalarType mitk::LevelWindow::GetDefaultUpperBound (  ) const

Get the default range maximum value

Definition at line 177 of file mitkLevelWindow.cpp.

Referenced by mitkLevelWindowTest(), operator=(), and operator==().

{
  return m_DefaultUpperBound;
}
mitk::ScalarType mitk::LevelWindow::GetDefaultWindow (  ) const

returns the default window size for the image

Definition at line 95 of file mitkLevelWindow.cpp.

Referenced by mitkLevelWindowTest(), mitk::RegionGrowingTool::OnMousePressedOutside(), and mitk::LevelWindowPropertySerializer::Serialize().

bool mitk::LevelWindow::GetFixed (  ) const

Returns whether the level window settings are fixed (

See also:
SetFixed(bool)) or not

Definition at line 410 of file mitkLevelWindow.cpp.

Referenced by operator=(), and QmitkLevelWindowWidgetContextMenu::setFixed().

{
  return m_Fixed;
}
mitk::ScalarType mitk::LevelWindow::GetLevel (  ) const
mitk::ScalarType mitk::LevelWindow::GetLowerWindowBound (  ) const
mitk::ScalarType mitk::LevelWindow::GetRange (  ) const
mitk::ScalarType mitk::LevelWindow::GetRangeMax (  ) const
mitk::ScalarType mitk::LevelWindow::GetRangeMin (  ) const
mitk::ScalarType mitk::LevelWindow::GetUpperWindowBound (  ) const
mitk::ScalarType mitk::LevelWindow::GetWindow (  ) const
bool mitk::LevelWindow::IsFixed (  ) const
bool mitk::LevelWindow::operator!= ( const LevelWindow levWin ) const [virtual]

non equality operator implementation that allows to compare two level windows

Definition at line 434 of file mitkLevelWindow.cpp.

{
  return ! ( (*this) == levWin);
}
mitk::LevelWindow & mitk::LevelWindow::operator= ( const LevelWindow levWin ) [virtual]

implementation necessary because operator made private in itk::Object

Definition at line 439 of file mitkLevelWindow.cpp.

References GetDefaultLowerBound(), GetDefaultUpperBound(), GetFixed(), GetLowerWindowBound(), GetRangeMax(), GetRangeMin(), and GetUpperWindowBound().

{
  if (this == &levWin) {
    return *this;
  }
  else {
    m_RangeMin = levWin.GetRangeMin();
    m_RangeMax = levWin.GetRangeMax();
    m_LowerWindowBound= levWin.GetLowerWindowBound();
    m_UpperWindowBound= levWin.GetUpperWindowBound();
    m_DefaultLowerBound = levWin.GetDefaultLowerBound();
    m_DefaultUpperBound = levWin.GetDefaultUpperBound();
    m_Fixed = levWin.GetFixed();
    return *this;
  }
}
bool mitk::LevelWindow::operator== ( const LevelWindow levWin ) const [virtual]

equality operator implementation that allows to compare two level windows

Definition at line 420 of file mitkLevelWindow.cpp.

References GetDefaultLowerBound(), GetDefaultUpperBound(), GetLowerWindowBound(), GetRangeMax(), GetRangeMin(), GetUpperWindowBound(), and IsFixed().

{
  if ( m_RangeMin == levWin.GetRangeMin() && 
    m_RangeMax == levWin.GetRangeMax() && 
    m_LowerWindowBound == levWin.GetLowerWindowBound() && m_UpperWindowBound == levWin.GetUpperWindowBound() &&
    m_DefaultLowerBound == levWin.GetDefaultLowerBound() && m_DefaultUpperBound == levWin.GetDefaultUpperBound() && m_Fixed == levWin.IsFixed() ) {

      return true;
    }
  else {
    return false;
  }
}
void mitk::LevelWindow::ResetDefaultLevelWindow (  )

Resets the level and the window value to the default values.

Definition at line 100 of file mitkLevelWindow.cpp.

Referenced by mitkLevelWindowTest(), and QmitkLevelWindowWidgetContextMenu::setDefaultLevelWindow().

void mitk::LevelWindow::ResetDefaultRangeMinMax (  )

the default min and max range for image will be reset

Definition at line 187 of file mitkLevelWindow.cpp.

Referenced by mitkLevelWindowTest(), and QmitkLevelWindowWidgetContextMenu::setDefaultScaleRange().

void mitk::LevelWindow::SetAuto ( const mitk::Image image,
bool  tryPicTags = true,
bool  guessByCentralSlice = true 
)

sets level/window to the min/max greyvalues of the given Image

!

This method initializes a mitk::LevelWindow from an mitk::Image. The algorithm is as follows:

Default to taking the central image slice for quick analysis.

Compute the smallest (minValue), second smallest (min2ndValue), second largest (max2ndValue), and largest (maxValue) data value by traversing the pixel values only once. In the same scan it also computes the count of minValue values and maxValue values. After that a basic histogram with specific information about the extrems is complete.

If minValue == maxValue, the center slice is uniform and the above scan is repeated for the complete image, not just one slice

Next, special cases of images with only 1, 2 or 3 distinct data values have hand assigned level window ranges.

Next the level window is set relative to the inner range IR = lengthOf([min2ndValue, max2ndValue])

For count(minValue) > 20% the smallest values are frequent and should be distinct from the min2ndValue and larger values (minValue may be std:min, may signify something special) hence the lower end of the level window is set to min2ndValue - 0.5 * IR

For count(minValue) <= 20% the smallest values are not so important and can blend with the next ones => min(level window) = min2ndValue

And analog for max(level window): count(max2ndValue) > 20%: max(level window) = max2ndValue + 0.5 * IR count(max2ndValue) < 20%: max(level window) = max2ndValue

In both 20+ cases the level window bounds are clamped to the [minValue, maxValue] range

In consequence the level window maximizes contrast with minimal amount of computation and does do useful things if the data contains std::min or std:max values or has only 1 or 2 or 3 data values.

Examples:
Step6RegionGrowing.txx.

Definition at line 228 of file mitkLevelWindow.cpp.

References mitk::PixelType::GetBpe(), mitk::Image::GetCountOfMaxValuedVoxelsNoRecompute(), mitk::Image::GetCountOfMinValuedVoxelsNoRecompute(), mitk::Image::GetDimension(), mitk::Image::GetDimensions(), mitk::Image::GetPixelType(), mitk::Image::GetScalarValue2ndMaxNoRecompute(), mitk::Image::GetScalarValue2ndMinNoRecompute(), mitk::Image::GetScalarValueMaxNoRecompute(), mitk::Image::GetScalarValueMin(), mitk::PixelType::GetType(), mitk::BaseData::IsInitialized(), QuadProgPP::max(), min, mitk::ImageSliceSelector::New(), and QuadProgPP::pow().

Referenced by QmitkDeformableRegistrationView::Calculate(), QmitkPointBasedRegistrationView::calculateLandmarkWarping(), QmitkBSplineRegistrationView::CalculateTransformation(), mitkImageMapper2DTest(), mitkLevelWindowTest(), mitkVtkPropRendererTest(), RegionGrowing(), mitk::VolumeDataVtkMapper3D::SetDefaultProperties(), mitk::ImageMapperGL2D::SetDefaultProperties(), mitk::GPUVolumeMapper3D::SetDefaultProperties(), QmitkBasicImageProcessing::StartButton2Clicked(), QmitkBasicImageProcessing::StartButtonClicked(), and QmitkLevelWindowWidgetContextMenu::useAllGreyvaluesFromImage().

{
  if ( IsFixed() )
    return;
  
  if ( image == NULL || !image->IsInitialized() ) return;

  const mitk::Image* wholeImage = image;
  ScalarType minValue = 0.0;
  ScalarType maxValue = 0.0;
  ScalarType min2ndValue = 0.0; 
  ScalarType max2ndValue = 0.0;
  mitk::ImageSliceSelector::Pointer sliceSelector = mitk::ImageSliceSelector::New();
  if ( guessByCentralSlice )
  {
    sliceSelector->SetInput(image);
    sliceSelector->SetSliceNr(image->GetDimension(2)/2);
    sliceSelector->SetTimeNr(image->GetDimension(3)/2);
    sliceSelector->SetChannelNr(image->GetDimension(4)/2);
    sliceSelector->Update();
    image = sliceSelector->GetOutput();
    if ( image == NULL || !image->IsInitialized() ) return;

    minValue    = image->GetScalarValueMin();
    maxValue    = image->GetScalarValueMaxNoRecompute();
    min2ndValue = image->GetScalarValue2ndMinNoRecompute(); 
    max2ndValue = image->GetScalarValue2ndMaxNoRecompute();
    if ( minValue == maxValue )
    {
      // guessByCentralSlice seems to have failed, lets look at all data
      image       = wholeImage;
      minValue    = image->GetScalarValueMin();                   
      maxValue    = image->GetScalarValueMaxNoRecompute();
      min2ndValue = image->GetScalarValue2ndMinNoRecompute();
      max2ndValue = image->GetScalarValue2ndMaxNoRecompute();
    }
  }
  else
  {
    const_cast<Image*>(image)->Update();
    minValue    = image->GetScalarValueMin(0);
    maxValue    = image->GetScalarValueMaxNoRecompute(0);
    min2ndValue = image->GetScalarValue2ndMinNoRecompute(0);
    max2ndValue = image->GetScalarValue2ndMaxNoRecompute(0);
    for (unsigned int i = 1; i < image->GetDimension(3); ++i)
    {
      ScalarType minValueTemp = image->GetScalarValueMin(i);
      if (minValue > minValueTemp)
        minValue    = minValueTemp;
      ScalarType maxValueTemp = image->GetScalarValueMaxNoRecompute(i);
      if (maxValue < maxValueTemp)
        maxValue = maxValueTemp;
      ScalarType min2ndValueTemp = image->GetScalarValue2ndMinNoRecompute(i);
      if (min2ndValue > min2ndValueTemp)
        min2ndValue = min2ndValueTemp; 
      ScalarType max2ndValueTemp = image->GetScalarValue2ndMaxNoRecompute(i);
      if (max2ndValue > max2ndValueTemp)
        max2ndValue = max2ndValueTemp; 
    }
  }

  // Fix for bug# 344 Level Window wird bei Eris Cut bildern nicht richtig gesetzt
  if (image->GetPixelType().GetType() == mitkIpPicInt && image->GetPixelType().GetBpe() >= 8)
  {
    if (minValue == -(pow((double)2.0,image->GetPixelType().GetBpe())/2))
    {
      minValue = min2ndValue;
    }
  }
  // End fix

  if ( minValue == maxValue )
  {
    minValue = maxValue-1;
  }
  SetRangeMinMax(minValue, maxValue);
  SetDefaultBoundaries(minValue, maxValue);

  if ( tryPicTags ) // level and window will be set by informations provided directly by the mitkIpPicDescriptor
  {
    if ( SetAutoByPicTags(const_cast<Image*>(image)->GetPic()) )
    {
      return;
    }
  }
   
   
  unsigned int numPixelsInDataset = image->GetDimensions()[0];
  for ( unsigned int k=0;  k<image->GetDimension();  ++k ) numPixelsInDataset *= image->GetDimensions()[k];
  unsigned int minCount = image->GetCountOfMinValuedVoxelsNoRecompute();
  unsigned int maxCount = image->GetCountOfMaxValuedVoxelsNoRecompute();
  float minCountFraction = minCount/float(numPixelsInDataset);
  float maxCountFraction = maxCount/float(numPixelsInDataset);

  if ( min2ndValue == maxValue )
  {
    // noop; full range is fine
  }

  else if ( min2ndValue == max2ndValue )
  {
    ScalarType minDelta = std::min(min2ndValue-minValue, maxValue-min2ndValue);
    minValue = min2ndValue - minDelta;
    maxValue = min2ndValue + minDelta;
  }

  // now we can assume more than three distict scalar values
  else
  {
    ScalarType innerRange = max2ndValue - min2ndValue;
    
    if ( minCountFraction > 0.2 )  
    {
      ScalarType halfInnerRangeGapMinValue = min2ndValue - innerRange/2.0;
      minValue = std::max(minValue, halfInnerRangeGapMinValue);
    }
    else  
    {
      minValue = min2ndValue;
    }

    if ( maxCountFraction > 0.2 )  
    {
      ScalarType halfInnerRangeGapMaxValue = max2ndValue + innerRange/2.0;
      maxValue = std::min(maxValue, halfInnerRangeGapMaxValue);
    }
    else  
    {
      maxValue = max2ndValue;
    }
  }
  SetWindowBounds(minValue, maxValue);
  SetDefaultLevelWindow((maxValue - minValue) / 2 + minValue, maxValue - minValue);
}
bool mitk::LevelWindow::SetAutoByPicTags ( const mitkIpPicDescriptor *  pic ) [protected]

sets level/window according to the tags in the given mitkIpPicDescriptor

!

Returns:
true if tags where found

Definition at line 366 of file mitkLevelWindow.cpp.

References GET_C_W, and mitkIpPicDescriptor.

{
  if ( IsFixed() )
    return false;
  
  mitkIpPicDescriptor* pic = const_cast<mitkIpPicDescriptor*>(aPic);
  if ( pic == NULL )
  {
    return false;
  }
  mitkIpPicTSV_t *tsv = mitkIpPicQueryTag( pic, "LEVEL/WINDOW" );
  if( tsv != NULL )
  {
    double level = 0;
    double window = 0;
    #define GET_C_W( type, tsv, C, W )    \
      level = ((type *)tsv->value)[0];    \
      window = ((type *)tsv->value)[1];

    mitkIpPicFORALL_2( GET_C_W, tsv, level, window );
    
    ScalarType min = GetRangeMin();
    ScalarType max = GetRangeMax();
    if ((double)(GetRangeMin()) > (level - window/2))
    {
      min = level - window/2;
    }
    if ((double)(GetRangeMax()) < (level + window/2))
    {
      max = level + window/2;
    }
    SetRangeMinMax(min, max);
    SetDefaultBoundaries(min, max);
    SetLevelWindow( level, window );
    return true;
  }
  return false;
}
void mitk::LevelWindow::SetDefaultBoundaries ( ScalarType  low,
ScalarType  up 
)

set the default Bounderies

Referenced by mitkLevelWindowTest().

void mitk::LevelWindow::SetDefaultLevelWindow ( ScalarType  level,
ScalarType  window 
)

set the default level and window value

Referenced by mitk::LevelWindowPropertyDeserializer::Deserialize(), and mitkLevelWindowTest().

void mitk::LevelWindow::SetFixed ( bool  fixed )

If a level window is set to fixed, the set and get methods won't accept modifications to the level window settings anymore. This behaviour can be turned of by setting fixed to false;

Definition at line 405 of file mitkLevelWindow.cpp.

Referenced by mitk::LevelWindowPropertyDeserializer::Deserialize(), mitk::LabeledImageLookupTable::LabeledImageLookupTable(), and QmitkLevelWindowWidgetContextMenu::setFixed().

{
  m_Fixed = fixed; 
}
void mitk::LevelWindow::SetLevelWindow ( ScalarType  level,
ScalarType  window 
)
void mitk::LevelWindow::SetRangeMinMax ( ScalarType  min,
ScalarType  max 
)
void mitk::LevelWindow::SetToMaxWindowSize (  )

sets the window to its maximum Size in scaleRange

Definition at line 157 of file mitkLevelWindow.cpp.

Referenced by QmitkLevelWindowWidgetContextMenu::setMaximumWindow().

void mitk::LevelWindow::SetWindowBounds ( ScalarType  lowerBound,
ScalarType  upperBound 
)

Member Data Documentation

default minimum gray value of the window

Definition at line 216 of file mitkLevelWindow.h.

default maximum gray value of the window

Definition at line 221 of file mitkLevelWindow.h.

bool mitk::LevelWindow::m_Fixed [protected]

Defines whether the level window settings may be changed after initialization or not.

Definition at line 228 of file mitkLevelWindow.h.

ScalarType mitk::LevelWindow::m_LowerWindowBound [protected]

lower bound of current window

Definition at line 196 of file mitkLevelWindow.h.

Referenced by EnsureConsistency().

ScalarType mitk::LevelWindow::m_RangeMax [protected]

maximum gray value of the window

Definition at line 211 of file mitkLevelWindow.h.

Referenced by EnsureConsistency().

ScalarType mitk::LevelWindow::m_RangeMin [protected]

minimum gray value of the window

Definition at line 206 of file mitkLevelWindow.h.

Referenced by EnsureConsistency().

ScalarType mitk::LevelWindow::m_UpperWindowBound [protected]

upper bound of current window

Definition at line 201 of file mitkLevelWindow.h.

Referenced by EnsureConsistency().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines