Discussion:
[ITK-users] VariableLengthVector and multiplication
Cyril Mory
2017-11-22 09:41:35 UTC
Permalink
Dear ITK users,

I am using itk::VectorImage in some of my code, which uses
itk::VariableLengthVector as pixel type. And I am wondering why
itk::VariableLengthVector has so little support for multiplication.
Currently, the * operator only supports multiplication by a scalar.

It probably isn't simple, but I would need three additional kinds of
multiplication:

- dot product with another VariableLengthVector (that has the same
length, although it is probably a waste of time to perform the check
every time), returning a scalar

- component-wise multiplication, returning a VariableLengthVector of the
same length

- left or right multiplication with a matrix (possibly an
itk::VariableSizeMatrix) that has the correct size, but I understand
that this is probably the most complex one, and since it only occurs
rarely in my code, I can handle it with conversions to vnl::vector and
vnl::matrix

Are there constraints that prevent at least the dot product and
component-wise multiplication operators from being implemented ? If not,
then I'd be happy to give it a try. Since both differ only by the return
type, two different operators would have to be used (I guess). Do you
have suggestions (which one should use *, and what should be the other
operator) ? In itk::Vector and itk::CovariantVector, the * operator is
used for dot product.

Regards,

Cyril


The ITK community is transitioning from this mailing list to discourse.itk.org. Please join us there!
________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

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://public.kitware.com/mailman/listinfo/insight-users
Lowekamp, Bradley (NIH/NLM/LHC) [C]
2017-11-22 13:38:41 UTC
Permalink
Hello,

There are an incredible number of different per-pixel operations that could be implemented as ITK filters. We cannot provide them all. Many of the basic operations are implemented as ITK filters these include performing the basic C++ operators, such as +, -, * and /, on a per-pixel basis.

As you indicate there are many possible meanings for multiplication of vector images, which can lead to confusion.

ITK has a flexible set of Unary[1], Binary[2] functor filters. Classes like the MultiplyImageFilter[3], are implemented by deriving from the base functor classes. However it is easier to just use the base functor filter and set the proper or custom functor, as in this example [4].

It is fairly easy to write a functor for your specific purposes by following the existing set [5]. It is common for filters to internally define a private functor to perform one step in a large filter. Moving from writing for loops on pixels to writing custom functors is part of good usage of ITK.


Brad


[1] https://itk.org/Doxygen/html/classitk_1_1UnaryFunctorImageFilter.html
[2] https://itk.org/Doxygen/html/classitk_1_1BinaryFunctorImageFilter.html
[3] https://itk.org/Doxygen/html/classitk_1_1MultiplyImageFilter.html
[4] https://itk.org/Doxygen/html/WikiExamples_2ImageProcessing_2BinaryFunctorImageFilter_8cxx-example.html#_a1

On 11/22/17, 5:15 AM, "Cyril Mory" <***@creatis.insa-lyon.fr> wrote:

Dear ITK users,

I am using itk::VectorImage in some of my code, which uses
itk::VariableLengthVector as pixel type. And I am wondering why
itk::VariableLengthVector has so little support for multiplication.
Currently, the * operator only supports multiplication by a scalar.

It probably isn't simple, but I would need three additional kinds of
multiplication:

- dot product with another VariableLengthVector (that has the same
length, although it is probably a waste of time to perform the check
every time), returning a scalar

- component-wise multiplication, returning a VariableLengthVector of the
same length

- left or right multiplication with a matrix (possibly an
itk::VariableSizeMatrix) that has the correct size, but I understand
that this is probably the most complex one, and since it only occurs
rarely in my code, I can handle it with conversions to vnl::vector and
vnl::matrix

Are there constraints that prevent at least the dot product and
component-wise multiplication operators from being implemented ? If not,
then I'd be happy to give it a try. Since both differ only by the return
type, two different operators would have to be used (I guess). Do you
have suggestions (which one should use *, and what should be the other
operator) ? In itk::Vector and itk::CovariantVector, the * operator is
used for dot product.

Regards,

Cyril


The ITK community is transitioning from this mailing list to discourse.itk.org. Please join us there!
________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

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://public.kitware.com/mailman/listinfo/insight-users


