00001 #ifndef QD_OMNIMASK_HPP
00002 #define QD_OMNIMASK_HPP
00003
00004 #include <string>
00005 #include <vector>
00006
00007 #include <math.h>
00008
00009 #include "kernel/jafarMacro.hpp"
00010 #include "jmath/jblas.hpp"
00011 #include "image/Image.hpp"
00012 #include "camera/cameraBarreto.hpp"
00013
00014 #include "boost/timer.hpp"
00015
00016 namespace jafar {
00017
00018 namespace quasidense {
00019
00031 class omniMask {
00032
00033
00034 boost::numeric::ublas::matrix<bool> maskIma;
00035
00036
00037 unsigned int nbcol;
00038 unsigned int nbrow;
00039
00040 public :
00041
00055 omniMask (jafar::camera::CameraBarreto &omniCam, unsigned int nbcol_, unsigned int nbrow_, float winOverAngle) :
00056 maskIma(nbrow_, nbcol_),
00057 nbcol(nbcol_),
00058 nbrow(nbrow_)
00059 {
00060 unsigned int cpt1, cpt2, margin=20;
00061 int cpt3;
00062 double elev, azim, elevTest, azimTest, elevMin, elevMax;
00063
00064 boost::timer chrono;
00065
00066 chrono.restart();
00067
00068
00069 for (cpt1=0;cpt1<nbrow;++cpt1)
00070 for (cpt2=0;cpt2<nbcol;++cpt2)
00071 maskIma(cpt1,cpt2) = false;
00072
00073
00074 jblas::vec2 onePix, onePixTest;
00075 jblas::vec3 pt3D, pt3DTest;
00076
00077
00078 onePix[0] = omniCam.mirrorCenterU;
00079 onePix[1] = omniCam.mirrorCenterV + omniCam.imageRadius;
00080
00081 omniCam.imageToCameraFrame<jblas::vec2,jblas::vec3>(onePix,pt3D);
00082 double n = ublas::norm_2(pt3D);
00083
00084
00085
00086
00087
00088 elev = M_PI_2 - asin(pt3D[2]/n);
00089 elevMax = elev-winOverAngle;
00090
00091
00092 onePix[0] = omniCam.mirrorCenterU + omniCam.maskRadius;
00093 onePix[1] = omniCam.mirrorCenterV;
00094
00095 omniCam.imageToCameraFrame<jblas::vec2,jblas::vec3>(onePix,pt3D);
00096 n = ublas::norm_2(pt3D);
00097 elev = M_PI_2 - asin(pt3D[2]/n);
00098 elevMin = elev+winOverAngle;
00099
00100
00101 for (cpt1=0;cpt1<nbrow;++cpt1)
00102 {
00103 onePix[1] = (double)cpt1;
00104 for (cpt2=0;cpt2<nbcol;++cpt2)
00105 {
00106 onePix[0] = (double)cpt2;
00107 omniCam.imageToCameraFrame<jblas::vec2,jblas::vec3>(onePix,pt3D);
00108 n = ublas::norm_2(pt3D);
00109 elev = M_PI_2 - asin(pt3D[2]/n);
00110 if (elev<=elevMin && elev>=elevMax)
00111 maskIma(cpt1,cpt2) = true;
00112 }
00113 }
00114
00115
00116
00117
00118 if (omniCam.mirrorCenterU-omniCam.imageRadius<0)
00119
00120
00121 {
00122 std::cout << "Circle goes out on the left of image " << std::endl;
00123
00124 for (cpt1=0;cpt1<nbrow;++cpt1)
00125 {
00126 onePix[1] = (double)cpt1;
00127 for (cpt2=0;cpt2<margin;cpt2++)
00128 {
00129 onePix[0] = (double)cpt2;
00130
00131 if (maskIma(cpt1,cpt2)==false)
00132 continue;
00133
00134 omniCam.imageToCameraFrame<jblas::vec2,jblas::vec3>(onePix,pt3D);
00135 n = ublas::norm_2(pt3D);
00136
00137 elev = M_PI_2 - asin(pt3D[2]/n);
00138 azim = atan2(pt3D[1],pt3D[0]);
00139 elevTest = elev-winOverAngle;
00140
00141 for (cpt3=-1;cpt3<=1;++cpt3)
00142 {
00143 azimTest = azim+cpt3*winOverAngle;
00144
00145
00146 pt3DTest[0] = sin(elevTest)*cos(azimTest);
00147 pt3DTest[1] = sin(elevTest)*sin(azimTest);
00148 pt3DTest[2] = cos(elevTest);
00149
00150
00151 omniCam.project<jblas::vec3,jblas::vec2>(pt3DTest,onePixTest);
00152
00153
00154
00155 if (onePixTest[0]<0 || onePixTest[0]>=(nbcol-1) || onePixTest[1]<0 || onePixTest[1]>=(nbrow-1))
00156 {
00157 maskIma(cpt1,cpt2) = false;
00158 continue;
00159 }
00160 }
00161 }
00162 }
00163 }
00164
00165 if (omniCam.mirrorCenterU+omniCam.imageRadius>(nbcol-1))
00166
00167
00168 {
00169 std::cout << " Circle goes out on the right of image " << std::endl;
00170
00171 for (cpt1=0;cpt1<nbrow;++cpt1)
00172 {
00173 onePix[1] = (double)cpt1;
00174 for (cpt2=nbcol-margin;cpt2<nbcol;++cpt2)
00175 {
00176 onePix[0] = (double)cpt2;
00177
00178 if (maskIma(cpt1,cpt2)==false)
00179 continue;
00180
00181 omniCam.imageToCameraFrame<jblas::vec2,jblas::vec3>(onePix,pt3D);
00182 n = ublas::norm_2(pt3D);
00183
00184 elev = M_PI_2 - asin(pt3D[2]/n);
00185 azim = atan2(pt3D[1],pt3D[0]);
00186 elevTest = elev-winOverAngle;
00187
00188 for (cpt3=-1;cpt3<=1;++cpt3)
00189 {
00190 azimTest = azim+cpt3*winOverAngle;
00191
00192
00193 pt3DTest[0] = sin(elevTest)*cos(azimTest);
00194 pt3DTest[1] = sin(elevTest)*sin(azimTest);
00195 pt3DTest[2] = cos(elevTest);
00196
00197
00198 omniCam.project<jblas::vec3,jblas::vec2>(pt3DTest,onePixTest);
00199
00200
00201
00202 if (onePixTest[0]<0 || onePixTest[0]>=(nbcol-1) || onePixTest[1]<0 || onePixTest[1]>=(nbrow-1))
00203 {
00204 maskIma(cpt1,cpt2) = false;
00205 continue;
00206 }
00207 }
00208 }
00209 }
00210 }
00211
00212 if (omniCam.mirrorCenterV+omniCam.imageRadius>(nbrow-1))
00213
00214 {
00215 std::cout << " Circle goes out on the bottom of image " << std::endl;
00216
00217 for (cpt1=nbrow-margin;cpt1<nbrow;++cpt1)
00218 {
00219
00220 onePix[1] = (double)cpt1;
00221 for (cpt2=0;cpt2<nbcol;++cpt2)
00222 {
00223 onePix[0] = cpt2;
00224
00225 if (maskIma(cpt1,cpt2)==false)
00226 continue;
00227
00228 omniCam.imageToCameraFrame<jblas::vec2,jblas::vec3>(onePix,pt3D);
00229 n = ublas::norm_2(pt3D);
00230
00231 elev = M_PI_2 - asin(pt3D[2]/n);
00232 azim = atan2(pt3D[1],pt3D[0]);
00233 elevTest = elev-winOverAngle;
00234
00235 for (cpt3=-1;cpt3<2;++cpt3)
00236 {
00237 azimTest = azim+cpt3*winOverAngle;
00238
00239
00240 pt3DTest[0] = sin(elevTest)*cos(azimTest);
00241 pt3DTest[1] = sin(elevTest)*sin(azimTest);
00242 pt3DTest[2] = cos(elevTest);
00243
00244
00245 omniCam.project<jblas::vec3,jblas::vec2>(pt3DTest,onePixTest);
00246
00247
00248
00249 if (onePixTest[0]<0 || onePixTest[0]>=(nbcol-1) || onePixTest[1]<0 || onePixTest[1]>=(nbrow-1))
00250 {
00251 maskIma(cpt1,cpt2) = false;
00252 continue;
00253 }
00254 }
00255 }
00256 }
00257 }
00258
00259 if (omniCam.mirrorCenterV-omniCam.imageRadius<0)
00260
00261 {
00262 std::cout << " Circle goes out on the top of image " << std::endl;
00263
00264 for (cpt1=0;cpt1<margin;++cpt1)
00265 {
00266
00267 onePix[1] = (double)cpt1;
00268 for (cpt2=0;cpt2<nbcol;++cpt2)
00269 {
00270 onePix[0] = cpt2;
00271
00272 if (maskIma(cpt1,cpt2)==false)
00273 continue;
00274
00275 omniCam.imageToCameraFrame<jblas::vec2,jblas::vec3>(onePix,pt3D);
00276 n = ublas::norm_2(pt3D);
00277
00278 elev = M_PI_2 - asin(pt3D[2]/n);
00279 azim = atan2(pt3D[1],pt3D[0]);;
00280 elevTest = elev-winOverAngle;
00281
00282 for (cpt3=-1;cpt3<2;++cpt3)
00283 {
00284 azimTest = azim+cpt3*winOverAngle;
00285
00286
00287 pt3DTest[0] = sin(elevTest)*cos(azimTest);
00288 pt3DTest[1] = sin(elevTest)*sin(azimTest);
00289 pt3DTest[2] = cos(elevTest);
00290
00291
00292 omniCam.project<jblas::vec3,jblas::vec2>(pt3DTest,onePixTest);
00293
00294
00295 if (onePixTest[0]<0 || onePixTest[0]>=(nbcol-1) || onePixTest[1]<0 || onePixTest[1]>=(nbrow-1))
00296 {
00297 maskIma(cpt1,cpt2) = false;
00298 continue;
00299 }
00300 }
00301 }
00302 }
00303 }
00304
00305 std::cout << " Mask computation accomplished in " << chrono.elapsed() << "sec." << std::endl;
00306
00307 jafar::image::Image exportIma(nbcol,nbrow,IPL_DEPTH_8U,JfrImage_CS_GRAY);
00308
00309 unsigned int nbpixelvalid = 0;
00310
00311 for (unsigned int cptr = 0; cptr<nbrow; ++cptr)
00312 for (unsigned int cptc = 0; cptc<nbcol; ++cptc)
00313 if (maskIma(cptr,cptc))
00314 {
00315 exportIma.setPixelValue<unsigned char>(255,cptc,cptr,0);
00316 ++nbpixelvalid;
00317 }
00318 else
00319 exportIma.setPixelValue<unsigned char>(0,cptc,cptr,0);
00320 exportIma.save("/home/msanfour/mask.tiff","maskCorrel");
00321
00322
00323 }
00324
00334 bool isPixelValid (unsigned int icol, unsigned irow)
00335 {
00336 return maskIma(irow,icol);
00337 }
00338
00339 };
00340
00341 }
00342
00343 }
00344
00345
00346 #endif