OpenCV3,SIFT 计算向量<vector<KeyPoint>>

OpenCV3, SIFT compute with vector<vector<KeyPoint>>

根据文档,SIFT 对象可以使用以下函数计算多个图像的描述符:

virtual void compute (InputArrayOfArrays images, std::vector< std::vector< KeyPoint > > &keypoints, OutputArrayOfArrays descriptors)

我正在尝试使用以下代码计算多个图像的 SIFT 描述符:

Ptr<Feature2D> f2d = xfeatures2d::SIFT::create();
vector<vector<KeyPoint>> train_keypoints;
f2d->detect(train_imgs, train_keypoints);

vector<Mat> train_descriptors;
f2d->compute(train_imgs, train_keypoints, train_descriptors);

它可以在 Mac OS 10.10.5 下使用 opencv3 编译,但它可能会在执行期间终止并出现错误:

libc++abi.dylib: terminating with uncaught exception of type std::length_error: vector

或者我可以将 train_descriptors 的类型更改为 Mat (而不是 vector< Mat >),它在执行期间仍然会失败并出现另一个错误:

OpenCV Error: Assertion failed (_descriptors.kind() == _InputArray::STD_VECTOR_MAT) in compute, file /tmp/opencv320151228-32931-2p5ggk/opencv-3.1.0/modules/features2d/src/feature2d.cpp, line 126 libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv320151228-32931-2p5ggk/opencv-3.1.0/modules/features2d/src/feature2d.cpp:126: error: (-215) _descriptors.kind() == _InputArray::STD_VECTOR_MAT in function compute

我应该使用什么类型的 train_descriptors 来使此代码编译并 运行 正确?


谁能告诉我 vectorOutputArrayOfArrays 有什么区别?

我猜这段代码可以工作:

Ptr<Feature2D> f2d = xfeatures2d::SIFT::create();
vector<vector<KeyPoint>> train_keypoints;
f2d->detect(train_imgs, train_keypoints);

vector<Mat> train_descriptors = vector<Mat>(5e4);
f2d->compute(train_imgs, train_keypoints, train_descriptors);

您的代码

Ptr<Feature2D> f2d = xfeatures2d::SIFT::create();
vector<vector<KeyPoint>> train_keypoints;
f2d->detect(train_imgs, train_keypoints);

vector<Mat> train_descriptors;
f2d->compute(train_imgs, train_keypoints, train_descriptors);
如果 train_imgsvector<Mat>

效果很好。


您不需要创建包含 50000 个元素的向量,只需使用 vector<Mat> train_descriptors;


OutputArrayOfArrays,与InputArrayOutputArray等类似,是OpenCV用来允许传递给函数的抽象层cv::Matstd::vector你不应该明确地使用这些 类。 来自 OpenCV doc:

The class is designed solely for passing parameters. That is, normally you should not declare class members, local and global variables of this type.

此外,请注意 OutputArrayOfArrays 只是 OutputArray 的类型定义。