00001 /*!@file Channels/ChannelMaps.C Classes to hold maps from a Channel hierarchy */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Channels/ChannelMaps.C $ 00035 // $Id: ChannelMaps.C 12820 2010-02-11 05:44:51Z itti $ 00036 // 00037 00038 #include "Channels/ChannelMaps.H" 00039 #include "Channels/ChannelBase.H" 00040 #include "Channels/SingleChannel.H" 00041 #include "Channels/ComplexChannel.H" 00042 #include "Channels/IntegerSimpleChannel.H" 00043 #include "Channels/IntegerComplexChannel.H" 00044 #include "Neuro/EnvVisualCortex.H" 00045 00046 // ###################################################################### 00047 ChannelMaps::ChannelMaps(ChannelBase* chan, const std::string& prefix) : 00048 itsOutputMap(), itsSubmaps(), itsRawCSmaps(), itsPyramid(), itsSubchanMaps() 00049 { 00050 const std::string newprefix = prefix.empty() ? "" : prefix + ":"; 00051 00052 if (prefix.empty()) // VisualCortex always has an output 00053 itsOutputMap = NamedImage<float>(chan->getOutput(), "SaliencyMap"); 00054 else 00055 { 00056 if (chan->outputAvailable()) 00057 itsOutputMap = NamedImage<float>(chan->getOutput(), newprefix + chan->tagName()); 00058 else 00059 itsOutputMap = NamedImage<float>(newprefix + chan->tagName()); 00060 } 00061 00062 // let's traverse the hierarchy 00063 if (SingleChannel* ch = dynamic_cast<SingleChannel*>(chan)) 00064 { 00065 // get all the submaps: 00066 const uint n = ch->numSubmaps(); 00067 for (uint i = 0; i < n; ++i) 00068 { 00069 const std::string name = newprefix + ch->getSubmapNameShort(i); 00070 if (ch->outputAvailable()) 00071 { 00072 itsSubmaps.push_back(NamedImage<float>(ch->getSubmap(i), name)); 00073 itsRawCSmaps.push_back(NamedImage<float>(ch->getRawCSmap(i), name + "raw")); 00074 } 00075 else 00076 { 00077 itsSubmaps.push_back(NamedImage<float>(name)); // empty image 00078 itsRawCSmaps.push_back(NamedImage<float>(name + "raw")); 00079 } 00080 } 00081 00082 // get our latest pyramid: 00083 if (ch->hasPyramid()) itsPyramid = ch->pyramid(0); 00084 } 00085 else if (ComplexChannel* ch = dynamic_cast<ComplexChannel*>(chan)) 00086 { 00087 const uint n = ch->numChans(); 00088 for (uint i = 0; i < n; ++i) 00089 itsSubchanMaps.push_back(rutz::make_shared(new ChannelMaps(ch->subChan(i).get(), newprefix + ch->tagName()))); 00090 } 00091 else if (IntegerSimpleChannel* ch = dynamic_cast<IntegerSimpleChannel*>(chan)) 00092 { 00093 // get all the submaps: 00094 const uint n = ch->numSubmaps(); 00095 for (uint i = 0; i < n; ++i) 00096 { 00097 const std::string name = newprefix + ch->getSubmapNameShort(i); 00098 if (ch->outputAvailable()) 00099 { 00100 itsSubmaps.push_back(NamedImage<float>(ch->getSubmap(i), name)); 00101 itsRawCSmaps.push_back(NamedImage<float>(ch->getRawCSmap(i), name + "raw")); 00102 } 00103 else 00104 { 00105 itsSubmaps.push_back(NamedImage<float>(name)); // empty image 00106 itsRawCSmaps.push_back(NamedImage<float>(name + "raw")); 00107 } 00108 } 00109 00110 // get our latest pyramid: 00111 itsPyramid = ImageSet<float>(ch->intgPyramid()); 00112 } 00113 else if (IntegerComplexChannel* ch = dynamic_cast<IntegerComplexChannel*>(chan)) 00114 { 00115 const uint n = ch->numChans(); 00116 for (uint i = 0; i < n; ++i) 00117 itsSubchanMaps.push_back(rutz::make_shared(new ChannelMaps(ch->subChan(i).get(), newprefix + ch->tagName()))); 00118 } 00119 else if (ChannelBase* ch = dynamic_cast<ChannelBase*>(chan)) 00120 { 00121 // get all the submaps: 00122 const uint n = ch->numSubmaps(); 00123 for (uint i = 0; i < n; ++i) 00124 { 00125 const std::string name = newprefix + ch->getSubmapNameShort(i); 00126 if (ch->outputAvailable()) 00127 itsSubmaps.push_back(NamedImage<float>(ch->getSubmap(i), name)); 00128 else 00129 itsSubmaps.push_back(NamedImage<float>(name)); // empty image 00130 } 00131 } 00132 else LFATAL("Inconsistency in Channel hierarchy!"); 00133 } 00134 00135 // ###################################################################### 00136 ChannelMaps::ChannelMaps(const NamedImage<float>& outmap) : 00137 itsOutputMap(outmap), itsSubmaps(), itsRawCSmaps(), itsSubchanMaps() 00138 { } 00139 00140 // ###################################################################### 00141 ChannelMaps::ChannelMaps(EnvVisualCortexFloat* v, const std::string& prefix) : 00142 itsOutputMap(), itsSubmaps(), itsRawCSmaps(), itsSubchanMaps() 00143 { 00144 const std::string npfx = prefix.empty() ? "VisualCortex:" : prefix + ":"; 00145 00146 // things are very simple here given the limitations of EnvVisualCortex: 00147 itsOutputMap = NamedImage<float>(v->getVCXmap(), "SaliencyMap"); 00148 itsSubchanMaps.push_back(rutz::make_shared(new ChannelMaps(NamedImage<float>(v->getImap(), npfx + "intensity")))); 00149 itsSubchanMaps.push_back(rutz::make_shared(new ChannelMaps(NamedImage<float>(v->getCmap(), npfx + "color")))); 00150 itsSubchanMaps.push_back(rutz::make_shared(new ChannelMaps(NamedImage<float>(v->getOmap(), npfx + "orientation")))); 00151 #ifdef ENV_WITH_DYNAMIC_CHANNELS 00152 itsSubchanMaps.push_back(rutz::make_shared(new ChannelMaps(NamedImage<float>(v->getFmap(), npfx + "flicker")))); 00153 itsSubchanMaps.push_back(rutz::make_shared(new ChannelMaps(NamedImage<float>(v->getMmap(), npfx + "motion")))); 00154 #endif 00155 } 00156 00157 // ###################################################################### 00158 ChannelMaps::~ChannelMaps() 00159 { } 00160 00161 // ###################################################################### 00162 const NamedImage<float>& ChannelMaps::getMap() const 00163 { return itsOutputMap; } 00164 00165 // ###################################################################### 00166 uint ChannelMaps::numSubchans() const 00167 { return itsSubchanMaps.size(); } 00168 00169 // ###################################################################### 00170 rutz::shared_ptr<ChannelMaps> ChannelMaps::subChanMaps(const uint idx) const 00171 { 00172 ASSERT(idx < itsSubchanMaps.size()); 00173 return itsSubchanMaps[idx]; 00174 } 00175 00176 // ###################################################################### 00177 uint ChannelMaps::numSubmaps() const 00178 { 00179 uint count = 0; 00180 for (uint i = 0; i < itsSubchanMaps.size(); ++i) 00181 count += itsSubchanMaps[i]->numSubmaps(); 00182 00183 return count + itsSubmaps.size(); 00184 } 00185 00186 // ###################################################################### 00187 const NamedImage<float>& ChannelMaps::getSubmap(const uint idx) const 00188 { 00189 if (itsSubchanMaps.size()) // recurse through the subchans 00190 { 00191 uint subchan = 0, subidx = 0; 00192 lookupSubmap(idx, subchan, subidx); 00193 return itsSubchanMaps[subchan]->getSubmap(subidx); 00194 } 00195 else 00196 { 00197 ASSERT(idx < itsSubmaps.size()); 00198 return itsSubmaps[idx]; 00199 } 00200 } 00201 00202 // ###################################################################### 00203 const NamedImage<float>& ChannelMaps::getRawCSmap(const uint idx) const 00204 { 00205 if (itsSubchanMaps.size()) // recurse through the subchans 00206 { 00207 uint subchan = 0, subidx = 0; 00208 lookupSubmap(idx, subchan, subidx); 00209 return itsSubchanMaps[subchan]->getRawCSmap(subidx); 00210 } 00211 else 00212 { 00213 ASSERT(idx < itsRawCSmaps.size()); 00214 return itsRawCSmaps[idx]; 00215 } 00216 } 00217 00218 // ###################################################################### 00219 void ChannelMaps::lookupSubmap(const uint idx, uint& subchan, uint& subidx) const 00220 { 00221 uint offset = 0; 00222 for (subchan = 0; subchan < itsSubchanMaps.size(); ++subchan) 00223 { 00224 subidx = idx - offset; 00225 const uint nsub = itsSubchanMaps[subchan]->numSubmaps(); 00226 if (subidx < nsub) return; // found the right subchan+submap combination 00227 else offset += nsub; 00228 } 00229 LFATAL("invalid submap index: %d", idx); 00230 } 00231 00232 // ###################################################################### 00233 bool ChannelMaps::hasPyramid() const 00234 { return itsPyramid.isNonEmpty(); } 00235 00236 // ###################################################################### 00237 const ImageSet<float>& ChannelMaps::getPyramid() const 00238 { return itsPyramid; } 00239 00240 00241 // ###################################################################### 00242 /* So things look consistent in everyone's emacs... */ 00243 /* Local Variables: */ 00244 /* mode: c++ */ 00245 /* indent-tabs-mode: nil */ 00246 /* End: */