extractsalientpatches.C
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 #include "Component/ModelManager.H"
00039 #include "Image/ColorOps.H"
00040 #include "Image/CutPaste.H"
00041 #include "Image/DrawOps.H"
00042 #include "Image/FilterOps.H"
00043 #include "Image/Image.H"
00044 #include "Image/ImageSet.H"
00045 #include "Image/Layout.H"
00046 #include "Image/MathOps.H"
00047 #include "Image/Pixels.H"
00048 #include "Image/PyramidOps.H"
00049 #include "Image/ShapeOps.H"
00050 #include "Image/Transforms.H"
00051 #include "Media/FrameSeries.H"
00052 #include "Media/MediaOpts.H"
00053 #include "Raster/GenericFrame.H"
00054 #include "Raster/Raster.H"
00055 #include "Transport/FrameInfo.H"
00056 #include "Transport/TransportOpts.H"
00057 #include "Util/csignals.H"
00058 #include "rutz/shared_ptr.h"
00059 #include "rutz/trace.h"
00060 #include "rutz/rand.h"
00061
00062 #include "CUDA/CudaSaliency.H"
00063
00064 #include <ctype.h>
00065 #include <deque>
00066 #include <iterator>
00067 #include <limits>
00068 #include <stdlib.h>
00069 #include <string.h>
00070 #include <sys/resource.h>
00071 #include <signal.h>
00072 #include <time.h>
00073 #include <vector>
00074
00075
00076
00077
00078 int submain(int argc, const char** argv)
00079 {
00080 volatile int signum = 0;
00081 signal(SIGPIPE, SIG_IGN);
00082 catchsignals(&signum);
00083
00084
00085
00086 ModelManager manager("Nv2");
00087
00088 nub::ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00089 manager.addSubComponent(ifs);
00090
00091 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00092 manager.addSubComponent(ofs);
00093
00094 nub::ref<CudaSaliency> cus(new CudaSaliency(manager));
00095 manager.addSubComponent(cus);
00096
00097 if (manager.parseCommandLine((const int)argc, (const char**)argv, "<sampleimagesdir>", 1, 1) == false)
00098 return 1;
00099
00100 manager.exportOptions(MC_RECURSE);
00101
00102 manager.start();
00103
00104 std::string sampleImagesDir;
00105
00106 sampleImagesDir = manager.getExtraArg(0);
00107
00108
00109 int retval = 0;
00110 ifs->startStream();
00111 const GenericFrameSpec fspec = ifs->peekFrameSpec();
00112 Image<PixRGB<byte> > rgbin_last;
00113
00114
00115
00116 rutz::urand r(0);
00117
00118 int curPickedFrame=0;
00119
00120 while (true)
00121 {
00122 if (signum != 0) {
00123 LINFO("quitting because %s was caught", signame(signum));
00124 retval = -1;
00125 break;
00126 }
00127
00128 if (ofs->becameVoid()) {
00129 LINFO("quitting because output stream was closed or became void");
00130 break;
00131 }
00132
00133
00134
00135
00136 const FrameState is = ifs->updateNext();
00137 if (is == FRAME_COMPLETE) break;
00138
00139 GenericFrame input = ifs->readFrame();
00140 if (!input.initialized()) break;
00141
00142 const Image<PixRGB<byte> > rgbin = input.asRgb();
00143
00144 rgbin_last = rgbin;
00145 cus->doInput(rgbin);
00146
00147
00148
00149 int willPick = r.idraw(100);
00150
00151 if(willPick==0)
00152 {
00153
00154 CudaImage<float> out = cus->getCudaOutput();
00155
00156 std::vector<Point2D<int> > loc = cus->getSalMaxLoc();
00157
00158 int rectW = cus->getPatchSize();
00159 int rectH = cus->getPatchSize();
00160
00161 for(unsigned int i=0; i<loc.size(); i++)
00162 {
00163 Point2D<int> modLoc;
00164 modLoc.i = loc[i].i * rgbin.getWidth() / double(out.getWidth());
00165 modLoc.j = loc[i].j * rgbin.getHeight() / double(out.getHeight());
00166
00167 int fi = std::max(modLoc.i-int(rectW/2.0),0);
00168 int fj = std::max(modLoc.j-int(rectH/2.0),0);
00169 Point2D<int> p = Point2D<int>(fi,fj);
00170 Rectangle foa = Rectangle(p,Dims(rectW,rectH));
00171 printf("FOA:<%s> ",convertToString(modLoc).c_str());
00172 Image<PixRGB<byte> > patch = crop(rgbin,foa);
00173 Image<PixRGB<byte> > patchResize = rescale(patch, 256, 256);
00174
00175 char fname[200];
00176 sprintf(fname,"%s/Sample-%d-%d.png",sampleImagesDir.c_str(),curPickedFrame,i);
00177 Raster::WriteRGB(patchResize,fname);
00178
00179 }
00180 printf("\n");
00181 curPickedFrame++;
00182 }
00183
00184 }
00185
00186 manager.stop();
00187 return retval;
00188 }
00189
00190
00191 int main(int argc, const char** argv)
00192 {
00193 try {
00194 return submain(argc, argv);
00195 } catch (...) {
00196 REPORT_CURRENT_EXCEPTION;
00197 }
00198 }
00199
00200
00201
00202
00203
00204
00205