Feature detection with opencv fails with seg fault
Asked Answered
R

2

6

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()

Riyadh answered 17/5, 2015 at 20:45 Comment(6)
It's not imread? You include a filename in your code? I think openCV fails ungracefully if you don't give a filename for imread. The other reason could be that you need cv::detect instead of detect.Tellurize
@Tellurize Sry, I was not explicit, there is a file name, I thought saying that that image.data is true would imply that...Riyadh
check, if your detector is valid: detector.empty() (yes, with a dot, not pointer)Bedspread
@Bedspread that would seg fault as well. Both detector->empty() and (*detector).empty()Riyadh
just saying, you're actually checking if the cv::Ptr is empty. but if so - it did not give you a SURF detector. are you running some weird os, that restricts you from running nonfree stuff ? did you build the opencv libs, or get them via some ppm ?Bedspread
@berek I'm on Ubuntu 14.04. How would it be correct with the nonzero stuff? If you mean the one namespace with xfeatured2d I get an error that xfeature2d is not a namespace of cv::Riyadh
U
27

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.

Ustulation answered 31/10, 2019 at 13:23 Comment(1)
I am using openCV 4.2, and I can verify that using cv2.SimpleBlobDetector_create worked for me whereas cv2.SimpleBlobDetector was causing a seg fault.Lib
A
0

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.

Antho answered 18/5, 2015 at 5:31 Comment(2)
Sry, I was not explicit, there is a file name, I thought saying that that image.data is true would imply that...Riyadh
ha, sorry, i was careless and missed the comments. I think maybe the seg fault was caused by some runtime version mismatch. I suggest rebuilding opencv from src with your own gcc toolchain.Antho

© 2022 - 2024 — McMap. All rights reserved.