The ITK community is transitioning from this mailing list to discourse.itk.org. Please join us there!
________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

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://public.kitware.com/mailman/listinfo/insight-users
Cyril Mory
2017-11-22 14:54:49 UTC
Permalink
Thanks for this answer.

I am trying the approach you suggested, and would like to write a dot
product functor that would work either on scalars (in which case it
would perform a simple product) or on itk::VariableLengthVector of
scalars (in which case it would perform a dot product).

I found the "itk::NumericTraits<TPixel>::GetLength()" method, which
works in both cases, and so I tried this:


namespace Functor
{
  template< class TPixel, class TInternal>
  class DotProduct
  {
  public:
    DotProduct() {}
    ~DotProduct() {}
    bool operator!=(const DotProduct &) const
    {
      return false;
    }

    bool operator==(const DotProduct & other) const
    {
      return !( *this != other );
    }

    inline TInternal operator()(const TPixel & A, const TPixel & B) const
    {
    TInternal out = 0;
    for (unsigned int component=0; component <
itk::NumericTraits<TPixel>::GetLength(); component++)
      {
      out += A[component] * B[component];
      }
    return out;
    }
  };
} // end namespace functor


But it does not compile with itk::Image inputs, since the pixels A and B
have no [ ] operator. Is there a standard way around this problem ?

Regards,
Cyril
Post by Lowekamp, Bradley (NIH/NLM/LHC) [C]
Hello,
There are an incredible number of different per-pixel operations that could be implemented as ITK filters. We cannot provide them all. Many of the basic operations are implemented as ITK filters these include performing the basic C++ operators, such as +, -, * and /, on a per-pixel basis.
As you indicate there are many possible meanings for multiplication of vector images, which can lead to confusion.
ITK has a flexible set of Unary[1], Binary[2] functor filters. Classes like the MultiplyImageFilter[3], are implemented by deriving from the base functor classes. However it is easier to just use the base functor filter and set the proper or custom functor, as in this example [4].
It is fairly easy to write a functor for your specific purposes by following the existing set [5]. It is common for filters to internally define a private functor to perform one step in a large filter. Moving from writing for loops on pixels to writing custom functors is part of good usage of ITK.
Brad
[1] https://itk.org/Doxygen/html/classitk_1_1UnaryFunctorImageFilter.html
[2] https://itk.org/Doxygen/html/classitk_1_1BinaryFunctorImageFilter.html
[3] https://itk.org/Doxygen/html/classitk_1_1MultiplyImageFilter.html
[4] https://itk.org/Doxygen/html/WikiExamples_2ImageProcessing_2BinaryFunctorImageFilter_8cxx-example.html#_a1
Dear ITK users,
I am using itk::VectorImage in some of my code, which uses
itk::VariableLengthVector as pixel type. And I am wondering why
itk::VariableLengthVector has so little support for multiplication.
Currently, the * operator only supports multiplication by a scalar.
It probably isn't simple, but I would need three additional kinds of
- dot product with another VariableLengthVector (that has the same
length, although it is probably a waste of time to perform the check
every time), returning a scalar
- component-wise multiplication, returning a VariableLengthVector of the
same length
- left or right multiplication with a matrix (possibly an
itk::VariableSizeMatrix) that has the correct size, but I understand
that this is probably the most complex one, and since it only occurs
rarely in my code, I can handle it with conversions to vnl::vector and
vnl::matrix
Are there constraints that prevent at least the dot product and
component-wise multiplication operators from being implemented ? If not,
then I'd be happy to give it a try. Since both differ only by the return
type, two different operators would have to be used (I guess). Do you
have suggestions (which one should use *, and what should be the other
operator) ? In itk::Vector and itk::CovariantVector, the * operator is
used for dot product.
Regards,
Cyril
The ITK community is transitioning from this mailing list to discourse.itk.org. Please join us there!
________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
http://www.kitware.com/products/protraining.php
http://www.itk.org/Wiki/ITK_FAQ
http://public.kitware.com/mailman/listinfo/insight-users
The ITK community is transitioning from this mailing list to discourse.itk.org. Please join us there!
________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

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://public.kitware.com/mailman/listinfo/in
Dženan Zukić
2017-11-22 15:17:15 UTC
Permalink
Hi Cyril,

Francois has recently used compileif construct. That might help you. But I
can't find an instance of it right now.

