天天看點

OpenCV使用brisk做一個簡單的場景比對

作者:音視訊開發老舅

1、概述

  案例:使用opencv+qt+brisk實作關鍵點提前、生成描述子,并使用BFMatcher實作場景對象比對,最後使用drawMatches将比對結果繪制出來

  實作步驟:

      1.使用Qt制作場景對象及場景圖像的選擇頁面,并放置一個按鈕開始檢測

      2.使用imread載入場景圖像及場景對象

      3.執行個體化Brisk,并使用其detectAndCompute方法提取場景圖像及場景對象的關鍵點及描述子

      4.執行個體化BFMatcher,并使用期match方法來比對

      5.繪制出比對結果

2、代碼示例

  1.使用Qt制作場景選擇視窗

this->setWindowTitle("brisk場景對象比對");
    //選擇場景對象
    Custom_Choice_File_Widget *ccfw = new Custom_Choice_File_Widget(this,"場景對象選擇","場景對象");
    ccfw->setFixedSize(320,85);
    //選擇場景圖檔
    Custom_Choice_File_Widget *ccfw2 = new Custom_Choice_File_Widget(this,"場景圖選擇","場景圖");
    ccfw2->move(0,ccfw->height());

    QPushButton *btnCheck = new QPushButton(this);
    btnCheck->setFixedSize(100,47);
    btnCheck->setText("開始檢測");
    btnCheck->move(0,ccfw2->y()+ccfw2->height()+10);
    connect(btnCheck,&QPushButton::clicked,[=](){
        if(ccfw->getFilePath()!=NULL&&ccfw2->getFilePath()!=NULL){
            checkObject(ccfw->getFilePath().toStdString().c_str(),ccfw2->getFilePath().toStdString().c_str());
        }else{
            QMessageBox::warning(this,"溫馨提示","請先選擇場景圖檔和場景對象");
        }

    });           

2.真正的檢測部分

//【1】載入場景圖像及場景對象
    Mat img1 = imread(senceFilePath, IMREAD_GRAYSCALE);//加載場景圖檔
    Mat img2 = imread(objectFilePath, IMREAD_GRAYSCALE);//加載場景對象圖檔
    if (img1.empty() || img2.empty()) {
        printf("could not load images...\n");
        return;
    }
    //先顯示原圖
    imshow("box image", img1);
    imshow("scene image", img2);

   //【2】執行個體化Brisk并調用其detectAndCompute方法提取關鍵點及特征描述子
    Ptr<Feature2D> detector = BRISK::create();
    vector<KeyPoint> keypoints_obj;
    vector<KeyPoint> keypoints_scene;
    Mat descriptor_obj, descriptor_scene;
    detector->detectAndCompute(img1, Mat(), keypoints_obj, descriptor_obj);
    detector->detectAndCompute(img2, Mat(), keypoints_scene, descriptor_scene);

    //【3】執行個體化BFMatcher并調用其matcher.match方法比對特征描述子
    BFMatcher matcher(NORM_L2);
    vector<DMatch> matches;
    matcher.match(descriptor_obj, descriptor_scene, matches);

    //【4】将比對結果繪制出來(關鍵點及特征描述子)
    Mat matchesImg;
    drawMatches(img1, keypoints_obj, img2, keypoints_scene, matches, matchesImg);
    imshow("BRISK MATCH RESULT", matchesImg);           

3、圖檔示範

1.Qt選擇圖像視窗

OpenCV使用brisk做一個簡單的場景比對

2.原圖

OpenCV使用brisk做一個簡單的場景比對

3.檢測結果

OpenCV使用brisk做一個簡單的場景比對
QT開發交流資料群:714620761

繼續閱讀