opencv 特征匹配

        opencv的特征匹配,大家应该都很熟悉,网上资料也很多,不了解的上网一查就知道了,这里的不具体说明,因老是忘记怎么使用,把代码粘贴出来方便查看

int main()
{
    VideoCapture cap(0);
    Mat frame;
    Mat save;
    if (frame.empty())
    {
        cout << "frame is empty!" << endl;
    }
    Ptr<FeatureDetector> fast = FastFeatureDetector::create(20); // fast特征提取方式
    //Ptr<DescriptorExtractor> descriptor = SIFT::create();  // 特征提取方式1
    Ptr<BRISK> descriptor = BRISK::create();  // 特征提取方式2
    //Ptr<DescriptorMatcher> bfmatcher = DescriptorMatcher::create("BruteForce-Hamming");  // 暴力匹配方式1
    Ptr<BFMatcher> bfmatcher = BFMatcher::create();  // 暴力匹配方式2
    
    vector<KeyPoint> frame_points;
    Mat frame_descs;
    vector<DescType> orb_descs;
    clock_t t1;

    vector<KeyPoint> temp_points;
    Mat temp_descs;
    Mat temp = imread("temp_image_5.jpg");
    descriptor->detect(temp, temp_points);
    descriptor->compute(temp, temp_points, temp_descs);
    Mat temp_result;
    drawKeypoints(temp, temp_points, temp_result);
    imshow("temp_result", temp_result);

    int num = 0;
    while (1)
    {
        t1 = clock();
        cap >> frame;
        Rect box(12, 12, 122, 111);
        save = frame(box);
        imshow("save", save);
        if (num > 10)
        {
            frame_points.clear();
            descriptor->detect(frame, frame_points);
            descriptor->compute(frame, frame_points, frame_descs);
            drawKeypoints(frame, frame_points, frame);   // 画特征点
            //brisk->detectAndCompute(frame, cv::Mat(), points, descs);

            vector<vector<DMatch> > matches;
            vector<Point2d> temp_pos;
            vector<Point2d> img_pos;
            vector<vector<DMatch> > goodmatches;
            bfmatcher->knnMatch(temp_descs, frame_descs, matches, 2);
            for (int i = 0; i < matches.size(); i++)   // 过滤特征点
            {
                if (matches[i][0].distance < 0.75 * matches[i][1].distance)
                {
                    goodmatches.push_back(matches[i]);
                    temp_pos.push_back(temp_points[matches[i][0].queryIdx].pt);
                    img_pos.push_back(frame_points[matches[i][0].trainIdx].pt);
                }
            }
            cout << "the good points num is : " << temp_pos.size() << " " << img_pos.size() << " " << endl;
            Mat matches_image;
            drawMatches(temp, temp_points, frame, frame_points, goodmatches, matches_image);   // 画匹配结果
            imshow("matches_image", matches_image);
        }
        

        char key = waitKey(1);
        if (key == 'q')
        {
            break;
        }
        num++;
        imshow("frame", frame);
        
        cout << frame_points.size() << endl;
        cout << (double)(clock() - t1) * 1000 / CLOCKS_PER_SEC << "ms" << endl;
    }
    return 0;
}

 fast特征提取实现:

void fast_feature_detect(cv::Mat src_image, int threshold, std::vector<cv::Point>& fast_points, bool drawflag)
{
	cv::Mat image = src_image.clone();
	cv::Mat fastimg(image.size(), CV_8U, cv::Scalar(0)); // 用于保存fast特征点候选点
	cv::Mat fastscore(image.size(), CV_32F, cv::Scalar(0));// 用于计算候选点score

	int rows, cols;
	rows = image.rows;
	cols = image.cols;
	for (int x = 3; x < rows - 3; x++)
	{
		for (int y = 3; y < cols - 3; y++)
		{
			uchar delta[16] = { 0 };
			uchar diff[16] = { 0 };
			delta[0] = (diff[0] = abs(image.at<uchar>(x, y) - image.at<uchar>(x, y - 3))) > threshold;
			delta[8] = (diff[8] = abs(image.at<uchar>(x, y) - image.at<uchar>(x, y + 3))) > threshold;
			if (getSum(delta, 16) != 2)
				continue;
			else
			{
				delta[12] = (diff[12] = abs(image.at<uchar>(x, y) - image.at<uchar>(x - 3, y))) > threshold;
				delta[4] = (diff[4] = abs(image.at<uchar>(x, y) - image.at<uchar>(x + 3, y))) > threshold;
				if (getSum(delta, 16) < 3)
					continue;
				else
				{
					delta[1] = (diff[1] = abs(image.at<uchar>(x, y) - image.at<uchar>(x + 1, y - 3))) > threshold;
					delta[2] = (diff[2] = abs(image.at<uchar>(x, y) - image.at<uchar>(x + 2, y - 2))) > threshold;
					delta[3] = (diff[3] = abs(image.at<uchar>(x, y) - image.at<uchar>(x + 3, y - 1))) > threshold;
					delta[5] = (diff[5] = abs(image.at<uchar>(x, y) - image.at<uchar>(x + 3, y + 1))) > threshold;
					delta[6] = (diff[6] = abs(image.at<uchar>(x, y) - image.at<uchar>(x + 2, y + 2))) > threshold;
					delta[7] = (diff[7] = abs(image.at<uchar>(x, y) - image.at<uchar>(x + 1, y + 3))) > threshold;
					delta[9] = (diff[9] = abs(image.at<uchar>(x, y) - image.at<uchar>(x - 1, y + 3))) > threshold;
					delta[10] = (diff[10] = abs(image.at<uchar>(x, y) - image.at<uchar>(x - 2, y + 2))) > threshold;
					delta[11] = (diff[11] = abs(image.at<uchar>(x, y) - image.at<uchar>(x - 3, y + 1))) > threshold;
					delta[13] = (diff[13] = abs(image.at<uchar>(x, y) - image.at<uchar>(x - 3, y - 1))) > threshold;
					delta[14] = (diff[14] = abs(image.at<uchar>(x, y) - image.at<uchar>(x - 2, y - 2))) > threshold;
					delta[15] = (diff[15] = abs(image.at<uchar>(x, y) - image.at<uchar>(x - 1, y - 3))) > threshold;
					if (getSum(delta, 16) >= 12)
					{
						fast_points.push_back(cv::Point(y, x));
						fastscore.at<float>(x, y) = getSum(diff, 16);
						fastimg.at<uchar>(x, y) = 255;
					}
				}
			}
		}
	}
	if (drawflag)
	{
		std::vector<cv::Point>::const_iterator itp = fast_points.begin();
		while (itp != fast_points.end())
		{
			circle(image, *itp, 3, cv::Scalar(255), 1);
			++itp;
		}
		imshow("fast_image", image);
        waitKey(5);
	}
	
	return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值