Also, we are moving to discourse <https://discourse.itk.org/>. Please post
follow-ups there.

Regards,
DÅŸenan
Post by Cyril Mory
Thanks for this answer.
I am trying the approach you suggested, and would like to write a dot
product functor that would work either on scalars (in which case it would
perform a simple product) or on itk::VariableLengthVector of scalars (in
which case it would perform a dot product).
I found the "itk::NumericTraits<TPixel>::GetLength()" method, which works
namespace Functor
{
template< class TPixel, class TInternal>
class DotProduct
{
DotProduct() {}
~DotProduct() {}
bool operator!=(const DotProduct &) const
{
return false;
}
bool operator==(const DotProduct & other) const
{
return !( *this != other );
}
inline TInternal operator()(const TPixel & A, const TPixel & B) const
{
TInternal out = 0;
for (unsigned int component=0; component <
itk::NumericTraits<TPixel>::GetLength(); component++)
{
out += A[component] * B[component];
}
return out;
}
};
} // end namespace functor
But it does not compile with itk::Image inputs, since the pixels A and B
have no [ ] operator. Is there a standard way around this problem ?
Regards,
Cyril
Post by Lowekamp, Bradley (NIH/NLM/LHC) [C]
Hello,
There are an incredible number of different per-pixel operations that
could be implemented as ITK filters. We cannot provide them all. Many of
the basic operations are implemented as ITK filters these include
performing the basic C++ operators, such as +, -, * and /, on a per-pixel
basis.
As you indicate there are many possible meanings for multiplication of
vector images, which can lead to confusion.
ITK has a flexible set of Unary[1], Binary[2] functor filters. Classes
like the MultiplyImageFilter[3], are implemented by deriving from the base
functor classes. However it is easier to just use the base functor filter
and set the proper or custom functor, as in this example [4].
It is fairly easy to write a functor for your specific purposes by
following the existing set [5]. It is common for filters to internally
define a private functor to perform one step in a large filter. Moving from
writing for loops on pixels to writing custom functors is part of good
usage of ITK.
Brad
[1] https://itk.org/Doxygen/html/classitk_1_1UnaryFunctorImageFilter.html
[2] https://itk.org/Doxygen/html/classitk_1_1BinaryFunctorImageF
ilter.html
[3] https://itk.org/Doxygen/html/classitk_1_1MultiplyImageFilter.html
[4] https://itk.org/Doxygen/html/WikiExamples_2ImageProcessing_2
BinaryFunctorImageFilter_8cxx-example.html#_a1
Dear ITK users,
I am using itk::VectorImage in some of my code, which uses
itk::VariableLengthVector as pixel type. And I am wondering why
itk::VariableLengthVector has so little support for multiplication.
Currently, the * operator only supports multiplication by a scalar.
It probably isn't simple, but I would need three additional kinds of
- dot product with another VariableLengthVector (that has the same
length, although it is probably a waste of time to perform the check
every time), returning a scalar
- component-wise multiplication, returning a
VariableLengthVector of the
same length
- left or right multiplication with a matrix (possibly an
itk::VariableSizeMatrix) that has the correct size, but I understand
that this is probably the most complex one, and since it only occurs
rarely in my code, I can handle it with conversions to vnl::vector and
vnl::matrix
Are there constraints that prevent at least the dot product and
component-wise multiplication operators from being implemented ? If not,
then I'd be happy to give it a try. Since both differ only by the return
type, two different operators would have to be used (I guess). Do you
have suggestions (which one should use *, and what should be the other
operator) ? In itk::Vector and itk::CovariantVector, the * operator is
used for dot product.
Regards,
Cyril
The ITK community is transitioning from this mailing list
to discourse.itk.org. Please join us there!
________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
http://www.kitware.com/products/protraining.php
http://www.itk.org/Wiki/ITK_FAQ
http://public.kitware.com/mailman/listinfo/insight-users
The ITK community is transitioning from this mailing list to
discourse.itk.org. Please join us there!
________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
http://www.kitware.com/products/protraining.php
http://www.itk.org/Wiki/ITK_FAQ
http://public.kitware.com/mailman/listinfo/insight-users
Dženan Zukić
2017-11-22 17:42:44 UTC
Permalink
I found it. Construct is called *EnableIf*, and you can see how it is used
in *ITK\Modules\Core\Common\test\itkEnableIfTest.cxx*

