[Insight-users] NumericLimits::min float / int Inconsistency Workaround?

Luis Ibanez luis.ibanez at kitware.com
Thu May 21 00:17:22 EDT 2009


Hi Geoff,

I see your point,...

But extending functionalities of existing classes using booleans
and enums is a sleepery slope that leads to disaster.

You will end up with filters that are "swiss-army-knives" that
do a little bit of every thing, but nothing quite well, and
that are plenty of bugs and prone to gather more beause all
their possible combinations can't possibly be tested.


The design of the toolkit is to have well defined pieces.
The ideal C++ class does *one* thing and does it very well.


It is true that we could add a new trait. PositiveMin may
not be the appropriate name, you are thinking more on the
minimum quanta of that type.  However, you have underestimated
the difficutlty of adding one more trait.

We regret that this appears as a surprising omission to you.
Unfortunately there will always be omission in the toolkit.
There is no way to implement all possible traits.


Please open the itkNumericTraits.h file, and find all occurrences
of "NonpositiveMin()".  Then, think that for each one of them we
will have to add the new trait. Then go to the files:


itkNumericTraitsCovariantVectorPixel.cxx
itkNumericTraitsCovariantVectorPixel.h
itkNumericTraitsFixedArrayPixel.cxx
itkNumericTraitsFixedArrayPixel.h
itkNumericTraitsRGBAPixel.cxx
itkNumericTraitsRGBAPixel.h
itkNumericTraitsRGBPixel.cxx
itkNumericTraitsRGBPixel.h
itkNumericTraitsTensorPixel.cxx
itkNumericTraitsTensorPixel.h
itkNumericTraitsVariableLengthVectorPixel.cxx
itkNumericTraitsVariableLengthVectorPixel.h
itkNumericTraitsVectorPixel.cxx
itkNumericTraitsVectorPixel.h

and follow the same exercise.


At this point you probably will agree that writing that new
class is quite an easy option, and that it will take less
time to write that class than to write this email.



Yes, there are precedents on the toolkit on how to
manage this need for variations of behaviors in filters.

Here is how we have managed:

       itkBinaryThresholdImageFilter.h
       itkBinaryThresholdProjectionImageFilter.h
       itkDoubleThresholdImageFilter.h
       itkThresholdImageFilter.h
       itkThresholdLabelerImageFilter.h


    Many classes that each do *one* thing,
    and do it well.



     Regards,


        Luis



