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 #include "Channels/RGBConvolveChannel.H"
00040 #include "Component/ModelManager.H"
00041 #include "Image/ColorOps.H"
00042 #include "Image/MathOps.H"
00043 #include "Image/MatrixOps.H"
00044 #include "Image/Pixels.H"
00045 #include "Image/Transforms.H"
00046 #include "Media/FrameSeries.H"
00047 #include "Neuro/NeuroOpts.H"
00048 #include "Channels/RawVisualCortex.H"
00049 #include "Raster/Raster.H"
00050 #include "Util/sformat.H"
00051
00052 #include <fstream>
00053
00054 #define NB_FILTERS 3
00055 #define NB_COEFFS 8
00056
00057 int main(const int argc, const char **argv)
00058 {
00059 int n = NB_COEFFS;
00060 int m = NB_COEFFS * NB_COEFFS * NB_FILTERS * 3;
00061
00062 MYLOGVERB = LOG_INFO;
00063
00064
00065 Image<float> hmat(n, n, ZEROS);
00066 for(int i = 0; i < n; i++)
00067 {
00068 hmat.setVal(i, 0, 1.0f);
00069 }
00070 for(int i = 0; i < n / 2; i++)
00071 {
00072 hmat.setVal(i, 1, 1.0f);
00073 hmat.setVal(i + n / 2, 1, -1.0f);
00074 if (i - 2 < 0)
00075 {
00076 hmat.setVal(i, 2, 1.0f);
00077 hmat.setVal(i + 2, 2, -1.0f);
00078 }
00079 else
00080 {
00081 hmat.setVal(i + 2, 3, 1.0f);
00082 hmat.setVal(i + 4, 3, -1.0f);
00083 }
00084 hmat.setVal(2 * i, i + n / 2, 1.0f);
00085 hmat.setVal(2 * i + 1, i + n / 2, -1.0f);
00086 }
00087
00088
00089 ModelManager manager("Open Attention Model");
00090
00091
00092 nub::soft_ref<RawVisualCortex> vcx(new RawVisualCortex(manager));
00093 manager.addSubComponent(vcx);
00094
00095
00096
00097 nub::soft_ref<RGBConvolveChannel> channel(new RGBConvolveChannel(manager));
00098 vcx->addSubChan(channel);
00099
00100
00101 if (manager.parseCommandLine(argc, argv, "<data.txt> <image.ppm>",
00102 2, -1) == false)
00103 return(1);
00104
00105
00106
00107 vcx->removeAllSubChans();
00108
00109
00110 char framename[1024];
00111 strncpy(framename, manager.getExtraArg(1).c_str(), 1023);
00112
00113
00114 float data[m];
00115 char dataname[1024];
00116 strncpy(dataname, manager.getExtraArg(0).c_str(), 1023);
00117 std::ifstream inputfile (dataname);
00118 if (inputfile.is_open())
00119 {
00120 for (int j = 0; j < m; j++)
00121 inputfile >> data[j];
00122 inputfile.close();
00123 }
00124 else
00125 {
00126 LERROR("*** Cannot open input file !");
00127 return 1;
00128 }
00129
00130
00131 ImageSet<float> trans(NB_FILTERS * 3);
00132 for (int i = 0; i < NB_FILTERS * 3; i++)
00133 trans[i] = Image<float>(data + (n * n * i), n, n);
00134 ImageSet<float> filter(NB_FILTERS * 3);
00135 Dims filterdim(8, 8);
00136 for (int i = 0; i < NB_FILTERS * 3; i++)
00137 filter[i] = scaleBlock(matrixMult(transpose(hmat),
00138 matrixMult(trans[i], hmat)),
00139 filterdim);
00140
00141 for (int i = 0; i < NB_FILTERS; i++)
00142 {
00143 Image<float> rf = filter[3 * i];
00144 Image<float> gf = filter[3 * i + 1];
00145 Image<float> bf = filter[3 * i + 2];
00146 float min, max, fmax;
00147 getMinMax(rf, min, max);
00148 if (fabs(min) > fabs(max))
00149 fmax = min;
00150 else
00151 fmax = max;
00152 getMinMax(gf, min, max);
00153 if (fabs(min) > fmax)
00154 fmax = min;
00155 if (fabs(max) > fmax)
00156 fmax = max;
00157 getMinMax(bf, min, max);
00158 if (fabs(min) > fmax)
00159 fmax = min;
00160 if (fabs(max) > fmax)
00161 fmax = max;
00162 if (fmax < 1.0e-10F)
00163 fmax = 1;
00164 float scale = 128.0F / fmax;
00165 Image<float>::iterator rptr = rf.beginw();
00166 Image<float>::iterator gptr = gf.beginw();
00167 Image<float>::iterator bptr = bf.beginw();
00168 Image<float>::iterator stop = rf.endw();
00169 while (rptr != stop)
00170 {
00171 *rptr = (float)(float(*rptr) * scale);
00172 *gptr = (float)(float(*gptr) * scale);
00173 *bptr = (float)(float(*bptr) * scale);
00174 ++rptr;
00175 ++gptr;
00176 ++bptr;
00177 }
00178 Image< PixRGB<byte> > color_filter = makeRGB(rf + 128.0F,
00179 gf + 128.0F,
00180 bf + 128.0F);
00181 Raster::WriteRGB(color_filter, sformat("filter%i.ppm", i));
00182 }
00183
00184 for (int i = 0; i < NB_FILTERS; i++)
00185 {
00186
00187 nub::soft_ref<RGBConvolveChannel> channel(new RGBConvolveChannel(manager));
00188
00189 channel->setDescriptiveName(sformat("RGBConvolve%d", i));
00190 channel->setTagName(sformat("rgbconv%d", i));
00191
00192 channel->exportOptions(MC_RECURSE);
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 channel->setFilters(filter[3 * i], filter[3 * i + 1],
00233 filter[3 * i + 2],
00234 CONV_BOUNDARY_ZERO);
00235
00236
00237 vcx->addSubChan(channel);
00238 }
00239
00240
00241 manager.start();
00242
00243
00244
00245
00246
00247 LINFO("*** Loading image %s", framename);
00248 Image< PixRGB<byte> > picture = Raster::ReadRGB(framename, RASFMT_PNM);
00249
00250
00251 vcx->input(InputFrame::fromRgb(&picture));
00252
00253
00254 Image<float> sm = vcx->getOutput();
00255
00256
00257 inplaceNormalize(sm, 0.0f, 255.0f);
00258 Image<byte> smb = sm;
00259
00260
00261 int i = strlen(framename) - 1; while(i > 0 && framename[i] != '.') i--;
00262 framename[i] = '\0';
00263 LINFO("*** Saving '%s-SM.pgm'...", framename);
00264 Raster::WriteGray(smb, sformat("%s-SM.pgm", framename));
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 manager.stop();
00297
00298
00299 for (int i = 0; i < NB_FILTERS; i++)
00300 {
00301 RGBConvolvePyrBuilder<float> rgbcpb(filter[3 * i],
00302 filter[3 * i + 1],
00303 filter[3 * i + 2],
00304 CONV_BOUNDARY_ZERO);
00305 ImageSet< PixRGB<float> > rgbpyr = rgbcpb.build2(picture, 0, 4);
00306 for (int j = 0; j < 4; j++)
00307 {
00308 Image<float> rc, gc, bc;
00309 getComponents(rgbpyr[j], rc, gc, bc);
00310 float min, max, fmax;
00311 getMinMax(rc, min, max);
00312 if (fabs(min) > fabs(max))
00313 fmax = min;
00314 else
00315 fmax = max;
00316 getMinMax(gc, min, max);
00317 if (fabs(min) > fmax)
00318 fmax = min;
00319 if (fabs(max) > fmax)
00320 fmax = max;
00321 getMinMax(bc, min, max);
00322 if (fabs(min) > fmax)
00323 fmax = min;
00324 if (fabs(max) > fmax)
00325 fmax = max;
00326 if (fmax < 1.0e-10F)
00327 fmax = 1;
00328 float scale = 255.0F / fmax;
00329 Image<float>::iterator rptr = rc.beginw();
00330 Image<float>::iterator gptr = gc.beginw();
00331 Image<float>::iterator bptr = bc.beginw();
00332 Image<float>::iterator stop = rc.endw();
00333 while (rptr != stop)
00334 {
00335 *rptr = (float)(float(*rptr) * scale);
00336 *gptr = (float)(float(*gptr) * scale);
00337 *bptr = (float)(float(*bptr) * scale);
00338 ++rptr;
00339 ++gptr;
00340 ++bptr;
00341 }
00342 Image<float> prc, nrc, pgc, ngc, pbc, nbc;
00343 splitPosNeg(rc, prc, nrc);
00344 splitPosNeg(gc, pgc, ngc);
00345 splitPosNeg(bc, pbc, nbc);
00346 rgbpyr[j] = makeRGB((rc / 2.0F) + 128.0F,
00347 (gc / 2.0F) + 128.0F,
00348 (bc / 2.0F) + 128.0F);
00349 Image< PixRGB<byte> > rgbpyrb = rgbpyr[j];
00350 Raster::WriteRGB(rgbpyrb, sformat("%s/conv-f%i-l%i.ppm",
00351 framename, i, j));
00352 rgbpyr[j] = makeRGB(prc, pgc, pbc);
00353 rgbpyrb = rgbpyr[j];
00354 Raster::WriteRGB(rgbpyrb, sformat("%s/conv-f%i-l%i-pos.ppm",
00355 framename, i, j));
00356 rgbpyr[j] = makeRGB(nrc, ngc, nbc);
00357 rgbpyrb = rgbpyr[j];
00358 Raster::WriteRGB(rgbpyrb, sformat("%s/conv-f%i-l%i-neg.ppm",
00359 framename, i, j));
00360 }
00361 }
00362
00363
00364 return 0;
00365 }
00366
00367
00368
00369
00370
00371