Regards
Post by Dženan Zukić
Hi Cyril,
Francois has recently used compileif construct. That might help you. But I
can't find an instance of it right now.
Also, we are moving to discourse <https://discourse.itk.org/>. Please
post follow-ups there.
Regards,
DÅŸenan
Post by Cyril Mory
Thanks for this answer.
I am trying the approach you suggested, and would like to write a dot
product functor that would work either on scalars (in which case it would
perform a simple product) or on itk::VariableLengthVector of scalars (in
which case it would perform a dot product).
I found the "itk::NumericTraits<TPixel>::GetLength()" method, which
namespace Functor
{
template< class TPixel, class TInternal>
class DotProduct
{
DotProduct() {}
~DotProduct() {}
bool operator!=(const DotProduct &) const
{
return false;
}
bool operator==(const DotProduct & other) const
{
return !( *this != other );
}
inline TInternal operator()(const TPixel & A, const TPixel & B) const
{
TInternal out = 0;
for (unsigned int component=0; component <
itk::NumericTraits<TPixel>::GetLength(); component++)
{
out += A[component] * B[component];
}
return out;
}
};
} // end namespace functor
But it does not compile with itk::Image inputs, since the pixels A and B
have no [ ] operator. Is there a standard way around this problem ?
Regards,
Cyril
Post by Lowekamp, Bradley (NIH/NLM/LHC) [C]
Hello,
There are an incredible number of different per-pixel operations that
could be implemented as ITK filters. We cannot provide them all. Many of
the basic operations are implemented as ITK filters these include
performing the basic C++ operators, such as +, -, * and /, on a per-pixel
basis.
As you indicate there are many possible meanings for multiplication of
vector images, which can lead to confusion.
ITK has a flexible set of Unary[1], Binary[2] functor filters. Classes
like the MultiplyImageFilter[3], are implemented by deriving from the base
functor classes. However it is easier to just use the base functor filter
and set the proper or custom functor, as in this example [4].
It is fairly easy to write a functor for your specific purposes by
following the existing set [5]. It is common for filters to internally
define a private functor to perform one step in a large filter. Moving from
writing for loops on pixels to writing custom functors is part of good
usage of ITK.
Brad
[1] https://itk.org/Doxygen/html/classitk_1_1UnaryFunctorImageFi
lter.html
[2] https://itk.org/Doxygen/html/classitk_1_1BinaryFunctorImageF
ilter.html
[3] https://itk.org/Doxygen/html/classitk_1_1MultiplyImageFilter.html
[4] https://itk.org/Doxygen/html/WikiExamples_2ImageProcessing_2
BinaryFunctorImageFilter_8cxx-example.html#_a1
Dear ITK users,
I am using itk::VectorImage in some of my code, which uses
itk::VariableLengthVector as pixel type. And I am wondering why
itk::VariableLengthVector has so little support for multiplication.
Currently, the * operator only supports multiplication by a scalar.
It probably isn't simple, but I would need three additional kinds of
- dot product with another VariableLengthVector (that has the same
length, although it is probably a waste of time to perform the check
every time), returning a scalar
- component-wise multiplication, returning a
VariableLengthVector of the
same length
- left or right multiplication with a matrix (possibly an
itk::VariableSizeMatrix) that has the correct size, but I understand
that this is probably the most complex one, and since it only occurs
rarely in my code, I can handle it with conversions to vnl::vector and
vnl::matrix
Are there constraints that prevent at least the dot product and
component-wise multiplication operators from being implemented ? If not,
then I'd be happy to give it a try. Since both differ only by the return
type, two different operators would have to be used (I guess). Do you
have suggestions (which one should use *, and what should be the other
operator) ? In itk::Vector and itk::CovariantVector, the * operator is
used for dot product.
Regards,
Cyril
The ITK community is transitioning from this mailing list
to discourse.itk.org. Please join us there!
________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
http://www.kitware.com/products/protraining.php
http://www.itk.org/Wiki/ITK_FAQ
http://public.kitware.com/mailman/listinfo/insight-users
The ITK community is transitioning from this mailing list to
discourse.itk.org. Please join us there!
________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
http://www.kitware.com/products/protraining.php
http://www.itk.org/Wiki/ITK_FAQ
http://public.kitware.com/mailman/listinfo/insight-users
Cyril Mory
2017-11-24 11:54:25 UTC
Permalink
Thanks.