--------------------
Geoff Topping wrote:
> Hi again,
> 
> It seems odd to write (and thus need to test and support) a separate specialized NonInclusiveLowerThresholdFilter when there already is a more general filter that could do the job given appropriate inputs.
> 
> Is there a reason NumericTraits<>::PositiveMin can't / shouldn't be added?  This would have applications outside the scope of my particular problem, and is a surprising omission, to me.
> 
> If PositiveMin  can't be added, I'd suggest modifying BinaryThresholdImageFilter to take optional second parameters for the SetLowerThreshold and SetUpperThreshold functions.  These parameters would be bools that indicate whether the whether the threshold test should include values equal to the threshold value.  The default value would be inclusive thresholds, so as to not break existing code that expects the current behaviour.
> 
> Alternatively, new functions named something like SetLowerThresholdInclusive(bool) and SetUpperThresholdInclusive(bool) could set whether each threshold is inclusive, defaulting to the current behaviour, if default parameters are frowned upon.
> 
> Similarly, SetUpperThresholdInclusive(), SetUpperThresholdExclusive) and equivalents for the lower threshold could work.
> 
> I don't know if there is an established precedent elsewhere in ITK for how such situations should be handled...
> 
> Geoff
> 
> ----------------------------------------
> 
>>Date: Wed, 20 May 2009 23:41:40 -0400
>>From: luis.ibanez at kitware.com
>>To: g_topping at hotmail.com
>>CC: insight-users at itk.org
>>Subject: Re: [Insight-users] NumericLimits::min float / int Inconsistency Workaround?
>>
>>
>>Hi Geoff,
>>
>>Thanks for the clarifications.
>>
>>The simple option is to write your own thresholding filter.
>>
>>It is actually quite easy.
>>
>>
>>Just do the following:
>>
>>A) Copy paste the files itkBinaryThresholdImageFilter. h and txx
>>
>>B) Rename them, and rename the class inside.
>>
>>C) Modify the .h file in lines 98-105.
>>
>>Replace:
>>
>>inline TOutput operator()( const TInput & A ) const
>>{
>>if ( m_LowerThreshold <= A && A <= m_UpperThreshold )
>>{
>>return m_InsideValue;
>>}
>>return m_OutsideValue;
>>}
>>
>>with
>>
>>inline TOutput operator()( const TInput & A ) const
>>{
>>if ( m_LowerThreshold < A )
>>{
>>return m_InsideValue;
>>}
>>return m_OutsideValue;
>>}
>>
>>D) Get rid of all references to Upper threshold
>>
>>
>>
>>and once you get it to work,
>>
>>
>>We encourage you to share your new filter with the community
>>by submitting it to the Insight Journal
>>
>>http://www.insight-journal.org
>>
>>
>>
>>Regards,
>>
>>
>>Luis
>>
>>
>>
>>---------------------
>>Geoff Topping wrote:
>>
>>>Hi Luis,
>>>
>>>If I use a lower threshold of NumericTraits::Zero, voxels with value equal to 0 are set to the inside value in the output image.
>>>
>>>Also, the comments in itkBinaryThresholdImageFilter.h say:
>>>
>>>* Values equal to either threshold is considered to be between the thresholds.
>>>
>>>Geoff Topping
>>>
>>>----------------------------------------
>>>
>>>
>>>>Date: Wed, 20 May 2009 22:08:52 -0400
>>>>From: luis.ibanez at kitware.com
>>>>To: g_topping at hotmail.com
>>>>CC: insight-users at itk.org
>>>>Subject: Re: [Insight-users] NumericLimits::min float / int Inconsistency Workaround?
>>>>
>>>>
>>>>Hi Geoff,
>>>>
>>>>If what you want is to get all values larger than zero,
>>>>
>>>>what is wrong with using:
>>>>
>>>>
>>>>UpperThreshold = NumericTraits::max()
>>>>LowerThreshold = NumericTraits::Zero
>>>>
>>>>??
>>>>
>>>>
>>>>Something is contradictory in your description of
>>>>requiremets.
>>>>
>>>>
>>>>
>>>>Luis
>>>>
>>>>
>>>>
>>>>-------------------------
>>>>Geoff Topping wrote:
>>>>
>>>>
>>>>>Hi all,
>>>>>
>>>>>I'm trying to make an itk::BinaryThresholdImageFilter with a lower bound that includes all values larger than zero. Doing this in a generic way has proven difficult due to the inconsistency in how itk::NumericLimits reports min values for float or integer types.
>>>>>
>>>>>To clarify, this code:
>>>>>
>>>>>std::cout << "NumericTraits float min: " << itk::NumericTraits::min() << std::endl;
>>>>>std::cout << "NumericTraits signed short min: " << itk::NumericTraits::min() << std::endl;
>>>>>
>>>>>outputs this:
>>>>>
>>>>>NumericTraits float min: 1.17549e-38
>>>>>NumericTraits signed short min: -32768
>>>>>
>>>>>And since I'm working with signed short pixels, I can't use NumericTraits::min() to set the lower threshold for my filter (although I presumably could if I was using float pixel type).
>>>>>
>>>>>
>>>>>This issue is not unique to itk::NumericTraits, but is present in std::numeric_limits as well, for reasons that are irrelivant to this discussion.
>>>>>
>>>>>
>>>>>My question: Is there a reccommended ITK style workaround for this?
>>>>>
>>>>>I want to be able to set the lower-threshold for my filter in a generic way, so don't want to set it to constants 1 (integer), as that would be well above the minimum positive value for a float type pixel.
>>>>>
>>>>>I can't use NumericTraits to get the positive minimum value representable by the type, although NumericTraits seems to be the standard way of getting this sort of information in ITK code.
>>>>>
>>>>>I could use a separate thresholding step to get a mask image with NumericLimits::Zero for values between NumericLimits::NonpositiveMin and NumericLimits::Zero and NumericLimits::One outside that range, and then multiply my original image by this mask to get a nonnegative image, and then apply a separate threshold filter on that, but I shouldn't need to do those extra steps.
>>>>>
>>>>>
>>>>>Notably, there exist implmentations of similar functions to resolve this, including one in the Boost libraries:
>>>>>
>>>>>http://www.boost.org/doc/libs/1_38_0/libs/numeric/conversion/doc/html/boost_numericconversion/bounds___traits_class.html
>>>>>
>>>>>I'm not using any Boost code in my current programs, though.
>>>>>
>>>>>
>>>>>Is there a reason NumericTraits doesn't have a function to provide the value I need?
>>>>>
>>>>>NumericTraits does have a NonpositiveMin() function, for a similar purpose, which is implemented differently for float and integer types:
>>>>>
>>>>>static float NonpositiveMin() { return -vcl_numeric_limits::max(); }
>>>>>
>>>>>static int NonpositiveMin() { return vcl_numeric_limits::min(); }
>>>>>
>>>>>
>>>>>Thanks,
>>>>>Geoff Topping
>>>>>
>>>>>
>>>>>_________________________________________________________________
>>>>>Create a cool, new character for your Windows Live™ Messenger.
>>>>>http://go.microsoft.com/?linkid=9656621
>>>>>_____________________________________
>>>>>Powered by www.kitware.com
>>>>>
>>>>>Visit other Kitware open-source projects at
>>>>>http://www.kitware.com/opensource/opensource.html
>>>>>
>>>>>Please keep messages on-topic and check the ITK FAQ at: http://www.itk.org/Wiki/ITK_FAQ
>>>>>
>>>>>Follow this link to subscribe/unsubscribe:
>>>>>http://www.itk.org/mailman/listinfo/insight-users
>>>>>
>>>
>>>
>>>_________________________________________________________________
>>>One at a time or all at once? Get updates from your friends in one place.
>>>http://go.microsoft.com/?linkid=9660827
> 
> 
> _________________________________________________________________
> Windows Live helps you keep up with all your friends, in one place.
> http://go.microsoft.com/?linkid=9660826


More information about the Insight-users mailing list