4

I'm using Python 2.7 and opencv version 2.4.2. I'm having trouble with a segmentation fault. Here is the code I try:

import cv2
img = cv2.imread(img_path)
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detector = cv2.FeatureDetector_create("SURF") # or "SIFT"
kp  = detector.detect(img2)

the last line causes a segmentation fault and I don't understand why. I realize there is at least another post on the subject, namely : Does anyone have any examples of using OpenCV with python for descriptor extraction? but it doesn't seem to solve my problem.

Any help will be much appreciated! Thanks!

Community
  • 1
  • 1
cenna75
  • 539
  • 1
  • 5
  • 13
  • Have you tried displaying the img or check that it is read properly? – lightalchemist Jul 16 '13 at 11:31
  • I have, yes, everything is fine up until I try to detect the features. Good point though, sometimes it's that easy :) – cenna75 Jul 16 '13 at 13:51
  • What happens if you try `cv2.Feature2D_create('SURF')`? It appears to have the same functionality for me. – Aurelius Jul 16 '13 at 15:00
  • Unfortunately, Feature2D_create() doesn't seem to be available in this version of opencv (AttributeError...). The more I read the more I figure a lot must have changed inbetween recent versions! – cenna75 Jul 16 '13 at 15:31

2 Answers2

1

I'm using Ubuntu 12.04, which includes OpenCV 2.3.1. I wanted a newer version of OpenCV, so I found a PPA with an OpenCV 2.4.5 backport. When I tried to use I cv2.FeatureDetector_create("SURF") and cv2.FeatureDetector_create("SIFT"), I encountered the segmentation fault just as you did. I realized that both of these methods are nonfree, and observed that my OpenCV install was missing the libopencv-nonfree2.4 package. I switched to another PPA that includes it and this seems to have solved the problem.

David G.
  • 371
  • 4
  • 11
  • SIFT and SURF are patented, nonfree algos. some package managers are really picky about this – berak Jan 08 '14 at 21:04
0

I'm pretty sure cv2.FeatureDetector_create() is really only in the C++ interface. You want to do something like this:

import numpy as np
import cv2

img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
surf = cv2.SURF()
mask = np.uint8(np.ones(gray.shape))
surf_points = surf.detect(gray, mask)
Eric Workman
  • 1,425
  • 12
  • 13
  • Thanks for the answer. It turns out that this version of opencv does not have the SURF attribute coded (cv2.SURF() yields : "AttributeError: 'module' object has no attribute 'SURF'"). I wonder if there could be an incompatibility problem between python and opencv versions (resp. 2.7 and 2.4.2) maybe? – cenna75 Jul 16 '13 at 13:54
  • I'm running Python 2.7.2 and OpenCV 2.4.5. Does `import cv2` followed by `cv2.SURF()` error in IDLE? – Eric Workman Jul 16 '13 at 14:30
  • Oh, the [docs](http://docs.opencv.org/modules/nonfree/doc/feature_detection.html#surf-operator) also have `cv2.SURF.detect(image[, mask]) --> key_points`, but it might be available to you. – Eric Workman Jul 16 '13 at 14:33
  • It is not available indeed. However, you might have a point: maybe I should update to 2.4.5, it looks like this could solve my problems (not that I'm eager to do that, I recall quite a hassle when installing :)) – cenna75 Jul 16 '13 at 15:33
  • If you're on a Windows box, you can get the [Unofficial Binaries](http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv). The [Official Site](http://opencv.org/downloads.html) also has the binaries and the packages for other systems. – Eric Workman Jul 16 '13 at 17:33