00001 /*!@file plugins/SceneUnderstanding/LineFitting.C */ 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: Lior Elazary <elazary@usc.edu> 00034 // $HeadURL: $ 00035 // $Id: $ 00036 // 00037 00038 #ifndef LineFitting_C_DEFINED 00039 #define LineFitting_C_DEFINED 00040 00041 #include "Image/OpenCVUtil.H" //Need to be first to avoid type def conf 00042 #include "plugins/SceneUnderstanding/LineFitting.H" 00043 #include "plugins/SceneUnderstanding/V2.H" 00044 00045 #include "Image/DrawOps.H" 00046 #include "Raster/Raster.H" 00047 #include "Simulation/SimEventQueue.H" 00048 #include "GUI/DebugWin.H" 00049 #include "plugins/SceneUnderstanding/LFLineFitter/LFLineFitter.h" 00050 00051 00052 const ModelOptionCateg MOC_LineFitting = { 00053 MOC_SORTPRI_3, "LineFitting-Related Options" }; 00054 00055 // Used by: SimulationViewerEyeMvt 00056 const ModelOptionDef OPT_LineFittingShowDebug = 00057 { MODOPT_ARG(bool), "LineFittingShowDebug", &MOC_LineFitting, OPTEXP_CORE, 00058 "Show debug img", 00059 "linefitting-debug", '\0', "<true|false>", "false" }; 00060 00061 //Define the inst function name 00062 SIMMODULEINSTFUNC(LineFitting); 00063 00064 00065 // ###################################################################### 00066 LineFitting::LineFitting(OptionManager& mgr, const std::string& descrName, 00067 const std::string& tagName) : 00068 SimModule(mgr, descrName, tagName), 00069 SIMCALLBACK_INIT(SimEventV1Output), 00070 SIMCALLBACK_INIT(SimEventInputFrame), 00071 SIMCALLBACK_INIT(SimEventSaveOutput), 00072 itsShowDebug(&OPT_LineFittingShowDebug, this) 00073 { 00074 } 00075 00076 // ###################################################################### 00077 LineFitting::~LineFitting() 00078 { 00079 } 00080 00081 // ###################################################################### 00082 void LineFitting::onSimEventV1Output(SimEventQueue& q, 00083 rutz::shared_ptr<SimEventV1Output>& e) 00084 { 00085 //itsV1EdgesState = e->getEdgesState(); 00086 00087 //evolve(q); 00088 } 00089 00090 00091 // ###################################################################### 00092 void LineFitting::onSimEventInputFrame(SimEventQueue& q, 00093 rutz::shared_ptr<SimEventInputFrame>& e) 00094 { 00095 // here is the inputs image: 00096 GenericFrame frame = e->frame(); 00097 00098 itsInImage = frame.asRgb(); 00099 00100 evolve(q); 00101 } 00102 00103 void LineFitting::evolve(SimEventQueue& q) 00104 { 00105 00106 Image<float> edges = luminance(itsInImage); 00107 itsLines = FitLine(edges); 00108 00109 q.post(rutz::make_shared(new SimEventV2Output(this, itsLines, itsInImage.getDims()))); 00110 00111 } 00112 00113 00114 std::vector<V2::LineSegment> LineFitting::FitLine(const Image<float>& edges) 00115 { 00116 std::map<int,Point2D<int> > edgesMap; 00117 00118 LFLineFitter lf; 00119 lf.Configure("para_line_fitter.txt"); 00120 lf.Init(); 00121 lf.FitLine(img2ipl(edges)); 00122 00123 int nLines = lf.rNLineSegments(); 00124 LFLineSegment* lineSegmentMap = lf.rOutputEdgeMap(); 00125 00126 LFLineSegment* lines = new LFLineSegment[nLines]; 00127 00128 std::vector<V2::LineSegment> lineSegments; 00129 for (int i=0 ; i<nLines ; i++) 00130 { 00131 lines[i] = lineSegmentMap[i]; 00132 lineSegments.push_back(V2::LineSegment( 00133 Point2D<float>(lines[i].sx_, lines[i].sy_), 00134 Point2D<float>(lines[i].ex_, lines[i].ey_))); 00135 } 00136 00137 return lineSegments; 00138 00139 00140 } 00141 00142 00143 // ###################################################################### 00144 void LineFitting::onSimEventSaveOutput(SimEventQueue& q, 00145 rutz::shared_ptr<SimEventSaveOutput>& e) 00146 { 00147 if (itsShowDebug.getVal()) 00148 { 00149 // get the OFS to save to, assuming sinfo is of type 00150 // SimModuleSaveInfo (will throw a fatal exception otherwise): 00151 nub::ref<FrameOstream> ofs = 00152 dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs; 00153 Layout<PixRGB<byte> > disp = getDebugImage(); 00154 if (disp.initialized()) 00155 ofs->writeRgbLayout(disp, "LineFitting", FrameInfo("LineFitting", SRC_POS)); 00156 } 00157 } 00158 00159 00160 Layout<PixRGB<byte> > LineFitting::getDebugImage() 00161 { 00162 00163 Layout<PixRGB<byte> > disp; 00164 00165 Image<PixRGB<byte> > tmp = itsInImage; 00166 for(uint i=0; i<itsLines.size(); i++) 00167 drawLine(tmp, (Point2D<int>)itsLines[i].p1, (Point2D<int>)itsLines[i].p2, PixRGB<byte>(0,255,0)); 00168 00169 disp = hcat(itsInImage, tmp); 00170 00171 usleep(10000); 00172 return disp; 00173 00174 00175 } 00176 00177 #endif