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 Geons2D_C_DEFINED
00039 #define Geons2D_C_DEFINED
00040
00041 #include "plugins/SceneUnderstanding/Geons2D.H"
00042
00043 #include "Image/DrawOps.H"
00044 #include "Image/MathOps.H"
00045 #include "Image/Kernels.H"
00046 #include "Image/FilterOps.H"
00047 #include "Image/Convolutions.H"
00048 #include "Image/fancynorm.H"
00049 #include "Image/Point3D.H"
00050 #include "Simulation/SimEventQueue.H"
00051 #include "Neuro/EnvVisualCortex.H"
00052 #include "GUI/DebugWin.H"
00053 #include <math.h>
00054 #include <fcntl.h>
00055 #include <limits>
00056 #include <string>
00057
00058 const ModelOptionCateg MOC_Geons2D = {
00059 MOC_SORTPRI_3, "Geons2D-Related Options" };
00060
00061
00062 const ModelOptionDef OPT_Geons2DShowDebug =
00063 { MODOPT_ARG(bool), "Geons2DShowDebug", &MOC_Geons2D, OPTEXP_CORE,
00064 "Show debug img",
00065 "Geons2D-debug", '\0', "<true|false>", "false" };
00066
00067
00068
00069 Geons2D::Geons2D(OptionManager& mgr, const std::string& descrName,
00070 const std::string& tagName) :
00071 SimModule(mgr, descrName, tagName),
00072 SIMCALLBACK_INIT(SimEventGanglionOutput),
00073 SIMCALLBACK_INIT(SimEventSaveOutput),
00074 itsShowDebug(&OPT_Geons2DShowDebug, this)
00075
00076 {
00077
00078 itsMemStorage = cvCreateMemStorage(0);
00079 }
00080
00081
00082 Geons2D::~Geons2D()
00083 {
00084 cvReleaseMemStorage(&itsMemStorage);
00085
00086 }
00087
00088
00089 void Geons2D::onSimEventGanglionOutput(SimEventQueue& q,
00090 rutz::shared_ptr<SimEventGanglionOutput>& e)
00091 {
00092 itsGeons2DCellsInput = e->getCells()[0];
00093 evolve();
00094
00095 q.post(rutz::make_shared(new SimEventGeons2DOutput(this, itsGeons2DState)));
00096
00097 }
00098
00099
00100 void Geons2D::onSimEventSaveOutput(SimEventQueue& q, rutz::shared_ptr<SimEventSaveOutput>& e)
00101 {
00102 if (itsShowDebug.getVal())
00103 {
00104
00105
00106 nub::ref<FrameOstream> ofs =
00107 dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs;
00108 Layout<PixRGB<byte> > disp = getDebugImage();
00109 ofs->writeRgbLayout(disp, "Geons2D", FrameInfo("Geons2D", SRC_POS));
00110 }
00111 }
00112
00113
00114 void Geons2D::setBias(const Image<float> &biasImg)
00115 {
00116
00117
00118 }
00119
00120
00121 void Geons2D::evolve()
00122 {
00123
00124 Image<byte> in = itsGeons2DCellsInput;
00125 cvSmooth( img2ipl(in), img2ipl(in), CV_GAUSSIAN, 9, 9 );
00126 CvSeq* circles = cvHoughCircles( img2ipl(in),
00127 itsMemStorage,
00128 CV_HOUGH_GRADIENT,
00129 2,
00130 in.getHeight()/6,
00131 100,
00132 10 );
00133
00134 itsGeons2DState.clear();
00135 for(int i = 0; i < circles->total; i++ )
00136 {
00137 float* p = (float*)cvGetSeqElem( circles, i );
00138 Geons2DState state(Point2D<int>(cvRound(p[0]),(int)cvRound(p[1])), cvRound(p[2]));
00139 itsGeons2DState.push_back(state);
00140 }
00141
00142 cvClearMemStorage(itsMemStorage);
00143 }
00144
00145 Layout<PixRGB<byte> > Geons2D::getDebugImage()
00146 {
00147 Layout<PixRGB<byte> > outDisp;
00148
00149 Image<float> in = itsGeons2DCellsInput;
00150 inplaceNormalize(in, 0.0F, 255.0F);
00151
00152 Image<float> perc = in;
00153 LINFO("%lu circles", itsGeons2DState.size());
00154 for(uint i=0; i<itsGeons2DState.size(); i++)
00155 {
00156 Geons2DState geons2DState = itsGeons2DState[i];
00157 drawCircle(perc, geons2DState.pos, (int)geons2DState.radius, 255.0F, 2);
00158
00159
00160 }
00161
00162 inplaceNormalize(perc, 0.0F, 255.0F);
00163
00164
00165 Layout<PixRGB<byte> > disp;
00166
00167 outDisp = hcat(toRGB(Image<byte>(in)), toRGB(Image<byte>(perc)));
00168
00169 return outDisp;
00170
00171 }
00172
00173
00174
00175
00176
00177
00178
00179 #endif
00180