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 NEURO_TARGETCHECKER_C_DEFINED
00039 #define NEURO_TARGETCHECKER_C_DEFINED
00040
00041 #include "Neuro/TargetChecker.H"
00042
00043 #include "Channels/InputFrame.H"
00044 #include "Component/GlobalOpts.H"
00045 #include "Component/OptionManager.H"
00046 #include "Image/DrawOps.H"
00047 #include "Image/MathOps.H"
00048 #include "Image/Transforms.H"
00049 #include "Neuro/NeuroOpts.H"
00050 #include "Raster/Raster.H"
00051 #include "Util/TextLog.H"
00052 #include "Util/sformat.H"
00053 #include "Simulation/SimEvents.H"
00054 #include "Neuro/NeuroSimEvents.H"
00055 #include "Simulation/SimEventQueue.H"
00056
00057
00058 TargetChecker::TargetChecker(OptionManager& mgr, const std::string& descrName,
00059 const std::string& tagName)
00060 :
00061 SimModule(mgr, descrName, tagName),
00062 SIMCALLBACK_INIT(SimEventWTAwinner),
00063 SIMCALLBACK_INIT(SimEventRetinaImage),
00064 SIMCALLBACK_INIT(SimEventTargetMask),
00065 itsFOAradius(&OPT_FOAradius, this),
00066 itsLogFile(&OPT_TextLogFile, this),
00067 itsTargetMaskFname(&OPT_TargetMaskFname, this),
00068 itsVisualField(),
00069 itsRawTargetMask(),
00070 itsTargetMask(),
00071 itsNumTargetsRemaining(0)
00072 { }
00073
00074
00075 TargetChecker::~TargetChecker()
00076 { }
00077
00078
00079 void TargetChecker::reset1()
00080 {
00081 itsVisualField.freeMem();
00082 SimModule::reset1();
00083 }
00084
00085
00086 void TargetChecker::
00087 onSimEventWTAwinner(SimEventQueue& q, rutz::shared_ptr<SimEventWTAwinner>& e)
00088 {
00089
00090
00091 const WTAwinner winner = e->winner();
00092
00093
00094
00095 if (itsTargetMask.initialized())
00096 {
00097
00098 const int numhit =
00099 drawDiskCheckTarget(itsVisualField, itsTargetMask, winner.p,
00100 itsFOAradius.getVal(), byte(255), byte(255), byte(250));
00101
00102
00103 if (numhit > 0)
00104 {
00105 LINFO("###### %d new target(s) hit at t=%fms ######", numhit, winner.t.msecs());
00106 itsNumTargetsRemaining -= numhit;
00107
00108 textLog(itsLogFile.getVal(), "HitTargets", sformat("%d", numhit), winner.t);
00109
00110 q.post(rutz::shared_ptr<SimEventTargetsHit> (new SimEventTargetsHit(this, numhit)));
00111 }
00112
00113
00114 if (itsTargetMask.initialized() && itsNumTargetsRemaining <= 0)
00115 {
00116 LINFO("##### All targets found -- EXIT #####");
00117 textLog(itsLogFile.getVal(), "AllTargetsFound", "", winner.t);
00118
00119 q.post(rutz::shared_ptr<SimEventBreak>(new SimEventBreak(this)));
00120 }
00121 }
00122
00123
00124 else if (itsVisualField.initialized())
00125 drawDisk(itsVisualField, winner.p, itsFOAradius.getVal(), byte(255));
00126 }
00127
00128
00129 void TargetChecker::
00130 onSimEventRetinaImage(SimEventQueue& q, rutz::shared_ptr<SimEventRetinaImage>& e)
00131 {
00132
00133 const InputFrame inframe = e->frame();
00134
00135 if (itsRawTargetMask.initialized())
00136 {
00137 itsTargetMask = itsRawTargetMask;
00138 inplaceNormalize(itsTargetMask, byte(0), byte(255));
00139 itsNumTargetsRemaining = countParticles(itsTargetMask, byte(255));
00140 LINFO("Counting targets... %d detected", itsNumTargetsRemaining);
00141
00142 textLog(itsLogFile.getVal(), "TargetsRemaining",
00143 sformat("%d", itsNumTargetsRemaining), q.now());
00144 }
00145 else
00146 itsTargetMask.freeMem();
00147
00148
00149 if (itsVisualField.isSameSize(inframe.colorByte()) == false)
00150 itsVisualField = Image<byte>(inframe.colorByte().getDims(), ZEROS);
00151 }
00152
00153
00154 void TargetChecker::
00155 onSimEventTargetMask(SimEventQueue& q, rutz::shared_ptr<SimEventTargetMask>& e)
00156 {
00157 LINFO("Loading new target mask at time %.2fms...", q.now().msecs());
00158 itsRawTargetMask = e->mask();
00159 }
00160
00161
00162 void TargetChecker::start1()
00163 {
00164 if (!itsTargetMaskFname.getVal().empty())
00165 {
00166 itsRawTargetMask = Raster::ReadGray(itsTargetMaskFname.getVal());
00167 LINFO("Using targetmask from image file %s", itsTargetMaskFname.getVal().c_str());
00168 }
00169
00170 SimModule::start1();
00171 }
00172
00173
00174
00175
00176
00177
00178
00179 #endif // NEURO_TARGETCHECKER_C_DEFINED