ORB特征匹配——图片序列

本文档介绍了如何使用ORB特征进行图片序列匹配。首先,下载并解压kitti_Image数据集,接着在项目文件夹下创建computeORB2.cpp文件并编写代码。经过编译与运行,展示了特征匹配图以及如何将结果图片序列转换为GIF格式。
摘要由CSDN通过智能技术生成

一、下载并解压kitti_Image数据集

在这里插入图片描述
我放到了这次完成项目的文件夹下

二、代码部分

1.在目录下新建一个computeORB2.cpp文件并写入代码

gedit computeORB2.cpp

完整代码如下:

#include <opencv2/opencv.hpp>
 #include <iostream>
#include <vector>
#include <string>
#include<sstream>
#include <fstream>
using namespace cv;
using namespace std;
// global variables
const double pi = 3.1415926;    // pi
// TODO implement this function
/**
 * compute the angle for ORB descriptor
 * @param [in] image input image
 * @param [in|out] detected keypoints
 */
void computeAngle(const cv::Mat &image, vector<cv::KeyPoint> &keypoints);
// TODO implement this function
/**
 * compute ORB descriptor
 * @param [in] image the input image
 * @param [in] keypoints detected keypoints
 * @param [out] desc descriptor
 */
typedef vector<bool> DescType;  // type of descriptor, 256 bools
void computeORBDesc(const cv::Mat &image, vector<cv::KeyPoint> &keypoints, vector<DescType> &desc); 
// TODO implement this function
/**
 * brute-force match two sets of descriptors
 * @param desc1 the first descriptor
 * @param desc2 the second descriptor
 * @param matches matches of two images
 */
void bfMatch(const vector<DescType> &desc1, const vector<DescType> &desc2, vector<cv::DMatch> &matches);
 
int main(int argc, char **argv) {
   
    std::string pattern_jpg ="PATH";
    std::vector<cv::String> image_files;
    cv::glob(pattern_jpg, image_files);
    for (unsigned int frame = 0; frame < image_files.size(); ++frame) {
   //image_file.size()代表文件中总共的图片个数
		//imshow("1", image);
		//imshow("2", image1);
		//waitKey(30);
          // load image
        cv::Mat first_image = cv::imread(image_files[frame], 0);    // load grayscale image
        cv::Mat second_image = cv::imread(image_files[frame+1], 0);  // load grayscale image
 
    // plot the image
        //cv::imshow("first image", first_image);
        //cv::imshow("second image", second_image);
        //cv::waitKey(0);
    // detect FAST keypoints using threshold=40
        vector<cv::KeyPoint> keypoints;
        cv::FAST(first_image, keypoints, 40);
        cout << "keypoints: " << keypoints.size() << endl;
 
    // compute angle for each keypoint
        computeAngle(first_image, keypoints);
 
    // compute ORB descriptors
         vector<DescType> descriptors;
        computeORBDesc(first_image, keypoints, descriptors);
 
    // plot the keypoints
        cv::Mat image_show;
        cv::drawKeypoints(first_image, keypoints, image_show, cv::Scalar::all(-1),
                      cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
        string a="feature";
        string b=".png";
        stringstream ss;
        ss<<a<<frame<<b;
       // cv::imshow(ss.str(), image_show);
      
        cv::imwrite(ss.str(), image_show);
        cv::waitKey(0);
 
    // we can also match descriptors between images
    // same for the second
        vector<cv::KeyPoint> keypoints2;
        cv::FAST(second_image, keypoints2, 40);
        cout << "keypoints: " << keypoints2.size() << endl;
 
    // compute angle for each keypoint
        computeAngle(second_image, keypoints2);
 
    // compute ORB descriptors
        vector<DescType> descriptors2;
        computeORBDesc(second_image, keypoints2, descriptors2);
 
    // find matches
        vector<cv::DMatch> matches;
        bfMatch(descriptors, descriptors2, matches);
        if (matches.size()<4)
        {
   
            cout<<"匹配点过少!"<<endl;
        }
        cout << "matches: " << matches.size() << endl;
 
    // plot the matches
        cv::drawMatches(first_image, keypoints, second_image, keypoints2, matches, image_show);
        string c="matches";
        string d=".png";
        stringstream sb;
        sb<<c<<frame<<d;
        cv::imshow(sb.str(), image_show);
        cv::imwrite(sb.str(), image_show);
        cv::waitKey(0);
 
        cout << "done." << endl;
        cout<<endl;
	}
 
    return 0;
}
 
// ----------------------------------------------------//
 
// compute the angle
void computeAngle(const cv::Mat &image, vector<cv::KeyPoint> &keypoints) {
   
    int half_patch_size = 8;
    for (auto &kp : keypoints) {
   
    // START YOUR CODE HERE (~7 lines)
//        kp.angle = 0; // compute kp.angle
        double m10 = 0;
        double m01 = 0;
        int x =cvRound(kp.pt.x);
        int y =cvRound(kp.pt.y);
        if(x-half_patch_size<0||x+half_patch_size>image.cols||
           y-half_patch_size<0||y+half_patch_size>image.rows)
            continue;
        for(int u = x - half_patch_size;u<x + half_patch_size;++u)
        {
   
            for(int v = y -half_patch_size;v< y + half_patch_size;++v)
            {
   
                m10 +=  (u-x)*image.at<uchar>(v,u);
                m01 +=  (v-y)*image.at<uchar>(v,u);
            }
        }
 
        double theta = std::atan(m01/m10);
        kp.angle = theta * 180/pi;
        cout<<"kp.angel:"<<kp.angle<<endl;
//         END YOUR CODE HERE
    }
    return;
}
 
// ------------------------------------------- //
// ORB pattern
int ORB_pattern[256 * 4] = {
   
        8, -3, 9, 5/*mean (0), correlation (0)*/,
        4, 2, 7, -12/*mean (1.12461e-05), correlation (0.0437584)*/,
        -11, 9, -8, 2/*mean (3.37382e-05), correlation (0.0617409)*/,
        7, -12, 12, -13/*mean (5.62303e-05), correlation (0.0636977)*/,
        2, -13, 2, 12/*mean (0.000134953), correlation (0.085099)*/,
        1, -7, 1, 6/*mean (0.000528565), correlation (0.0857175)*/,
        -2, -10, -2, -4/*mean (0.0188821), correlation (0.0985774)*/,
        -13, -13, -11, -8/*mean (0.0363135), correlation (0.0899616)*/,
        -13, -3, -12, -9/*mean (0.121806), correlation (0.099849)*/,
        10, 4, 11, 9/*mean (0.122065), correlation (0.093285)*/,
        -13, -8, -8, -9/*mean (0.162787), correlation (0.0942748)*/,
        -11, 7, -9, 12/*mean (0.21561), correlation (0.0974438)*/,
        7, 7, 12, 6/*mean (0.160583), correlation (0.130064)*/,
        -4, 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值