sift_alg.cpp
Go to the documentation of this file.
00001 #include "sift_alg.h"
00002 
00003 extern "C" {
00004 #include "dsift.h"
00005 }
00006 
00007 using namespace cv;
00008 
00009 SiftAlgorithm::SiftAlgorithm(void)
00010 {
00011         centroids_ready_ = false;
00012 //      sift_centroids_ = Mat(128, 128, CV_32FC1);
00013 }
00014 
00015 SiftAlgorithm::~SiftAlgorithm(void)
00016 {
00017 }
00018 
00019 void SiftAlgorithm::config_update(Config& new_cfg, uint32_t level)
00020 {
00021         this->lock();
00022 
00023         this->descriptors_file_ = new_cfg.descriptors_file;
00024         // save the current configuration
00025         this->config_=new_cfg;
00026         
00027         this->unlock();
00028 }
00029 
00030 // SiftAlgorithm Public API
00031 
00032 iri_perception_msgs::DescriptorSet SiftAlgorithm::get_descriptors_from_image(cv::Mat &image)
00033 {
00034         iri_perception_msgs::DescriptorSet result;
00035         cv::Mat descriptors;
00036         std::vector<cv::KeyPoint> keypoints;
00037         
00038         get_sift_descriptors(image, descriptors, keypoints);
00039         for (int i=0; i < descriptors.rows; i++) {
00040                 iri_perception_msgs::Descriptor descriptor;
00041                 for (int j=0; j < descriptors.cols; j++) {
00042                         descriptor.descriptor.push_back(descriptors.at<float>(i,j));
00043                 }
00044                 descriptor.u = keypoints[i].pt.x;
00045                 descriptor.v = keypoints[i].pt.y;
00046                 result.descriptors.push_back(descriptor);
00047         }
00048         
00049         result.num = descriptors.rows;
00050         result.len = descriptors.cols;
00051         result.width = 640;
00052         result.height = 480;
00053         //result.num_orient_bins = ; TODO ARNAU
00054         //result.num_spa_bins = ; TODO ARNAU
00055         
00056         return result;
00057 }
00058 
00059 
00060 iri_perception_msgs::GeoVwSet SiftAlgorithm::get_geovw_from_image(cv::Mat &image)
00061 {
00062         cv::Mat descriptors;
00063         std::vector<cv::KeyPoint> keypoints;
00064         
00065         get_sift_descriptors(image, descriptors, keypoints);
00066         
00067         return get_geo_vw_from_descriptors(descriptors, keypoints);
00068 }
00069 
00070 iri_perception_msgs::GeoVwSet SiftAlgorithm::get_geo_vw_from_descriptors(cv::Mat descriptors, std::vector<cv::KeyPoint> keypoints)
00071 {
00072         std::vector<DMatch> matches;
00073         
00074         ROS_INFO("MATCHING! %d, %d", descriptors.rows, sift_centroids_.rows);
00075         
00076         cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create("BruteForce");
00077         matcher->match( descriptors, sift_centroids_, matches);
00078         
00079         iri_perception_msgs::GeoVwSet geo_vw_set;
00080         vector<DMatch>::iterator it;
00081         for ( it=matches.begin() ; it < matches.end(); it++ ) {
00082                 iri_perception_msgs::GeoVw geo_vw;
00083                 geo_vw.x = keypoints[it->queryIdx].pt.x;
00084                 geo_vw.y = keypoints[it->queryIdx].pt.y;
00085                 geo_vw.word = it->trainIdx;
00086                 geo_vw_set.geo_vws.push_back(geo_vw);
00087         }
00088         
00089         return geo_vw_set;
00090 }
00091 
00092 void SiftAlgorithm::get_sift_descriptors(cv::Mat const& image, cv::Mat& descriptors, std::vector<cv::KeyPoint>& keypoints)
00093 {
00094         cv::Mat image_gray;
00095         
00096         //cv::DenseFeatureDetector::Params(1.f,1,0.1f,6));
00097         //cv::Ptr<FeatureDetector> feature_detector = new DenseFeatureDetector();
00098         //cv::Ptr<DescriptorExtractor> descriptor_extractor = DescriptorExtractor::create("SIFT");
00099 
00100         cvtColor(image, image_gray, CV_RGB2GRAY);
00101         cv::Mat ima;
00102         image_gray.convertTo(ima, CV_32F);
00103 
00104         
00105         ROS_DEBUG("SiftAlgNode::get_sift_descriptors: Getting keypoints..."); 
00106         
00107         float* img=ima.ptr<float>(0);
00108         int width = ima.cols; 
00109         int height = ima.rows;
00110 
00111         VlDsiftFilter* dsift_data = vl_dsift_new (width, height);
00112         VlDsiftDescriptorGeometry dsift_geo;
00113         dsift_geo.binSizeX=8;
00114         dsift_geo.binSizeY=8;
00115         dsift_geo.numBinT=8;
00116         dsift_geo.numBinX=4;
00117         dsift_geo.numBinY=4;
00118         
00119         int descsize = dsift_geo.numBinT*dsift_geo.numBinX*dsift_geo.numBinY;
00120         
00121         vl_dsift_set_geometry(dsift_data, &dsift_geo);
00122         vl_dsift_set_steps (dsift_data,6,6);
00123         vl_dsift_process(dsift_data, img);
00124         
00125         int npoints = vl_dsift_get_keypoint_num (dsift_data);
00126         
00127         float const* descs = vl_dsift_get_descriptors (dsift_data);
00128         VlDsiftKeypoint const * kpoints = vl_dsift_get_keypoints (dsift_data);
00129         
00130         ROS_DEBUG("SiftAlgNode::get_sift_descriptors: Getting descriptors..."); 
00131 
00132         for(int i=0; i<npoints; i++)
00133                 keypoints.push_back(cv::KeyPoint(kpoints[i].x, kpoints[i].y, kpoints[i].s, 0, kpoints[i].norm));
00134 
00135         cv::Mat fdescs(npoints, 128, CV_32FC1);
00136         if(!fdescs.isContinuous())
00137         {
00138                 std::cout<<"Matrix not continuous!"<<std::endl;
00139                 exit (-1);
00140         }
00141         memcpy(fdescs.ptr<float>(0), descs, sizeof(float)*npoints*128);
00142         descriptors = cv::Mat(npoints, 128, CV_32FC1);
00143         fdescs.convertTo(descriptors, CV_32F, 256); // 07/08/12 Arnau: Originally though to converting it to uchar (less space faster to match). Turns out cv::Matcher can only work wit floats, apparently. 
00144         //std::cout<<descriptors<<std::endl;
00145         vl_dsift_delete(dsift_data);
00146         //descriptor_extractor->compute( image_gray, keypoints, descriptors );
00147 }
00148 
00149 cv::Mat SiftAlgorithm::read_trained_descriptors()
00150 {
00151         FileStorage fs; 
00152         fs.open(this->descriptors_file_, FileStorage::READ);
00153         
00154         Mat trained_desc;
00155         fs["descriptors"] >> trained_desc;
00156         
00157         fs.release();
00158         return trained_desc;
00159 }
00160 
00161 bool SiftAlgorithm::save_descriptors(Mat descriptors)
00162 {
00163         ROS_INFO("SiftAlgNode::save_descriptors: Saving descriptors"); 
00164         
00165         FileStorage fs(this->descriptors_file_, FileStorage::WRITE);
00166         
00167         fs << "descriptors" << descriptors;
00168         
00169         fs.release();
00170         
00171         ROS_INFO("SiftAlgNode::save_descriptors: Finished"); 
00172         
00173         return true;
00174 }


iri_sift
Author(s): dmartinez
autogenerated on Fri Dec 6 2013 22:44:31