OpenCV提取SIFT特徵
阿新 • • 發佈:2019-01-05
// FeatureDetector.cpp : Defines the entry point for the console application. // // Created by Rachel on 14-1-12. // Copyright (c) 2013年 ZJU. All rights reserved. // #include "stdafx.h" #include "highgui.h" #include "cv.h" #include "vector" #include "opencv\cxcore.hpp" #include "iostream" #include "opencv.hpp" #include <opencv2/nonfree/features2d.hpp> //#include "nonfree.hpp" //#include "showhelper.h" using namespace cv; using namespace std; int _tmain(int argc, _TCHAR* argv[]) { //Load Image Mat c_src1 = imread( "F:\\Pics\\15.jpg");//OriImg Mat c_src2 = imread("F:\\Pics\\16.jpg");//OriImg Mat src1 = imread( "F:\\Pics\\15.jpg", CV_LOAD_IMAGE_GRAYSCALE);//Grayscale Mat src2 = imread( "F:\\Pics\\16.jpg", CV_LOAD_IMAGE_GRAYSCALE);//Grayscale if( !src1.data || !src2.data ) { std::cout<< " --(!) Error reading images " << std::endl; return -1; } //sift feature detect SiftFeatureDetector detector; std::vector<KeyPoint> kp1, kp2; detector.detect( src1, kp1 ); detector.detect( src2, kp2 ); cout<<"The number of keypoints of Img1: "<<kp1.size()<<endl; cout<<"The number of keypoints of Img2: "<<kp2.size()<<endl; SiftDescriptorExtractor extractor; Mat des1,des2;//descriptor extractor.compute(src1,kp1,des1); extractor.compute(src2,kp2,des2); cout<<"The size of SIFT descriptor of Img1: "<<des1.size()<<endl; cout<<"The size of SIFT descriptor of Img2: "<<des2.size()<<endl; Mat res1,res2; int drawmode = DrawMatchesFlags::DRAW_RICH_KEYPOINTS; drawKeypoints(c_src1,kp1,res1,Scalar::all(-1),drawmode);//在記憶體中畫出特徵點 drawKeypoints(c_src2,kp2,res2,Scalar::all(-1),drawmode); //write the size of features on picture CvFont font; double hScale=1; double vScale=1; int lineWidth=2;// 相當於寫字的線條 cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth);//初始化字型,準備寫到圖片上的 // cvPoint 為起筆的x,y座標 IplImage* transimg1 = cvCloneImage(&(IplImage) res1); IplImage* transimg2 = cvCloneImage(&(IplImage) res2); char str1[20],str2[20]; sprintf(str1,"%d",kp1.size()); sprintf(str2,"%d",kp2.size()); const char* str = str1; cvPutText(transimg1,str1,cvPoint(280,230),&font,CV_RGB(255,0,0));//在圖片中輸出字元 str = str2; cvPutText(transimg2,str2,cvPoint(280,230),&font,CV_RGB(255,0,0));//在圖片中輸出字元 //imshow("Description 1",res1); cvShowImage("descriptor1",transimg1); cvShowImage("descriptor2",transimg2); BFMatcher matcher(NORM_L2); vector<DMatch> matches; matcher.match(des1,des2,matches); Mat img_match; drawMatches(src1,kp1,src2,kp2,matches,img_match);//,Scalar::all(-1),Scalar::all(-1),vector<char>(),drawmode); cout<<"The number of matched points: "<<matches.size()<<endl; imshow("matches",img_match); cvWaitKey(); cvDestroyAllWindows(); return 0; }