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

| vigra/initimage.hxx |
--- | --- | ---

    00001 /************************************************************************/
    00002 /*                                                                      */
    00003 /*               Copyright 1998-2002 by Ullrich Koethe                  */
    00004 /*       Cognitive Systems Group, University of Hamburg, Germany        */
    00005 /*                                                                      */
    00006 /*    This file is part of the VIGRA computer vision library.           */
    00007 /*    ( Version 1.4.0, Dec 21 2005 )                                    */
    00008 /*    The VIGRA Website is                                              */
    00009 /*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
    00010 /*    Please direct questions, bug reports, and contributions to        */
    00011 /*        koethe@informatik.uni-hamburg.de          or                  */
    00012 /*        vigra@kogs1.informatik.uni-hamburg.de                         */
    00013 /*                                                                      */
    00014 /*    Permission is hereby granted, free of charge, to any person       */
    00015 /*    obtaining a copy of this software and associated documentation    */
    00016 /*    files (the "Software"), to deal in the Software without           */
    00017 /*    restriction, including without limitation the rights to use,      */
    00018 /*    copy, modify, merge, publish, distribute, sublicense, and/or      */
    00019 /*    sell copies of the Software, and to permit persons to whom the    */
    00020 /*    Software is furnished to do so, subject to the following          */
    00021 /*    conditions:                                                       */
    00022 /*                                                                      */
    00023 /*    The above copyright notice and this permission notice shall be    */
    00024 /*    included in all copies or substantial portions of the             */
    00025 /*    Software.                                                         */
    00026 /*                                                                      */
    00027 /*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
    00028 /*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
    00029 /*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
    00030 /*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
    00031 /*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
    00032 /*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
    00033 /*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
    00034 /*    OTHER DEALINGS IN THE SOFTWARE.                                   */
    00035 /*                                                                      */
    00036 /************************************************************************/
    00037
    00038
    00039 #ifndef VIGRA_INITIMAGE_HXX
    00040 #define VIGRA_INITIMAGE_HXX
    00041
    00042 #include "vigra/utilities.hxx"
    00043 #include "vigra/iteratortraits.hxx"
    00044 #include "vigra/functortraits.hxx"
    00045
    00046 namespace vigra {
    00047
    00048 /** \addtogroup InitAlgo Algorithms to Initialize Images
    00049
    00050     Init images or image borders
    00051 */
    00052 //@{
    00053
    00054 /********************************************************/
    00055 /*                                                      */
    00056 /*                       initLine                       */
    00057 /*                                                      */
    00058 /********************************************************/
    00059
    00060 template <class DestIterator, class DestAccessor, class VALUETYPE>
    00061 void
    00062 initLineImpl(DestIterator d, DestIterator dend, DestAccessor dest,
    00063              VALUETYPE v, VigraFalseType)
    00064 {
    00065     for(; d != dend; ++d)
    00066         dest.set(v, d);
    00067 }
    00068
    00069 template <class DestIterator, class DestAccessor, class FUNCTOR>
    00070 void
    00071 initLineImpl(DestIterator d, DestIterator dend, DestAccessor dest,
    00072              FUNCTOR const & f, VigraTrueType)
    00073 {
    00074     for(; d != dend; ++d)
    00075         dest.set(f(), d);
    00076 }
    00077
    00078 template <class DestIterator, class DestAccessor, class VALUETYPE>
    00079 inline void
    00080 initLine(DestIterator d, DestIterator dend, DestAccessor dest,
    00081          VALUETYPE v)
    00082 {
    00083     initLineImpl(d, dend, dest, v, typename FunctorTraits<VALUETYPE>::isInitializer());
    00084 }
    00085
    00086 template <class DestIterator, class DestAccessor, class FUNCTOR>
    00087 inline void
    00088 initLineFunctor(DestIterator d, DestIterator dend, DestAccessor dest,
    00089          FUNCTOR f)
    00090 {
    00091     initLineImpl(d, dend, dest, f, VigraTrueType());
    00092 }
    00093
    00094 template <class DestIterator, class DestAccessor,
    00095           class MaskIterator, class MaskAccessor,
    00096           class VALUETYPE>
    00097 void
    00098 initLineIfImpl(DestIterator d, DestIterator dend, DestAccessor dest,
    00099                MaskIterator m, MaskAccessor mask,
    00100                VALUETYPE v, VigraFalseType)
    00101 {
    00102     for(; d != dend; ++d, ++m)
    00103         if(mask(m))
    00104             dest.set(v, d);
    00105 }
    00106
    00107 template <class DestIterator, class DestAccessor,
    00108           class MaskIterator, class MaskAccessor,
    00109           class FUNCTOR>
    00110 void
    00111 initLineIfImpl(DestIterator d, DestIterator dend, DestAccessor dest,
    00112                MaskIterator m, MaskAccessor mask,
    00113                FUNCTOR const & f, VigraTrueType)
    00114 {
    00115     for(; d != dend; ++d, ++m)
    00116         if(mask(m))
    00117             dest.set(f(), d);
    00118 }
    00119
    00120 template <class DestIterator, class DestAccessor,
    00121           class MaskIterator, class MaskAccessor,
    00122           class VALUETYPE>
    00123 inline void
    00124 initLineIf(DestIterator d, DestIterator dend, DestAccessor dest,
    00125            MaskIterator m, MaskAccessor mask,
    00126            VALUETYPE v)
    00127 {
    00128     initLineIfImpl(d, dend, dest, m, mask, v, typename FunctorTraits<VALUETYPE>::isInitializer());
    00129 }
    00130
    00131 template <class DestIterator, class DestAccessor,
    00132           class MaskIterator, class MaskAccessor,
    00133           class FUNCTOR>
    00134 void
    00135 initLineFunctorIf(DestIterator d, DestIterator dend, DestAccessor dest,
    00136                   MaskIterator m, MaskAccessor mask,
    00137                   FUNCTOR f)
    00138 {
    00139     initLineIfImpl(d, dend, dest, m, mask, f, VigraTrueType());
    00140 }
    00141
    00142 /********************************************************/
    00143 /*                                                      */
    00144 /*                        initImage                     */
    00145 /*                                                      */
    00146 /********************************************************/
    00147
    00148 /** \brief Write a value to every pixel in an image or rectangular ROI.
    00149
    00150     This function can be used to init the image.
    00151     It uses an accessor to access the pixel data.
    00152
    00153     <b> Declarations:</b>
    00154
    00155     pass arguments explicitly:
    00156     \code
    00157     namespace vigra {
    00158         template <class ImageIterator, class Accessor, class VALUETYPE>
    00159         void
    00160         initImage(ImageIterator upperleft, ImageIterator lowerright,
    00161               Accessor a, VALUETYPE v)
    00162     }
    00163     \endcode
    00164
    00165     use argument objects in conjunction with \ref ArgumentObjectFactories:
    00166     \code
    00167     namespace vigra {
    00168         template <class ImageIterator, class Accessor, class VALUETYPE>
    00169         void
    00170         initImage(triple<ImageIterator, ImageIterator, Accessor> img, VALUETYPE v)
    00171     }
    00172     \endcode
    00173
    00174     <b> Usage:</b>
    00175
    00176         <b>\#include</b> "<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>"<br>
    00177         Namespace: vigra
    00178
    00179     \code
    00180     vigra::BImage img(100, 100);
    00181
    00182     // zero the image
    00183     vigra::initImage(destImageRange(img),
    00184                      vigra::NumericTraits<vigra::BImage::PixelType>::zero());
    00185     \endcode
    00186
    00187     <b> Required Interface:</b>
    00188
    00189     \code
    00190     ImageIterator upperleft, lowerright;
    00191     ImageIterator::row_iterator ix = upperleft.rowIterator();
    00192
    00193     Accessor accessor;
    00194     VALUETYPE v;
    00195
    00196     accessor.set(v, ix);
    00197     \endcode
    00198
    00199 */
    00200 template <class ImageIterator, class Accessor, class VALUETYPE>
    00201 void
    00202 initImage(ImageIterator upperleft, ImageIterator lowerright,
    00203           Accessor a,  VALUETYPE v)
    00204 {
    00205     int w = lowerright.x - upperleft.x;
    00206
    00207     for(; upperleft.y < lowerright.y; ++upperleft.y)
    00208     {
    00209         initLine(upperleft.rowIterator(),
    00210                  upperleft.rowIterator() + w, a, v);
    00211     }
    00212 }
    00213
    00214 template <class ImageIterator, class Accessor, class VALUETYPE>
    00215 inline
    00216 void
    00217 initImage(triple<ImageIterator, ImageIterator, Accessor> img, VALUETYPE v)
    00218 {
    00219     initImage(img.first, img.second, img.third, v);
    00220 }
    00221
    00222 /********************************************************/
    00223 /*                                                      */
    00224 /*                        initImage                     */
    00225 /*                                                      */
    00226 /********************************************************/
    00227
    00228 /** \brief Write the result of a functor call to every pixel in an image or rectangular ROI.
    00229
    00230     This function can be used to init the image by calling the given
    00231     functor for each pixel.
    00232     It uses an accessor to access the pixel data.
    00233
    00234     <b> Declarations:</b>
    00235
    00236     pass arguments explicitly:
    00237     \code
    00238     namespace vigra {
    00239         template <class ImageIterator, class Accessor, class FUNCTOR>
    00240         void
    00241         initImageWithFunctor(ImageIterator upperleft, ImageIterator lowerright,
    00242                   Accessor a,  FUNCTOR f);
    00243     }
    00244     \endcode
    00245
    00246     use argument objects in conjunction with \ref ArgumentObjectFactories:
    00247     \code
    00248     namespace vigra {
    00249         template <class ImageIterator, class Accessor, class FUNCTOR>
    00250         void
    00251         initImageWithFunctor(triple<ImageIterator, ImageIterator, Accessor> img, FUNCTOR f);
    00252     }
    00253     \endcode
    00254
    00255     <b> Usage:</b>
    00256
    00257         <b>\#include</b> "<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>"<br>
    00258         Namespace: vigra
    00259
    00260     \code
    00261     struct Counter {
    00262         Counter() : count(0) {}
    00263
    00264         int operator()() const { return count++; }
    00265
    00266         mutable int count;
    00267     };
    00268
    00269     vigra::IImage img(100, 100);
    00270
    00271     // write the current count in every pixel
    00272     vigra::initImageWithFunctor(destImageRange(img), Counter());
    00273     \endcode
    00274
    00275     <b> Required Interface:</b>
    00276
    00277     \code
    00278     ImageIterator upperleft, lowerright;
    00279     ImageIterator::row_iterator ix = upperleft.rowIterator();
    00280
    00281     Accessor accessor;
    00282     Functor f;
    00283
    00284     accessor.set(f(), ix);
    00285     \endcode
    00286
    00287 */
    00288 template <class ImageIterator, class Accessor, class FUNCTOR>
    00289 void
    00290 initImageWithFunctor(ImageIterator upperleft, ImageIterator lowerright,
    00291           Accessor a,  FUNCTOR f)
    00292 {
    00293     int w = lowerright.x - upperleft.x;
    00294
    00295     for(; upperleft.y < lowerright.y; ++upperleft.y)
    00296     {
    00297         initLineFunctor(upperleft.rowIterator(),
    00298                  upperleft.rowIterator() + w, a, f);
    00299     }
    00300 }
    00301
    00302 template <class ImageIterator, class Accessor, class FUNCTOR>
    00303 inline
    00304 void
    00305 initImageWithFunctor(triple<ImageIterator, ImageIterator, Accessor> img, FUNCTOR f)
    00306 {
    00307     initImageWithFunctor(img.first, img.second, img.third, f);
    00308 }
    00309
    00310 /********************************************************/
    00311 /*                                                      */
    00312 /*                      initImageIf                     */
    00313 /*                                                      */
    00314 /********************************************************/
    00315
    00316 /** \brief Write value to pixel in the image if mask is true.
    00317
    00318     This function can be used to init a region-of-interest of the image.
    00319     It uses an accessor to access the pixel data.
    00320
    00321     <b> Declarations:</b>
    00322
    00323     pass arguments explicitly:
    00324     \code
    00325     namespace vigra {
    00326         template <class ImageIterator, class Accessor,
    00327               class MaskImageIterator, class MaskAccessor,
    00328               class VALUETYPE>
    00329         void
    00330         initImageIf(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
    00331               MaskImageIterator mask_upperleft, MaskAccessor ma,
    00332               VALUETYPE v)
    00333     }
    00334     \endcode
    00335
    00336     use argument objects in conjunction with \ref ArgumentObjectFactories:
    00337     \code
    00338     namespace vigra {
    00339         template <class ImageIterator, class Accessor,
    00340               class MaskImageIterator, class MaskAccessor,
    00341               class VALUETYPE>
    00342         void
    00343         initImageIf(triple<ImageIterator, ImageIterator, Accessor> img,
    00344             pair<MaskImageIterator, MaskAccessor> mask,
    00345             VALUETYPE v)
    00346     }
    00347     \endcode
    00348
    00349     <b> Usage:</b>
    00350
    00351         <b>\#include</b> "<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>"<br>
    00352         Namespace: vigra
    00353
    00354     \code
    00355     vigra::BImage img(100, 100);
    00356     vigra::BImage mask(100, 100);
    00357
    00358     // zero the ROI
    00359     vigra::initImageIf(destImageRange(img),
    00360                 maskImage(mask),
    00361                 vigra::NumericTraits<vigra::BImage::PixelType>::zero());
    00362     \endcode
    00363
    00364     <b> Required Interface:</b>
    00365
    00366     \code
    00367     ImageIterator upperleft, lowerright;
    00368     MaskImageIterator mask_upperleft;
    00369     ImageIterator::row_iterator ix = upperleft.rowIterator();
    00370     MaskImageIterator::row_iterator mx = mask_upperleft.rowIterator();
    00371
    00372     Accessor accessor;
    00373     MaskAccessor mask_accessor;
    00374     VALUETYPE v;
    00375
    00376     if(mask_accessor(mx)) accessor.set(v, ix);
    00377     \endcode
    00378
    00379 */
    00380 template <class ImageIterator, class Accessor,
    00381           class MaskImageIterator, class MaskAccessor,
    00382           class VALUETYPE>
    00383 void
    00384 initImageIf(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
    00385           MaskImageIterator mask_upperleft, MaskAccessor ma,
    00386           VALUETYPE v)
    00387 {
    00388     int w = lowerright.x - upperleft.x;
    00389
    00390     for(; upperleft.y < lowerright.y; ++upperleft.y, ++mask_upperleft.y)
    00391     {
    00392         initLineIf(upperleft.rowIterator(),
    00393                    upperleft.rowIterator() + w, a,
    00394                    mask_upperleft.rowIterator(), ma, v);
    00395     }
    00396 }
    00397
    00398 template <class ImageIterator, class Accessor,
    00399           class MaskImageIterator, class MaskAccessor,
    00400           class VALUETYPE>
    00401 inline
    00402 void
    00403 initImageIf(triple<ImageIterator, ImageIterator, Accessor> img,
    00404             pair<MaskImageIterator, MaskAccessor> mask,
    00405             VALUETYPE v)
    00406 {
    00407     initImageIf(img.first, img.second, img.third, mask.first, mask.second, v);
    00408 }
    00409
    00410 /********************************************************/
    00411 /*                                                      */
    00412 /*                    initImageBorder                   */
    00413 /*                                                      */
    00414 /********************************************************/
    00415
    00416 /** \brief Write value to the specified border pixels in the image.
    00417
    00418     A pixel is initialized if its distance to the border
    00419     is at most 'borderwidth'.
    00420     It uses an accessor to access the pixel data.
    00421
    00422     <b> Declarations:</b>
    00423
    00424     pass arguments explicitly:
    00425     \code
    00426     namespace vigra {
    00427         template <class ImageIterator, class Accessor, class VALUETYPE>
    00428         void
    00429         initImageBorder(ImageIterator upperleft, ImageIterator lowerright,
    00430                 Accessor a,  int border_width, VALUETYPE v)
    00431     }
    00432     \endcode
    00433
    00434     use argument objects in conjunction with \ref ArgumentObjectFactories:
    00435     \code
    00436     namespace vigra {
    00437         template <class ImageIterator, class Accessor, class VALUETYPE>
    00438         void
    00439         initImageBorder(triple<ImageIterator, ImageIterator, Accessor> img,
    00440                 int border_width, VALUETYPE v)
    00441     }
    00442     \endcode
    00443
    00444     <b> Usage:</b>
    00445
    00446         <b>\#include</b> "<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>"<br>
    00447         Namespace: vigra
    00448
    00449     \code
    00450     vigra::BImage img(100, 100);
    00451
    00452     // zero a border of 5 pixel
    00453     vigra::initImageBorder(destImageRange(img),
    00454                     5, vigra::NumericTraits<vigra::BImage::PixelType>::zero());
    00455     \endcode
    00456
    00457     <b> Required Interface:</b>
    00458
    00459     see \ref initImage()
    00460
    00461 */
    00462 template <class ImageIterator, class Accessor, class VALUETYPE>
    00463 inline
    00464 void
    00465 initImageBorder(ImageIterator upperleft, ImageIterator lowerright,
    00466                 Accessor a,  int border_width, VALUETYPE v)
    00467 {
    00468     int w = lowerright.x - upperleft.x;
    00469     int h = lowerright.y - upperleft.y;
    00470
    00471     int hb = (border_width > h) ? h : border_width;
    00472     int wb = (border_width > w) ? w : border_width;
    00473
    00474     initImage(upperleft, upperleft+Diff2D(w,hb), a, v);
    00475     initImage(upperleft, upperleft+Diff2D(wb,h), a, v);
    00476     initImage(upperleft+Diff2D(0,h-hb), lowerright, a, v);
    00477     initImage(upperleft+Diff2D(w-wb,0), lowerright, a, v);
    00478 }
    00479
    00480 template <class ImageIterator, class Accessor, class VALUETYPE>
    00481 inline
    00482 void
    00483 initImageBorder(triple<ImageIterator, ImageIterator, Accessor> img,
    00484                 int border_width, VALUETYPE v)
    00485 {
    00486     initImageBorder(img.first, img.second, img.third, border_width, v);
    00487 }
    00488
    00489 //@}
    00490
    00491
    00492 } // namespace vigra
    00493
    00494 #endif // VIGRA_INITIMAGE_HXX

© Ullrich Köthe (koethe@informatik.uni-hamburg.de) Cognitive Systems Group, University of Hamburg, Germany | html generated using doxygen and Python VIGRA 1.4.0 (21 Dec 2005)