Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
dataManagerActiveSearch.impl.hpp
Go to the documentation of this file.
00001 
00008 #if 0
00009 #include "rtslam/dataManagerActiveSearch.hpp"
00010 #include "rtslam/observationAbstract.hpp"
00011 #include "rtslam/rawImage.hpp"
00012 #include "rtslam/sensorPinhole.hpp"
00013 #include "rtslam/descriptorImagePoint.hpp"
00014 
00015 #include "rtslam/imageTools.hpp"
00016 
00017 namespace jafar {
00018   namespace rtslam {
00019 
00020     template<class RawSpec,class SensorSpec, class Detector, class Matcher >
00021     void DataManagerActiveSearch<RawSpec,SensorSpec, Detector, Matcher >::
00022     processKnownObs( boost::shared_ptr<RawSpec> rawData )
00023     {
00024       int numObs = 0;
00025       asGrid->renew();
00026       obsListSorted.clear();
00027 
00028       // loop all observations
00029       for (ObservationList::iterator obsIter = observationList().begin(); obsIter
00030           != observationList().end(); obsIter++) {
00031         observation_ptr_t obsPtr = *obsIter;
00032 
00033         obsPtr->clearEvents();
00034         obsPtr->measurement.matchScore = 0;
00035 
00036 
00037         // 1a. project
00038         obsPtr->project();
00039 
00040         // 1b. check visibility
00041         obsPtr->predictVisibility();
00042 
00043         if (obsPtr->isVisible()) {
00044 
00045 
00046           // Add to tesselation grid for active search
00047           asGrid->addObs(obsPtr->expectation.x());
00048 
00049 
00050           // predict information gain
00051           obsPtr->predictInfoGain();
00052 
00053 
00054           // add to sorted list of observations
00055           obsListSorted[obsPtr->expectation.infoGain] = obsPtr;
00056 
00057         } // visible obs
00058       } // for each obs
00059 
00060 
00061       // loop only the N_UPDATES most interesting obs, from largest info gain to smallest
00062       for (ObservationListSorted::reverse_iterator obsIter = obsListSorted.rbegin(); obsIter
00063           != obsListSorted.rend(); obsIter++) {
00064         observation_ptr_t obsPtr = obsIter->second;
00065 
00066 
00067         // 1a. re-project to get up-to-date means and Jacobians
00068         obsPtr->project();
00069 
00070         obsPtr->events.predicted = true;
00071 
00072         // 1b. re-check visibility, just in case re-projection caused this obs to be invisible
00073         obsPtr->predictVisibility();
00074 
00075         if (obsPtr->isVisible()) {
00076 
00077           obsPtr->events.visible = true;
00078 
00079           if (numObs < algorithmParams_.n_updates) {
00080 
00081             obsPtr->events.measured = true;
00082 
00083             // update counter
00084             obsPtr->counters.nSearch++;
00085 
00086 
00087             // 1c. predict search area and appearance
00088             //cv::Rect roi = gauss2rect(obsPtr->expectation.x(), obsPtr->expectation.P() + matcherParams_.measVar*identity_mat(2), matcherParams_.regionSigma);
00089             image::ConvexRoi roi(obsPtr->expectation.x(), obsPtr->expectation.P() + matcherParams_.measVar*identity_mat(2), matcherParams_.regionSigma);
00090             obsPtr->predictAppearance();
00091 
00092             // 1d. match predicted feature in search area
00093             //            kernel::Chrono match_chrono;
00094             obsPtr->measurement.std(detectorParams_.measStd);
00095             boost::shared_ptr<RawSpec> rawSpecPtr = SPTR_CAST<RawSpec>(sensorPtr()->getRaw());
00096 
00097             match(rawSpecPtr, obsPtr->predictedAppearance, roi, obsPtr->measurement,
00098                                     obsPtr->observedAppearance);
00099             //            total_match_time += match_chrono.elapsedMicrosecond();
00100 
00101 /*
00102               // DEBUG: save some appearances to file
00103               ((AppearanceImagePoint*)(((DescriptorImagePoint*)(obsPtr->landmark().descriptorPtr.get()))->featImgPntPtr->appearancePtr.get()))->patch.save("descriptor_app.png");
00104               ((AppearanceImagePoint*)(obsPtr->predictedAppearance.get()))->patch.save("predicted_app.png");
00105               ((AppearanceImagePoint*)(obsPtr->observedAppearance.get()))->patch.save("matched_app.png");
00106 */
00107             // DEBUG: display predicted appearances on image, disable it when operating normally because can have side effects
00108 /*
00109             if (SHOW_PATCH) {
00110               AppearanceImagePoint * appImgPtr =
00111                   PTR_CAST<AppearanceImagePoint*> (obsPtr->predictedAppearance.get());
00112               jblas::veci shift(2);
00113               shift(0) = (appImgPtr->patch.width() - 1) / 2;
00114               shift(1) = (appImgPtr->patch.height() - 1) / 2;
00115               appImgPtr->patch.robustCopy(*PTR_CAST<RawImage*> (senPtr->getRaw().get())->img, 0, 0,
00116                                           obsPtr->expectation.x(0) - shift(0), obsPtr->expectation.x(1) - shift(1));
00117             }
00118 */
00119 
00120             // 1e. if feature is found
00121             if (obsPtr->getMatchScore() > matcherParams_.threshold) {
00122               obsPtr->counters.nMatch++;
00123               obsPtr->events.matched = true;
00124               obsPtr->computeInnovation();
00125 
00126 
00127 
00128               // 1f. if feature is inlier
00129               if (obsPtr->compatibilityTest(matcherParams_.mahalanobisTh)) { // use 3.0 for 3-sigma or the 5% proba from the chi-square tables.
00130                 numObs++;
00131                 obsPtr->counters.nInlier++;
00132                 //                kernel::Chrono update_chrono;
00133                 obsPtr->update();
00134                 //                total_update_time += update_chrono.elapsedMicrosecond();
00135                 obsPtr->events.updated = true;
00136               } // obsPtr->compatibilityTest(M_TH)
00137 
00138             } // obsPtr->getScoreMatchInPercent()>SC_TH
00139 
00140 //            cout << *obsPtr << endl;
00141 
00142 
00143           } // number of observations
00144 
00145         } // obsPtr->isVisible()
00146 
00147         // cout << "\n-------------------------------------------------- " << endl;
00148         // cout << *obsPtr << endl;
00149 
00150       } // foreach observation
00151 
00152       obsListSorted.clear(); // clear the list now or it will prevent the observation to be destroyed until next frame, and will still be displayed
00153     }
00154 
00155 
00156     template<>
00157     bool DataManagerActiveSearch<RawImage, SensorPinhole, QuickHarrisDetector, correl::FastTranslationMatcherZncc>::
00158 		match(const boost::shared_ptr<RawImage> & rawPtr, const appearance_ptr_t & targetApp, image::ConvexRoi &roi, Measurement & measure, const appearance_ptr_t & app)
00159     {
00160           app_img_pnt_ptr_t targetAppImg = SPTR_CAST<AppearanceImagePoint>(targetApp);
00161           app_img_pnt_ptr_t appImg = SPTR_CAST<AppearanceImagePoint>(app);
00162 
00163           measure.matchScore = matcher->match(targetAppImg->patch, *(rawPtr->img),
00164             roi, measure.x()(0), measure.x()(1));
00165 /*
00166           measure.matchScore = correl::Explorer<correl::Zncc>::exploreTranslation(
00167               targetAppImg->patch, *(rawPtr->img), roi.x, roi.x+roi.width-1, 2, roi.y, roi.y+roi.height-1, 2,
00168               measure.x()(0), measure.x()(1));
00169 */
00170           // set appearance
00171           // FIXME reenable this when Image::robustCopy will be fixed
00172 //          rawPtr->img->robustCopy(appImg->patch, (int)(measure.x()(0)-0.5)-appImg->patch.width()/2,
00173 //             (int)(measure.x()(1)-0.5)-appImg->patch.height()/2, 0, 0, appImg->patch.width(), appImg->patch.height());
00174 
00175           return true;
00176 
00177     }
00178 
00179 //    template<>
00180 //    bool DataManagerActiveSearch<RawImage, SensorPinhole, QuickHarrisDetector, correl::Explorer<correl::Zncc> >::
00181 //    detect(const boost::shared_ptr<RawImage> & rawPtr, cv::Rect &roi)
00182 //    {
00183 //      feat_img_pnt_ptr_t featPtr(new FeatureImagePoint(detectorParams_.patchSize,
00184 //                                                       detectorParams_.patchSize,
00185 //                                                       CV_8U));
00186 //
00187 //      featPtr->measurement.std(detectorParams_.measStd);
00188 //      if (detector->detectIn(*(rawData->img.get()), featPtr, &roi)) {
00189 //        vec pix = featPtr->measurement.x();
00190 //        extractAppearance(pix, featPtr->appearancePtr);
00191 //
00192 //        app_img_pnt_ptr_t app = SPTR_CAST<AppearanceImagePoint>(appPtr);
00193 //        cv::Size size = app->patch.size();
00194 //        int shift_x = (size.width-1)/2;
00195 //        int shift_y = (size.height-1)/2;
00196 //        int x_src = pix(0)-shift_x;
00197 //        int y_src = pix(1)-shift_y;
00198 //        img->copy(app->patch, x_src, y_src, 0, 0, size.width, size.height);
00199 //
00200 //
00201 //((AppearanceImagePoint*)(featPtr->appearancePtr.get()))->patch.save("detected_feature.png");
00202 //((AppearanceImagePoint*)(featPtr->appearancePtr.get()))->patch.save("detected_feature.png");
00203 //        cout << "Detected pix: " << featPtr->measurement << endl;
00204 //
00205 //        return true;
00206 //
00207 //    }
00208 
00209     //template<class SensorSpec>
00210     //void DataManagerActiveSearch<RawImage,SensorSpec>::
00211     // FIXME make this more abstract...
00212     template<>
00213     void DataManagerActiveSearch<RawImage, SensorPinhole, QuickHarrisDetector, correl::FastTranslationMatcherZncc>::
00214     detectNewObs( boost::shared_ptr<RawImage> rawData )
00215     {
00216       if (mapManagerPtr()->mapSpaceForInit()) {
00217         //boost::shared_ptr<RawImage> rawDataSpec = SPTR_CAST<RawImage>(rawData);
00218         ROI roi;
00219         if (asGrid->getRoi(roi)) {
00220           feat_img_pnt_ptr_t featPtr(new FeatureImagePoint(detectorParams_.patchSize,
00221                                                            detectorParams_.patchSize,
00222                                                            CV_8U));
00223           featPtr->measurement.std(detectorParams_.measStd);
00224           if (detector->detectIn(*(rawData->img.get()), featPtr, &roi)) {
00225             vec pix = featPtr->measurement.x();
00226             app_img_pnt_ptr_t appPtr = SPTR_CAST<AppearanceImagePoint>(featPtr->appearancePtr);
00227 
00228             // FIXME see if we can use detectorParams_.patchSize instead.
00229             cv::Size size = appPtr->patch.size();
00230 
00231             int shift_x = (size.width-1)/2;
00232             int shift_y = (size.height-1)/2;
00233             int x_src = pix(0)-shift_x;
00234             int y_src = pix(1)-shift_y;
00235             rawData->img->copy(appPtr->patch, x_src, y_src, 0, 0, size.width, size.height);
00236 
00237 
00238 //appPtr->patch.save("detected_feature.png");
00239 
00240 //            cout << "Detected pix: " << featPtr->measurement << endl;
00241 
00242             // 2a. Create the lmk and associated obs object.
00243             observation_ptr_t obsPtr =
00244                 mapManagerPtr()->createNewLandmark(shared_from_this());
00245 
00246             // 2b. fill data for this obs
00247             obsPtr->counters.nSearch = 1;
00248             obsPtr->counters.nMatch = 1;
00249             obsPtr->counters.nInlier = 1;
00250             obsPtr->events.visible = true;
00251             obsPtr->events.predicted = false;
00252             obsPtr->events.measured = true;
00253             obsPtr->events.matched = false;
00254             obsPtr->events.updated = false;
00255             obsPtr->measurement = featPtr->measurement;
00256 //            obsPtr->measurement.x(featPtr->measurement.x());
00257 //            obsPtr->measurement.P(featPtr->measurement.P());
00258 //            obsPtr->measurement.matchScore = featPtr->measurement.matchScore;
00259 
00260 //            cout << "Measured pix: " << obsPtr->measurement << endl;
00261 
00262 //            obsPtr->setup(sensorSpecPtr()->params.pixNoise, matcherParams_.patchSize);
00263 
00264             app_img_pnt_ptr_t app_src = SPTR_CAST<AppearanceImagePoint>(featPtr->appearancePtr);
00265             app_img_pnt_ptr_t app_dst = SPTR_CAST<AppearanceImagePoint>(obsPtr->observedAppearance);
00266             app_src->patch.copy(app_dst->patch, (app_src->patch.width()-app_dst->patch.width())/2,
00267                                 (app_src->patch.height()-app_dst->patch.height())/2, 0, 0,
00268                                 app_dst->patch.width(), app_dst->patch.height());
00269 
00270             // 2c. compute and fill stochastic data for the landmark
00271             obsPtr->backProject();
00272 
00273             // 2d. Create lmk descriptor
00274             vec7 globalSensorPose = sensorPtr()->globalPose();
00275             desc_img_pnt_ptr_t
00276                 descPtr(new DescriptorImagePointFirstView(featPtr, globalSensorPose,
00277                                                  obsPtr));
00278             obsPtr->landmarkPtr()->setDescriptor(descPtr);
00279 
00280           } // create&init
00281         } // getRoi()
00282       } // if space in map
00283     } // detect()
00284 
00285     template<class RawSpec,class SensorSpec, class Detector, class Matcher>
00286     void DataManagerActiveSearch<RawSpec,SensorSpec,Detector,Matcher>::
00287     process( boost::shared_ptr<RawAbstract> data )
00288     {
00289       boost::shared_ptr<RawImage> dataSpec = SPTR_CAST<RawImage>(data);
00290       // 1. Observe known landmarks.
00291       processKnownObs(dataSpec); // process known landmarks
00292       // 2. Initialize new landmark.
00293       detectNewObs(dataSpec);
00294     }
00295 
00296   }  // namespace ::rtslam
00297 }; // namespace jafar::
00298 
00299 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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