Here is the solution I used, which works but may be sub-optimal:

namespace Functor
{
  template< class TPixel, class TInternal>
  class DotProduct
  {
  public:
    DotProduct() {}
    ~DotProduct() {}
    bool operator!=(const DotProduct &) const
    {
      return false;
    }

    bool operator==(const DotProduct & other) const
    {
      return !( *this != other );
    }

  template<typename T = TPixel>
  inline
  typename itk::mpl::EnableIf<itk::mpl::IsSame<T, TInternal>,
TInternal>::Type
  operator()(const TInternal & A, const TInternal & B) const
  { return static_cast<TInternal>( A * B ); }

  template<typename T = TPixel>
  typename itk::mpl::DisableIf<itk::mpl::IsSame<T, TInternal>,
TInternal>::Type
  operator()(const TPixel & A, const TPixel & B) const
    {
    TInternal out = 0;
    for (unsigned int component=0; component <
itk::NumericTraits<TPixel>::GetLength(A); component++)
      {
      out += A[component] * B[component];
      }
    return out;
    }
  };
} // end namespace functor
Post by Dženan Zukić
I found it. Construct is called *EnableIf*, and you can see how it is
used in /ITK\Modules\Core\Common\test\itkEnableIfTest.cxx/
Regards
Hi Cyril,
Francois has recently used compileif construct. That might help
you. But I can't find an instance of it right now.
Also, we are moving to discourse <https://discourse.itk.org/>.
Please post follow-ups there.
Regards,
DÅŸenan
On Wed, Nov 22, 2017 at 9:54 AM, Cyril Mory
Thanks for this answer.
I am trying the approach you suggested, and would like to
write a dot product functor that would work either on scalars
(in which case it would perform a simple product) or on
itk::VariableLengthVector of scalars (in which case it would
perform a dot product).
I found the "itk::NumericTraits<TPixel>::GetLength()" method,
namespace Functor
{
  template< class TPixel, class TInternal>
  class DotProduct
  {
    DotProduct() {}
    ~DotProduct() {}
    bool operator!=(const DotProduct &) const
    {
      return false;
    }
    bool operator==(const DotProduct & other) const
    {
      return !( *this != other );
    }
    inline TInternal operator()(const TPixel & A, const TPixel
& B) const
    {
    TInternal out = 0;
    for (unsigned int component=0; component <
itk::NumericTraits<TPixel>::GetLength(); component++)
      {
      out += A[component] * B[component];
      }
    return out;
    }
  };
} // end namespace functor
But it does not compile with itk::Image inputs, since the
pixels A and B have no [ ] operator. Is there a standard way
around this problem ?
Regards,
Cyril
Hello,
There are an incredible number of different per-pixel
operations that could be implemented as ITK filters. We
cannot provide them all. Many of the basic operations are
implemented as ITK filters these include performing the
basic C++ operators, such as +, -, * and /, on a per-pixel
basis.
As you indicate there are many possible meanings for
multiplication of vector images, which can lead to confusion.
ITK has a flexible set of Unary[1], Binary[2] functor
filters. Classes like the MultiplyImageFilter[3], are
implemented by deriving from the base functor classes.
However it is easier to just use the base functor filter
and set the proper or custom functor, as in this example [4].
It is fairly easy to write a functor for your specific
purposes by following the existing set [5]. It is common
for filters to internally define a private functor to
perform one step in a large filter. Moving from writing
for loops on pixels to writing custom functors is part of
good usage of ITK.
Brad
[1]
https://itk.org/Doxygen/html/classitk_1_1UnaryFunctorImageFilter.html
<https://itk.org/Doxygen/html/classitk_1_1UnaryFunctorImageFilter.html>
[2]
https://itk.org/Doxygen/html/classitk_1_1BinaryFunctorImageFilter.html
<https://itk.org/Doxygen/html/classitk_1_1BinaryFunctorImageFilter.html>
[3]
https://itk.org/Doxygen/html/classitk_1_1MultiplyImageFilter.html
<https://itk.org/Doxygen/html/classitk_1_1MultiplyImageFilter.html>
[4]
https://itk.org/Doxygen/html/WikiExamples_2ImageProcessing_2BinaryFunctorImageFilter_8cxx-example.html#_a1
<https://itk.org/Doxygen/html/WikiExamples_2ImageProcessing_2BinaryFunctorImageFilter_8cxx-example.html#_a1>
On 11/22/17, 5:15 AM, "Cyril Mory"
     Dear ITK users,
          I am using itk::VectorImage in some of my code,
