00001 /*!@file Learn/test-QuadTree.C QuadTree Multi-Class Classifier */ 00002 // //////////////////////////////////////////////////////////////////// // 00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00004 // University of Southern California (USC) and the iLab at USC. // 00005 // See http://iLab.usc.edu for information about this project. // 00006 // //////////////////////////////////////////////////////////////////// // 00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00009 // in Visual Environments, and Applications'' by Christof Koch and // 00010 // Laurent Itti, California Institute of Technology, 2001 (patent // 00011 // pending; application number 09/912,225 filed July 23, 2001; see // 00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00013 // //////////////////////////////////////////////////////////////////// // 00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00015 // // 00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00017 // redistribute it and/or modify it under the terms of the GNU General // 00018 // Public License as published by the Free Software Foundation; either // 00019 // version 2 of the License, or (at your option) any later version. // 00020 // // 00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00024 // PURPOSE. See the GNU General Public License for more details. // 00025 // // 00026 // You should have received a copy of the GNU General Public License // 00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00029 // Boston, MA 02111-1307 USA. // 00030 // //////////////////////////////////////////////////////////////////// // 00031 // 00032 // Primary maintainer for this file: John Shen <shenjohn@usc.edu> 00033 // $HeadURL$ 00034 // $Id$ 00035 // 00036 00037 #include "Component/ModelComponent.H" 00038 #include "Component/ModelManager.H" 00039 #include "Image/ColorMap.H" 00040 #include "Image/ColorOps.H" // for colorize() 00041 #include "Image/CutPaste.H" 00042 #include "Image/Dims.H" 00043 #include "Image/Image.H" 00044 #include "Image/ShapeOps.H" 00045 #include "Learn/QuadTree.H" 00046 #include "Channels/RawVisualCortex.H" 00047 #include "Raster/GenericFrame.H" 00048 #include "Raster/Raster.H" 00049 00050 #include "GUI/DebugWin.H" 00051 00052 std::vector<byte> perm3Number(int key) 00053 { 00054 key = key % 6; 00055 std::vector<byte> ret; 00056 int rem = key % 3; 00057 ret.push_back(rem); 00058 if(key > 3) { 00059 ret.push_back((rem + 1) % 3); 00060 ret.push_back((rem + 2) % 3); 00061 } else { 00062 ret.push_back((rem + 2) % 3); 00063 ret.push_back((rem + 1) % 3); 00064 } 00065 return ret; 00066 } 00067 00068 QuadNode::NodeState hashNodeState(int key) { 00069 key = key % (30 * 6); 00070 return QuadNode::NodeState(key % 30, perm3Number(key / 30)); 00071 } 00072 00073 int submain(const int argc, char** argv) 00074 { 00075 MYLOGVERB = LOG_INFO; 00076 00077 ModelManager manager("Test Quad Tree"); 00078 00079 nub::ref<RawVisualCortex> vc(new RawVisualCortex(manager)); 00080 manager.addSubComponent(vc); 00081 // vc->setModelParamVal("LevelSpec",LevelSpec(2,4,3,4,4)); 00082 00083 00084 uint nArgs = 2; 00085 if (manager.parseCommandLine((const int)argc, (const char**)argv, "<image filename>", nArgs, nArgs) == false) 00086 return 1; 00087 vc->setModelParamVal("RawVisualCortexChans",std::string("CIO")); 00088 vc->setModelParamVal("LevelSpec",LevelSpec(0,1,2,3,0),MC_RECURSE); // bug in resetting pyramids, need to check this 00089 //vc->setModelParamVal("LevelSpec",LevelSpec(2,4,3,4,4)); 00090 00091 // fix image size 00092 Dims dims(320,240); 00093 00094 // import image 00095 const GenericFrame input = Raster::ReadFrame(manager.getExtraArg(0).c_str()); 00096 Image<PixRGB<byte> > im = rescale(input.asRgb(), dims); 00097 Image<uint> cl_im(dims, ZEROS); 00098 // test GistPixelClassifier 00099 00100 // GistPixelClassifier gpc; 00101 //gpc.learnInput(im, cl_im); 00102 00103 // create Quad Tree for a given image size 00104 QuadTree myQuadTree(1,im); 00105 00106 // // create tester Classifier based on color alone 00107 // rutz::shared_ptr<ColorPixelClassifier> cpc(new ColorPixelClassifier); 00108 // cpc->addCategory(ColorPixelClassifier::ColorCat 00109 // (PixRGB<byte>(140,140,100), 25)); //grass 00110 // cpc->addCategory(ColorPixelClassifier::ColorCat 00111 // (PixRGB<byte>(20,200,20), 25)); 00112 // cpc->addCategory(ColorPixelClassifier::ColorCat 00113 // (PixRGB<byte>(20,20,200), 25)); 00114 // cpc->addCategory(ColorPixelClassifier::ColorCat 00115 // (PixRGB<byte>(32,32,32), 25)); //brown 00116 // cpc->addCategory(ColorPixelClassifier::ColorCat 00117 // (PixRGB<byte>(255,0,0), 25)); //red 00118 // cpc->addCategory(ColorPixelClassifier::ColorCat 00119 // (PixRGB<byte>(0,255,0), 25)); //green 00120 // cpc->addCategory(ColorPixelClassifier::ColorCat 00121 // (PixRGB<byte>(0,0,255), 25)); //blue 00122 // myQuadTree.setClassifier(cpc); 00123 00124 manager.start(); 00125 00126 // input 00127 vc->input(InputFrame::fromRgb(&im)); 00128 00129 for(uint i = 0; i < vc->numSubmaps(); i++) { 00130 Image<float> im_fl = vc->getSubmap(i); 00131 // output 00132 inplaceNormalize(im_fl,0.0f,255.0f); 00133 LINFO("%s", toStr(vc->getSubmapName(i)).c_str()); 00134 Image<PixRGB<byte> > im_out = im_fl; 00135 } 00136 00137 rutz::shared_ptr<QuadNode> root = myQuadTree.getRootNode(); 00138 LINFO("caching classifier result"); 00139 myQuadTree.cacheClassifierResult(); 00140 LINFO("generating proposals"); 00141 std::vector<QuadNode::NodeState> pr = myQuadTree.generateProposalsAt(root, manager.getExtraArgAs<double>(1)); 00142 LINFO("done; %zu proposals made", pr.size()); 00143 00144 uint i,i_max = 0; 00145 for(i = 0; i < pr.size(); i++) { 00146 LINFO("proposal %d: %s -> %f", i, toStr(pr[i]).c_str(),pr[i].E); 00147 if(pr[i].E < pr[i_max].E) i_max = i; 00148 } 00149 00150 if(pr.size() > 0) { 00151 LINFO("proposal with least energy %f: %s", pr[i_max].E, toStr(pr[i_max]).c_str()); 00152 root->setState(pr[i_max]); 00153 } 00154 else 00155 { 00156 LINFO("no proposals match!"); return 0; 00157 } 00158 00159 // image reporting 00160 Image<PixRGB<byte> > im_L0, im_L1, im_tot; 00161 im_L0 = root->getColorizedSegImage(); 00162 im_L1 = root->getColorizedChildSegImage(); 00163 00164 im_tot = concatX(im, concatX(im_L0, im_L1)); 00165 drawLine(im_tot, Point2D<int>(dims.w(), 0), Point2D<int>(dims.w(),dims.h()-1),PixRGB<byte>(255,255,255)); 00166 drawLine(im_tot, Point2D<int>(2*dims.w(), 0), Point2D<int>(2*dims.w(),dims.h()-1),PixRGB<byte>(255,255,255)); 00167 00168 // output 00169 Raster::WriteRGB(im_tot,"result.png"); 00170 00171 SHOWIMG(im_tot); 00172 //myQuadTree.printTree(); 00173 // Raster::WriteRGB(root->getColorizedSegImage(), 00174 // sformat("seg%02d.png",i)); 00175 00176 manager.stop(); 00177 return 0; 00178 } 00179 00180 extern "C" int main(const int argc, char** argv) 00181 { 00182 try 00183 { 00184 return submain(argc, argv); 00185 } 00186 catch (...) 00187 { 00188 REPORT_CURRENT_EXCEPTION; 00189 } 00190 00191 return 1; 00192 } 00193 00194 // ###################################################################### 00195 /* So things look consistent in everyone's emacs... */ 00196 /* Local Variables: */ 00197 /* indent-tabs-mode: nil */ 00198 /* End: */