Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
explorer.hpp
00001 /* $Id$ */
00002 
00003 #ifndef _JAFAR_CORREL_EXPLORER_HPP_
00004 #define _JAFAR_CORREL_EXPLORER_HPP_
00005 
00006 #include <fstream>  
00007 #include <iostream>  
00008 
00009 #include "kernel/jafarMacro.hpp"
00010 #include "correl/zncc.hpp"
00011 #include "jmath/interpol.hpp"
00012 #include "image/roi.hpp"
00013 
00014 namespace jafar {
00015   namespace correl {
00016 
00021     template<class Correl>
00022     class Explorer {
00023     public:
00024 
00037       static double exploreTranslation(image::Image const& im1, image::Image const& im2, int xmin, int xmax, int xstep, int ymin, int ymax, int ystep, double &xres, double &yres, float const* weightMatrix = NULL);
00038       
00042       static double exploreRotation(image::Image const& im1, image::Image const& im2, int amin, int amax, int astep, double &ares, float const* weightMatrix = NULL);
00043       
00047       static double exploreScale(image::Image const& im1, image::Image const& im2, int smin, int smax, int sstep, double &sres, float const* weightMatrix = NULL);
00048       
00049       // TODO combinations
00050       
00051     }; // class Explorer
00052     
00053 
00054     class FastTranslationMatcherZncc {
00055       protected:
00056         inline void doCorrelationFast(image::Image const& im1, image::Image& im2, int x, int y,
00057           cv::Rect const &patch_in_im1, cv::Rect const &hpatch_in_im1, int& bestx, int& besty, double& best_score, 
00058           cv::Rect const &int_in_im2, double sum1, double sumSqr1, cv::Mat const &sum2_, cv::Mat const &sumSqr2_, 
00059           double minScore, int partialLine, double partialSum1, double partialSumSqr1, 
00060           cv::Rect const &result_in_im2, cv::Mat *results);
00061         inline void doCorrelationRobust(image::Image const& im1, image::Image& im2, int x, int y,
00062           cv::Rect const &patch_in_im1, cv::Rect const &hpatch_in_im1, int& bestx, int& besty, double& best_score,
00063           cv::Rect const &result_in_im2, bool force, cv::Mat *results);
00064         
00065         inline void rawExploreTranslationFast(image::Image const& im1, image::Image& im2, 
00066           image::ConvexRoi const &roi_in_im2, int &bestx, int &besty, double &best_score, 
00067           double minScore, int partialLine, cv::Rect const &result_in_im2, cv::Mat *results);
00068         inline void rawExploreTranslationRobust(image::Image const& im1, image::Image& im2, 
00069           image::ConvexRoi const &roi_in_im2, int &bestx, int &besty, double &best_score, 
00070           cv::Rect const &result_in_im2, cv::Mat *results);
00071           
00072       private:
00073         double minScore;
00074         double partialPosition;
00075         
00076       public:
00077 
00082         FastTranslationMatcherZncc(double minScore, double partialPosition):
00083           minScore(minScore), partialPosition(partialPosition) {}
00084         
00096         double match(image::Image const& im1, image::Image const& im2_, image::ConvexRoi const &roi, double &xres, 
00097                      double &xstd, double &ystd, double &yres, cv::Mat **results_ = NULL);
00098     };
00099 
00100     
00101     
00102 #if 0
00103 
00104 
00105 
00106     class FastTranslationMatcherZncc {
00107       protected:
00108         inline void doCorrelationFast(image::Image const& im1, image::Image& im2, int x, int y, int xmin, int ymin, 
00109           int width, int height, int hwidth, int hheight, int& bestx, int& besty, double& best_score, cv::Rect& intRoi, 
00110           double sum1, double sumSqr1, cv::Mat& sum2_, cv::Mat& sumSqr2_, double minScore, int partialLine, double partialSum1,
00111           double partialSumSqr1, cv::Mat *results);
00112         inline void doCorrelationRobust(image::Image const& im1, image::Image& im2, int x, int y, int xmin, int ymin, 
00113           int width, int height, int hwidth, int hheight, int& bestx, int& besty, double& best_score, cv::Mat *results);
00114         
00115         inline void rawExploreTranslationFast(image::Image const& im1, image::Image& im2, 
00116           image::ConvexRoi const &iroi, image::ConvexRoi const &croi, int &bestx, int &besty, double &best_score, 
00117           double minScore, int partialLine, cv::Mat *results);
00118         inline void rawExploreTranslationRobust(image::Image const& im1, image::Image& im2, 
00119           image::ConvexRoi const &iroi, image::ConvexRoi const &croi, int &bestx, int &besty, double &best_score, 
00120           cv::Mat *results);
00121           
00122       private:
00123         double minScore;
00124         double partialPosition;
00125         
00126       public:
00127 
00132         FastTranslationMatcherZncc(double minScore, double partialPosition):
00133           minScore(minScore), partialPosition(partialPosition) {}
00134         
00146         double match(image::Image const& im1, image::Image const& im2_, image::ConvexRoi const &roi, double &xres, double &yres, cv::Mat **results_ = NULL);
00147     };
00148 
00149 
00150 
00151 
00152 #endif
00153     
00154     
00155     
00156     
00157     
00158     
00159     
00162 // Implementation
00165     
00166     
00167     
00168     
00169     
00170     
00171     
00172     
00173     
00174     
00175     
00176     #define RESULTS(y,x) results[((y)-(ymin-1))*sa_w+((x)-(xmin-1))]
00177     #define DO_CORRELATION(im1, im2, weightMatrix, xx, yy, score, best_score, bestx, besty, roi) \
00178         { \
00179         cv::Rect roi2 = cv::Rect((xx)-roi.width/2,(yy)-roi.height/2,roi.width,roi.height); \
00180         im2.setROI(roi2); \
00181         score = Correl::compute(im1, im2, weightMatrix); \
00182         RESULTS(yy,xx) = score; \
00183         if (score > best_score) { best_score = score; bestx = xx; besty = yy; } \
00184         }
00185     
00186 //std::cout << "correl " << xx <<"," << yy << " " << score <<std::endl;
00187     template<class Correl>
00188     double Explorer<Correl>::exploreTranslation(image::Image const& im1, image::Image const& im2_, int xmin, int xmax, int xstep, int ymin, int ymax, int ystep, double &xres, double &yres, float const* weightMatrix)
00189     {
00190       cv::Rect roi = im1.getROI();
00191 //      image::Image im2(im2_, cv::Rect(0,0,im2_.width(),im2_.height()));
00192       image::Image im2(im2_);
00193       double score;
00194       double best_score = -1.;
00195       int bestx = -1, besty = -1;
00196       
00197       if (xmin < 0) xmin = 0; if (xmax >= im2.width ()) xmax = im2.width ()-1;
00198       if (ymin < 0) ymin = 0; if (ymax >= im2.height()) ymax = im2.height()-1;
00199       
00200       int sa_w = (xmax-xmin+1), sa_h = (ymax-ymin+1); // search area
00201       if (sa_w < 5) xstep = 1; if (sa_h < 5) ystep = 1;
00202       int nresults = (sa_w+2)*(sa_h+2);
00203       double *results = new double[nresults]; // add 1 border for interpolation
00204       for(int i = 0; i < nresults; i++) results[i] = -1e6;
00205       
00206       // explore
00207       for(int y = ymin; y <= ymax; y += ystep)
00208       for(int x = xmin; x <= xmax; x += xstep)
00209         DO_CORRELATION(im1, im2, weightMatrix, x, y, score, best_score, bestx, besty, roi);
00210 
00211       // refine
00212 // JFR_DEBUG("refine (" << bestx << "," << besty << " " << best_score << ")");
00213       // TODO refine several local maxima
00214       // TODO refine by dichotomy for large steps ?
00215       int newbestx = bestx, newbesty = besty;
00216       for(int y = besty-ystep+1; y <= besty+ystep-1; y++)
00217       for(int x = bestx-xstep+1; x <= bestx+xstep-1; x++)
00218       {
00219         if (x == bestx && y == besty) continue;
00220         DO_CORRELATION(im1, im2, weightMatrix, x, y, score, best_score, newbestx, newbesty, roi);
00221       }
00222       
00223       // ensure that all values that will be used by interpolation are computed
00224       int newnewbestx = newbestx, newnewbesty = newbesty;
00225 /*      if (((newbestx == bestx-xstep+1 || newbestx == bestx+xstep-1) && (newbesty-ymin)%ystep) ||
00226           ((newbesty == besty-ystep+1 || newbesty == besty+ystep-1) && (newbestx-xmin)%xstep))
00227       {
00228         if (newbestx == bestx-xstep+1) DO_CORRELATION(im1, im2, weightMatrix, newbestx-1, newbesty, score, best_score, newnewbestx, newnewbesty, roi);
00229         if (newbestx == bestx+xstep-1) DO_CORRELATION(im1, im2, weightMatrix, newbestx+1, newbesty, score, best_score, newnewbestx, newnewbesty, roi);
00230         if (newbesty == besty-ystep+1) DO_CORRELATION(im1, im2, weightMatrix, newbestx, newbesty-1, score, best_score, newnewbestx, newnewbesty, roi);
00231         if (newbesty == besty+ystep-1) DO_CORRELATION(im1, im2, weightMatrix, newbestx, newbesty+1, score, best_score, newnewbestx, newnewbesty, roi);
00232       }*/
00233 // JFR_DEBUG("extra interpol (" << newbestx << "," << newbesty << " " << best_score << ")");
00234       do {
00235         newbestx = newnewbestx, newbesty = newnewbesty;
00236         if (newbestx>0 && RESULTS(newbesty,newbestx-1)<-1e5)
00237           DO_CORRELATION(im1, im2, weightMatrix, newbestx-1, newbesty, score, best_score, newnewbestx, newnewbesty, roi);
00238         if (newbestx<im2.width()-1 && RESULTS(newbesty,newbestx+1)<-1e5)
00239           DO_CORRELATION(im1, im2, weightMatrix, newbestx+1, newbesty, score, best_score, newnewbestx, newnewbesty, roi);
00240         if (newbesty>0 && RESULTS(newbesty-1,newbestx)<-1e5)
00241           DO_CORRELATION(im1, im2, weightMatrix, newbestx, newbesty-1, score, best_score, newnewbestx, newnewbesty, roi);
00242         if (newbesty<im2.height()-1 && RESULTS(newbesty+1,newbestx)<-1e5)
00243           DO_CORRELATION(im1, im2, weightMatrix, newbestx, newbesty+1, score, best_score, newnewbestx, newnewbesty, roi);
00244       } while (newbestx != newnewbestx || newbesty != newnewbesty);
00245       // FIXME this could go out of bounds
00246 // JFR_DEBUG("final : " << newnewbestx << "," << newnewbesty << " " << best_score);
00247       
00248       bestx = newbestx;
00249       besty = newbesty;
00250       
00251       // TODO interpolate the score as well
00252       // interpolate x
00253       
00254       double a1 = RESULTS(besty,bestx-1), a2 = RESULTS(besty,bestx-0), a3 = RESULTS(besty,bestx+1);
00255       if (a1 > -1e5 && a3 > -1e5) jmath::parabolicInterpolation(a1,a2,a3, xres); else xres = 0;
00256 // JFR_DEBUG("interpolating " << a1 << " " << a2 << " " << a3 << " gives shift " << xres << " plus " << bestx+0.5);
00257       xres += bestx+0.5;
00258       // interpolate y
00259       a1 = RESULTS(besty-1,bestx), a2 = RESULTS(besty-0,bestx), a3 = RESULTS(besty+1,bestx);
00260       if (a1 > -1e5 && a3 > -1e5) jmath::parabolicInterpolation(a1,a2,a3, yres); else yres = 0;
00261 // JFR_DEBUG("interpolating " << a1 << " " << a2 << " " << a3 << " gives shift " << yres << " plus " << besty+0.5);
00262       yres += besty+0.5;
00263       
00264       delete[] results;
00265       return best_score;
00266     }
00267       
00268     template<class Correl>
00269     double Explorer<Correl>::exploreRotation(image::Image const& im1, image::Image const& im2, int amin, int amax, int astep, double &ares, float const* weightMatrix)
00270     {
00271       return Correl::compute(im1, im2, weightMatrix);
00272       // TODO do the exploration !
00273     }
00274 
00275     template<class Correl>
00276     double Explorer<Correl>::exploreScale(image::Image const& im1, image::Image const& im2, int smin, int smax, int sstep, double &sres, float const* weightMatrix)
00277     {
00278       return Correl::compute(im1, im2, weightMatrix);
00279       // TODO do the exploration !
00280     }
00281     
00282   } /* correl */
00283 } /* jafar */
00284 
00285 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:17 for Jafar by doxygen 1.7.6.1
LAAS-CNRS