1. 程式人生 > >0046-OpenCV下的SIFT特徵檢測

0046-OpenCV下的SIFT特徵檢測

一個物體,不管遠近以及角度如何,我們人都能判斷為同一物體,因為我們人腦是根據物體的特徵來判斷是不是同一物體的,計算機在處理影象時,我們也希望具備這樣的能力,所以需要對影象進行特徵提取

本文給出利用OpenCV的SIFT類來進行影象的特徵提取與匹配的程式碼

由於OpenCV3.0沒有編譯nonfree,而SIFT類包含在這個模組中,所以我們使用OpenCV2.4.9。
OpenCV2.4.9的Windows版安裝包下載連結見https://blog.csdn.net/opencv_source/article/details/83785987


程式碼如下
程式碼請加Q2034196302獲取
程式碼請加Q2034196302獲取
程式碼請加Q2034196302獲取


代友中的相關繪製函式說明
drawKeypoints函式,原型如下
C++: void drawKeypoints(const Mat& image, const vector<KeyPoint>& keypoints, Mat& outImage, const Scalar& color=Scalar::all(-1), int flags=DrawMatchesFlags::DEFAULT )
引數意義如下
image:源影象
keypoints:儲存特徵點的向量
outImage:輸出影象
color:特徵點顏色,如果設定為Scalar::all(-1),則顏色隨機。
flags
:繪圖設定的標誌,可能的值如下:
struct DrawMatchesFlags
{
    enum
    {
       DEFAULT = 0, // Output image matrix will be created (Mat::create),
                     // i.e. existing memory of output image may be reused.
                     // Two source images, matches, and single keypoints
                     // will be drawn.
                     // For each keypoint, only the center point will be
                     // drawn (without a circle around the keypoint with the
                     // keypoint size and orientation).
        DRAW_OVER_OUTIMG = 1, // Output image matrix will not be
                       // created (using Mat::create). Matches will be drawn
                       // on existing content of output image.
        NOT_DRAW_SINGLE_POINTS = 2, // Single keypoints(沒有匹配到的點) will not be drawn.
       DRAW_RICH_KEYPOINTS = 4 // For each keypoint, the circle around
                       // keypoint with keypoint size and orientation will
                       // be drawn.
    };
};

drawMatches函式,原型如下:
C++: void drawMatches(const Mat& img1, const vector<KeyPoint>& keypoints1, const Mat& img2, const vector<KeyPoint>& keypoints2, const vector<DMatch>& matches1to2, Mat& outImg, const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1), const vector<char>& matchesMask=vector<char>(), int flags=DrawMatchesFlags::DEFAULT )
C++: void drawMatches(const Mat& img1, const vector<KeyPoint>& keypoints1, const Mat& img2, const vector<KeyPoint>& keypoints2, const vector<vector<DMatch>>& matches1to2, Mat& outImg, const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1), const vector<vector<char>>& matchesMask=vector<vector<char> >(), int flags=DrawMatchesFlags::DEFAULT )
引數意義如下
img1:第一張影象
keypoints1:來自第一張影象的特徵點
img2:第二張影象
keypoints2:來自第二張影象的特徵點
matches1to2:第一張影象向第二張影象的匹配特徵點。
outImg:輸出影象,它的內容取決於flags的設定。
matchColor:匹配圖的顏色設定(特徵點和線的顏色),如果設定為Scalar::all(-1),則顏色隨機。
singlePointColor:沒有匹配到的點的顏色。如果設定為Scalar::all(-1),則顏色隨機。
matchesMask:匹配掩碼,可以通過設定它來設定哪些特徵點被繪製。
flags:繪圖設定的標誌,可能的值如下:
struct DrawMatchesFlags
{
    enum
    {
        DEFAULT = 0, // Output image matrix will be created (Mat::create),
                     // i.e. existing memory of output image may be reused.
                     // Two source images, matches, and single keypoints
                     // will be drawn.
                     // For each keypoint, only the center point will be
                     // drawn (without a circle around the keypoint with the
                     // keypoint size and orientation).
        DRAW_OVER_OUTIMG = 1, // Output image matrix will not be
                       // created (using Mat::create). Matches will be drawn
                       // on existing content of output image.
       NOT_DRAW_SINGLE_POINTS = 2, // Single keypoints(沒有匹配到的點) will not be drawn.
        DRAW_RICH_KEYPOINTS = 4 // For each keypoint, the circle around
                       // keypoint with keypoint size and orientation will
                       // be drawn.
    };
};
執行結果如下圖所示