VIGRA - vigra/multi_impex.hxx Source File

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

| vigra/multi_impex.hxx |
--- | --- | ---

    00001 /************************************************************************/
    00002 /*                                                                      */
    00003 /*               Copyright 2003 by Gunnar Kedenburg                     */
    00004 /*       Cognitive Systems Group, University of Hamburg, Germany        */
    00005 /*                                                                      */
    00006 /*    This file is part of the VIGRA computer vision library.           */
    00007 /*    ( Version 1.5.0, Dec 07 2006 )                                    */
    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_MULTI_IMPEX_HXX
    00040 #define VIGRA_MULTI_IMPEX_HXX
    00041
    00042 #include <memory>
    00043 #include <iomanip>
    00044 #include <sstream>
    00045 #include <iostream>
    00046 #include <string>
    00047 #include "config.hxx"
    00048 #include "basicimageview.hxx"
    00049 #include "impex.hxx"
    00050 #include "multi_array.hxx"
    00051
    00052 namespace vigra {
    00053
    00054 VIGRA_EXPORT void findImageSequence(const std::string &name_base,
    00055                        const std::string &name_ext,
    00056                        std::vector<std::string> & numbers);
    00057
    00058 /** \addtogroup VolumeImpex Import/export of volume data.
    00059 */
    00060
    00061 //@{
    00062
    00063 /********************************************************/
    00064 /*                                                      */
    00065 /*                    importVolume                      */
    00066 /*                                                      */
    00067 /********************************************************/
    00068
    00069 /** \brief Function for importing a 3D volume.
    00070
    00071     The data are expected to be stored in a by-slice manner,
    00072     where the slices are enumerated from <tt>name_base+"[0-9]+"+name_ext</tt>.
    00073     <tt>name_base</tt> may contain a path. All slice files with the same name base and
    00074     extension are considered part of the same volume. Slice numbers must be non-negative,
    00075     but can otherwise start anywhere and need not be successive. Slices will be read
    00076     in ascending numerical (not lexicographic) order. All slices must have the
    00077     same size. The <tt>volume</tt> will be reshaped to match the count and
    00078     size of the slices found.
    00079
    00080     <b>\#include</b>
    00081     "<a href="multi__impex_8hxx-source.html">vigra/multi_impex.hxx</a>"
    00082
    00083     Namespace: vigra
    00084 */
    00085 template <class T, class Allocator>
    00086 void importVolume (MultiArray <3, T, Allocator> & volume,
    00087                    const std::string &name_base,
    00088                    const std::string &name_ext)
    00089 {
    00090     std::vector<std::string> numbers;
    00091     findImageSequence(name_base, name_ext, numbers);
    00092
    00093     std::string message("importVolume(): No files matching '");
    00094     message += name_base + "[0-9]+" + name_ext + "' found.";
    00095     vigra_precondition(numbers.size() > 0, message.c_str());
    00096
    00097     for (unsigned int i = 0; i < numbers.size(); ++i)
    00098     {
    00099         // build the filename
    00100         std::string name = name_base + numbers[i] + name_ext;
    00101
    00102         // import the image
    00103         ImageImportInfo info (name.c_str ());
    00104
    00105         // reshape the array according to size of first image
    00106         if(i == 0)
    00107         {
    00108             typedef typename MultiArray <3, T>::difference_type Size;
    00109             volume.reshape(Size(info.width(), info.height(), numbers.size()));
    00110         }
    00111
    00112         // generate a basic image view to the current layer
    00113         MultiArrayView <2, T> array_view (volume.bindOuter (i));
    00114         BasicImageView <T> view = makeBasicImageView (array_view);
    00115         vigra_precondition(view.size() == info.size(),
    00116             "importVolume(): image size mismatch.");
    00117
    00118         importImage (info, destImage(view));
    00119     }
    00120 }
    00121
    00122
    00123 /********************************************************/
    00124 /*                                                      */
    00125 /*                    exportVolume                      */
    00126 /*                                                      */
    00127 /********************************************************/
    00128
    00129 /** \brief Function for exporting a 3D volume.
    00130
    00131     The volume is exported in a by-slice manner, where the number of slices equals
    00132     the depth of the volume. The file names will be enumerated like
    00133     <tt>name_base+"000"+name_ext</tt>, <tt>name_base+"001"+name_ext</tt> etc.
    00134     (the actual number of zeros depends on the depth).
    00135
    00136     <b>\#include</b>
    00137     "<a href="multi__impex_8hxx-source.html">vigra/multi_impex.hxx</a>"
    00138
    00139     Namespace: vigra
    00140 */
    00141 template <class T, class Tag>
    00142 void exportVolume (MultiArrayView <3, T, Tag> const & volume,
    00143                    const std::string &name_base,
    00144                    const std::string &name_ext)
    00145 {
    00146
    00147     const unsigned int depth = volume.shape (2);
    00148     int numlen = static_cast <int> (std::ceil (std::log10 ((double)depth)));
    00149     for (unsigned int i = 0; i < depth; ++i)
    00150     {
    00151
    00152         // build the filename
    00153         std::stringstream stream;
    00154         stream << std::setfill ('0') << std::setw (numlen) << i;
    00155         std::string name_num;
    00156         stream >> name_num;
    00157         std::string name = name_base + name_num + name_ext;
    00158
    00159         // generate a basic image view to the current layer
    00160         MultiArrayView <2, T, Tag> array_view (volume.bindOuter (i));
    00161         BasicImageView <T> view = makeBasicImageView (array_view);
    00162
    00163         // export the image
    00164         ImageExportInfo info(name.c_str ());
    00165         exportImage(srcImageRange(view), info);
    00166     }
    00167 }
    00168
    00169 //@}
    00170
    00171 } // namespace vigra
    00172
    00173 #endif // VIGRA_MULTI_IMPEX_HXX

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