OpenCV 2.2 を使用した SIFT 実装の例のリンクを知っている人はいますか。よろしく、
6 に答える
以下は最小限の例です。
#include <opencv/cv.h>
#include <opencv/highgui.h>
int main(int argc, const char* argv[])
{
const cv::Mat input = cv::imread("input.jpg", 0); //Load as grayscale
cv::SiftFeatureDetector detector;
std::vector<cv::KeyPoint> keypoints;
detector.detect(input, keypoints);
// Add results to image and save.
cv::Mat output;
cv::drawKeypoints(input, keypoints, output);
cv::imwrite("sift_result.jpg", output);
return 0;
}
OpenCV 2.3でテスト済み
SIFT 検出器と SIFT ベースの抽出器は、いくつかの方法で取得できます。他の人がより直接的な方法を既に提案しているように、私はコードをより柔軟に変更できるようにする (つまり、他の検出器や抽出器に簡単に変更できる) "ソフトウェア エンジニアリング" アプローチを提供します。
まず、組み込みパラメーターを使用して検出器を取得する場合、OpenCV のファクトリー メソッドを使用して作成するのが最善の方法です。方法は次のとおりです。
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
Mat image = imread("TestImage.jpg");
// Create smart pointer for SIFT feature detector.
Ptr<FeatureDetector> featureDetector = FeatureDetector::create("SIFT");
vector<KeyPoint> keypoints;
// Detect the keypoints
featureDetector->detect(image, keypoints); // NOTE: featureDetector is a pointer hence the '->'.
//Similarly, we create a smart pointer to the SIFT extractor.
Ptr<DescriptorExtractor> featureExtractor = DescriptorExtractor::create("SIFT");
// Compute the 128 dimension SIFT descriptor at each keypoint.
// Each row in "descriptors" correspond to the SIFT descriptor for each keypoint
Mat descriptors;
featureExtractor->compute(image, keypoints, descriptors);
// If you would like to draw the detected keypoint just to check
Mat outputImage;
Scalar keypointColor = Scalar(255, 0, 0); // Blue keypoints.
drawKeypoints(image, keypoints, outputImage, keypointColor, DrawMatchesFlags::DEFAULT);
namedWindow("Output");
imshow("Output", outputImage);
char c = ' ';
while ((c = waitKey(0)) != 'q'); // Keep window there until user presses 'q' to quit.
return 0;
}
ファクトリ メソッドを使用する理由は柔軟です。これは、次のように「作成」ファクトリ メソッドに渡される引数を変更するだけで、別のキーポイント検出器または特徴抽出器 (SURF など) に変更できるためです。
Ptr<FeatureDetector> featureDetector = FeatureDetector::create("SURF");
Ptr<DescriptorExtractor> featureExtractor = DescriptorExtractor::create("SURF");
他の検出器または抽出器を作成するために渡すことができる他の引数については、http: //opencv.itseez.com/modules/features2d/doc/common_interfaces_of_feature_detectors.html#featuredetector-createを参照してください。
ここで、ファクトリ メソッドを使用すると、検出器または抽出器のそれぞれに渡す適切なパラメーターを推測する必要がないという利便性が得られます。これは、それらを初めて使用する人にとって便利です。ただし、独自のカスタム SIFT 検出器を作成する場合は、カスタム パラメーターで作成された SiftDetector オブジェクトをラップしてスマート ポインターにラップし、上記のように featureDetector スマート ポインター変数を使用して参照できます。
opencv 2.4 で SIFT 不自由な機能検出器を使用した簡単な例
#include <opencv2/opencv.hpp>
#include <opencv2/nonfree/nonfree.hpp>
using namespace cv;
int main(int argc, char** argv)
{
if(argc < 2)
return -1;
Mat img = imread(argv[1]);
SIFT sift;
vector<KeyPoint> key_points;
Mat descriptors;
sift(img, Mat(), key_points, descriptors);
Mat output_img;
drawKeypoints(img, key_points, output_img);
namedWindow("Image");
imshow("Image", output_img);
waitKey(0);
destroyWindow("Image");
return 0;
}
opencv2.4でSIFT非フリー機能検出器を使用する別の簡単な例必ずopencv_nonfree240.lib依存関係を追加してください
#include "cv.h"
#include "highgui.h"
#include <opencv2/nonfree/nonfree.hpp>
int main(int argc, char** argv)
{
cv::Mat img = cv::imread("image.jpg");
cv::SIFT sift(10); //number of keypoints
cv::vector<cv::KeyPoint> key_points;
cv::Mat descriptors, mascara;
cv::Mat output_img;
sift(img,mascara,key_points,descriptors);
drawKeypoints(img, key_points, output_img);
cv::namedWindow("Image");
cv::imshow("Image", output_img);
cv::waitKey(0);
return 0;
}
誰かが2つの画像でそれを行う方法を疑問に思っている場合:
import numpy as np
import cv2
print ('Initiate SIFT detector')
sift = cv2.xfeatures2d.SIFT_create()
print ('find the keypoints and descriptors with SIFT')
gcp1, des1 = sift.detectAndCompute(src_img,None)
gcp2, des2 = sift.detectAndCompute(trg_img,None)
# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1,des2)
# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)
#print only the first 100 matches
img3 = drawMatches(src_img, gcp1, trg_img, gcp2, matches[:100])