mexSaliencySkin.C
Go to the documentation of this file.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
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 #define FPEXCEPTIONSDISABLED
00084
00085 #include "Channels/ChannelOpts.H"
00086 #include "Channels/SkinHueChannel.H"
00087 #include "Component/GlobalOpts.H"
00088 #include "Component/ModelManager.H"
00089 #include "Component/ModelOptionDef.H"
00090 #include "Image/fancynorm.H"
00091 #include "Image/LevelSpec.H"
00092 #include "Image/MathOps.H"
00093 #include "Image/Pixels.H"
00094 #include "Image/ShapeOps.H"
00095 #include "Matlab/mexConverts.H"
00096 #include "Media/MediaSimEvents.H"
00097 #include "Neuro/NeuroOpts.H"
00098 #include "Neuro/NeuroSimEvents.H"
00099 #include "Neuro/ShapeEstimator.H"
00100 #include "Neuro/StdBrain.H"
00101 #include "Neuro/VisualCortex.H"
00102 #include "Simulation/SimEventQueueConfigurator.H"
00103 #include "Util/StringConversions.H"
00104
00105 #include <cmath>
00106 #include <mex.h>
00107 #include <sstream>
00108 #include <string>
00109 #include <vector>
00110
00111
00112
00113 #ifdef DEBUG
00114 #include <iostream>
00115 #endif
00116
00117
00118
00119
00120
00121 void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
00122 {
00123
00124 #ifdef DEBUG
00125 LOG_FLAGS |= LOG_FULLTRACE;
00126 #else
00127 LOG_FLAGS &= (~LOG_FULLTRACE);
00128 #endif
00129
00130
00131 if (nrhs < 1) mexErrMsgTxt("At least one parameter required: image");
00132 if (nrhs > 8) mexErrMsgTxt("Too many parameters (max. 8).");
00133 if (nlhs > 7) mexErrMsgTxt("Too many return values (max. 7).");
00134
00135
00136 int p = 1;
00137
00138
00139 Image< PixRGB<byte> > img = mexArray2RGBImage<byte>(prhs[p-1]);
00140 p++;
00141
00142
00143 Image<byte> targets;
00144 if ((nrhs >= p) && (mxGetNumberOfElements(prhs[p-1]) > 1))
00145 targets = mexArray2Image<byte>(prhs[p-1]);
00146 p++;
00147
00148
00149 double max_time = 0.7;
00150 if (nrhs >= p) max_time = (double)mxGetScalar(prhs[p-1]);
00151 p++;
00152
00153
00154
00155
00156 p++;
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 p++;
00167
00168
00169 int norm_type = VCXNORM_FANCY;
00170 if (nrhs >= p) norm_type = (int)mxGetScalar(prhs[p-1]);
00171 p++;
00172
00173
00174
00175 int smoothMethod = 1;
00176 if (nrhs >= p) smoothMethod = (int)mxGetScalar(prhs[p-1]);
00177 p++;
00178
00179
00180 int sml=4, level_min=2, level_max=4, delta_min=3, delta_max=4, nborients=4;
00181 if (nrhs >= p)
00182 {
00183 std::vector<int> lev = mexArr2Vector<int>(prhs[p-1], 6);
00184 sml = lev[0]; level_min = lev[1]; level_max = lev[2];
00185 delta_min = lev[3]; delta_max = lev[4]; nborients = lev[5];
00186 }
00187 p++;
00188
00189
00190
00191 if (max_time < 0.0) mexErrMsgTxt("max_time must be positive.");
00192 if ((norm_type < VCXNORM_NONE))
00193 mexErrMsgTxt("norm_type can only have the following values:\n"
00194 "0 - no normalization\n"
00195 "1 - maximum normalization\n"
00196 "2 - fancy normalization (default)\n"
00197 "3 - fancy normalization - fast implementation\n"
00198 "4 - fancy normalization with one iteration");
00199 if ((smoothMethod < 0) || (smoothMethod >= NBSHAPEESTIMATORSMOOTHMETHODS))
00200 mexErrMsgTxt("smoothing methods can have the following values:\n"
00201 "0 - no smoothing\n"
00202 "1 - Gaussian smoothing (default)\n"
00203 "2 - Chamfer smoothing");
00204 if (nborients <= 0) mexErrMsgTxt("nborients must be positive");
00205 if (level_min > level_max) mexErrMsgTxt("must have level_min <= level_max");
00206 if (delta_min > delta_max) mexErrMsgTxt("must have delta_min <= delta_max");
00207 if (sml < level_max) mexErrMsgTxt("must have sml >= level_max");
00208
00209
00210 #ifdef DEBUG
00211
00212 std::cout << "#### mexSaliency\n";
00213 std::cout << "max_time = " << max_time << "\n";
00214 std::cout << "norm_type = " << norm_type <<"\n";
00215 std::cout << "foa_size = " << foa_size <<"\n";
00216 std::cout << "wIntens = " << wIntens <<"\n";
00217 std::cout << "wOrient = " << wOrient <<"\n";
00218 std::cout << "wColor = " << wColor <<"\n";
00219 std::cout << "nborient = " << nborients <<"\n";
00220 std::cout << "level_min = " << level_min <<"\n";
00221 std::cout << "level_max = " << level_max <<"\n";
00222 std::cout << "delta_min = " << delta_min <<"\n";
00223 std::cout << "delta_max = " << delta_max <<"\n";
00224 std::cout << "sml = " << sml <<"\n";
00225
00226 #endif
00227
00228
00229
00230 ModelManager manager("Mex Attention Model");
00231 manager.allowOptions(OPTEXP_CORE);
00232
00233 manager.setOptionValString(&OPT_UsingFPE,"false");
00234
00235 #ifdef DEBUG
00236 manager.setOptionValString(&OPT_DebugMode,"true");
00237 #else
00238 manager.setOptionValString(&OPT_DebugMode,"false");
00239 #endif
00240
00241 nub::soft_ref<SimEventQueueConfigurator>
00242 seqc(new SimEventQueueConfigurator(manager));
00243 manager.addSubComponent(seqc);
00244
00245 nub::soft_ref<StdBrain> brain(new StdBrain(manager));
00246 manager.addSubComponent(brain);
00247 manager.exportOptions(MC_RECURSE);
00248
00249
00250 manager.setOptionValString(&OPT_NumOrientations,
00251 convertToString(nborients));
00252 manager.setOptionValString(&OPT_LevelSpec,
00253 convertToString(LevelSpec(level_min,level_max,
00254 delta_min,delta_max,sml)));
00255 manager.setOptionValString(&OPT_MaxNormType,
00256 convertToString(MaxNormType(norm_type)));
00257
00258 manager.setOptionValString(&OPT_UseRandom, "false");
00259 manager.setOptionValString(&OPT_IORtype,"ShapeEst");
00260 manager.setOptionValString(&OPT_ShapeEstimatorMode, "FeatureMap");
00261 manager.setOptionValString(&OPT_ShapeEstimatorSmoothMethod,
00262 convertToString(ShapeEstimatorSmoothMethod
00263 (smoothMethod)));
00264 manager.setOptionValString(&OPT_RawVisualCortexChans,"OIC");
00265
00266 nub::soft_ref<SimEventQueue> seq = seqc->getQ();
00267
00268
00269 manager.start();
00270
00271 LFATAL("fixme");
00272
00273 nub::soft_ref<VisualCortex> vc;
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 seq->post(rutz::make_shared(new SimEventTargetMask(brain.get(), targets)));
00287
00288
00289 seq->post(rutz::make_shared(new SimEventInputFrame(brain.get(), GenericFrame(img), 0)));
00290
00291 Image<float> salmap;
00292 inplaceNormalize(salmap, 0.0f, 1.0f);
00293 salmap = rescale(salmap,img.getDims());
00294
00295 std::vector<Point2D<int> > coords;
00296 std::vector<double> times;
00297 std::vector<int> areas;
00298 ImageSet<float> s_vect;
00299 std::vector<std::string> labelstrings;
00300
00301 int num_sal = 0;
00302
00303 const bool forever = (targets.initialized() && (max_time == 0.0));
00304
00305
00306 while (((seq->now().secs() < max_time)||forever))
00307 {
00308 (void) seq->evolve();
00309
00310
00311 if (SeC<SimEventWTAwinner> e = seq->check<SimEventWTAwinner>(0))
00312 {
00313 const Point2D<int> winner = e->winner().p;
00314
00315 num_sal++;
00316
00317 coords.push_back(winner);
00318 times.push_back(seq->now().secs());
00319
00320
00321 if (nlhs > 5)
00322 {
00323 Image<float> semask; std::string selabel; int searea;
00324 if (SeC<SimEventShapeEstimatorOutput>
00325 ese = seq->check<SimEventShapeEstimatorOutput>(0))
00326 {
00327 semask = ese->smoothMask();
00328 selabel = ese->winningLabel();
00329 searea = ese->objectArea();
00330 }
00331
00332 if (!semask.initialized()) semask.resize(img.getDims(),ZEROS);
00333 s_vect.push_back(semask);
00334
00335 areas.push_back(searea);
00336
00337 std::ostringstream os;
00338 os << (seq->now().msecs()) << " ms - " << selabel;
00339 labelstrings.push_back(os.str());
00340 }
00341 }
00342 }
00343
00344 #ifdef DEBUG
00345
00346 std::cout << "#### mexSaliency\n";
00347 std::cout << "x\ty\tt\n";
00348 for (int i = 0; i < num_sal; i++)
00349 std::cout << coords[i].i << "\t" << coords[i].j << "\t" << times[i] << "\n";
00350
00351 #endif
00352
00353
00354 manager.stop();
00355
00356
00357 p = 1;
00358
00359
00360 if (nlhs >= p) plhs[p-1] = mxCreateScalarDouble(num_sal);
00361 p++;
00362
00363
00364 if (nlhs >= p) plhs[p-1] = Point2DVec2mexArr(coords);
00365 p++;
00366
00367
00368 if (nlhs >= p) plhs[p-1] = Vector2mexArr(times);
00369 p++;
00370
00371
00372 if (nlhs >= p) plhs[p-1] = Image2mexArray(salmap);
00373 p++;
00374
00375
00376 mxArray* Labels = mxCreateCellArray(1, &num_sal);
00377 if (nlhs >= p)
00378 {
00379 for (int i = 0; i < num_sal; i++)
00380 {
00381 mxSetCell(Labels, i, mxCreateString(labelstrings[i].c_str()));
00382 #ifdef DEBUG
00383 std::cout << labelstrings[i].c_str() << "\n";
00384 #endif
00385 }
00386 plhs[p-1] = ImgVec2mexArr(s_vect);
00387 }
00388 p++;
00389
00390
00391 if (nlhs >= p) plhs[p-1] = Vector2mexArr(areas);
00392 p++;
00393
00394
00395 if (nlhs >= p) plhs[p-1] = Labels;
00396 p++;
00397
00398
00399 return;
00400 }
00401
00402
00403
00404
00405
00406