00001 #include <highgui.h>
00002 #include <Eigen/Eigen>
00003
00004 namespace ndt_feature_reg
00005 {
00006
00007 template <typename PointT>
00008 void ExpNDTFrameProc<PointT>::addFrameIncremental (NDTFrame<PointT> *f, bool skipMatching, bool ndtEstimateDI,
00009 bool match_full, bool match_no_association)
00010 {
00011
00012 if(!match_full)
00013 {
00014 detectKeypoints(f);
00015 }
00016
00017 f->computeNDT(ndtEstimateDI, this->non_mean);
00018
00019 if(!(match_no_association||match_full))
00020 {
00021 f->assignPts();
00022 calcDescriptors(f);
00023 }
00024 frames.push_back(f);
00025
00026 ExpNDTFrameProc<PointT>::EigenTransform tr, tr2;
00027 tr.setIdentity();
00028 if(frames.size() == 1)
00029 {
00030
00031 transformVector.clear();
00032 transformVector.push_back(tr);
00033 return;
00034 }
00035
00036
00037 Eigen::Affine3d transl_transform;
00038 Eigen::Affine3d rot_transform;
00039 std::vector<std::pair<int,int> > corr;
00040
00041 int i = frames.size()-1;
00042
00043 if(!(match_full || match_no_association))
00044 {
00045 pe.estimate(*frames[0],*frames[i]);
00046 transl_transform = (Eigen::Affine3d)Eigen::Translation3d(pe.trans);
00047 rot_transform = (Eigen::Affine3d)Eigen::Matrix3d(pe.rot);
00048 tr = transl_transform*rot_transform;
00049 std::cout<<pe.inliers.size()<<std::endl;
00050 if (!skipMatching)
00051 {
00052 corr.clear();
00053 corr = convertMatches(pe.inliers);
00054 lslgeneric::NDTMatcherFeatureD2D<PointT,PointT> matcher_feat(corr, trim_factor);
00055 matcher_feat.match(frames[0]->ndt_map, frames[i]->ndt_map, tr, true);
00056
00057 }
00058 }
00059 else
00060 {
00061 lslgeneric::NDTMatcherD2D<PointT,PointT> matcher;
00062 matcher.current_resolution = frames[0]->current_res/2;
00063 matcher.match(frames[0]->ndt_map, frames[i]->ndt_map, tr);
00064
00065 }
00066
00067
00068
00069 transformVector.push_back(tr);
00070
00071 }
00072
00073 template <typename PointT>
00074 void ExpNDTFrameProc<PointT>::trimNbFrames (size_t maxNbFrames)
00075 {
00076 if (frames.size() > maxNbFrames)
00077 {
00078
00079 for(size_t i =1; i<frames.size(); i++)
00080 {
00081
00082 delete frames[i];
00083 }
00084 frames.erase(frames.begin()+1, frames.end());
00085 transformVector.erase(transformVector.begin()+1, transformVector.end());
00086 }
00087 }
00088
00089 template <typename PointT>
00090 void
00091 ExpNDTFrameProc<PointT>::detectKeypoints(NDTFrame<PointT> *f) const
00092 {
00093 if (img_scale == 1.)
00094 {
00095 detector->detect(f->img, f->kpts);
00096 }
00097 else
00098 {
00099 cv::Mat tmp;
00100 cv::resize(f->img, tmp, cv::Size(), img_scale, img_scale, cv::INTER_LINEAR);
00101 detector->detect(tmp, f->kpts);
00102 float factor = 1./img_scale;
00103 scaleKeyPointPosition(f->kpts, factor);
00104 }
00105 }
00106
00107 template <typename PointT>
00108 void
00109 ExpNDTFrameProc<PointT>::calcDescriptors(NDTFrame<PointT> *f) const
00110 {
00111 if (img_scale == 1.)
00112 {
00113
00114 double dsize = f->kpts.size();
00115 int N_DIMS = ceil(sqrt(dsize));
00116
00117 int support_size_max = 0;
00118 for (unsigned int q=0; q<f->kpts.size(); ++q) {
00119 support_size_max = (f->kpts[q].size > support_size_max) ? f->kpts[q].size : support_size_max;
00120 }
00121 std::cout<<"support_size_max = "<<support_size_max<<std::endl;
00122
00123 int support_size;
00124 cv::Mat projected (support_size_max*N_DIMS,support_size_max*N_DIMS,f->img.type());
00125 cv::Mat projected_drawn (support_size_max*N_DIMS,support_size_max*N_DIMS,f->img.type());
00126 cv::Mat orig_drawn (f->img.size(),f->img.type());
00127 int i, j, p, k;
00128 std::vector<cv::KeyPoint> new_kpts;
00129 cv::KeyPoint kp;
00130 cv::Size sz;
00131
00132
00133 for (unsigned int q=0; q<f->kpts.size(); ++q) {
00134 support_size = (f->kpts[q].size > support_size_max) ? support_size_max : f->kpts[q].size;
00135
00136 i = support_size_max * (q / N_DIMS);
00137 j = support_size_max * (q % N_DIMS);
00138 cv::Mat roiMat (projected,cv::Rect(i,j,support_size,support_size));
00139 p = f->kpts[q].pt.x - support_size/2;
00140 k = f->kpts[q].pt.y - support_size/2;
00141 cv::Mat roi2Mat(f->img,cv::Rect(p,k,support_size,support_size));
00142 roi2Mat.copyTo(roiMat);
00143 kp = f->kpts[q];
00144 kp.pt.x = i+support_size/2;
00145 kp.pt.y = j+support_size/2;
00146 kp.size = support_size;
00147 new_kpts.push_back(kp);
00148 }
00149
00150 cv::drawKeypoints(projected,new_kpts,projected_drawn,cv::Scalar::all(-1),4);
00151 cv::namedWindow("projected",0);
00152 cv::imshow("projected", projected_drawn);
00153
00154 cv::drawKeypoints(f->img,f->kpts,orig_drawn,cv::Scalar::all(-1),4);
00155 cv::namedWindow("original",0);
00156 cv::imshow("original", orig_drawn);
00157
00158 cv::waitKey(0);
00159
00160 cv::Mat temptors;
00161 std::cout<<"valid kpts: "<<f->kpts.size()<<" "<<new_kpts.size()<<std::endl;
00162 extractor->compute(f->img, f->kpts, f->dtors);
00163 extractor->compute(projected, new_kpts, temptors);
00164 std::cout<<"after kpts: "<<f->kpts.size()<<" "<<new_kpts.size()<<std::endl;
00165 }
00166 else
00167 {
00168 cv::Mat tmp;
00169 cv::resize(f->img, tmp, cv::Size(), img_scale, img_scale, cv::INTER_LINEAR);
00170 float factor = img_scale;
00171 scaleKeyPointPosition(f->kpts, factor);
00172 extractor->compute(tmp, f->kpts, f->dtors);
00173 factor = 1./img_scale;
00174 scaleKeyPointPosition(f->kpts, factor);
00175 }
00176 }
00177
00178 }