vigra - vigra/cellimage.hxx Source File

[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

vigra/cellimage.hxx |

    00001 /************************************************************************/
    00002 /*                                                                      */
    00003 /*             Copyright 2009-2010 by Ullrich Koethe                    */
    00004 /*                                                                      */
    00005 /*    This file is part of the VIGRA computer vision library.           */
    00006 /*    The VIGRA Website is                                              */
    00007 /*        http://hci.iwr.uni-heidelberg.de/vigra/                       */
    00008 /*    Please direct questions, bug reports, and contributions to        */
    00009 /*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
    00010 /*        vigra@informatik.uni-hamburg.de                               */
    00011 /*                                                                      */
    00012 /*    Permission is hereby granted, free of charge, to any person       */
    00013 /*    obtaining a copy of this software and associated documentation    */
    00014 /*    files (the "Software"), to deal in the Software without           */
    00015 /*    restriction, including without limitation the rights to use,      */
    00016 /*    copy, modify, merge, publish, distribute, sublicense, and/or      */
    00017 /*    sell copies of the Software, and to permit persons to whom the    */
    00018 /*    Software is furnished to do so, subject to the following          */
    00019 /*    conditions:                                                       */
    00020 /*                                                                      */
    00021 /*    The above copyright notice and this permission notice shall be    */
    00022 /*    included in all copies or substantial portions of the             */
    00023 /*    Software.                                                         */
    00024 /*                                                                      */
    00025 /*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
    00026 /*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
    00027 /*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
    00028 /*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
    00029 /*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
    00030 /*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
    00031 /*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
    00032 /*    OTHER DEALINGS IN THE SOFTWARE.                                   */
    00033 /*                                                                      */
    00034 /************************************************************************/
    00035
    00036 #ifndef VIGRA_CELLIMAGE_HXX
    00037 #define VIGRA_CELLIMAGE_HXX
    00038
    00039 #include <vigra/basicimage.hxx>
    00040 #include <vigra/pixelneighborhood.hxx>
    00041 #include <functional>
    00042
    00043 namespace vigra {
    00044
    00045 namespace cellimage {
    00046
    00047 enum CellType { CellTypeRegion = 0,
    00048                 CellTypeLine = 1,
    00049                 CellTypeVertex = 2,
    00050                 CellTypeError = 3,
    00051                 CellTypeVertexOrLine = 4,
    00052                 CellTypeErrorOrLine = 5 };
    00053
    00054 // -------------------------------------------------------------------
    00055 //                          CellPixel, CellImage
    00056 // -------------------------------------------------------------------
    00057 typedef unsigned int CellLabel;
    00058
    00059 struct CellPixel
    00060 {
    00061 private:
    00062     CellLabel typeLabel_;
    00063
    00064     friend struct CellPixelSerializer;
    00065
    00066 public:
    00067     CellPixel() {}
    00068     CellPixel(CellType type, CellLabel label = 0)
    00069     : typeLabel_((label << 2) | type)
    00070     {}
    00071
    00072     inline CellType type() const
    00073         { return (CellType)(typeLabel_ & 3); }
    00074     inline void setType(CellType type)
    00075         { typeLabel_ = (label() << 2) | type; }
    00076     inline void setType(CellType type, CellLabel label)
    00077         { typeLabel_ = label << 2 | type; }
    00078
    00079     inline CellLabel label() const
    00080         { return typeLabel_ >> 2; }
    00081     inline void setLabel(CellLabel label)
    00082         { typeLabel_ = label << 2 | type(); }
    00083     inline void setLabel(CellLabel label, CellType type)
    00084         { typeLabel_ = label << 2 | type; }
    00085
    00086     bool operator==(CellPixel const & rhs) const
    00087         { return typeLabel_ == rhs.typeLabel_; }
    00088
    00089     bool operator!=(CellPixel const & rhs) const
    00090         { return typeLabel_ != rhs.typeLabel_; }
    00091 };
    00092
    00093 typedef BasicImage<CellPixel> CellImage;
    00094
    00095 typedef vigra::NeighborhoodCirculator<CellImage::Iterator, EightNeighborCode>
    00096     CellImageEightCirculator;
    00097
    00098 // -------------------------------------------------------------------
    00099 //                     CellPixel Serialization
    00100 // -------------------------------------------------------------------
    00101 struct CellPixelSerializer
    00102 {
    00103     int operator()(CellPixel const &p) const
    00104     {
    00105         return p.typeLabel_;
    00106     }
    00107
    00108     CellPixel operator()(int i) const
    00109     {
    00110         CellPixel result;
    00111         result.typeLabel_ = i;
    00112         return result;
    00113     }
    00114 };
    00115
    00116 // -------------------------------------------------------------------
    00117 //                   CellPixel/CellImage Accessors
    00118 // -------------------------------------------------------------------
    00119 template<class VALUE_TYPE = CellType>
    00120 struct TypeAccessor
    00121 {
    00122     typedef VALUE_TYPE value_type;
    00123     typedef VALUE_TYPE result_type;
    00124
    00125     template<class Iterator>
    00126     value_type operator()(const Iterator &it) const
    00127     {
    00128         return it->type();
    00129     }
    00130
    00131     template<class Iterator>
    00132     void set(value_type type, const Iterator &it) const
    00133     {
    00134         it->setType(type);
    00135     }
    00136 };
    00137
    00138 typedef TypeAccessor<unsigned char> TypeAsByteAccessor;
    00139
    00140 typedef TypeAccessor<> CellTypeAccessor;
    00141
    00142 struct LabelAccessor
    00143 {
    00144     typedef CellLabel value_type;
    00145
    00146     template<class Iterator>
    00147     CellLabel operator()(const Iterator &it) const
    00148     {
    00149         return it->label();
    00150     }
    00151
    00152     template<class Iterator>
    00153     void set(CellLabel label, const Iterator &it) const
    00154     {
    00155         it->setLabel(label);
    00156     }
    00157 };
    00158
    00159 template<CellType type>
    00160 struct LabelWriter
    00161 {
    00162     typedef CellLabel value_type;
    00163
    00164     template<class Iterator>
    00165     void set(CellLabel label, const Iterator &it) const
    00166     {
    00167         it->setLabel(label, type);
    00168     }
    00169 };
    00170
    00171 template<CellType type>
    00172 struct CellTypeEquals : public std::unary_function<CellType, bool>
    00173 {
    00174     bool operator()(CellType t) const
    00175     {
    00176         return t == type;
    00177     }
    00178
    00179     template<class Iterator>
    00180     bool operator()(const Iterator &it) const
    00181     {
    00182         return it->type() == type;
    00183     }
    00184 };
    00185
    00186 struct CellMask : public std::unary_function<vigra::cellimage::CellPixel, bool>
    00187 {
    00188     vigra::cellimage::CellPixel maskPixel_;
    00189
    00190     CellMask(vigra::cellimage::CellPixel maskPixel)
    00191     : maskPixel_(maskPixel)
    00192     {}
    00193
    00194     template<class Iterator>
    00195     bool operator()(const Iterator &it) const
    00196     {
    00197         return *it == maskPixel_;
    00198     }
    00199 };
    00200
    00201 // -------------------------------------------------------------------
    00202 //                        RelabelFunctor (unused!)
    00203 // -------------------------------------------------------------------
    00204 template<class VALUETYPE>
    00205 struct RelabelFunctor
    00206 {
    00207     typedef VALUETYPE value_type;
    00208     typedef VALUETYPE argument_type;
    00209     typedef VALUETYPE result_type;
    00210
    00211     RelabelFunctor(VALUETYPE oldValue, VALUETYPE newValue)
    00212         : oldValue_(oldValue),
    00213           newValue_(newValue)
    00214     {}
    00215
    00216     VALUETYPE operator()(VALUETYPE value) const
    00217     {
    00218         return (value == oldValue_) ? newValue_ : value;
    00219     }
    00220
    00221     VALUETYPE oldValue_, newValue_;
    00222 };
    00223
    00224 // -------------------------------------------------------------------
    00225 //                              inspectCell
    00226 // -------------------------------------------------------------------
    00227 // thinking about "typename IteratorTraits<EndIterator>::DefaultAccessor":
    00228 // is not needed since we're not implementing srcCellRange here, but
    00229 // algorithms.
    00230 // srcCellRange can not be implemented that easy, because most VIGRA
    00231 // functions expect an ImageIterator, not a std::iterator
    00232 template <class EndIterator, class Accessor, class Functor>
    00233 void inspectCell(EndIterator endIterator, Accessor a, Functor & f)
    00234 {
    00235     for(; endIterator.inRange(); ++endIterator)
    00236         f(a(endIterator));
    00237 }
    00238
    00239 template <class EndIterator, class Functor>
    00240 void inspectCell(EndIterator endIterator, Functor & f)
    00241 {
    00242     for(; endIterator.inRange(); ++endIterator)
    00243         f(*endIterator);
    00244 }
    00245
    00246 // -------------------------------------------------------------------
    00247 //                             transformCell
    00248 // -------------------------------------------------------------------
    00249 template <class SrcEndIterator, class SrcAccessor,
    00250           class DestEndIterator, class DestAccessor, class Functor>
    00251 void transformCell(SrcEndIterator srcEndIterator, SrcAccessor sa,
    00252                    DestEndIterator destEndIterator, DestAccessor da,
    00253                    Functor const & f)
    00254 {
    00255     for(; srcEndIterator.inRange(); ++srcEndIterator, ++destEndIterator)
    00256         da.set(f(sa(srcEndIterator)), destEndIterator);
    00257 }
    00258
    00259 template <class SrcEndIterator, class DestEndIterator, class Functor>
    00260 void transformCell(SrcEndIterator srcEndIterator,
    00261                    DestEndIterator destEndIterator,
    00262                    Functor const & f)
    00263 {
    00264     for(; srcEndIterator.inRange(); ++srcEndIterator, ++destEndIterator)
    00265         *destEndIterator = f(*srcEndIterator);
    00266 }
    00267
    00268 } // namespace cellimage
    00269
    00270 } // namespace vigra
    00271
    00272 #endif // VIGRA_CELLIMAGE_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.9.0 (Tue Nov 6 2012)