VIGRA - VIGRA: vigra/initimage.hxx Source File

[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | 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.6.0, Aug 13 2008 )                                    */
    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 /*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
    00012 /*        vigra@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 "utilities.hxx"
    00043 #include "iteratortraits.hxx"
    00044 #include "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 inline void
    00062 initLineImpl(DestIterator d, DestIterator dend, DestAccessor dest,
    00063              VALUETYPE const & v, VigraFalseType)
    00064 {
    00065     for(; d != dend; ++d)
    00066         dest.set(v, d);
    00067 }
    00068
    00069 template <class DestIterator, class DestAccessor, class FUNCTOR>
    00070 inline 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 const & 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     for(; d != dend; ++d)
    00092         dest.set(f(), d);
    00093 }
    00094
    00095 template <class DestIterator, class DestAccessor,
    00096           class MaskIterator, class MaskAccessor,
    00097           class VALUETYPE>
    00098 inline void
    00099 initLineIfImpl(DestIterator d, DestIterator dend, DestAccessor dest,
    00100                MaskIterator m, MaskAccessor mask,
    00101                VALUETYPE const & v, VigraFalseType)
    00102 {
    00103     for(; d != dend; ++d, ++m)
    00104         if(mask(m))
    00105             dest.set(v, d);
    00106 }
    00107
    00108 template <class DestIterator, class DestAccessor,
    00109           class MaskIterator, class MaskAccessor,
    00110           class FUNCTOR>
    00111 inline void
    00112 initLineIfImpl(DestIterator d, DestIterator dend, DestAccessor dest,
    00113                MaskIterator m, MaskAccessor mask,
    00114                FUNCTOR const & f, VigraTrueType)
    00115 {
    00116     for(; d != dend; ++d, ++m)
    00117         if(mask(m))
    00118             dest.set(f(), d);
    00119 }
    00120
    00121 template <class DestIterator, class DestAccessor,
    00122           class MaskIterator, class MaskAccessor,
    00123           class VALUETYPE>
    00124 inline void
    00125 initLineIf(DestIterator d, DestIterator dend, DestAccessor dest,
    00126            MaskIterator m, MaskAccessor mask,
    00127            VALUETYPE const & v)
    00128 {
    00129     initLineIfImpl(d, dend, dest, m, mask, v, typename FunctorTraits<VALUETYPE>::isInitializer());
    00130 }
    00131
    00132 template <class DestIterator, class DestAccessor,
    00133           class MaskIterator, class MaskAccessor,
    00134           class FUNCTOR>
    00135 inline void
    00136 initLineFunctorIf(DestIterator d, DestIterator dend, DestAccessor dest,
    00137                   MaskIterator m, MaskAccessor mask,
    00138                   FUNCTOR & f)
    00139 {
    00140     for(; d != dend; ++d, ++m)
    00141         if(mask(m))
    00142             dest.set(f(), d);
    00143 }
    00144
    00145 /********************************************************/
    00146 /*                                                      */
    00147 /*                        initImage                     */
    00148 /*                                                      */
    00149 /********************************************************/
    00150
    00151 /** \brief Write a value to every pixel in an image or rectangular ROI.
    00152
    00153     This function can be used to init the image.
    00154     It uses an accessor to access the pixel data.
    00155
    00156     The initial value can either be a constant of appropriate type (compatible with
    00157     the destination's value_type), or a functor with compatible result_type. These two
    00158     cases are automatically distinguished when <tt>FunctorTraits<FUNCTOR>::isInitializer</tt>
    00159     yields <tt>VigraTrueType</tt>. Since the functor is passed by <tt>const</tt> reference, its
    00160     <tt>operator()</tt> must be const, and its internal state may need to be <tt>mutable</tt>.
    00161
    00162     <b> Declarations:</b>
    00163
    00164     pass arguments explicitly:
    00165     \code
    00166     namespace vigra {
    00167         template <class ImageIterator, class Accessor, class VALUETYPE>
    00168         void initImage(ImageIterator upperleft, ImageIterator lowerright,
    00169                        Accessor a, VALUETYPE const & v);
    00170
    00171         template <class ImageIterator, class Accessor, class FUNCTOR>
    00172         void initImage(ImageIterator upperleft, ImageIterator lowerright,
    00173                        Accessor a, FUNCTOR const & v);
    00174     }
    00175     \endcode
    00176
    00177     use argument objects in conjunction with \ref ArgumentObjectFactories :
    00178     \code
    00179     namespace vigra {
    00180         template <class ImageIterator, class Accessor, class VALUETYPE>
    00181         void initImage(triple<ImageIterator, ImageIterator, Accessor> img, VALUETYPE const & v);
    00182
    00183         template <class ImageIterator, class Accessor, class FUNCTOR>
    00184         void initImage(triple<ImageIterator, ImageIterator, Accessor> img, FUNCTOR const & v);
    00185     }
    00186     \endcode
    00187
    00188     <b> Usage:</b>
    00189
    00190         <b>\#include</b> <<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>><br>
    00191         Namespace: vigra
    00192
    00193     Initialize with a constant:
    00194
    00195     \code
    00196     vigra::BImage img(100, 100);
    00197
    00198     // init the image with the value 128
    00199     vigra::initImage(destImageRange(img), 128);
    00200     \endcode
    00201
    00202     Initialize with a functor:
    00203
    00204     \code
    00205     struct Counter {
    00206         Counter() : count(0) {}
    00207
    00208         int operator()() const { return count++; }
    00209
    00210         mutable int count;
    00211     };
    00212
    00213     vigra::IImage img(100, 100);
    00214
    00215     // write the current count in every pixel
    00216     vigra::initImage(destImageRange(img), Counter());
    00217     \endcode
    00218
    00219
    00220     <b> Required Interface:</b>
    00221
    00222     \code
    00223     ImageIterator upperleft, lowerright;
    00224     ImageIterator::row_iterator ix = upperleft.rowIterator();
    00225
    00226     Accessor accessor;
    00227     VALUETYPE v;
    00228
    00229     accessor.set(v, ix);
    00230     \endcode
    00231
    00232 */
    00233 doxygen_overloaded_function(template <...> void initImage)
    00234
    00235 template <class ImageIterator, class Accessor, class VALUETYPE>
    00236 void
    00237 initImage(ImageIterator upperleft, ImageIterator lowerright,
    00238           Accessor a,  VALUETYPE const & v)
    00239 {
    00240     int w = lowerright.x - upperleft.x;
    00241
    00242     for(; upperleft.y < lowerright.y; ++upperleft.y)
    00243     {
    00244         initLineImpl(upperleft.rowIterator(), upperleft.rowIterator() + w, a,
    00245                      v, typename FunctorTraits<VALUETYPE>::isInitializer());
    00246     }
    00247 }
    00248
    00249 template <class ImageIterator, class Accessor, class VALUETYPE>
    00250 inline
    00251 void
    00252 initImage(triple<ImageIterator, ImageIterator, Accessor> img, VALUETYPE const & v)
    00253 {
    00254     initImage(img.first, img.second, img.third, v);
    00255 }
    00256
    00257 /********************************************************/
    00258 /*                                                      */
    00259 /*                 initImageWithFunctor                 */
    00260 /*                                                      */
    00261 /********************************************************/
    00262
    00263 /** \brief Write the result of a functor call to every pixel in an image or rectangular ROI.
    00264
    00265     This function can be used to init the image by calling the given
    00266     functor for each pixel. It uses an accessor to access the pixel data. The functor is
    00267     passed by reference, so that its internal state can be updated in each call.
    00268
    00269     <b> Declarations:</b>
    00270
    00271     pass arguments explicitly:
    00272     \code
    00273     namespace vigra {
    00274         template <class ImageIterator, class Accessor, class FUNCTOR>
    00275         void
    00276         initImageWithFunctor(ImageIterator upperleft, ImageIterator lowerright,
    00277                   Accessor a,  FUNCTOR & f);
    00278     }
    00279     \endcode
    00280
    00281     use argument objects in conjunction with \ref ArgumentObjectFactories :
    00282     \code
    00283     namespace vigra {
    00284         template <class ImageIterator, class Accessor, class FUNCTOR>
    00285         void
    00286         initImageWithFunctor(triple<ImageIterator, ImageIterator, Accessor> img, FUNCTOR & f);
    00287     }
    00288     \endcode
    00289
    00290     <b> Usage:</b>
    00291
    00292         <b>\#include</b> <<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>><br>
    00293         Namespace: vigra
    00294
    00295     \code
    00296     struct Counter {
    00297         Counter() : count(0) {}
    00298
    00299         int operator()() const { return count++; }
    00300
    00301         mutable int count;
    00302     };
    00303
    00304     vigra::IImage img(100, 100);
    00305
    00306     // write the current count in every pixel
    00307     Counter counter;
    00308     vigra::initImageWithFunctor(destImageRange(img), counter);
    00309     \endcode
    00310
    00311     <b> Required Interface:</b>
    00312
    00313     \code
    00314     ImageIterator upperleft, lowerright;
    00315     ImageIterator::row_iterator ix = upperleft.rowIterator();
    00316
    00317     Accessor accessor;
    00318     Functor f;
    00319
    00320     accessor.set(f(), ix);
    00321     \endcode
    00322
    00323 */
    00324 doxygen_overloaded_function(template <...> void initImageWithFunctor)
    00325
    00326 template <class ImageIterator, class Accessor, class FUNCTOR>
    00327 void
    00328 initImageWithFunctor(ImageIterator upperleft, ImageIterator lowerright,
    00329           Accessor a,  FUNCTOR & f)
    00330 {
    00331     int w = lowerright.x - upperleft.x;
    00332
    00333     for(; upperleft.y < lowerright.y; ++upperleft.y)
    00334     {
    00335         initLineFunctor(upperleft.rowIterator(), upperleft.rowIterator() + w, a, f);
    00336     }
    00337 }
    00338
    00339 template <class ImageIterator, class Accessor, class FUNCTOR>
    00340 inline
    00341 void
    00342 initImageWithFunctor(triple<ImageIterator, ImageIterator, Accessor> img, FUNCTOR & f)
    00343 {
    00344     initImageWithFunctor(img.first, img.second, img.third, f);
    00345 }
    00346
    00347 /********************************************************/
    00348 /*                                                      */
    00349 /*                      initImageIf                     */
    00350 /*                                                      */
    00351 /********************************************************/
    00352
    00353 /** \brief Write value to pixel in the image if mask is true.
    00354
    00355     This function can be used to init a region-of-interest of the image.
    00356     It uses an accessor to access the pixel data.
    00357
    00358     The initial value can either be a constant of appropriate type (compatible with
    00359     the destination's value_type), or a functor with compatible result_type. These two
    00360     cases are automatically distinguished when <tt>FunctorTraits<FUNCTOR>::isInitializer</tt>
    00361     yields <tt>VigraTrueType</tt>. Since the functor is passed by <tt>const</tt> reference, its
    00362     <tt>operator()</tt> must be const, and its internal state may need to be <tt>mutable</tt>.
    00363
    00364     <b> Declarations:</b>
    00365
    00366     pass arguments explicitly:
    00367     \code
    00368     namespace vigra {
    00369         template <class ImageIterator, class Accessor,
    00370                   class MaskImageIterator, class MaskAccessor,
    00371                   class VALUETYPE>
    00372         void initImageIf(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
    00373                          MaskImageIterator mask_upperleft, MaskAccessor ma,
    00374                          VALUETYPE const & v);
    00375
    00376         template <class ImageIterator, class Accessor,
    00377                   class MaskImageIterator, class MaskAccessor,
    00378                   class FUNCTOR>
    00379         void initImageIf(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
    00380                          MaskImageIterator mask_upperleft, MaskAccessor ma,
    00381                          FUNCTOR const & v);
    00382     }
    00383     \endcode
    00384
    00385     use argument objects in conjunction with \ref ArgumentObjectFactories :
    00386     \code
    00387     namespace vigra {
    00388         template <class ImageIterator, class Accessor,
    00389                   class MaskImageIterator, class MaskAccessor,
    00390                   class VALUETYPE>
    00391         void initImageIf(triple<ImageIterator, ImageIterator, Accessor> img,
    00392                          pair<MaskImageIterator, MaskAccessor> mask,
    00393                          VALUETYPE const & v);
    00394
    00395         template <class ImageIterator, class Accessor,
    00396                   class MaskImageIterator, class MaskAccessor,
    00397                   class FUNCTOR>
    00398         void initImageIf(triple<ImageIterator, ImageIterator, Accessor> img,
    00399                          pair<MaskImageIterator, MaskAccessor> mask,
    00400                          FUNCTOR const & v);
    00401     }
    00402     \endcode
    00403
    00404     <b> Usage:</b>
    00405
    00406         <b>\#include</b> <<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>><br>
    00407         Namespace: vigra
    00408
    00409     \code
    00410     vigra::BImage img(100, 100);
    00411     vigra::BImage mask(100, 100);
    00412
    00413     // zero the ROI
    00414     vigra::initImageIf(destImageRange(img),
    00415                 maskImage(mask),
    00416                 vigra::NumericTraits<vigra::BImage::PixelType>::zero());
    00417     \endcode
    00418
    00419     <b> Required Interface:</b>
    00420
    00421     \code
    00422     ImageIterator upperleft, lowerright;
    00423     MaskImageIterator mask_upperleft;
    00424     ImageIterator::row_iterator ix = upperleft.rowIterator();
    00425     MaskImageIterator::row_iterator mx = mask_upperleft.rowIterator();
    00426
    00427     Accessor accessor;
    00428     MaskAccessor mask_accessor;
    00429     VALUETYPE v;
    00430
    00431     if(mask_accessor(mx)) accessor.set(v, ix);
    00432     \endcode
    00433
    00434 */
    00435 doxygen_overloaded_function(template <...> void initImageIf)
    00436
    00437 template <class ImageIterator, class Accessor,
    00438           class MaskImageIterator, class MaskAccessor,
    00439           class VALUETYPE>
    00440 void
    00441 initImageIf(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
    00442           MaskImageIterator mask_upperleft, MaskAccessor ma,
    00443           VALUETYPE const & v)
    00444 {
    00445     int w = lowerright.x - upperleft.x;
    00446
    00447     for(; upperleft.y < lowerright.y; ++upperleft.y, ++mask_upperleft.y)
    00448     {
    00449         initLineIfImpl(upperleft.rowIterator(),
    00450                    upperleft.rowIterator() + w, a,
    00451                    mask_upperleft.rowIterator(), ma,
    00452                    v, typename FunctorTraits<VALUETYPE>::isInitializer());
    00453     }
    00454 }
    00455
    00456 template <class ImageIterator, class Accessor,
    00457           class MaskImageIterator, class MaskAccessor,
    00458           class VALUETYPE>
    00459 inline
    00460 void
    00461 initImageIf(triple<ImageIterator, ImageIterator, Accessor> img,
    00462             pair<MaskImageIterator, MaskAccessor> mask,
    00463             VALUETYPE const & v)
    00464 {
    00465     initImageIf(img.first, img.second, img.third, mask.first, mask.second, v);
    00466 }
    00467
    00468 /********************************************************/
    00469 /*                                                      */
    00470 /*                    initImageBorder                   */
    00471 /*                                                      */
    00472 /********************************************************/
    00473
    00474 /** \brief Write value to the specified border pixels in the image.
    00475
    00476     A pixel is initialized if its distance to the border
    00477     is at most 'borderwidth'. It uses an accessor to access the pixel data.
    00478
    00479     The initial value can either be a constant of appropriate type (compatible with
    00480     the destination's value_type), or a functor with compatible result_type. These two
    00481     cases are automatically distinguished when <tt>FunctorTraits<FUNCTOR>::isInitializer</tt>
    00482     yields <tt>VigraTrueType</tt>. Since the functor is passed by <tt>const</tt> reference, its
    00483     <tt>operator()</tt> must be const, and its internal state may need to be <tt>mutable</tt>.
    00484
    00485     <b> Declarations:</b>
    00486
    00487     pass arguments explicitly:
    00488     \code
    00489     namespace vigra {
    00490         template <class ImageIterator, class Accessor, class VALUETYPE>
    00491         void initImageBorder(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
    00492                              int border_width, VALUETYPE const & v);
    00493
    00494         template <class ImageIterator, class Accessor, class FUNCTOR>
    00495         void initImageBorder(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
    00496                              int border_width, FUNCTOR const & v);
    00497     }
    00498     \endcode
    00499
    00500     use argument objects in conjunction with \ref ArgumentObjectFactories :
    00501     \code
    00502     namespace vigra {
    00503         template <class ImageIterator, class Accessor, class VALUETYPE>
    00504         void initImageBorder(triple<ImageIterator, ImageIterator, Accessor> img,
    00505                              int border_width, VALUETYPE const & v);
    00506
    00507         template <class ImageIterator, class Accessor, class FUNCTOR>
    00508         void initImageBorder(triple<ImageIterator, ImageIterator, Accessor> img,
    00509                              int border_width, FUNCTOR const & v);
    00510     }
    00511     \endcode
    00512
    00513     <b> Usage:</b>
    00514
    00515         <b>\#include</b> <<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>><br>
    00516         Namespace: vigra
    00517
    00518     \code
    00519     vigra::BImage img(100, 100);
    00520
    00521     // zero a border of 5 pixel
    00522     vigra::initImageBorder(destImageRange(img),
    00523                     5, vigra::NumericTraits<vigra::BImage::PixelType>::zero());
    00524     \endcode
    00525
    00526     <b> Required Interface:</b>
    00527
    00528     see \ref initImage()
    00529
    00530 */
    00531 doxygen_overloaded_function(template <...> void initImageBorder)
    00532
    00533 template <class ImageIterator, class Accessor, class VALUETYPE>
    00534 inline
    00535 void
    00536 initImageBorder(ImageIterator upperleft, ImageIterator lowerright,
    00537                 Accessor a,  int border_width, VALUETYPE const & v)
    00538 {
    00539     int w = lowerright.x - upperleft.x;
    00540     int h = lowerright.y - upperleft.y;
    00541
    00542     int hb = (border_width > h) ? h : border_width;
    00543     int wb = (border_width > w) ? w : border_width;
    00544
    00545     initImage(upperleft, upperleft+Diff2D(w,hb), a, v);
    00546     initImage(upperleft, upperleft+Diff2D(wb,h), a, v);
    00547     initImage(upperleft+Diff2D(0,h-hb), lowerright, a, v);
    00548     initImage(upperleft+Diff2D(w-wb,0), lowerright, a, v);
    00549 }
    00550
    00551 template <class ImageIterator, class Accessor, class VALUETYPE>
    00552 inline
    00553 void
    00554 initImageBorder(triple<ImageIterator, ImageIterator, Accessor> img,
    00555                 int border_width, VALUETYPE const & v)
    00556 {
    00557     initImageBorder(img.first, img.second, img.third, border_width, v);
    00558 }
    00559
    00560 //@}
    00561
    00562
    00563 } // namespace vigra
    00564
    00565 #endif // VIGRA_INITIMAGE_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.6.0 (13 Aug 2008)