void CalMatchesThroughMatcher(const Mat &img1, const Mat &img2, std::vector<KeyPoint> &left_keypoints, std::vector<KeyPoint> &right_keypoints, std::vector<DMatch> &matches) { SurfDescriptorExtractor surfDesc; Mat descriptros1, descriptros2; surfDesc.compute(img1, left_keypoints, descriptros1); surfDesc.compute(img2, right_keypoints, descriptros2); BruteForceMatcher<L2<float> > testMatcher; BFMatcher newMatcher(NORM_L2, false); /* testMatcher.match(descriptros1, descriptros2, matches); if(matches.size()>9) { std::nth_element(matches.begin(), matches.begin() + 8, matches.end()); matches.erase(matches.begin() + 9, matches.end()); } */ std::vector<vector<DMatch> > knnMatches; const int k = 2; const float minRatio = 1.f / 2.5f; newMatcher.knnMatch(descriptros1, descriptros2, knnMatches, k); for (size_t i = 0; i < knnMatches.size(); i++) { const DMatch& bestMatch = knnMatches[i][0]; const DMatch& betterMatch = knnMatches[i][1]; float distanceRatio = bestMatch.distance / betterMatch.distance; if (distanceRatio < minRatio) matches.push_back(bestMatch); } }
/** @function main */ int main( int argc, char** argv ) { int i; // Load the base image Mat base_image = imread(argv[1], CV_LOAD_IMAGE_UNCHANGED), base_descriptor; std::vector<KeyPoint> base_keypoints; Mat tmp_image; int minHessian = 400; std::vector<Mat> image_vector; std::vector< std::vector<KeyPoint> > keypoints_vector; std::vector<Mat> descriptors_vector; std::vector< std::vector< DMatch > > matches_vector; SurfFeatureDetector detector( minHessian ); int image_count = argc - 2; // Computes the base_image descriptor SurfDescriptorExtractor extractor; detector.detect( base_image, base_keypoints); extractor.compute( base_image, base_keypoints, base_descriptor ); image_vector.resize(image_count); keypoints_vector.resize(image_count); descriptors_vector.resize(image_count); matches_vector.resize(image_count); for(i=0; i<image_count; i++) { std::cout << "Image " << i+1 << std::endl; tmp_image = imread(argv[2+i]); if( !tmp_image.data) { std::cout<< " --(!) Error reading images " << std::endl; return -1; } image_vector.push_back(tmp_image); detector.detect( tmp_image, keypoints_vector[i]); extractor.compute( tmp_image, keypoints_vector[i], descriptors_vector[i] ); //-- Step 3: Matching descriptor vectors with a brute force matcher BFMatcher matcher(NORM_L2); std::vector< DMatch > matches; matcher.match( base_descriptor, descriptors_vector[i], matches ); Mat img_matches; drawMatches( tmp_image, keypoints_vector[i], base_image, base_keypoints, matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS ); imshow("Test", img_matches); waitKey(2000); } return 0; }
void CvSURF::onNewImage() { LOG(LTRACE) << "CvSURF::onNewImage\n"; try { // Input: a grayscale image. cv::Mat input = in_img.read(); //-- Step 1: Detect the keypoints using SURF Detector. SurfFeatureDetector detector( minHessian ); std::vector<KeyPoint> keypoints; detector.detect( input, keypoints ); //-- Step 2: Calculate descriptors (feature vectors). SurfDescriptorExtractor extractor; Mat descriptors; extractor.compute( input, keypoints, descriptors); // Write features to the output. Types::Features features(keypoints); out_features.write(features); // Write descriptors to the output. out_descriptors.write(descriptors); } catch (...) { LOG(LERROR) << "CvSURF::onNewImage failed\n"; } }
int OpenCVImageProcessor::saveImageDescriptor(Mat srcImage,std::string *name,std::string *path,int firstItem){ Size size(320,220); int minHessian = 400; Mat src_image,object_descriptor,resizedImage; resize(srcImage, resizedImage, size); cvtColor( resizedImage,src_image, cv::COLOR_BGR2GRAY); SurfFeatureDetector detector( minHessian ); std::vector<KeyPoint> keypoints_object, keypoints_scene; detector.detect( src_image, keypoints_object ); SurfDescriptorExtractor extractor; extractor.compute( src_image, keypoints_object, object_descriptor ); if(firstItem){ FileStorage fs(*path,FileStorage::APPEND); write(fs,*name,object_descriptor); name->append("_keypoints"); write(fs,*name,keypoints_object); fs.release(); } else { FileStorage fs(*path,FileStorage::WRITE); write(fs,*name,object_descriptor); name->append("_keypoints"); write(fs,*name,keypoints_object); fs.release(); } return 1; }
bool RTAB_feature_extraction::RTAB_feature_extraction_exe(Mat img, Mat &descriptors) { //-- Step 1: Detect the keypoints using SURF Detector int minHessian = 400; SurfFeatureDetector detector(minHessian); SurfDescriptorExtractor extractor; vector<KeyPoint> keyPoints; detector.detect(img, keyPoints); extractor.compute(img, keyPoints, descriptors); if(descriptors.rows>300) { cout<<"has enough descriptors "<<descriptors.size()<<endl; return true; } else { cout<<"Not Enough descriptors, Only has "<<descriptors.size()<<endl; return false; } }
void addTrainImage(string name) { /* Добавление нового эталонного изображения и вычисление его дескриптора */ Mat train_img = imread(_train_img_dir + "template_" + name + ".jpg"); if(!train_img.empty()) { resize(train_img, train_img, Size(SIGN_SIZE, SIGN_SIZE), 0, 0); _train_images.push_back(train_img); _train_sign_names.push_back(name); vector<KeyPoint> points; _detector.detect( train_img, points ); _train_keypoints.push_back(points); Mat descriptors; _extractor.compute( train_img, points, descriptors); _train_descriptors.push_back(descriptors); } else { cout << ERROR_STR << "Could not load train image " << _train_img_dir << name << ".jpg" << endl; } }
void process( Mat image ) { long best = -1; for( int trial = 0; trial < 10; trial++ ) { clock_t start = clock(); // location of detected points std::vector<KeyPoint> ipts; // SurfFeatureDetector detector(250,4,4,false); // 6485 features SurfFeatureDetector detector(1110,4,4,false); // 2019 features graf 1 detector.detect(image,ipts); // Create Surf Descriptor Object SurfDescriptorExtractor extractor; Mat descriptors; extractor.compute( image, ipts, descriptors ); clock_t end = clock(); long mtime = (long)(1000*(float(end - start) / CLOCKS_PER_SEC)); if( best == -1 || mtime < best ) best = mtime; printf("time = %d detected = %d\n",(int)mtime,(int)ipts.size()); } printf("best time = %d\n",(int)best); }
int OpenCVImageProcessor::getImageInfo(Mat srcImage, std::string *path, std::vector<std::string> desc){ FileStorage fs(*path,FileStorage::READ); int minHessian = 400; Mat src_image,object_descriptor,scene_descriptor,resizedImage; Size size(320,220); resize(srcImage, resizedImage, size); cvtColor( resizedImage,src_image, cv::COLOR_BGR2GRAY); SurfFeatureDetector detector( minHessian ); std::vector<KeyPoint> keypoints_object, keypoints_scene; detector.detect( src_image, keypoints_object ); SurfDescriptorExtractor extractor; extractor.compute( src_image, keypoints_object, object_descriptor ); std::vector<cv::DMatch> matches; printf("se desi %d; %d %d\n",src_image.cols,src_image.rows,desc.size()); for(int i=0;i<desc.size();i++){ FileNode descriptor=fs[desc[i]]; std::string keypoints=desc[i]; keypoints.append("_keypoints"); FileNode keypointsfn=fs[keypoints]; read(descriptor, scene_descriptor); read(keypointsfn , keypoints_scene); RobustMatcher matcher; int result=matcher.matchRnasac(matches, keypoints_object, keypoints_scene, object_descriptor, scene_descriptor); if(result>0) return i; } return -1; }
void init(Mat img){ Pose = Mat::eye(4, 4, CV_64F); PreviousImageGrayScale = img; PreviousFeatures.clear(); SurfDetector.detect(img, PreviousFeatures); SurfDescriptor.compute(img, PreviousFeatures, PreviousFeatureDescriptors); }
void computeSURF(Mat image, std::vector<KeyPoint> &keypoint, Mat &descriptors) { SurfFeatureDetector detector(50); detector.detect(image, keypoint); SurfDescriptorExtractor extractor; extractor.compute(image, keypoint, descriptors); }
//-----------------------------------【main( )函数】-------------------------------------------- // 描述:控制台应用程序的入口函数,我们的程序从这里开始执行 //----------------------------------------------------------------------------------------------- int main( ) { //【0】改变console字体颜色 system("color 1F"); //【0】显示欢迎和帮助文字 ShowHelpText( ); //【1】载入素材图 Mat srcImage1 = imread("1.jpg",1); Mat srcImage2 = imread("2.jpg",1); if( !srcImage1.data || !srcImage2.data ) { printf("读取图片错误,请确定目录下是否有imread函数指定的图片存在~! \n"); return false; } //【2】使用SURF算子检测关键点 int minHessian = 700;//SURF算法中的hessian阈值 SurfFeatureDetector detector( minHessian );//定义一个SurfFeatureDetector(SURF) 特征检测类对象 std::vector<KeyPoint> keyPoint1, keyPoints2;//vector模板类,存放任意类型的动态数组 //【3】调用detect函数检测出SURF特征关键点,保存在vector容器中 detector.detect( srcImage1, keyPoint1 ); detector.detect( srcImage2, keyPoints2 ); //【4】计算描述符(特征向量) SurfDescriptorExtractor extractor; Mat descriptors1, descriptors2; extractor.compute( srcImage1, keyPoint1, descriptors1 ); extractor.compute( srcImage2, keyPoints2, descriptors2 ); //【5】使用BruteForce进行匹配 // 实例化一个匹配器 BruteForceMatcher< L2<float> > matcher; std::vector< DMatch > matches; //匹配两幅图中的描述子(descriptors) matcher.match( descriptors1, descriptors2, matches ); //【6】绘制从两个图像中匹配出的关键点 Mat imgMatches; drawMatches( srcImage1, keyPoint1, srcImage2, keyPoints2, matches, imgMatches );//进行绘制 //【7】显示效果图 imshow("匹配图", imgMatches ); waitKey(0); return 0; }
virtual void process() { std::vector<KeyPoint> ipts; detector.detect(inputImage,ipts); Mat descriptors; extractor.compute( inputImage, ipts, descriptors ); // printf("num points %d\n",(int)ipts.size()); }
bool findMatch(CvPoint &offset, FlannBasedMatcher matcher, SurfFeatureDetector detector, SurfDescriptorExtractor extractor, Mat des_object[]) { bool noMatch = true; Mat des_image, img_matches; vector<KeyPoint> kp_image; vector<vector<DMatch > > matches; vector<DMatch > good_matches; int iter = 0; Mat image = imread("/home/pi/opencv/photo.jpg" , CV_LOAD_IMAGE_GRAYSCALE ); detector.detect( image, kp_image ); extractor.compute( image, kp_image, des_image ); while ( noMatch ) { //printf("before kp and des detection 2\n"); matcher.knnMatch(des_object[iter], des_image, matches, 2); for(int i = 0; i < min(des_image.rows-1,(int) matches.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS { if((matches[i][0].distance < 0.6*(matches[i][1].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0)) { good_matches.push_back(matches[i][0]); } } //printf("Number of matches: %d\n", good_matches.size()); if (good_matches.size() >= 10) { CvPoint center = cvPoint(0,0); for ( int z = 0 ; z < good_matches.size() ; z++ ) { int index = good_matches.at(z).trainIdx; center.x += kp_image.at(index).pt.x; center.y += kp_image.at(index).pt.y; } center.x = center.x/good_matches.size(); center.y = center.y/good_matches.size(); int radius = 5; circle( image, center, radius, {0,0,255}, 3, 8, 0 ); namedWindow("test"); imshow("test", image); imwrite("centerPoint.jpg", image); waitKey(5000); int offsetX = center.x - image.cols/2; int offsetY = center.y - image.rows/2; offset = cvPoint(offsetX, offsetY); noMatch = false; } //printf("draw good matches\n"); //Show detected matches if ( iter++ == 3 || !noMatch ) break; good_matches.clear(); } return noMatch; }
int compare(Mat img_1, Mat img_2) { //-- Step 1: Detect the keypoints using SURF Detector int minHessian = 400; SurfFeatureDetector detector( minHessian ); std::vector<KeyPoint> keypoints_1, keypoints_2; detector.detect( img_1, keypoints_1 ); detector.detect( img_2, keypoints_2 ); //-- Step 2: Calculate descriptors (feature vectors) SurfDescriptorExtractor extractor; Mat descriptors_1, descriptors_2; extractor.compute( img_1, keypoints_1, descriptors_1 ); extractor.compute( img_2, keypoints_2, descriptors_2 ); //-- Step 3: Matching descriptor vectors using FLANN matcher FlannBasedMatcher matcher; std::vector< DMatch > matches; matcher.match( descriptors_1, descriptors_2, matches ); double max_dist = 0; double min_dist = 100; //-- Quick calculation of max and min distances between keypoints for( int i = 0; i < descriptors_1.rows; i++ ) { double dist = matches[i].distance; if( dist < min_dist ) min_dist = dist; if( dist > max_dist ) max_dist = dist; } //-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist, //-- or a small arbitary value ( 0.02 ) in the event that min_dist is very //-- small) //-- PS.- radiusMatch can also be used here. std::vector< DMatch > good_matches; for( int i = 0; i < descriptors_1.rows; i++ ) { if( matches[i].distance <= max(2 * min_dist, 0.02) ) { good_matches.push_back( matches[i]); } } // Mat img_matches; // drawMatches( img_1, keypoints_1, img_2, keypoints_2, // good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), // vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS ); // //-- Show detected matches // imshow( "Good Matches", img_matches ); waitKey(0); return good_matches.size(); }
int main(int argc, char** argv) { printf("BEGIN\n"); PyObject *pName, *pModule, *pDict, *pFunc, *pValue; printf("BEGIN\n"); Py_Initialize(); PySys_SetArgv(argc, argv); printf("BEGIN\n"); //PySys_SetPath("/home/pi/opencv"); //printf("BEGIN\n"); pName = PyString_FromString(argv[1]); printf("BEGIN\n"); pModule = PyImport_Import(pName); printf("BEGIN\n"); pDict = PyModule_GetDict(pModule); printf("BEGIN\n"); pFunc = PyDict_GetItemString(pDict, argv[2]); printf("END\n"); Mat objects[4]; Mat object1 = imread( "/home/pi/opencv/stockPhotos/sstock1.jpg", CV_LOAD_IMAGE_GRAYSCALE ); Mat object2 = imread( "/home/pi/opencv/stockPhotos/sstock3.jpg", CV_LOAD_IMAGE_GRAYSCALE ); Mat object3 = imread( "/home/pi/opencv/stockPhotos/phone3.jpg", CV_LOAD_IMAGE_GRAYSCALE ); Mat object4 = imread( "/home/pi/opencv/stockPhotos/phone2.jpg", CV_LOAD_IMAGE_GRAYSCALE ); objects[0] = object1; objects[1] = object2; objects[2] = object3; objects[3] = object4; int minHessian = 600; SurfFeatureDetector detector( minHessian ); vector<KeyPoint> kp_object; SurfDescriptorExtractor extractor; Mat des_object[4]; FlannBasedMatcher matcher; Mat object; for ( int k = 0 ; k < 4 ; k++ ) { object = objects[k]; detector.detect( object, kp_object ); extractor.compute( object, kp_object, des_object[k]); } bool noMatch = true; CvPoint offset = cvPoint(0,0); while(noMatch){ PyObject_CallObject(pFunc, NULL); noMatch = findMatch(offset, matcher, detector, extractor, des_object); } printf("Offset from the center:\nx = %d pixels to the right\ny = %d pixels below\n", offset.x, offset.y); cout << offset.x << "," << offset.y << endl; Py_DECREF(pModule); Py_DECREF(pName); Py_Finalize(); return 0; }
int surf_feature() { Mat img_1=imread("./samples/box.png",CV_LOAD_IMAGE_GRAYSCALE);//宏定义时CV_LOAD_IMAGE_GRAYSCALE=0,也就是读取灰度图像 Mat img_2=imread("./samples/box_in_scene.png",CV_LOAD_IMAGE_GRAYSCALE);//一定要记得这里路径的斜线方向,这与Matlab里面是相反的 if(!img_1.data || !img_2.data)//如果数据为空 { cout<<"opencv error:cann't open the image!"<<endl; return -1; } cout<<"open right"<<endl; //第一步,用SURF算子检测关键点 int minHessian=400; SurfFeatureDetector detector(minHessian); vector<KeyPoint> keypoints_1,keypoints_2;//构造2个专门由点组成的点向量用来存储特征点 detector.detect(img_1,keypoints_1);//将img_1图像中检测到的特征点存储起来放在keypoints_1中 detector.detect(img_2,keypoints_2);//同理 //在图像中画出特征点 Mat img_keypoints_1,img_keypoints_2; drawKeypoints(img_1,keypoints_1,img_keypoints_1,Scalar::all(-1),DrawMatchesFlags::DEFAULT); drawKeypoints(img_2,keypoints_2,img_keypoints_2,Scalar::all(-1),DrawMatchesFlags::DEFAULT); imshow("surf_keypoints_1",img_keypoints_1); imshow("surf_keypoints_2",img_keypoints_2); //计算特征向量 SurfDescriptorExtractor extractor;//定义描述子对象 Mat descriptors_1,descriptors_2;//存放特征向量的矩阵 extractor.compute(img_1,keypoints_1,descriptors_1); extractor.compute(img_2,keypoints_2,descriptors_2); //用burte force进行匹配特征向量 BruteForceMatcher<L2<float>>matcher;//定义一个burte force matcher对象 vector<DMatch>matches; matcher.match(descriptors_1,descriptors_2,matches); //绘制匹配线段 Mat img_matches; drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches);//将匹配出来的结果放入内存img_matches中 //显示匹配线段 imshow("surf_Matches",img_matches);//显示的标题为Matches waitKey(0); return 0; }
vector< Mat > PaperUtil::getDescriptorsFromKP(vector<Mat> templates, vector< vector<KeyPoint> > key_points){ SurfDescriptorExtractor extractor; vector< Mat > descriptor_objects; for(vector<int>::size_type i = 0; i != templates.size(); i++) { Mat des_object, eq_template; extractor.compute( templates[i], key_points[i], des_object ); descriptor_objects.push_back(des_object); std::cout << des_object.size() << endl; } return descriptor_objects; }
int main(int argc, char** argv) { help(); Mat img1 = imread(argv[1], IMREAD_GRAYSCALE); Mat img2 = imread(argv[2], IMREAD_GRAYSCALE); if(img1.empty() || img2.empty() || argc<2) { printf("Can't read one of the images\n"); return -1; } // detecting keypoints SurfFeatureDetector detector(5000); vector<KeyPoint> keypoints1, keypoints2; detector.detect(img1, keypoints1); detector.detect(img2, keypoints2); // computing descriptors SurfDescriptorExtractor extractor; Mat descriptors1, descriptors2; extractor.compute(img1, keypoints1, descriptors1); extractor.compute(img2, keypoints2, descriptors2); // matching descriptors BFMatcher matcher(NORM_L2); vector<DMatch> matches; matcher.match(descriptors1, descriptors2, matches); // drawing the results namedWindow("matches", 1); Mat img_matches; drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches); imshow("matches", img_matches); // extract points vector<Point2f> pts1, pts2; for (size_t ii=0; ii<keypoints1.size(); ii++) pts1.push_back( keypoints1[ii].pt ); for (size_t ii=0; ii<keypoints2.size(); ii++) pts2.push_back( keypoints2[ii].pt ); // Apply TPS Ptr<ThinPlateSplineShapeTransformer> mytps = createThinPlateSplineShapeTransformer(25000); //TPS with a relaxed constraint mytps->estimateTransformation(pts1, pts2, matches); mytps->warpImage(img2, img2); imshow("Tranformed", img2); waitKey(0); return 0; }
int main( int argc, char** argv ) { if( argc != 3 ) { return -1; } Mat img_1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE ); Mat img_2 = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE ); if( !img_1.data || !img_2.data ) { std::cout<< " --(!) Error reading images " << std::endl; return -1; } Mat img1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE); Mat img2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE); // detecting keypoints FastFeatureDetector detector(15); vector<KeyPoint> keypoints1; detector.detect(img1, keypoints1); // do the same for the second image FastFeatureDetector detector2(15); vector<KeyPoint> keypoints2; detector2.detect(img2, keypoints2); // computing descriptors SurfDescriptorExtractor extractor; Mat descriptors1; extractor.compute(img1, keypoints1, descriptors1); // process keypoints from the second image as well SurfDescriptorExtractor extractor2; Mat descriptors2; extractor2.compute(img2, keypoints2, descriptors2); // matching descriptors BruteForceMatcher<L2<float> > matcher; vector<DMatch> matches; matcher.match(descriptors1, descriptors2, matches); // drawing the results namedWindow("matches", 1); Mat img_matches; drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches); imshow("matches", img_matches); waitKey(0); }
std::vector<DMatch> Match::vecMatches(Mat * img1, Mat * img2, Mat &descriptors_object, vector<KeyPoint> &keypoints_object, vector<KeyPoint> &keypoints_scene) { Mat img_object = *img1; Mat img_scene = *img2; /* if( !img_object.data || !img_scene.data ) { std::cout<< " --(!) Error reading images " << std::endl; return -1; } */ //-- Step 1: Detect the keypoints using SURF Detector int minHessian = 400; SurfFeatureDetector detector(minHessian); //std::vector<KeyPoint> keypoints_object, keypoints_scene; detector.detect(img_object, keypoints_object); //TODO: use the third argument here? from previous match? detector.detect(img_scene, keypoints_scene); //-- Step 2: Calculate descriptors (feature vectors) SurfDescriptorExtractor extractor; Mat /*descriptors_object,*/ descriptors_scene; extractor.compute(img_object, keypoints_object, descriptors_object); extractor.compute(img_scene, keypoints_scene, descriptors_scene); //-- Step 3: Matching descriptor vectors using FLANN matcher FlannBasedMatcher matcher; std::vector<DMatch> matches; matcher.match(descriptors_object, descriptors_scene, matches); return matches; }
int main(int argc, char** argv) { if(argc != 3) { help(); return -1; } Mat img1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE); Mat img2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE); if(img1.empty() || img2.empty()) { printf("Can't read one of the images\n"); return -1; } // detecting keypoints SurfFeatureDetector detector(400); vector<KeyPoint> keypoints1, keypoints2; detector.detect(img1, keypoints1); detector.detect(img2, keypoints2); // computing descriptors SurfDescriptorExtractor extractor; Mat descriptors1, descriptors2; extractor.compute(img1, keypoints1, descriptors1); extractor.compute(img2, keypoints2, descriptors2); // matching descriptors BFMatcher matcher(NORM_L2); vector<DMatch> matches; matcher.match(descriptors1, descriptors2, matches); // drawing the results namedWindow("matches", 1); Mat img_matches; drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches); imshow("matches", img_matches); cout << "descriptors1 = " << descriptors1.rows << " " << descriptors1.cols << endl; cout << "descriptors2 = " << descriptors2.rows << " " << descriptors2.cols << endl; cout << "matches = " << matches.size() << "\nimg_matches = " << img_matches.rows << " " << img_matches.cols << endl; waitKey(0); return 0; }
//Realiza el Matching entre puntos void computeMatching(Mat& img1, Mat& img2,vector<KeyPoint>& keypoints1,vector<KeyPoint>& keypoints2, vector<DMatch>& matches ){ // computing descriptors #if _SURF_ SurfDescriptorExtractor extractor; #else if _SIFT_ SiftDescriptorExtractor extractor; #endif Mat descriptors1, descriptors2; extractor.compute(img1, keypoints1, descriptors1); extractor.compute(img2, keypoints2, descriptors2); FlannBasedMatcher matcher; matcher.match(descriptors1,descriptors2,matches); double max_dist = 0; double min_dist = 100; //-- Quick calculation of max and min distances between keypoints for( int i = 0; i < descriptors1.rows; i++ ){ double dist = matches[i].distance; if( dist < min_dist ) min_dist = dist; if( dist > max_dist ) max_dist = dist; } printf("-- Max dist : %f \n", max_dist ); printf("-- Min dist : %f \n", min_dist ); //-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist ) //-- PS.- radiusMatch can also be used here. std::vector< DMatch > good_matches; for( int i = 0; i < descriptors1.rows; i++ ){ if( matches[i].distance < 2*min_dist ){ good_matches.push_back( matches[i]); } } //-- Draw only "good" matches Mat img_matches; drawMatches( img1, keypoints1, img2, keypoints2, good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS ); //-- Show detected matches imshow( "Good Matches", img_matches ); }
/** @function main */ int main( int argc, char** argv ) { if( argc != 3 ) { return -1; } Mat img_1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE ); Mat img_2 = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE ); if( !img_1.data || !img_2.data ) { return -1; } //-- Step 1: Detect the keypoints using SURF Detector int minHessian = 5000; SurfFeatureDetector detector( minHessian ); std::vector<KeyPoint> keypoints_1, keypoints_2; detector.detect( img_1, keypoints_1 ); detector.detect( img_2, keypoints_2 ); //-- Step 2: Calculate descriptors (feature vectors) SurfDescriptorExtractor extractor; Mat descriptors_1, descriptors_2; extractor.compute( img_1, keypoints_1, descriptors_1 ); extractor.compute( img_2, keypoints_2, descriptors_2 ); //-- Step 3: Matching descriptor vectors with a brute force matcher BFMatcher matcher(NORM_L2); std::vector< DMatch > matches; matcher.match( descriptors_1, descriptors_2, matches ); //-- Draw matches Mat img_matches; drawMatches( img_1, keypoints_1, img_2, keypoints_2, matches, img_matches ); //-- Show detected matches imshow("Matches", img_matches ); waitKey(0); return 0; }
void get_keypoints_and_descriptors(const Mat &frame, Mat &descriptors, vector<KeyPoint> &keypoints) { detector.detect(frame, keypoints); extractor.compute(frame, keypoints, descriptors); /* cout << "keypoints detected: " << keypoints.size() << endl; for (int i = 0; i < keypoints.size(); i++) { cout << "\t" << keypoints[i].pt.x << ", " << keypoints[i].pt.y << " -- " << keypoints[i].angle << " : " << keypoints[i].size << " -- " << keypoints[i].response << endl; cout << "\t\t(" << descriptors.rows << ", " << descriptors.cols << ") "; for (int j = 0; j < descriptors.cols; j++) { cout << " " << descriptors.at<float>(i,j); } cout << endl; } cout << endl; */ }
void tryFindImage_features(Mat input) { /* Сравниваем входящее изрображение с набором эталонов и выбираем наиболее подходящее */ resize(input, input, Size(SIGN_SIZE, SIGN_SIZE), 0, 0); vector<KeyPoint> keyPoints; _detector.detect(input, keyPoints); Mat descriptors; _extractor.compute(input, keyPoints, descriptors); int max_value = 0, max_position = 0; for(int i=0; i < 5; i++) { vector< vector<DMatch> > matches; _matcher.knnMatch(descriptors, _train_descriptors[i], matches, 50); int good_matches_count = 0; for (size_t j = 0; j < matches.size(); ++j) { if (matches[j].size() < 2) continue; const DMatch &m1 = matches[j][0]; const DMatch &m2 = matches[j][1]; if(m1.distance <= 0.7 * m2.distance) good_matches_count++; } if(good_matches_count > max_value) { max_value = good_matches_count; max_position = i; } } cout << STATUS_STR << "Detected sign: " << _train_sign_names[max_position] << endl; }
int main(int argc, char** argv) { if(argc != 3) { help(); return -1; } Mat img1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE); Mat img2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE); if(img1.empty() || img2.empty()) { printf("Can't read one of the images\n"); return -1; } // detecting keypoints SurfFeatureDetector detector(400); vector<KeyPoint> keypoints1, keypoints2; detector.detect(img1, keypoints1); detector.detect(img2, keypoints2); // computing descriptors SurfDescriptorExtractor extractor; Mat descriptors1, descriptors2; extractor.compute(img1, keypoints1, descriptors1); extractor.compute(img2, keypoints2, descriptors2); // matching descriptors BruteForceMatcher<L2<float> > matcher; vector<DMatch> matches; matcher.match(descriptors1, descriptors2, matches); // drawing the results namedWindow("matches", 1); Mat img_matches; drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches); imshow("matches", img_matches); waitKey(0); return 0; }
Mat find_next_homography(Mat image, Mat image_next, vector<KeyPoint> keypoints_0, Mat descriptors_0, SurfFeatureDetector detector, SurfDescriptorExtractor extractor, BFMatcher matcher, vector<KeyPoint>& keypoints_next, Mat& descriptors_next) { //step 1 detect feature points in next image vector<KeyPoint> keypoints_1; detector.detect(image_next, keypoints_1); Mat img_keypoints_surf0, img_keypoints_surf1; drawKeypoints(image, keypoints_0, img_keypoints_surf0); drawKeypoints(image_next, keypoints_1, img_keypoints_surf1); //cout << "# im0 keypoints" << keypoints_0.size() << endl; //cout << "# im1 keypoints" << keypoints_1.size() << endl; imshow("surf 0", img_keypoints_surf0); imshow("surf 1", img_keypoints_surf1); //step 2: extract feature descriptors from feature points Mat descriptors_1; extractor.compute(image_next, keypoints_1, descriptors_1); //step 3: feature matching //cout << "fd matching" << endl; vector<DMatch> matches; vector<Point2f> matched_0; vector<Point2f> matched_1; matcher.match(descriptors_0, descriptors_1, matches); Mat img_feature_matches; drawMatches(image, keypoints_0, image_next, keypoints_1, matches, img_feature_matches ); imshow("Matches", img_feature_matches); for (int i = 0; i < matches.size(); i++ ) { matched_0.push_back(keypoints_0[matches[i].queryIdx].pt); matched_1.push_back(keypoints_1[matches[i].trainIdx].pt); } keypoints_next = keypoints_1; descriptors_next = descriptors_1; return findHomography(matched_0, matched_1, RANSAC); }
static void match(Mat& img1, Mat& img2, Mat& depth1, Mat& depth2) { SurfFeatureDetector detector(400); vector<KeyPoint> keypoint1, keypoint2; detector.detect(img1, keypoint1); detector.detect(img2, keypoint2); SurfDescriptorExtractor extractor; Mat des1, des2; extractor.compute(img1, keypoint1, des1); extractor.compute(img2, keypoint2, des2); FlannBasedMatcher matcher; vector< vector< DMatch > > matches; matcher.radiusMatch(des1, des2, matches, 0.2); Mat key = img1; Mat space( 1024, 640 , CV_8UC3, CV_RGB(0, 0, 0)); vector< Point > ref1, ref2; for(vector< vector<DMatch> >::iterator iter = matches.begin(); iter != matches.end(); iter++) { if( iter -> size() > 0) { DMatch match = iter->at(0); line( key, keypoint1[match.queryIdx].pt, keypoint2[match.trainIdx].pt, CV_RGB(255,0,0), 1); if( depth1.at<uint16_t>(keypoint1[match.queryIdx].pt) / 10 > 0 ) { circle( space, Point( depth1.at<uint16_t>(keypoint1[match.queryIdx].pt) / 10, keypoint1[match.queryIdx].pt.x), 5, CV_RGB(255, 0, 0)); } if( depth1.at<uint16_t>(keypoint1[match.queryIdx].pt) > 0 && depth2.at<uint16_t>(keypoint2[match.trainIdx].pt) > 0) { ref1.push_back( Point( depth1.at<uint16_t>(keypoint1[match.queryIdx].pt), keypoint1[match.queryIdx].pt.x) ); ref2.push_back( Point( depth2.at<uint16_t>(keypoint2[match.trainIdx].pt), keypoint2[match.trainIdx].pt.x) ); } } } imshow( "keypoint", key); imshow( "space", space); }
KDvoid SURF_Descriptor ( KDint nIdx ) { Mat tDst; Mat tImg1; Mat tImg2; tImg1 = imread ( "/res/image/box.png", CV_LOAD_IMAGE_GRAYSCALE ); tImg2 = imread ( "/res/image/box_in_scene.png", CV_LOAD_IMAGE_GRAYSCALE ); // -- Step 1: Detect the keypoints using SURF Detector KDint nMinHessian = 400; SurfFeatureDetector tDetector ( nMinHessian ); std::vector<KeyPoint> aKeypoints1, aKeypoints2; tDetector.detect ( tImg1, aKeypoints1 ); tDetector.detect ( tImg2, aKeypoints2 ); // -- Step 2: Calculate descriptors (feature vectors) SurfDescriptorExtractor tExtractor; Mat tDescriptors1, tDescriptors2; tExtractor.compute ( tImg1, aKeypoints1, tDescriptors1 ); tExtractor.compute ( tImg2, aKeypoints2, tDescriptors2 ); /* // -- Step 3: Matching descriptor vectors with a brute force matcher BruteForceMatcher< L2<KDfloat> > tMatcher; std::vector< DMatch > aMatches; tMatcher.match ( tDescriptors1, tDescriptors2, aMatches ); // -- Draw matches drawMatches ( tImg1, aKeypoints1, tImg2, aKeypoints2, aMatches, tDst ); g_pController->setFrame ( 0, tDst ); */ }
int SfM_Frame::detectKeypoints(void) { string fd_name = "SURF"; double SURF_minHessian = 50; FeatureDetector *detector; SurfDescriptorExtractor *extractor; // load parameters fd_name = svar.GetString("kp_detector", "SURF"); SURF_minHessian = svar.GetInt("kp_SURF_minHessian", 50); ///////////////////////////////////////////////// /// create feature detector ///////////////////////////////////////////////// if( fd_name == "SURF" ) { detector = new SurfFeatureDetector( SURF_minHessian ); } else if ( fd_name == "FAST" ) { detector = new FastFeatureDetector(); } else if ( fd_name == "PyramidFAST" ) { //detector = FeatureDetector::create("PyramidFAST"); } else if ( fd_name == "SIFT" ) { detector = new SiftFeatureDetector; } extractor = new SurfDescriptorExtractor(48, 12, true); detector->detect(imgGray, kpRaw); extractor->compute(imgGray, kpRaw, kpDesc); delete extractor; delete detector; return 0; }