GSoC2011SfM
0.1
Google Summer of Code 2011: Structure from motion
|
00001 #include "PointsToTrackWithImage.h" 00002 #include "TracksOfPoints.h" 00003 00004 00005 namespace OpencvSfM 00006 { 00007 using cv::FeatureDetector; 00008 using cv::DescriptorExtractor; 00009 using cv::Ptr; 00010 using cv::SIFT; 00011 using cv::Mat; 00012 using std::string; 00013 using cv::Scalar; 00014 using cv::KeyPoint; 00015 using cv::Point; 00016 using std::vector; 00017 using cv::line; 00018 using cv::circle; 00019 00020 PointsToTrackWithImage::PointsToTrackWithImage( int corresponding_image, 00021 cv::Mat imageToAnalyse, 00022 cv::Ptr<cv::FeatureDetector> feature_detector, 00023 cv::Ptr<cv::DescriptorExtractor> descriptor_detector, 00024 cv::Mat maskOfAnalyse ) 00025 :PointsToTrack( corresponding_image ), 00026 maskOfAnalyse_( maskOfAnalyse ),feature_detector_( feature_detector ), 00027 descriptor_detector_( descriptor_detector ) 00028 { 00029 imageToAnalyse_ = imageToAnalyse; 00030 } 00031 00032 PointsToTrackWithImage::PointsToTrackWithImage( int corresponding_image, 00033 cv::Mat imageToAnalyse,std::string feature_detector, 00034 std::string descriptor_detector/*=""SIFT""*/, cv::Mat maskOfAnalyse ) 00035 :PointsToTrack( corresponding_image ), 00036 maskOfAnalyse_( maskOfAnalyse ), 00037 feature_detector_( FeatureDetector::create( feature_detector )), 00038 descriptor_detector_( DescriptorExtractor::create( descriptor_detector )) 00039 { 00040 imageToAnalyse_ = imageToAnalyse; 00041 } 00042 00043 void PointsToTrackWithImage::setFeatureDetector( cv::Ptr<cv::FeatureDetector> feature_detector ) 00044 { 00045 feature_detector_=feature_detector; 00046 } 00047 00048 void PointsToTrackWithImage::setDescriptorExtractor( cv::Ptr<cv::DescriptorExtractor> descriptor_detector ) 00049 { 00050 descriptor_detector_=descriptor_detector; 00051 } 00052 00053 PointsToTrackWithImage::~PointsToTrackWithImage( void ) 00054 { 00055 imageToAnalyse_.release( ); 00056 maskOfAnalyse_.release( ); 00057 } 00058 00059 void PointsToTrackWithImage::computeColorOfPoints() 00060 { 00061 //find color of points: 00062 RGB_values_.clear(); 00063 unsigned int size_max = this->keypoints_.size( ); 00064 unsigned int colorFinal; 00065 char elemSize = imageToAnalyse_.elemSize(); 00066 for(unsigned int i=0; i<size_max; ++i ) 00067 { 00068 //as we don't know type of image, we use a different processing: 00069 switch ( elemSize ) 00070 { 00071 case 1://char 00072 { 00073 uchar color = imageToAnalyse_.at<uchar>( keypoints_[i].pt ); 00074 colorFinal = (unsigned int)((((int)color)<<16)|((int)color<<8)|((int)color)); 00075 break; 00076 } 00077 case 2://short??? 00078 { 00079 unsigned short color = imageToAnalyse_.at<unsigned short>( keypoints_[i].pt ); 00080 colorFinal = (unsigned int)((((int)color)<<16)|((int)color)); 00081 break; 00082 } 00083 case 3://RGB 00084 { 00085 cv::KeyPoint& kp = keypoints_[i]; 00086 uchar* ptr = (imageToAnalyse_.data + 00087 (imageToAnalyse_.step*(int)kp.pt.y) + 00088 (int)kp.pt.x * imageToAnalyse_.elemSize()); 00089 colorFinal = (unsigned int)((((int)ptr[2])<<16)|((int)ptr[1]<<8)|((int)ptr[0])); 00090 break; 00091 } 00092 default://RGBA 00093 { 00094 colorFinal = imageToAnalyse_.at<unsigned int>( keypoints_[i].pt ); 00095 break; 00096 } 00097 } 00098 RGB_values_.push_back(colorFinal); 00099 } 00100 } 00101 00102 int PointsToTrackWithImage::impl_computeKeypoints_( ) 00103 { 00104 this->keypoints_.clear(); 00105 feature_detector_->detect( imageToAnalyse_,this->keypoints_,maskOfAnalyse_ ); 00106 computeColorOfPoints(); 00107 return this->keypoints_.size(); 00108 } 00109 00110 void PointsToTrackWithImage::impl_computeDescriptors_( ) 00111 { 00112 this->descriptors_.release();//in case some descriptors were already found... 00113 descriptor_detector_->compute( imageToAnalyse_,this->keypoints_,this->descriptors_ ); 00114 computeColorOfPoints();//keypoints_ may have changed! 00115 } 00116 }