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 OBJREC_SERVER_C_DEFINED
00039 #define OBJREC_SERVER_C_DEFINED
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 #include "Component/ModelManager.H"
00043 #include "Image/Image.H"
00044 #include "Image/Pixels.H"
00045 #include "Media/FrameSeries.H"
00046 #include "NeovisionII/nv2_common.h"
00047 #include "NeovisionII/nv2_label_server.h"
00048 #include "Util/sformat.H"
00049 #include "Image/FilterOps.H"
00050 #include "Image/ColorOps.H"
00051 #include "Image/CutPaste.H"
00052 #include "Image/ShapeOps.H"
00053 #include "Image/DrawOps.H"
00054 #include "Image/MathOps.H"
00055 #include "Learn/SOFM.H"
00056 #include "GUI/DebugWin.H"
00057
00058 #include <iostream>
00059 #include <signal.h>
00060
00061 bool debug = 0;
00062 bool terminate = false;
00063 struct nv2_label_server* server;
00064
00065 void terminateProc(int s)
00066 {
00067 LINFO("Ending application\n");
00068 nv2_label_server_destroy(server);
00069 terminate = true;
00070 exit(0);
00071 }
00072
00073 void findMinMax(const std::vector<double> &vec, double &min, double &max)
00074 {
00075 max = vec[0];
00076 min = max;
00077 for (uint n = 1 ; n < vec.size() ; n++)
00078 {
00079 if (vec[n] > max) max = vec[n];
00080 if (vec[n] < min) min = vec[n];
00081 }
00082 }
00083
00084 Image<PixRGB<byte> > showHist(const std::vector<double> &hist, int loc)
00085 {
00086 int w = 256, h = 256;
00087 if (hist.size() > (uint)w) w = hist.size();
00088
00089 if (hist.size() == 0) return Image<PixRGB<byte> >();
00090
00091 int dw = w / hist.size();
00092 Image<byte> res(w, h, ZEROS);
00093
00094
00095 for (int j = 0; j < 10; j++)
00096 drawLine(res, Point2D<int>(0, int(j * 0.1F * h)),
00097 Point2D<int>(w-1, int(j * 0.1F * h)), byte(64));
00098 drawLine(res, Point2D<int>(0, h-1), Point2D<int>(w-1, h-1), byte(64));
00099
00100 double minii, maxii;
00101 findMinMax(hist, minii, maxii);
00102
00103
00104 if (maxii == minii) minii = maxii - 1.0F;
00105
00106 double range = maxii - minii;
00107
00108 for (uint i = 0; i < hist.size(); i++)
00109 {
00110 int t = abs(h - int((hist[i] - minii) / range * double(h)));
00111
00112
00113 if (t < h-1)
00114 {
00115 for (int j = 0; j < dw; j++)
00116 drawLine(res,
00117 Point2D<int>(dw * i + j, t),
00118 Point2D<int>(dw * i + j, h - 1),
00119 byte(255));
00120
00121 }
00122 }
00123 return res;
00124 }
00125
00126 void smoothHist(std::vector<double> &hist)
00127 {
00128 const uint siz = hist.size();
00129 float vect[siz];
00130
00131 for (uint n = 0 ; n < siz ; n++)
00132 {
00133 float val0 = hist[ (n-1+siz) % siz ];
00134 float val1 = hist[ (n +siz) % siz ];
00135 float val2 = hist[ (n+1+siz) % siz ];
00136
00137 vect[n] = 0.25F * (val0 + 2.0F*val1 + val2);
00138 }
00139
00140 for (uint n = 0 ; n < siz ; n++) hist[n] = vect[n];
00141 }
00142
00143 void normalizeHist(std::vector<double> &hist, double high, double low)
00144 {
00145
00146 double oldmin, oldmax;
00147 findMinMax(hist, oldmin, oldmax);
00148
00149 float scale = float(oldmax) - float(oldmin);
00150
00151 const float nscale = (float(high) - float(low)) / scale;
00152
00153 for(uint i=0; i<hist.size(); i++)
00154 {
00155 hist[i] = low + (float(hist[i]) - float(oldmin)) * nscale ;
00156 }
00157
00158
00159 }
00160
00161
00162
00163
00164
00165 float getOriProb(
00166 const Image<float> &mag,
00167 const Image<float> &ori,
00168 int x, int y)
00169 {
00170
00171 float eMag = mag.getVal(x,y);
00172 float eOri = ori.getVal(x,y);
00173
00174
00175 if (eMag == 0) return 0;
00176
00177
00178
00179
00180 float maxProb = 0;
00181 for (int i=-1; i<=1; i++)
00182 for(int j=-1; j<=1; j++)
00183 {
00184 if (i==0 && j==0) continue;
00185 float nMag = mag.getVal(x+i, y+j);
00186 float nOri = ori.getVal(x+i, y+j);
00187
00188 float prob = (1/fabs(eMag-nMag));
00189
00190 if (prob > maxProb)
00191 {
00192 maxProb = prob;
00193 }
00194
00195
00196 LDEBUG("%ix%i,%ix%i| E: %f:%f N:%f:%f %f",
00197 i,j,
00198 x,y,
00199 eMag, eOri,
00200 nMag, nOri,
00201 prob);
00202 }
00203
00204 float prob = eMag * maxProb;
00205 if (prob > 1000) prob = 1000;
00206 LDEBUG("Max prob %f\n", prob);
00207
00208
00209
00210 return prob;
00211
00212
00213 }
00214
00215 Image<float> getEdgeProb(
00216 const Image<float> &mag,
00217 const Image<float> &ori)
00218 {
00219
00220 Image<float> retEdge(mag.getDims(), NO_INIT);
00221 for(int y=1; y<mag.getHeight()-1; y++)
00222 for(int x=1; x<mag.getWidth()-1; x++)
00223 {
00224 float pOri = getOriProb(mag, ori, x, y);
00225
00226 retEdge.setVal(x, y, pOri);
00227 }
00228
00229 inplaceNormalize(retEdge, 0.0F, 255.0F);
00230
00231 SHOWIMG(mag, true);
00232 SHOWIMG(ori, true);
00233
00234
00235 return retEdge;
00236 }
00237
00238
00239 int main(const int argc, const char **argv)
00240 {
00241
00242 MYLOGVERB = LOG_INFO;
00243 ModelManager *mgr = new ModelManager("Test ObjRec");
00244
00245 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr));
00246 mgr->addSubComponent(ofs);
00247
00248
00249 if (mgr->parseCommandLine(
00250 (const int)argc, (const char**)argv, "<Network file> <server ip>", 2, 2) == false)
00251 return 1;
00252
00253
00254
00255
00256
00257
00258 mgr->start();
00259
00260
00261
00262 const char *server_ip = mgr->getExtraArg(1).c_str();
00263
00264 server = nv2_label_server_create(9930,
00265 server_ip,
00266 9931);
00267
00268 nv2_label_server_set_verbosity(server,1);
00269
00270
00271 while(!terminate)
00272 {
00273
00274 struct nv2_image_patch p;
00275 const enum nv2_image_patch_result res =
00276 nv2_label_server_get_current_patch(server, &p);
00277
00278 std::string objName = "nomatch";
00279 if (res == NV2_IMAGE_PATCH_END)
00280 {
00281 fprintf(stdout, "ok, quitting\n");
00282 break;
00283 }
00284 else if (res == NV2_IMAGE_PATCH_NONE)
00285 {
00286 usleep(10000);
00287 continue;
00288 }
00289 else if (res == NV2_IMAGE_PATCH_VALID &&
00290 p.type == NV2_PIXEL_TYPE_RGB24)
00291 {
00292
00293 const Image<PixRGB<byte> > im((const PixRGB<byte>*) p.data,
00294 p.width, p.height);
00295
00296 Image<byte> lum = luminance(im);
00297
00298
00299 Image<float> mag, ori;
00300 gradientSobel(lum, mag, ori, 3);
00301
00302 Image<float> edgeProb = getEdgeProb(mag,ori);
00303
00304 ofs->writeRGB(im, "headInput");
00305 ofs->writeGray(mag, "Sobel");
00306 ofs->writeGray(edgeProb, "Edges");
00307
00308 }
00309
00310 nv2_image_patch_destroy(&p);
00311
00312 sleep(1);
00313 }
00314
00315 nv2_label_server_destroy(server);
00316
00317 }
00318
00319 #endif