fanfuhan OpenCV 教學099 ~ opencv-099-SIFT特徵提取之描述子生成 比對/匹配 已知對象 定位/標記

fanfuhan OpenCV 教學099 ~ opencv-099-SIFT特徵提取之描述子生成 比對/匹配 已知對象 定位/標記

fanfuhan OpenCV 教學099 ~ opencv-099-SIFT特徵提取之描述子生成 比對/匹配 已知對象 定位/標記


資料來源: https://fanfuhan.github.io/

https://fanfuhan.github.io/2019/05/16/opencv-099/

GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV


SIFT特徵提取是圖像特徵提取中最經典的一個算法,歸納起來SIFT特徵提取主要有如下幾步:

   △構建高斯多尺度金字塔
   △關鍵點查找/過濾與精準定位
   △窗口區域角度方向直方圖

   △描述子生成


C++

#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <iostream>

using namespace cv;
using namespace cv::xfeatures2d;
using namespace std;

void find_known_object(Mat &box, Mat &box_scene);
int main(int argc, char** argv) {

	Mat box = imread("D:/images/box.bmp");
	Mat scene = imread("D:/images/scene.jpg");
	imshow("box image", box);
	imshow("scene image", scene);
	find_known_object(box, scene);

	//Mat gray;
	//cvtColor(src, gray, COLOR_BGR2GRAY);
	auto detector = SIFT::create();
	vector<KeyPoint> keypoints_box, keypoints_scene;
	Mat descriptor_box, descriptor_scene;
	detector->detectAndCompute(box, Mat(), keypoints_box, descriptor_box);
	detector->detectAndCompute(scene, Mat(), keypoints_scene, descriptor_scene);

	Ptr<FlannBasedMatcher> matcher = FlannBasedMatcher::create();
	vector<DMatch> matches;
	matcher->match(descriptor_box, descriptor_scene, matches);
	Mat dst;
	drawMatches(box, keypoints_box, scene, keypoints_scene, matches, dst);
	imshow("match-demo", dst);
	

	waitKey(0);
	return 0;
}


Python

"""
SIFT特征提取 – 描述子生成
"""

import cv2 as cv

box = cv.imread("D:/images/box.png")
box_in_sence = cv.imread("D:/images/box_in_scene.png")
cv.imshow("box", box)
cv.imshow("box_in_sence", box_in_sence)

# 创建sift特征检测器
sift = cv.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(box,None)
kp2, des2 = sift.detectAndCompute(box_in_sence,None)

# 暴力匹配
bf = cv.DescriptorMatcher_create(cv.DescriptorMatcher_BRUTEFORCE)
matches = bf.match(des1,des2)

# 绘制匹配
matches = sorted(matches, key = lambda x:x.distance)
result = cv.drawMatches(box, kp1, box_in_sence, kp2, matches[:15], None)
cv.imshow("orb-match", result)
cv.waitKey(0)
cv.destroyAllWindows()

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *