使用 opencv 进行特征检测失败并出现 seg 错误

Ale*_*sie 2 c++ opencv feature-extraction feature-detection

我有以下代码

cv::initModule_nonfree();
std::vector<cv::KeyPoint> keypoints_1;
cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create("SURF");
cv::Mat image = cv::imread("someFileNameHere",cv::IMREAD_COLOR);
// image.data is true, cv::imshow() dispalys the image
detector->detect(image, keypoints_1); // seg fault here
Run Code Online (Sandbox Code Playgroud)

段错误的原因可能是什么?我尝试在它上面运行 gdb,希望库有足够的元数据,但堆栈在调用detect()

小智 13

我在 Python 中遇到了类似的问题:

import cv2
import numpy as np;

params = cv2.SimpleBlobDetector_Params()
detector = cv2.SimpleBlobDetector(params)
detector.empty() # <- segfault
keypoints = detector.detect(image) # <- segfault
Run Code Online (Sandbox Code Playgroud)

我设法解决了这样的问题[来源]:

import cv2
import numpy as np;

params = cv2.SimpleBlobDetector_Params()

ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
    detector = cv2.SimpleBlobDetector(params)
else : 
    detector = cv2.SimpleBlobDetector_create(params)

detector.empty() # <- now works
keypoints = detector.detect(image) # <- now works
Run Code Online (Sandbox Code Playgroud)

不确定这在多大程度上适用于 C++ API,但在 ver3 中也可能有一些变化。

  • 我使用的是 openCV 4.2,我可以验证使用 `cv2.SimpleBlobDetector_create` 对我有用,而 `cv2.SimpleBlobDetector` 导致了段错误。 (2认同)

Yan*_*Kui 0

当您调用 detector->Detect 时,cv::Mat 图像是一个空图像。

当您调用 cv::imread 时,必须将第一个参数设置为非空字符串。