which uses
     itk::VariableLengthVector as pixel type. And I am
wondering why
     itk::VariableLengthVector has so little support for
multiplication.
     Currently, the * operator only supports
multiplication by a scalar.
          It probably isn't simple, but I would need three
additional kinds of
          - dot product with another VariableLengthVector
(that has the same
     length, although it is probably a waste of time to
perform the check
     every time), returning a scalar
          - component-wise multiplication, returning a
VariableLengthVector of the
     same length
          - left or right multiplication with a matrix
(possibly an
     itk::VariableSizeMatrix) that has the correct size,
but I understand
     that this is probably the most complex one, and since
it only occurs
     rarely in my code, I can handle it with conversions
to vnl::vector and
     vnl::matrix
          Are there constraints that prevent at least the
dot product and
     component-wise multiplication operators from being
implemented ? If not,
     then I'd be happy to give it a try. Since both differ
only by the return
     type, two different operators would have to be used
(I guess). Do you
     have suggestions (which one should use *, and what
should be the other
     operator) ? In itk::Vector and itk::CovariantVector,
the * operator is
     used for dot product.
          Regards,
          Cyril
               The ITK community is transitioning from
this mailing list to discourse.itk.org
<http://discourse.itk.org>. Please join us there!
     ________________________________
     Powered by www.kitware.com <http://www.kitware.com>
          Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
<http://www.kitware.com/opensource/opensource.html>
          Kitware offers ITK Training Courses, for more
http://www.kitware.com/products/protraining.php
<http://www.kitware.com/products/protraining.php>
          Please keep messages on-topic and check the ITK
http://www.itk.org/Wiki/ITK_FAQ
<http://www.itk.org/Wiki/ITK_FAQ>
http://public.kitware.com/mailman/listinfo/insight-users
<http://public.kitware.com/mailman/listinfo/insight-users>
The ITK community is transitioning from this mailing list to
discourse.itk.org <http://discourse.itk.org>. Please join us
there!
________________________________
Powered by www.kitware.com <http://www.kitware.com>
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
<http://www.kitware.com/opensource/opensource.html>
http://www.kitware.com/products/protraining.php
<http://www.kitware.com/products/protraining.php>
http://www.itk.org/Wiki/ITK_FAQ <http://www.itk.org/Wiki/ITK_FAQ>
http://public.kitware.com/mailman/listinfo/insight-users
<http://public.kitware.com/mailman/listinfo/insight-users>
Dženan Zukić
2017-11-24 13:46:05 UTC
Permalink
That looks about right. Thanks for sharing your solution.

