Alexandru Barbarosie
Alexandru Barbarosie

Reputation: 3002

Feature detection with opencv fails with seg fault

I have the following code

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

What can be the reason of the seg fault? I tried running gdb on it with hope that the library has enough meta data, but the stack ends at the call to detect()

Upvotes: 6

Views: 5260

Answers (2)

mareq
mareq

Reputation: 416

I have got into similar problem in Python:

import cv2
import numpy as np;

params = cv2.SimpleBlobDetector_Params()
detector = cv2.SimpleBlobDetector(params)
detector.empty() # <- segfault
keypoints = detector.detect(image) # <- segfault

I managed to solve the issue like this [source]:

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

Not sure how much is this applicable to the C++ API, but there may be some change in ver3 there as well.

Upvotes: 27

Yang Kui
Yang Kui

Reputation: 548

cv::Mat image is an empty image when you call detector->detect.

When you call cv::imread, you have to set the first paramter to an non-empty string.

Upvotes: 0

Related Questions