Skip to content

Instantly share code, notes, and snippets.

@kazoo04
Last active October 15, 2020 09:17
Show Gist options
  • Save kazoo04/9224390 to your computer and use it in GitHub Desktop.
Save kazoo04/9224390 to your computer and use it in GitHub Desktop.
メチャクチャわかりやすい cv::DenseFeatureDetector のサンプル
#include <iostream>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#define IMAGE_SIZE 280
int main(int argc, char* argv[])
{
cv::Mat src = cv::imread(argv[1]);
if (src.empty()) {
exit(EXIT_FAILURE);
}
double scale = (double)IMAGE_SIZE / (src.rows < src.cols ? src.cols : src.rows);
cv::Mat img(src.rows * scale, src.cols * scale, src.type());
cv::resize(src, img, cv::Size(), scale, scale);
cv::DenseFeatureDetector detector(
8.0f, //initFeatureScale: 初期の特徴のサイズ(半径)[px]
3, //featureScaleLevels: 何段階サイズ変更してしてサンプリングするか(>=1)
1.414f, //featureScaleMul: ScaleLevelごとにどれくらい拡大縮小するか(!=0)
4, //initXyStep: 特徴点をどれくらいの量ずらしながらサンプリングするか
0, //initImgBound: 画像の端からどれくらい離すか(>=0)
false, //varyXyStepWithScale: XyStepにもScaleMul を掛けるか
false //varyImgBoundWithScale: BoundにもScaleMul を掛けるか
);
std::vector<cv::KeyPoint> keypoints;
detector.detect(img, keypoints);
// 記述子として次のいずれかを指定する
// FAST, GFTT, SIFT (nonfree), SURF (nonfree), MSER, STAR, ORB, BRISK, FREAK, BRIEF
cv::Ptr<cv::DescriptorExtractor> extractor = cv::DescriptorExtractor::create("BRISK");
cv::Mat descriptors;
extractor->compute(img, keypoints, descriptors);
if(argc > 3) {
std::ofstream stream(argv[2]);
stream << cv::format(descriptors, "csv");
} else {
std::cout << cv::format(descriptors, "csv") << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment