質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Anaconda

Anacondaは、Python本体とPythonで利用されるライブラリを一括でインストールできるパッケージです。環境構築が容易になるため、Python開発者間ではよく利用されており、商用目的としても利用できます。

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

4956閲覧

SIFT特徴量の使い方、error: 'SiftFeatureDetector' was not declared in this scope

deb

総合スコア17

Anaconda

Anacondaは、Python本体とPythonで利用されるライブラリを一括でインストールできるパッケージです。環境構築が容易になるため、Python開発者間ではよく利用されており、商用目的としても利用できます。

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2019/02/05 08:58

前提・実現したいこと

C++で画像の特徴抽出をするプログラムを書いています。
SIFT特徴量を使って、読み込んだ画像に特徴点を描画するという内容です。
ブログから引用したのでほぼ自分では書いていませんが、OpenCVのバージョンがブログとは違い、ヘッダファイルのある場所が異なっていたので一部を書き換えました。

該当のソースコード

cpp

1///sift_keypoint_output.cpp 2 3#include <iostream> 4#include <opencv2/opencv.hpp> 5#include <opencv2/features2d/features2d.hpp> 6#include <opencv2/highgui/highgui.hpp> 7#include <opencv2/features2d.hpp> 8#include <opencv2/xfeatures2d/nonfree.hpp>//SIFTまたはSURFを使う場合は必要(変更ア) 9using namespace cv; 10using namespace std; 11 12#if _DEBUG 13#pragma comment(lib, "opencv_core2413d.lib") 14#pragma comment(lib, "opencv_features2d2413d.lib") 15#pragma comment(lib, "opencv_highgui2413d.lib") 16#pragma comment(lib, "opencv_imgproc2413d.lib") 17#pragma comment(lib, "opencv_nonfree2413d.lib") 18#else 19#pragma comment(lib, "opencv_core2413.lib") 20#pragma comment(lib, "opencv_features2d2413.lib") 21#pragma comment(lib, "opencv_highgui2413.lib") 22#pragma comment(lib, "opencv_imgproc2413.lib") 23#pragma comment(lib, "opencv_nonfree2413.lib") 24#endif 25 26int main(void) 27{ 28 // 入力画像の取得 29 Mat color_image = imread("miyazawa.png", 1); 30 if (color_image.empty()) { 31 return -1; 32 } 33 34 // カラー画像をグレースケールに変換 35 Mat gray_image; 36 cvtColor(color_image, gray_image, CV_RGB2GRAY); 37 normalize(gray_image, gray_image, 0, 255, NORM_MINMAX); 38 39 // SIFT特徴点の抽出 40 cv::initModule_nonfree();//SIFTまたはSURFを使う場合はこれを呼び出す(変更イ) 41 vector<KeyPoint> keypoints; 42 vector<KeyPoint>::iterator itk; 43 double threshold = 0.05; 44 double edge_threshold = 10.0; 45 SiftFeatureDetector detector(threshold, edge_threshold); 46 detector.detect(gray_image, keypoints); 47 48 // キーポイントの数を表示 49 int keypoint_num = keypoints.size(); 50 cout << "keypoint_num :" << keypoint_num << endl; 51 52 // 結果を表示 53 Mat output_image_1, output_image_2; 54 drawKeypoints(color_image, keypoints, output_image_1, Scalar(0, 255, 0), 0); 55 drawKeypoints(color_image, keypoints, output_image_2, Scalar(0, 255, 0), 4); 56 imshow("Result Keypoint", output_image_1); 57 imshow("Result Keypoint Size and Direction", output_image_2); 58 59 // 結果を保存 60 imwrite("output_image_1.png", output_image_1); 61 imwrite("output_image_2.png", output_image_2); 62 63 waitKey(0); 64}

発生している問題・エラーメッセージ

sift_keypoint_output.cpp: In function 'int main()': sift_keypoint_output.cpp:40:9: error: 'initModule_nonfree' is not a member of 'cv' cv::initModule_nonfree();//SIFT絛RF・1;31m^~~~~~~~~~~~~~~~~~ sift_keypoint_output.cpp:45:2: error: 'SiftFeatureDetector' was not declared in this scope SiftFeatureDetector detector(threshold, edge_threshold); ^~~~~~~~~~~~~~~~~~~ sift_keypoint_output.cpp:45:2: note: suggested alternative: In file included from sift_keypoint_output.cpp:8: \Anaconda3\Library\include\opencv2\opencv_contrib-3.1.0\opencv_contrib-3.1.0\modules\xfeatures2d\include/opencv2/xfeatures2d/nonfree.hpp:84:14: note: 'cv::xfeatures2d::SiftFeatureDetector' typedef SIFT SiftFeatureDetector; ^~~~~~~~~~~~~~~~~~~ sift_keypoint_output.cpp:46:2: error: 'detector' was not declared in this scope detector.detect(gray_image, keypoints); ^~~~~~~~ sift_keypoint_output.cpp:46:2: note: suggested alternative: 'getchar' detector.detect(gray_image, keypoints); ^~~~~~~~ getchar

エラーコード上から4行目の'SiftFeatureDetecter' was not ~~でそれが定義されていないと指摘されました。

試したこと

'SiftFeatureDetecter'が定義されているはずのヘッダファイル'nonfree.hpp'の中身を確認しましたが、問題ないように見えますし、ヘッダファイルのインクルードでのエラーは出ていません。
何が問題なのでしょうか。お力添えお願いします。

cpp

