Author Topic: PCL MorphologicalTransformation How to define StructuringElement?  (Read 5119 times)

Offline NKV

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 677
Hi,
There are PCL MorphologicalTransformation method:
Code: [Select]
MedianFilter M;
BoxStructure sK(3);
MorphologicalTransformation MT(M,sK);
MT >> filteredImage;

Please show me PCL example to define Structure shown on picture in attachment.

Best regards,
Nikolay.
« Last Edit: 2012 February 08 06:50:06 by NKV »

Offline Carlos Milovic

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2172
  • Join the dark side... we have cookies
    • http://www.astrophoto.cl
Re: PCL MorphologicalTransformation How to define StructuringElement?
« Reply #1 on: 2012 February 08 07:37:34 »
I guess that Juan knows a better way to do this... but meanwhile, this is a method to create any structure

First, you need to create a new class, "Structure" works fine. Inside, define an array of uint8 of three dimensions (row, column and way) (it may works also with pcl_bool variables).
Define a function that reads each element (returns false or true), another to get the size (number of rows or columns... should be a square), and to get the number of ways.

Now, you have to create another class, that inherits the properties of StructuringElement. This class has access to the contents of the class Structure you created before.
This is an example from the source code of MorphologicalTransform

Code: [Select]
class MorphologicalTransformStructure : public StructuringElement
{
public:

   MorphologicalTransformStructure( const MorphologicalTransformInstance& M ) :
   StructuringElement(), structure( M.GetStructure() )
   {
      Resize( structure.Size() );
   }

   virtual int NumberOfWays() const
   {
      return structure.NumberOfWays();
   }

   virtual bool ElementExists( int i, int j, int k ) const // row, col, way
   {
      return structure.Element( k, j, i );
   }

private:

   const Structure& structure;
};

Now you may just use that class:

      MorphologicalTransformStructure S( M ); //this is the class we created above... M is the instance of the MorphologicalTransform process... you may rewrite the class to create the structure you want, directly reading an array.
      MorphologicalTransform T;
      T.SetStructure( S );

It is quite a convoluted process to set the values... but I cannot find anything easier... hope this helps.
Regards,

Carlos Milovic F.
--------------------------------
PixInsight Project Developer
http://www.pixinsight.com

Offline Carlos Milovic

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2172
  • Join the dark side... we have cookies
    • http://www.astrophoto.cl
Re: PCL MorphologicalTransformation How to define StructuringElement?
« Reply #2 on: 2012 February 08 07:39:28 »
Now that I think it... you don't need the first class.... just create the array inside the second one, and output the values using the same functions.
Regards,

Carlos Milovic F.
--------------------------------
PixInsight Project Developer
http://www.pixinsight.com

Offline NKV

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 677
Re: PCL MorphologicalTransformation How to define StructuringElement?
« Reply #3 on: 2012 February 08 07:44:37 »
Carlos, thank you!

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PCL MorphologicalTransformation How to define StructuringElement?
« Reply #4 on: 2012 February 09 04:02:04 »
Hi Nikolay,

This is from PCL 2.0:

Code: [Select]
/*!
 * \class BitmapStructure
 * \brief A structuring element where static strings are used to define custom
 * existence matrices.
 *
 * %BitmapStructure allows you to build a structuring element using character
 * strings to define the existence of structure elements. Each string defines a
 * structure way, where 'x' characters correspond to existing structure
 * elements. For example, the following array of static strings:
 *
 * \verbatim
 * const char* B[] = { "--x--"
 *                     "-xxx-"
 *                     "xxxxx"
 *                     "-xxx-"
 *                     "--x--",
 *                     //
 *                     "-xxx-"
 *                     "xxxxx"
 *                     "xxxxx"
 *                     "xxxxx"
 *                     "-xxx-" };
 * \endverbatim
 *
 * would define a 5x5 structure with two ways. 'x' characters define existing
 * elements; other character values ('-' in this case) are interpreted as
 * nonexistent elements. The corresponding %BitmapStructure object would be
 * constructed as follows:
 *
 * \verbatim
 * BitmapStructure S( B, 5, 2 );
 * \endverbatim
 *
 * This class is a convenient, straightforward approach to defining multi-way
 * custom structuring elements that can be used directly with the
 * MorphologicalTransformation class.
 */
class PCL_CLASS BitmapStructure : public StructuringElement
{
public:

   /*!
    * \internal
    * Represents a structure bitmap. Each bitmap defines the existence matrix
    * for a \e way of the structuring element.
    */
   typedef IsoString       bitmap;

   /*!
    * \internal
    * Represents a set of bitmaps used to define the structure ways in a
    * %BitmapStructure object.
    */
   typedef IsoStringList   bitmap_set;

   /*!
    * Constructs a %BitmapStructure object of the specified \a size and \a n
    * number of ways.
    *
    * \param bitmaps    An array of null-terminated strings representing the
    *                   set of ways in this structuring element. There must be
    *                   \a n contiguous null-terminated strings of length
    *                   \a size*size characters at the address specified by
    *                   this argument. In these strings, 'x' characters
    *                   correspond to existing structure elements; other
    *                   character values will be interpreted as nonexistent
    *                   structure elements.
    *
    * \param size       Size of this structure. Must be an odd integer >= 3.
    *
    * \param n          Number of ways in this structure. Must be >= 1.
    */
   BitmapStructure( const char** bitmaps, int size, int n = 1 ) :
   StructuringElement( size, n ), m_bitmaps()
   {
      PCL_PRECONDITION( bitmaps != 0 )
      PCL_PRECONDITION( *bitmaps != '\0' )
      for ( int i = 0; i < NumberOfWays(); ++i )
         m_bitmaps.Add( bitmap( bitmaps[i] ) );
   }

   /*!
    * Copy constructor.
    */
   BitmapStructure( const BitmapStructure& x ) :
   StructuringElement( x ), m_bitmaps( x.m_bitmaps )
   {
   }

   /*!
    */
   virtual StructuringElement* Clone() const
   {
      return new BitmapStructure( *this );
   }

   /*!
    */
   virtual bool ElementExists( int i, int j, int k ) const
   {
      PCL_PRECONDITION( i >= 0 && i < Size() )
      PCL_PRECONDITION( j >= 0 && j < Size() )
      PCL_PRECONDITION( k >= 0 && k < NumberOfWays() )
      return m_bitmaps[k][i + j*Size()] == 'x';
   }

protected:

   bitmap_set m_bitmaps;
};

You need to #include pcl/StructuringElement.h and pcl/String.h to use it. It should be compatible with the current PCL version without problems.

Hope this helps.
« Last Edit: 2012 February 09 04:08:18 by Juan Conejero »
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline NKV

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 677
Re: PCL MorphologicalTransformation How to define StructuringElement?
« Reply #5 on: 2012 February 09 05:48:10 »
PCL 2.0  :surprised:

When it will available via Software Distribution ?