Mario Abbruscato
Mario Abbruscato

Reputation: 819

OpenCV segfault with SIFT detector in Python

small image

import cv2

img1 = cv2.imread('imref/ref-DN.png',0)    # queryImage


# Initiate SIFT detector
sift = cv2.SIFT()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)

print("end")

output it , en :

Errore di segmentazione (core dump creato)
Segmentation error (core dump created)

My Linux Kubuntu 18.04LTS desktop has 32GB of RAM

Other codes works fine but not SIFT

cat /proc/meminfo 

MemTotal:       32776404 kB
MemFree:        29683128 kB
MemAvailable:   31038428 kB

the image attached here is 2k (61x54)pixel

Why this appens? Thanks all

cv2.__version__ -> '4.5.1'

Thanks to alkasm

sift = cv2.SIFT_create()

solved my problem

Upvotes: 1

Views: 301

Answers (1)

alkasm
alkasm

Reputation: 23012

Feature detectors in OpenCV should be created using their static create methods. It isn't explicit in the API reference to not use the constructors, but you can see in the docs for the SIFT class that the create() methods are the only ones shown (and they show that the corresponding Python call is SIFT_create()).

The reason this is necessary is because the OpenCV Algorithm class (which Feature2D is a subclass of) is implemented using the PIMPL idiom so that SIFT is actually a base/virtual class not intended to be instantiated directly; instead SIFT::create returns a SIFT_Impl instance, as you can see in the code. The Python bindings can probably be "fixed" to mitigate this issue at some point in the future, but until then, you'll want to use SIFT_create(), BRISK_create(), and so on for all the feature detectors.

Note also that SIFT.create() and BRISK.create() and so on are valid generated names which are equivalent, but you'll find the underscore versions more common in tutorials and in people's projects, and they are currently the ones explicitly documented.

Upvotes: 3

Related Questions