3

I want to use functions like SIFT, SURF,etc. I am using Ubuntu 16.04 on a virtual machine with OpenCV 3.4.1 and using PyCharm as the IDE.

I can open Pycharm, create a project, go to file - settings - Project interpreter and download the modules I need.I have downloaded opencv-python and opencv-contrib-python.

But I still am not able to use the above mentioned fucntions.

Please help. Thanks.

Winbuntu
  • 127
  • 1
  • 4
  • 15

2 Answers2

5

With OpenCV 3, the non-free modules, namely SIFT and SURF, which are patented by their respective creators, were moved out of the default OpenCV install and into the opencv_contrib package. The implementations in opencv_contrib are not installed by default and you need to explicitly enable them when compiling and installing OpenCV to obtain access to them.

To get access to the original SIFT and SURF implementations found in OpenCV 2.4.X (versions with these implementations installed by default), you will need to pull down both the opencv and the opencv_contrib repositories from GitHub and then compile and install OpenCV3 from source.

As much as I would like to provide you with the steps to do this, I want to give credit to one tutorial available online to do this instead. So, follow this sweet tutorial - Install OpenCV 3.0 and Python 3.4+ on Ubuntu and you should be able to work with these implementations. Additionally, I would suggest looking at ORB which is a faster, free alternative to SIFT and SURF, but not sure if it will serve your purpose.

If you really don't want to work with the terminal, open a new project, Go to ->

Settings -> Project -> Project Interpreter -> +

Add the modules that you want by searching for them and you should be able to use them.

Rick M.
  • 3,045
  • 1
  • 21
  • 39
  • @Winbuntu If it helps you, accept it as a solution so it helps people in the future! – Rick M. May 23 '18 at 07:02
  • Thanks for the information. But I want to activate these functions in Pycharm. I don't wanna write codes in the terminal. So I have downloaded pip, opencv-python, opencv-contrib-python, numpy modules in the Project interpreter in **Pycharm**. So how do I activate these functions? – Winbuntu May 23 '18 at 07:36
  • Once you install all the modules, *PyCharm* will be able to recognize it. If it runs in the terminal, it will run on *PyCharm* as well. If you are so adamant on not using the terminal (don't get why since you are working on Ubuntu), open your project in *PyCharm*, go to Settings > Project > Project Interpreter > +, there you can search for the libraries you want to install – Rick M. May 23 '18 at 07:41
  • M I have done it. Using the '+' sign itself I have downloaded the modules mentioned above. Now when I write for example cv2.x then Pycharm should autocomplete it as cv2.xfeatures2d or whatever or let's say cv2.Sobel or whatever function it has. But it doesn't autocomplete it. That's the problem :/ I am adamant to use Pycharm as my Project has to be done on this IDE. – Winbuntu May 23 '18 at 07:49
  • Hey! I got it! I deleted the project I had previously created and started it all new. This time in the Project interpreter I just downloaded, opencv-contrib-python as a module and then Pycharm autocompletes it. Thank you for your help :) – Winbuntu May 23 '18 at 07:58
  • Hi! It autocompletes all the xfeatures2d functions like SIFT,SURF but still after compiling it gives this error. Traceback (most recent call last): File "/home/iamnirbhay/PycharmProjects/ScaleInvariantFeatureTransform/ScaleInvariantFeatureTransform.py", line 7, in sift = cv2.xfeatures2d_SIFT.create() AttributeError: module 'cv2.cv2' has no attribute 'xfeatures2d_SIFT' – Winbuntu May 23 '18 at 08:54
  • try `cv2.xfeatures2d.SIFT_create()`. If not, try following the link mentioned in the answer. It is pretty straight forward. Remove all opencv libraries currently on your system and follow the link. – Rick M. May 23 '18 at 08:58
  • I tried cv2.xfeatures2d.SIFT_create() as well. Your answer is correct. But how do I make Pycharm work with it!! Why is it not working even when it autocompletes it? I don't understand this. – Winbuntu May 23 '18 at 09:10
  • 1
    Now I created everything new and it is working again. I think there might have been cache or something.! Thanks anyway. I accepted the answer. – Winbuntu May 23 '18 at 09:18
3

I was having the same problem with opencv trying to use SIFT and SURF. I try so many ways including adding opencv-contrib and even compiling opencv using visual studio but none of those options worked for me. I would recommend to use anaconda and install opencv using the method show in this post.

How do I install Python OpenCV through Conda?

After installing it using

conda install -c menpo opencv

I was able to use without the annoying massage of "set OPENCV_ENABLE_NONFREE"

Edit: I needed to install in on my raspberry pi and Anaconda does not work the same on raspberry pi. I do some research and I found that if you run the command below, should fix the "NON_FREE /Patent protected" error.

pip install opencv-contrib-python==3.4.0.12

Adonis H.
  • 331
  • 2
  • 7