00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef LGN_C_DEFINED
00039 #define LGN_C_DEFINED
00040
00041 #include "plugins/SceneUnderstanding/LGN.H"
00042
00043 #include "Image/DrawOps.H"
00044
00045 #include "Image/MathOps.H"
00046 #include "Image/Layout.H"
00047 #include "Simulation/SimEventQueue.H"
00048 #include "Simulation/SimEvents.H"
00049 #include "Media/MediaSimEvents.H"
00050 #include "Channels/InputFrame.H"
00051 #include "Image/Kernels.H"
00052 #include "Image/FilterOps.H"
00053 #include "Image/Convolutions.H"
00054 #include "GUI/DebugWin.H"
00055 #include <math.h>
00056 #include <fcntl.h>
00057 #include <limits>
00058 #include <string>
00059
00060 const ModelOptionCateg MOC_LGN = {
00061 MOC_SORTPRI_3, "LGN-Related Options" };
00062
00063
00064 const ModelOptionDef OPT_LGNShowDebug =
00065 { MODOPT_ARG(bool), "LGNShowDebug", &MOC_LGN, OPTEXP_CORE,
00066 "Show debug img",
00067 "lgn-debug", '\0', "<true|false>", "false" };
00068
00069
00070
00071 SIMMODULEINSTFUNC(LGN);
00072
00073
00074 LGN::LGN(OptionManager& mgr, const std::string& descrName,
00075 const std::string& tagName) :
00076 SimModule(mgr, descrName, tagName),
00077 SIMCALLBACK_INIT(SimEventInputFrame),
00078 SIMCALLBACK_INIT(SimEventSaveOutput),
00079 itsShowDebug(&OPT_LGNShowDebug, this),
00080 itsInitialized(false)
00081
00082 {
00083 }
00084
00085
00086 LGN::~LGN()
00087 {
00088
00089 }
00090
00091
00092 void LGN::init(Dims numCells)
00093 {
00094 Image<float> img(numCells, ZEROS);
00095 for(int i=0; i<3; i++)
00096 {
00097 itsCellsInput.push_back(img);
00098 itsCellsMu.push_back(img);
00099 img.clear(1.0);
00100 itsCellsSig.push_back(img);
00101 }
00102
00103
00104
00105
00106
00107
00108 itsInitialized = true;
00109
00110 }
00111
00112
00113 void LGN::onSimEventInputFrame(SimEventQueue& q,
00114 rutz::shared_ptr<SimEventInputFrame>& e)
00115 {
00116
00117 GenericFrame frame = e->frame();
00118
00119 const Image<PixRGB<byte> > inimg = rescale(frame.asRgb(), 640, 480);
00120 itsCurrentImg = inimg;
00121
00122 if (!itsInitialized)
00123 init(itsCurrentImg.getDims());
00124
00125
00126 rutz::shared_ptr<GenericFrame::MetaData> metaData;
00127 if (frame.hasMetaData(std::string("ObjectsData")))
00128 metaData = frame.getMetaData(std::string("ObjectsData"));
00129
00130
00131 Image<float> lum,rg,by;
00132
00133 getLAB(inimg, lum, rg, by);
00134
00135
00136 inplaceNormalize(lum, 0.0F, 255.0F);
00137 inplaceNormalize(rg, 0.0F, 255.0F);
00138 inplaceNormalize(by, 0.0F, 255.0F);
00139
00140 Image<float> kernel = gaussian<float>(0.0F, 1.4, lum.getWidth(),1.0F);
00141 Image<float> kernel2 = gaussian<float>(0.0F, 1.5, lum.getWidth(),1.0F);
00142 Image<float> kernel3 = gaussian<float>(0.0F, 1.5, lum.getWidth(),1.0F);
00143
00144 itsCellsInput[LUM] = sepFilter(lum, kernel, kernel, CONV_BOUNDARY_CLEAN);
00145 itsCellsInput[RG] = sepFilter(rg, kernel2, kernel2, CONV_BOUNDARY_CLEAN);
00146 itsCellsInput[BY] = sepFilter(by, kernel3, kernel3, CONV_BOUNDARY_CLEAN);
00147
00148 itsCellsMu[LUM] = itsCellsInput[LUM];
00149 itsCellsMu[RG] = itsCellsInput[RG];
00150 itsCellsMu[BY] = itsCellsInput[BY];
00151
00152
00153
00154 q.post(rutz::make_shared(new SimEventLGNOutput(this, itsCellsMu, metaData)));
00155
00156 }
00157
00158
00159 void LGN::onSimEventSaveOutput(SimEventQueue& q, rutz::shared_ptr<SimEventSaveOutput>& e)
00160 {
00161 if (itsShowDebug.getVal())
00162 {
00163
00164
00165 nub::ref<FrameOstream> ofs =
00166 dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs;
00167 Layout<PixRGB<byte> > disp = getDebugImage();
00168 ofs->writeRgbLayout(disp, "LGN", FrameInfo("LGN", SRC_POS));
00169 }
00170 }
00171
00172
00173 void LGN::setBias(const ImageSet<float>& prior)
00174 {
00175
00176 }
00177
00178
00179 void LGN::evolve()
00180 {
00181 float R = 2;
00182 float Q=0.1;
00183
00184
00185 for(int i=0; i<3; i++)
00186 {
00187
00188
00189 Image<float>::const_iterator inPtr = itsCellsInput[i].begin();
00190 Image<float>::const_iterator inStop = itsCellsInput[i].end();
00191
00192 Image<float>::iterator muPtr = itsCellsMu[i].beginw();
00193 Image<float>::iterator sigPtr = itsCellsSig[i].beginw();
00194
00195
00196 while(inPtr != inStop)
00197 {
00198
00199 float mu_hat = *muPtr;
00200 float sig_hat = *sigPtr + Q;
00201
00202
00203 float K = (sig_hat)/(sig_hat + R);
00204
00205 *muPtr =*inPtr;
00206 *sigPtr = (1-K)*sig_hat;
00207
00208
00209
00210
00211
00212 float surprise = (((*muPtr-mu_hat)*(*muPtr-mu_hat)) + (*sigPtr * *sigPtr) + (sig_hat*sig_hat));
00213 surprise = surprise / (2*sig_hat*sig_hat);
00214 surprise += log(sig_hat / *sigPtr);
00215
00216
00217
00218
00219
00220
00221 ++inPtr;
00222 ++muPtr;
00223 ++sigPtr;
00224 }
00225 }
00226
00227 }
00228
00229 Layout<PixRGB<byte> > LGN::getDebugImage()
00230 {
00231
00232 Image<float> lumPerc = itsCellsMu[0];
00233 Image<float> rgPerc = itsCellsMu[1];
00234 Image<float> byPerc = itsCellsMu[2];
00235
00236
00237 inplaceNormalize(lumPerc, 0.0F, 255.0F);
00238 inplaceNormalize(rgPerc, 0.0F, 255.0F);
00239 inplaceNormalize(byPerc, 0.0F, 255.0F);
00240
00241
00242
00243 Layout<PixRGB<byte> > disp;
00244 disp = hcat(itsCurrentImg, toRGB(Image<byte>(lumPerc)));
00245 disp = hcat(disp, toRGB(Image<byte>(rgPerc)));
00246 disp = hcat(disp, toRGB(Image<byte>(byPerc)));
00247
00248 return disp;
00249
00250 }
00251
00252
00253
00254
00255
00256
00257
00258
00259 #endif
00260