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