00001 /*!@file INVT/mgzvisualize.C Display combined .mgz streams 00002 */ 00003 00004 // //////////////////////////////////////////////////////////////////// // 00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00006 // University of Southern California (USC) and the iLab at USC. // 00007 // See http://iLab.usc.edu for information about this project. // 00008 // //////////////////////////////////////////////////////////////////// // 00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00011 // in Visual Environments, and Applications'' by Christof Koch and // 00012 // Laurent Itti, California Institute of Technology, 2001 (patent // 00013 // pending; application number 09/912,225 filed July 23, 2001; see // 00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00015 // //////////////////////////////////////////////////////////////////// // 00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00017 // // 00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00019 // redistribute it and/or modify it under the terms of the GNU General // 00020 // Public License as published by the Free Software Foundation; either // 00021 // version 2 of the License, or (at your option) any later version. // 00022 // // 00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00026 // PURPOSE. See the GNU General Public License for more details. // 00027 // // 00028 // You should have received a copy of the GNU General Public License // 00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00031 // Boston, MA 02111-1307 USA. // 00032 // //////////////////////////////////////////////////////////////////// // 00033 // 00034 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/INVT/mgzvisualize.C $ 00036 // $Id: mgzvisualize.C 10794 2009-02-08 06:21:09Z itti $ 00037 // 00038 00039 #include "Component/ModelManager.H" 00040 #include "Image/DrawOps.H" 00041 #include "Image/Layout.H" 00042 #include "Image/MathOps.H" 00043 #include "Image/ShapeOps.H" 00044 #include "Media/FrameSeries.H" 00045 #include "Media/MgzDecoder.H" 00046 #include "Raster/GenericFrame.H" 00047 #include "Transport/FrameInfo.H" 00048 #include "Util/csignals.H" 00049 #include "Util/StringConversions.H" 00050 #include "Util/StringUtil.H" 00051 #include "Util/sformat.H" 00052 #include "rutz/trace.h" 00053 00054 //! Visualize mgz streams 00055 /*! Typically you would use that with VCX output files computed and 00056 saved by running ezvision. */ 00057 int main(const int argc, const char **argv) 00058 { 00059 GVX_TRACE("mgzvisualize"); 00060 00061 // 'volatile' because we will modify this from signal handlers 00062 volatile int signum = 0; 00063 00064 // catch signals and redirect them for a clean exit (in particular, 00065 // this gives us a chance to do useful things like flush and close 00066 // output files that would otherwise be left in a bogus state, like 00067 // mpeg output files): 00068 catchsignals(&signum); 00069 00070 MYLOGVERB = LOG_INFO; // suppress debug messages 00071 00072 // Instantiate a ModelManager: 00073 ModelManager manager("MGZ Visualizer"); 00074 00075 // Instantiate our various ModelComponents: 00076 nub::ref<InputFrameSeries> mpgifs(new InputFrameSeries(manager)); 00077 manager.addSubComponent(mpgifs); 00078 00079 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager)); 00080 manager.addSubComponent(ofs); 00081 00082 // Parse command-line: 00083 if (manager. 00084 parseCommandLine(argc, argv, 00085 "<in1.mgz:weight:av:st> ... <inN.mgz:weight:av:st>", 00086 1, -1) == false) 00087 return(1); 00088 00089 std::vector<rutz::shared_ptr<MgzDecoder> > ifs; 00090 std::vector<float> weight, avg, std; 00091 00092 for (uint i = 0; i < manager.numExtraArgs(); i ++) 00093 { 00094 std::vector<std::string> tokens; 00095 split(manager.getExtraArg(i), ":", std::back_inserter(tokens)); 00096 00097 rutz::shared_ptr<MgzDecoder> in(new MgzDecoder(tokens[0])); 00098 ifs.push_back(in); 00099 00100 float w = 1.0F; if (tokens.size() > 1) convertFromString(tokens[1], w); 00101 weight.push_back(w); 00102 00103 float av = 0.0F; if (tokens.size() > 2) convertFromString(tokens[2], av); 00104 avg.push_back(av); 00105 00106 float st = 1.0F; if (tokens.size() > 3) convertFromString(tokens[3], st); 00107 if (st == 0.0F) LFATAL("Cannot handle zero stdev!"); 00108 std.push_back(st); 00109 } 00110 00111 // let's get all our ModelComponent instances started: 00112 manager.start(); 00113 00114 int retval = 0; 00115 00116 // main loop: 00117 while(true) 00118 { 00119 if (signum != 0) 00120 { LINFO("Quit: %s was caught", signame(signum)); retval = -1; break; } 00121 00122 if (ofs->becameVoid()) 00123 { LINFO("Quit: output stream was closed or became void"); break; } 00124 00125 // Get the next input frames: 00126 mpgifs->updateNext(); 00127 GenericFrame input = mpgifs->readFrame(); 00128 if (input.initialized() == false) 00129 { LINFO("Quit: input stream exhausted"); break; } 00130 00131 Image<float> sm; 00132 for (uint i = 0; i < ifs.size(); i ++) 00133 { 00134 GenericFrame frame = ifs[i]->readFrame(); 00135 if (frame.initialized() == false) 00136 { LINFO("Quit: input MGZ stream %d exhausted", i); break; } 00137 Image<float> cm = frame.asFloat(); 00138 00139 if (avg[i] != 0.0F) cm -= avg[i]; 00140 00141 const float w = weight[i] / std[i]; 00142 if (w != 1.0F) cm *= w; 00143 00144 if (sm.initialized()) sm += cm; else sm = cm; 00145 } 00146 00147 // normalize the map: 00148 float mi, ma; getMinMax(sm, mi, ma); 00149 const float factor = 0.0F; 00150 if (factor == 0.0F) inplaceNormalize(sm, 0.0F, 255.0F); 00151 else if (factor != 1.0F) sm *= factor; 00152 00153 // zoom the map: 00154 Image<byte> smb = sm; // clamped conversion 00155 smb = rescaleOpt(smb, input.getDims(), false); 00156 Image<PixRGB<byte> > smc = smb; 00157 00158 writeText(smc, Point2D<int>(1,1), sformat("[%f ... %f]", mi, ma).c_str()); 00159 00160 Layout<PixRGB<byte> > out = hcat(input.asRgb(), smc); 00161 ofs->writeRgbLayout(out, "mgzvisualize", 00162 FrameInfo("mgzvisualize", SRC_POS)); 00163 } 00164 00165 // print final memory allocation stats 00166 LINFO("Done."); 00167 00168 // stop all our ModelComponents 00169 manager.stop(); 00170 00171 // all done! 00172 return retval; 00173 } 00174 00175 // ###################################################################### 00176 /* So things look consistent in everyone's emacs... */ 00177 /* Local Variables: */ 00178 /* indent-tabs-mode: nil */ 00179 /* End: */