Regards,
DÅŸenan
Post by Cyril Mory
Thanks.
namespace Functor
{
template< class TPixel, class TInternal>
class DotProduct
{
DotProduct() {}
~DotProduct() {}
bool operator!=(const DotProduct &) const
{
return false;
}
bool operator==(const DotProduct & other) const
{
return !( *this != other );
}
template<typename T = TPixel>
inline
typename itk::mpl::EnableIf<itk::mpl::IsSame<T, TInternal>,
TInternal>::Type
operator()(const TInternal & A, const TInternal & B) const
{ return static_cast<TInternal>( A * B ); }
template<typename T = TPixel>
typename itk::mpl::DisableIf<itk::mpl::IsSame<T, TInternal>,
TInternal>::Type
operator()(const TPixel & A, const TPixel & B) const
{
TInternal out = 0;
for (unsigned int component=0; component < itk::NumericTraits<TPixel>::GetLength(A);
component++)
{
out += A[component] * B[component];
}
return out;
}
};
} // end namespace functor
I found it. Construct is called *EnableIf*, and you can see how it is
used in *ITK\Modules\Core\Common\test\itkEnableIfTest.cxx*
Regards
Post by Dženan Zukić
Hi Cyril,
Francois has recently used compileif construct. That might help you. But
I can't find an instance of it right now.
Also, we are moving to discourse <https://discourse.itk.org/>. Please
post follow-ups there.
Regards,
DÅŸenan
On Wed, Nov 22, 2017 at 9:54 AM, Cyril Mory <
Post by Cyril Mory
Thanks for this answer.
I am trying the approach you suggested, and would like to write a dot
product functor that would work either on scalars (in which case it would
perform a simple product) or on itk::VariableLengthVector of scalars (in
which case it would perform a dot product).
I found the "itk::NumericTraits<TPixel>::GetLength()" method, which
namespace Functor
{
template< class TPixel, class TInternal>
class DotProduct
{
DotProduct() {}
~DotProduct() {}
bool operator!=(const DotProduct &) const
{
return false;
}
bool operator==(const DotProduct & other) const
{
return !( *this != other );
}
inline TInternal operator()(const TPixel & A, const TPixel & B) const
{
TInternal out = 0;
for (unsigned int component=0; component <
itk::NumericTraits<TPixel>::GetLength(); component++)
{
out += A[component] * B[component];
}
return out;
}
};
} // end namespace functor
But it does not compile with itk::Image inputs, since the pixels A and B
have no [ ] operator. Is there a standard way around this problem ?
Regards,
Cyril
Post by Lowekamp, Bradley (NIH/NLM/LHC) [C]
Hello,
There are an incredible number of different per-pixel operations that
could be implemented as ITK filters. We cannot provide them all. Many of
the basic operations are implemented as ITK filters these include
performing the basic C++ operators, such as +, -, * and /, on a per-pixel
basis.
As you indicate there are many possible meanings for multiplication of
vector images, which can lead to confusion.
ITK has a flexible set of Unary[1], Binary[2] functor filters. Classes
like the MultiplyImageFilter[3], are implemented by deriving from the base
functor classes. However it is easier to just use the base functor filter
and set the proper or custom functor, as in this example [4].
It is fairly easy to write a functor for your specific purposes by
following the existing set [5]. It is common for filters to internally
define a private functor to perform one step in a large filter. Moving from
writing for loops on pixels to writing custom functors is part of good
usage of ITK.
Brad
[1] https://itk.org/Doxygen/html/classitk_1_1UnaryFunctorImageFi
lter.html
[2] https://itk.org/Doxygen/html/classitk_1_1BinaryFunctorImageF
ilter.html
[3] https://itk.org/Doxygen/html/classitk_1_1MultiplyImageFilter.html
[4] https://itk.org/Doxygen/html/WikiExamples_2ImageProcessing_2
BinaryFunctorImageFilter_8cxx-example.html#_a1
Dear ITK users,
I am using itk::VectorImage in some of my code, which uses
itk::VariableLengthVector as pixel type. And I am wondering why
itk::VariableLengthVector has so little support for multiplication.
Currently, the * operator only supports multiplication by a scalar.
It probably isn't simple, but I would need three additional kinds of
- dot product with another VariableLengthVector (that has the same
length, although it is probably a waste of time to perform the check
every time), returning a scalar
- component-wise multiplication, returning a
VariableLengthVector of the
same length
- left or right multiplication with a matrix (possibly an
itk::VariableSizeMatrix) that has the correct size, but I understand
that this is probably the most complex one, and since it only occurs
rarely in my code, I can handle it with conversions to vnl::vector and
vnl::matrix
Are there constraints that prevent at least the dot product and
component-wise multiplication operators from being implemented ? If not,
then I'd be happy to give it a try. Since both differ only by the return
type, two different operators would have to be used (I guess). Do you
have suggestions (which one should use *, and what should be the other
operator) ? In itk::Vector and itk::CovariantVector, the * operator is
used for dot product.
Regards,
Cyril
The ITK community is transitioning from this mailing
list to discourse.itk.org. Please join us there!
________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
http://www.kitware.com/products/protraining.php
http://www.itk.org/Wiki/ITK_FAQ
http://public.kitware.com/mailman/listinfo/insight-users
The ITK community is transitioning from this mailing list to
discourse.itk.org. Please join us there!
________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
http://www.kitware.com/products/protraining.php
http://www.itk.org/Wiki/ITK_FAQ
http://public.kitware.com/mailman/listinfo/insight-users
Loading...