kernel.h

Go to the documentation of this file.
00001 
00015 #ifndef _DLRCOMPUTERVISION_KERNEL_H_
00016 #define _DLRCOMPUTERVISION_KERNEL_H_
00017 
00018 #include <dlrNumeric/array2D.h>
00019 
00020 namespace dlr {
00021 
00022   namespace computerVision {
00023     
00028     template <class ELEMENT_TYPE>
00029     class Kernel
00030     {
00031     public:
00032 
00033       /* ******** Public typedefs ******** */
00034 
00035       typedef ELEMENT_TYPE ElementType;
00036 
00037 
00038       /* ******** Public member functions ******** */
00039 
00043       Kernel();
00044 
00045     
00051       Kernel(const Kernel<ELEMENT_TYPE>& source);
00052 
00053     
00062       Kernel(const Array2D<ElementType>& source);
00063 
00064     
00078       Kernel(size_t rows, size_t columns, ElementType* const dataPtr);
00079 
00080 
00093       Kernel(const Array1D<ElementType>& rowArray,
00094              const Array1D<ElementType>& columnArray);
00095 
00096     
00101       virtual
00102       ~Kernel();
00103 
00104 
00111       Array2D<ElementType>
00112       getArray2D() const;
00113     
00114 
00123       Array1D<ElementType>
00124       getColumnComponent() const;
00125 
00126 
00132       size_t getColumns() const;
00133 
00134 
00143       Array1D<ElementType>
00144       getRowComponent() const;
00145 
00146 
00152       size_t getRows() const;
00153 
00154     
00162       bool
00163       isSeparable() const {return m_isSeparable;}
00164 
00165 
00173       void
00174       setArray2D(const Array2D<ElementType>& dataArray);
00175 
00176 
00191       void
00192       setSeparableComponents(const Array1D<ElementType>& rowArray,
00193                              const Array1D<ElementType>& columnArray);
00194 
00195 
00196     private:
00197 
00198       Array2D<ElementType> m_data;
00199       bool m_isSeparable;
00200       size_t m_separableColumns;
00201       size_t m_separableRows;
00202 
00203     };
00204 
00205   } // namespace computerVision
00206 
00207 } // namespace dlr
00208 
00209 
00210 /* =============== Implementation follows =============== */
00211 
00212 
00213 namespace dlr {
00214 
00215   namespace computerVision {
00216 
00217     // Default constructor initializes to zero size.
00218     template <class ELEMENT_TYPE>
00219     Kernel<ELEMENT_TYPE>::
00220     Kernel()
00221       : m_data(),
00222         m_isSeparable(false),
00223         m_separableColumns(0),
00224         m_separableRows(0)
00225     {
00226       // Empty.
00227     }
00228     
00229     
00230     // The copy constructor does a shallow copy.
00231     template <class ELEMENT_TYPE>
00232     Kernel<ELEMENT_TYPE>::
00233     Kernel(const Kernel<ELEMENT_TYPE> &source)
00234       : m_data(source.m_data.copy()),
00235         m_isSeparable(source.m_isSeparable),
00236         m_separableColumns(source.m_separableColumns),
00237         m_separableRows(source.m_separableRows)
00238     {
00239       // Empty.
00240     }
00241       
00242     
00243     // This constructor allows us to implicitly make a Kernel instance
00244     // from an Array2D.
00245     template <class ELEMENT_TYPE>
00246     Kernel<ELEMENT_TYPE>::
00247     Kernel(const Array2D<ElementType> &source)
00248       : m_data(source.copy()),
00249         m_isSeparable(false),
00250         m_separableColumns(0),
00251         m_separableRows(0)
00252     {
00253       // Empty.
00254     }
00255     
00256 
00257     // Construct a kernel around external data.
00258     template <class ELEMENT_TYPE>
00259     Kernel<ELEMENT_TYPE>::
00260     Kernel(size_t rows, size_t columns, ElementType* const dataPtr)
00261       : m_data(rows, columns),
00262         m_isSeparable(false),
00263         m_separableColumns(0),
00264         m_separableRows(0)
00265     {
00266       m_data.copy(dataPtr);
00267     }
00268 
00269 
00270     // This constructor allows us to implicitly make a separable
00271     // Kernel instance from a pair of Array1D instances.
00272     template <class ELEMENT_TYPE>
00273     Kernel<ELEMENT_TYPE>::
00274     Kernel(const Array1D<ElementType>& rowArray,
00275            const Array1D<ElementType>& columnArray)
00276       : m_data(1, rowArray.size() + columnArray.size()),
00277         m_isSeparable(true),
00278         m_separableColumns(rowArray.size()),
00279         m_separableRows(columnArray.size())
00280     {
00281       std::copy(rowArray.begin(), rowArray.end(), m_data.begin());
00282       std::copy(columnArray.begin(), columnArray.end(),
00283                 m_data.begin() + rowArray.size());
00284     }
00285 
00286 
00287     // Destroys the Kernel instance and deletes the internal data
00288     // store.
00289     template <class ELEMENT_TYPE>
00290     Kernel<ELEMENT_TYPE>::
00291     ~Kernel()
00292     {
00293       // Empty.
00294     }
00295 
00296 
00297     // Return a copy of the kernel data in an Array2D object.
00298     template <class ELEMENT_TYPE>
00299     Array2D<ELEMENT_TYPE>
00300     Kernel<ELEMENT_TYPE>::
00301     getArray2D() const
00302     {
00303       if(m_isSeparable) {
00304         Array2D<ELEMENT_TYPE> synthesizedArray(
00305           m_separableRows, m_separableColumns);
00306         typename Array2D<ELEMENT_TYPE>::iterator resultIterator =
00307           synthesizedArray.begin();
00308         for(size_t rowIndex = 0; rowIndex < m_separableRows; ++rowIndex) {
00309           for(size_t columnIndex = 0; columnIndex < m_separableColumns;
00310               ++columnIndex) {
00311             *resultIterator =
00312               m_data[columnIndex] * m_data[m_separableColumns + rowIndex];
00313             ++resultIterator;
00314           }
00315         }
00316         return synthesizedArray;
00317       }
00318       // Not separable.
00319       return m_data.copy();
00320     }
00321     
00322 
00323     // This method is only valid for separable kernels; it returns
00324     // the separable kernel component which is parallel to the columns
00325     // of the image.
00326     template <class ELEMENT_TYPE>
00327     Array1D<ELEMENT_TYPE>
00328     Kernel<ELEMENT_TYPE>::
00329     getColumnComponent() const
00330     {
00331       Array1D<ELEMENT_TYPE> columnComponent(m_separableRows);
00332       columnComponent.copy(m_data.begin() + m_separableColumns);
00333       return columnComponent;
00334     }
00335 
00336     
00337     // This member function returns the number of columns in the kernel.
00338     template <class ELEMENT_TYPE>
00339     size_t
00340     Kernel<ELEMENT_TYPE>::
00341     getColumns() const
00342     {
00343       if(this->isSeparable()) {
00344         return m_separableColumns;
00345       }
00346       return m_data.columns();
00347     }
00348 
00349 
00350     // This method is only valid for separable kernels; it returns
00351     // the separable kernel component which is parallel to the rows
00352     // of the image.
00353     template <class ELEMENT_TYPE>
00354     Array1D<ELEMENT_TYPE>
00355     Kernel<ELEMENT_TYPE>::
00356     getRowComponent() const
00357     {
00358       Array1D<ELEMENT_TYPE> rowComponent(m_separableColumns);
00359       rowComponent.copy(m_data.begin());
00360       return rowComponent;
00361     }
00362 
00363 
00364     // This member function returns the number of rows in the kernel.
00365     template <class ELEMENT_TYPE>
00366     size_t
00367     Kernel<ELEMENT_TYPE>::
00368     getRows() const
00369     {
00370       if(this->isSeparable()) {
00371         return m_separableRows;
00372       }
00373       return m_data.rows();
00374     }
00375 
00376   
00377     // This method allows the user to set the contents of the kernel.
00378     template <class ELEMENT_TYPE>
00379     void
00380     Kernel<ELEMENT_TYPE>::
00381     setArray2D(const Array2D<ElementType>& dataArray)
00382     {
00383       m_isSeparable = false;
00384       m_data = dataArray.copy();
00385       m_separableColumns = 0;
00386       m_separableRows = 0;
00387     }
00388 
00389 
00390     // This method allows the user to set the contents of a separable
00391     // kernel.
00392     template <class ELEMENT_TYPE>
00393     void
00394     Kernel<ELEMENT_TYPE>::
00395     setSeparableComponents(const Array1D<ElementType>& rowArray,
00396                            const Array1D<ElementType>& columnArray)
00397     {
00398       m_isSeparable = true;
00399       m_data.reinit(1, rowArray.size() + columnArray.size());
00400       std::copy(rowArray.begin(), rowArray.end(), m_data.begin());
00401       std::copy(columnArray.begin(), columnArray.end(),
00402                 m_data.begin() + rowArray.size());
00403       m_separableColumns = rowArray.size();
00404       m_separableRows = columnArray.size();
00405     }
00406     
00407   } // namespace computerVision
00408 
00409 } // namespace dlr
00410 
00411 #endif /* #ifndef _DLRCOMPUTERVISION_KERNEL_H_ */

Generated on Wed Nov 25 12:15:05 2009 for dlrComputerVision Utility Library by  doxygen 1.5.8