15

I am using OpenCV 3.1.0 , Python 2.7.11 and Windows 10. I want to build the extra modules (opencv_contrib) into OpenCV.

I follow the step in this GitHub.

$ cd <opencv_build_directory>
$ cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules <opencv_source_directory>
$ make -j5

When I type cmake _DOPENCV_EXTRA_MODULES_PATH=C:\opencv_contrib\opencv_contrib/modules C:\opencv\sources, there is an error.

enter image description here

I am looking for help how can I solve it. Thank you.

be_good_do_good
  • 4,311
  • 3
  • 28
  • 42
VICTOR
  • 1,894
  • 5
  • 25
  • 54

5 Answers5

30

Update 29-5-2017: This also works with OpenCV 3.2.0-dev. Now using Visual Studio 2015 or 2017 gives no errors.

The cv2.pyd is now automatically moved to site-packages. You just only need to copy the DLLs.

As mentioned by @AleksandrUsarov in the comments section, this method still works for OpenCV 4.1.2, but you need to check OPENCV_ENABLE_NONFREE parameter as well.

I have made a tutorial on how to compile and build your own OpenCV 3.1.0-dev from scratch. It is simple and organized and works with me very well. If you have time to recompile OpenCV check out the tutorial.

The steps to follow are:

  1. Install Visual Studio 2013
  2. Install CMake
  3. Download both opencv and opencv_contrib from github
  4. Create a directory opencv-3.1.0-dev and in this directory create other two directories one called build and the other called sources. The paths will be opencv-3.1.0-dev\build and opencv-3.1.0-dev\sources.
  5. Unzip opencv-master.zipand opencv_contrib-master.zip
  6. In the sources directory created in step 4, paste opencv and opencv_contrib directories
  7. Open CMake and provide two paths, one for the build directory and the other for the sources directory

cmake

  1. Press configure, a lot of lines highlighted in red will appear.

configure

  1. before these lines appear you will have to specify your compiler. (Also select correct bit version based on your needs, same procedure works for 32-bt and 64-bit)

compiler

  1. Choose whatever you want to build, but the most important thing to do is to provide the path of the non-free modules.

extra modules

  1. Press configure again and new lines will be highlighted in red.
  2. Make sure to uncheck BUILD_opencv_contrib_world before pressing generate.

new

  1. The last step with CMake is to press generate.
  2. Make sure generating is done before continuing.

generating done

  1. open the build that was empty and you will find a bunch of files. The most important file is OpenCV.sln. Right click on this .sln file and open it with the visual studio 2013 used in CMake.

the sln file

Be patient as this step and the next will take a lot of time.

  1. Build the 3 files with the order in the following image. Change the mode into Release. These step take about 10 to 15 minutes.

build

  1. Copy all the .dll files [present at opencv-3.1.0-dev\build\bin\Release] and the cv2.pyd and paste them in the site-packages directory in Pyhton27\Lib\site-packages.
  2. To verify the build open Python and execute import cv2. If no errors, execute cv2.__version__, and you should get 3.1.0-dev.

verification

If anything is not clear please watch the tutorial provided at the beginning of this answer.

Tes3awy
  • 2,166
  • 5
  • 29
  • 51
  • 3
    Instructions still work for 3.2.0-dev except: (1) step 16 only need to build INSTALL and (2) step 17 the .pyd file is already copied over for you but still need to copy over all the dlls – GroovyDotCom Jan 13 '17 at 14:23
  • @GroovyDotCom You can also use Visual Studio 2015 for compiling the 3.2.0-dev version. – Tes3awy Feb 13 '17 at 18:20
  • 2
    Thank you for this!! I just ran it on Windows 10 using Visual Studio 2017 and it worked fine (except I had to exclude the opencv folder in my antivirus as it didn't like the EXEs being built.) You're a legend :) – Lucas Young May 29 '17 at 12:11
  • @LucasYoung So glad I helped :) – Tes3awy May 29 '17 at 12:13
  • Compiling OpenCV using Python 3.7 does not work but I have successfully compiled it using Python 3.4 – jonilyn2730 Oct 12 '18 at 06:11
  • 1
    Still works for 4.1.2 but need to check OPENCV_ENABLE_NONFREE parameter – Aleksandr Usarov Nov 09 '19 at 10:57
15

For Python 3.6/3.5 you could use *.whl (Python on Wheels) module from here:

pip install opencv_python-3.2.0+contrib-cp36-cp36m-win_amd64.whl

For Python 2.7 there is no opencv+contrib in one module, but you could separately install opencv module without contrib from here at the end of the page and then add opencv-contrib-python module:

pip install opencv_python-3.2.0.7-cp27-cp27m-win_amd64.whl
pip install opencv-contrib-python

Do not mix opencv+contrib opencv-python (3.2.0+contrib) and opencv-contrib-python (3.2.0.7) modules, otherwise your "import cv2" stop to work till you delete opencv-contrib-python module.

FooBar167
  • 2,721
  • 1
  • 26
  • 37
  • i had an python 3 error where openCV did not find some of the contrib functions, i got it working by following your second example and "opencv_python-3.3.1-cp36-cp36m-win_amd64.whl" – GlabbichRulz Mar 21 '18 at 21:59
  • how about opencv-contrib-python-nonfree? any Idea how to install it – mamur Mar 03 '20 at 08:44
9

no need for complex stuff, just uninstall the version of opencv you have and do:

pip install opencv-contrib-python

Good luck, Amber

andreas
  • 16,357
  • 12
  • 72
  • 76
Amber
  • 91
  • 1
  • 1
3

The answer of foo bar is the best simple way to add opencv_contrib to python. But opencv_python-3.2.0.7-cp27-cp27m-win_amd64.whl is just for AMD chips. If you run it on Intel chip you will receive error message like this: "XXX is not a supported wheel on this platform". In this case, you must use opencv_python-3.2.0.7-cp27-cp27m-win32.whl.

Community
  • 1
  • 1
daithuongphu
  • 39
  • 1
  • 1
  • I've got this error `Requirement 'opencv_python-3.2.0.7-cp27-cp27m-win32.whl' looks like a filename, but the file does not exist` – Vasyl Zvarydchuk Aug 04 '17 at 15:44
  • To Vasyl Zvarydchuk -- You should download this file from here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv according to your version of Python (2.7, 3.4, 3.6, etc.) and according to your version of Windows (32bit or 64bit). Better with contributions (`contrib`). But first try to use command: `pip install opencv-contrib-python` – FooBar167 Jan 07 '18 at 12:10
  • amd64 means 64-bit architecture, not AMD chips per se, whereas the alternative package you suggested will work with 32-bit machines. – Łukasz Daniluk Feb 08 '18 at 21:01
1

It seems that you have no compiler installed or it is not found.

I have successfully worked with this guide:

http://docs.opencv.org/3.0-rc1/d5/de5/tutorial_py_setup_in_windows.html

However, it was necessary to use Visual Studio 12 2013, other versions did not work.

tfv
  • 6,016
  • 4
  • 36
  • 67