1//nonfree.hpp// 2 3#ifndef __OPENCV_XFEATURES2D_FEATURES_2D_HPP__ 4#define __OPENCV_XFEATURES2D_FEATURES_2D_HPP__ 5 6#include "opencv2/features2d.hpp" 7 8namespace cv 9{ 10namespace xfeatures2d 11{ 12 13//! @addtogroup xfeatures2d_nonfree 14//! @{ 15 16/** @brief Class for extracting keypoints and computing descriptors using the Scale Invariant Feature Transform 17(SIFT) algorithm by D. Lowe @cite Lowe04 . 18 */ 19class CV_EXPORTS_W SIFT : public Feature2D 20{ 21public: 22 /** 23 @param nfeatures The number of best features to retain. The features are ranked by their scores 24 (measured in SIFT algorithm as the local contrast) 25 26 @param nOctaveLayers The number of layers in each octave. 3 is the value used in D. Lowe paper. The 27 number of octaves is computed automatically from the image resolution. 28 29 @param contrastThreshold The contrast threshold used to filter out weak features in semi-uniform 30 (low-contrast) regions. The larger the threshold, the less features are produced by the detector. 31 32 @param edgeThreshold The threshold used to filter out edge-like features. Note that the its meaning 33 is different from the contrastThreshold, i.e. the larger the edgeThreshold, the less features are 34 filtered out (more features are retained). 35 36 @param sigma The sigma of the Gaussian applied to the input image at the octave \#0. If your image 37 is captured with a weak camera with soft lenses, you might want to reduce the number. 38 */ 39 CV_WRAP static Ptr<SIFT> create( int nfeatures = 0, int nOctaveLayers = 3, 40 double contrastThreshold = 0.04, double edgeThreshold = 10, 41 double sigma = 1.6); 42}; 43 44typedef SIFT SiftFeatureDetector; 45typedef SIFT SiftDescriptorExtractor; 46 47/** @brief Class for extracting Speeded Up Robust Features from an image @cite Bay06 . 48 49The algorithm parameters: 50- member int extended 51 - 0 means that the basic descriptors (64 elements each) shall be computed 52 - 1 means that the extended descriptors (128 elements each) shall be computed 53- member int upright 54 - 0 means that detector computes orientation of each feature. 55 - 1 means that the orientation is not computed (which is much, much faster). For example, 56if you match images from a stereo pair, or do image stitching, the matched features 57likely have very similar angles, and you can speed up feature extraction by setting 58upright=1. 59- member double hessianThreshold 60Threshold for the keypoint detector. Only features, whose hessian is larger than 61hessianThreshold are retained by the detector. Therefore, the larger the value, the less 62keypoints you will get. A good default value could be from 300 to 500, depending from the 63image contrast. 64- member int nOctaves 65The number of a gaussian pyramid octaves that the detector uses. It is set to 4 by default. 66If you want to get very large features, use the larger value. If you want just small 67features, decrease it. 68- member int nOctaveLayers 69The number of images within each octave of a gaussian pyramid. It is set to 2 by default. 70@note 71 - An example using the SURF feature detector can be found at 72 opencv_source_code/samples/cpp/generic_descriptor_match.cpp 73 - Another example using the SURF feature detector, extractor and matcher can be found at 74 opencv_source_code/samples/cpp/matcher_simple.cpp 75 */ 76class CV_EXPORTS_W SURF : public Feature2D 77{ 78public: 79 /** 80 @param hessianThreshold Threshold for hessian keypoint detector used in SURF. 81 @param nOctaves Number of pyramid octaves the keypoint detector will use. 82 @param nOctaveLayers Number of octave layers within each octave. 83 @param extended Extended descriptor flag (true - use extended 128-element descriptors; false - use 84 64-element descriptors). 85 @param upright Up-right or rotated features flag (true - do not compute orientation of features; 86 false - compute orientation). 87 */ 88 CV_WRAP static Ptr<SURF> create(double hessianThreshold=100, 89 int nOctaves = 4, int nOctaveLayers = 3, 90 bool extended = false, bool upright = false); 91 92 CV_WRAP virtual void setHessianThreshold(double hessianThreshold) = 0; 93 CV_WRAP virtual double getHessianThreshold() const = 0; 94 95 CV_WRAP virtual void setNOctaves(int nOctaves) = 0; 96 CV_WRAP virtual int getNOctaves() const = 0; 97 98 CV_WRAP virtual void setNOctaveLayers(int nOctaveLayers) = 0; 99 CV_WRAP virtual int getNOctaveLayers() const = 0; 100 101 CV_WRAP virtual void setExtended(bool extended) = 0; 102 CV_WRAP virtual bool getExtended() const = 0; 103 104 CV_WRAP virtual void setUpright(bool upright) = 0; 105 CV_WRAP virtual bool getUpright() const = 0; 106}; 107 108typedef SURF SurfFeatureDetector; 109typedef SURF SurfDescriptorExtractor; 110 111//! @} 112 113} 114} /* namespace cv */ 115 116#endif 117

補足情報(FW/ツールのバージョンなど)

Anaconda 3
OpenCV 3.1.0

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

C++

namespace cv
{
namespace xfeatures2d
{
...
typedef SIFT SiftFeatureDetector;
typedef SIFT SiftDescriptorExtractor;

cv::xfeatures2d::SiftFeatureDetectorでは。


エラーメッセージにも同じようなことが書いてありますね。

sift_keypoint_output.cpp:45:2: error: 'SiftFeatureDetector' was not declared in this scope
SiftFeatureDetector detector(threshold, edge_threshold);
^~~~~~~~~~~~~~~~~~~
sift_keypoint_output.cpp:45:2: note: suggested alternative:
In file included from sift_keypoint_output.cpp:8:
\Anaconda3\Library\include\opencv2\opencv_contrib-3.1.0\opencv_contrib-3.1.0\modules\xfeatures2d\include/opencv2/xfeatures2d/nonfree.hpp:84:14: note: 'cv::xfeatures2d::SiftFeatureDetector'
typedef SIFT SiftFeatureDetector;
^~~~~~~~~~~~~~~~~~~

投稿2019/02/05 10:06

LouiS0616

総合スコア35660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

deb

2019/02/06 03:26

なるほど!名前空間はしっかり意識しないと忘れてしまいますね…。 ご指摘ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問