00001 /*!@file NeovisionII/ColorObjRecBrain.C A mean RGB color classifier */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // 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 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/NeovisionII/ObjRec/ColorObjRecBrain.C $ 00035 // $Id: ColorObjRecBrain.C 12962 2010-03-06 02:13:53Z irock $ 00036 // 00037 00038 #include "Image/Image.H" 00039 #include "Image/ColorOps.H" 00040 #include "Image/Pixels.H" 00041 #include "Image/CutPaste.H" 00042 #include "Image/ShapeOps.H" 00043 #include "Image/Rectangle.H" 00044 #include "Image/MathOps.H" 00045 #include "Image/MatrixOps.H" 00046 #include "Image/Transforms.H" 00047 #include "Image/Convolutions.H" 00048 #include "nub/ref.h" 00049 #include "Util/Types.H" 00050 #include "Util/MathFunctions.H" 00051 #include "Util/log.H" 00052 #include "TestSuite/ObjRecBrain.h" 00053 00054 00055 #include <stdio.h> 00056 #include <vector> 00057 #include <string> 00058 #include <limits> 00059 00060 struct ObjectDBData 00061 { 00062 char name[255]; 00063 PixRGB<float> meanRGB; 00064 00065 }; 00066 00067 struct ObjectRecData 00068 { 00069 std::string name; 00070 PixRGB<float> meanRGB; 00071 }; 00072 00073 float distanceSq(PixRGB<float>& c1, PixRGB<float>& c2) 00074 { 00075 return (squareOf((c1[0] - c2[0])) + 00076 squareOf((c1[1] - c2[1])) + 00077 squareOf((c1[2] - c2[2]))); 00078 } 00079 00080 00081 class ColorBrain : public ObjRecBrain 00082 { 00083 00084 public: 00085 ColorBrain(std::string dbFile) : 00086 itsObjectsDBFile(dbFile) 00087 { 00088 } 00089 00090 ~ColorBrain() 00091 { 00092 } 00093 00094 void preTraining() 00095 { 00096 itsFP = fopen(itsObjectsDBFile.c_str(), "wb"); 00097 if (itsFP == NULL) 00098 LFATAL("Error loading data file"); 00099 } 00100 00101 void onTraining(Image<PixRGB<byte> > &img, ObjectData& objData) 00102 { 00103 ObjectDBData obj; 00104 strcpy(obj.name, objData.name.c_str()); 00105 //Use the mean RGB as the feature value 00106 obj.meanRGB = meanRGB(img); 00107 00108 00109 fwrite(&obj, 1, sizeof(ObjectDBData), itsFP); 00110 00111 }; 00112 void postTraining() 00113 { 00114 fclose(itsFP); 00115 } 00116 00117 00118 void preRecognition() 00119 { 00120 itsFP = fopen(itsObjectsDBFile.c_str(), "r"); 00121 if (itsFP == NULL) 00122 LFATAL("Error loading data file"); 00123 00124 itsObjects.clear(); 00125 ObjectDBData obj; 00126 while( fread(&obj, 1, sizeof(ObjectDBData), itsFP) > 0) 00127 { 00128 ObjectRecData objData; 00129 objData.name = obj.name; 00130 objData.meanRGB = obj.meanRGB; 00131 itsObjects.push_back(objData); 00132 } 00133 fclose(itsFP); 00134 } 00135 00136 ObjectData onRecognition(Image<PixRGB<byte> > &img) 00137 { 00138 00139 ObjectData obj; 00140 if (itsObjects.size() > 0) 00141 { 00142 PixRGB<float> fv = meanRGB(img); 00143 00144 //A simple NN search 00145 //Get the ratio of the min distance between the first nearest object 00146 //and the second different object 00147 std::string firstObj = itsObjects[0].name; 00148 float minDist1 = distanceSq(itsObjects[0].meanRGB, fv); 00149 for(uint i=1; i<itsObjects.size(); i++) 00150 { 00151 float dist = distanceSq(itsObjects[i].meanRGB, fv); 00152 if (dist < minDist1) 00153 { 00154 minDist1 = dist; 00155 firstObj = itsObjects[i].name; 00156 } 00157 } 00158 00159 //Find the score for the next best object 00160 std::string secondObj; 00161 float minDist2 = std::numeric_limits<float>::max(); 00162 00163 for(uint i=0; i<itsObjects.size(); i++) 00164 { 00165 if (itsObjects[i].name != firstObj) 00166 { 00167 float dist = distanceSq(itsObjects[i].meanRGB, fv); 00168 if (dist < minDist2) 00169 { 00170 minDist2 = dist; 00171 secondObj = itsObjects[i].name; 00172 } 00173 } 00174 } 00175 00176 obj.name = firstObj; 00177 obj.confidence = minDist2/(minDist1+2.22044604925031e-16); //avoid divide by 0 (eps from matlab) 00178 00179 //LINFO("fv (%f,%f,%f) first best=%s %f : second best=%s %f confidence=%f", 00180 // fv[0], fv[1], fv[2], 00181 // firstObj.c_str(), minDist1, 00182 // secondObj.c_str(), minDist2, 00183 // obj.confidence); 00184 } else { 00185 obj.confidence = -1; 00186 obj.name = "unknown"; 00187 } 00188 00189 return obj; 00190 } 00191 00192 void postRecognition() 00193 { 00194 } 00195 00196 private: 00197 00198 FILE* itsFP; 00199 std::vector<ObjectRecData> itsObjects; 00200 std::string itsObjectsDBFile; 00201 00202 }; 00203 00204 //Create and destory the brain 00205 extern "C" ObjRecBrain* createObjRecBrain(std::string dbFile) 00206 { 00207 return new ColorBrain(dbFile); 00208 } 00209 00210 extern "C" void destoryObjRecBrain(ObjRecBrain* brain) 00211 { 00212 delete brain; 00213 } 00214 00215 00216 int main(const int argc, const char **argv) 00217 { 00218 LFATAL("Use test-ObjRec"); 00219 return 0; 00220 } 00221 00222 // ###################################################################### 00223 /* So things look consistent in everyone's emacs... */ 00224 /* Local Variables: */ 00225 /* indent-tabs-mode: nil */ 00226 /* End: */