Install OpenCV 3.0 and Python 2.7+ on Ubuntu

opencv3_handwriting_reco

Last week we kicked-off the OpenCV 3.0 install fest by detailing how to install OpenCV 3.0 and Python 2.7+ on the OSX platform.

Today we are going to continue the OpenCV 3.0 install instruction series by moving over to the Ubuntu operating system.

In the remainder of the post I will provide instructions on how to configure and install OpenCV 3.0 and Python 2.7+ on Ubuntu. I have personally tested these instructions on Ubuntu 14.04, but they should pretty much work on any Debian-based operating system.

A quick note before we get started: Yes, OpenCV 3.0 is indeed compatible with Python 3+. However, the install instructions are slightly different between Python 2.7+ and Python 3+. In an effort to keep each article self-contained and easy to follow, I am creating separate OpenCV 3.0 install tutorials for Python 2.7 and Python 3+. If you would like to use OpenCV 3.0 and Python 3+ on your Ubuntu system, please keep an eye on this blog — I will be posting OpenCV 3.0 and Python 3+ install instructions later this month. But for the time being, let’s stick with Python 2.7.

How to Install OpenCV 3.0 and Python 2.7+ on Ubuntu

UPDATE: The tutorial you are reading now covers how to install OpenCV 3.0 with Python 2.7+ bindings on Ubuntu 14.04. This tutorial still works perfectly, but if you want to install OpenCV on the newer Ubuntu 16.04 with OpenCV 3.1 and Python 2.7 (or Python 3.5) bindings, please use this freshly updated tutorial:

http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/


This is the second article in the OpenCV 3.0 install-fest series. Last week we covered how to install OpenCV 3.0 and Python 2.7+ on OSX. Today we are going to perform the same OpenCV 3.0 and Python 2.7 installation, only on the Ubuntu operating system. In general, you should find installing OpenCV 3.0 and Python 2.7+ on Ubuntu much easier than installing on OSX.

Step 1:

Open up a terminal and update the apt-get  package manager followed by upgrading any pre-installed packages:

Step 2:

Now we need to install our developer tools:

The pkg-config  is likely already installed, but be sure to include it just in case. We’ll be using git  to pull down the OpenCV repositories from GitHub. The  cmake  package is used to configure our build.

Step 3:

OpenCV needs to be able to load various image file formats from disk, including JPEG, PNG, TIFF, etc. In order to load these image formats from disk, we’ll need our image I/O packages:

Step 4:

At this point, we have the ability to load a given image off of disk. But how do we display the actual image to our screen? The answer is the GTK development library, which the highgui  module of OpenCV depends on to guild Graphical User Interfaces (GUIs):

Step 5:

We can load images using OpenCV, but what about processing video streams and accessing individual frames? We’ve got that covered here:

Step 6:

Install libraries that are used to optimize various routines inside of OpenCV:

Step 7:

Install pip , a Python package manager:

Step 8:

Install virtualenv and virtualenvwrapper. These two packages allow us to create separate Python environments for each project we are working on. While installing virtualenv  and virtualenvwrapper  is not a requirement to get OpenCV 3.0 and Python 2.7+ up and running on your Ubuntu system, I highly recommend it and the rest of this tutorial will assume you have them installed!

Now that we have virtualenv  and virtualenvwrapper  installed, we need to update our ~/.bashrc  file:

This quick update will ensure that both virtualenv  and virtualenvwrapper  are loaded each time you login.

To make the changes to our ~/.bashrc  file take effect, you can either (1) logout and log back in, (2) close your current terminal window and open a new one, or preferably, (3) reload the contents of your ~/.bashrc  file:

Lastly, we can create our cv  virtual environment where we’ll be doing our computer vision development and OpenCV 3.0 + Python 2.7+ installation:

Step 9:

As I mentioned above, this tutorial covers how to install OpenCV 3.0 and Python 2.7+ (I’ll have a OpenCV 3.0 + Python 3 tutorial available later this month), so we’ll need to install our Python 2.7 development tools:

Since OpenCV represents images as multi-dimensional NumPy arrays, we better install NumPy into our cv  virtual environment:

Step 10:

Our environment is now all setup — we can proceed to change to our home directory, pull down OpenCV from GitHub, and checkout the 3.0.0  version:

Update (3 January 2016): You can replace the 3.0.0  version with whatever the current release is (as of right now, it’s 3.1.0 ). Be sure to check OpenCV.org for information on the latest release.

As I mentioned last week, we also need the opencv_contrib repo as well. Without this repository, we won’t have access to standard keypoint detectors and local invariant descriptors (such as SIFT, SURF, etc.) that were available in the OpenCV 2.4.X version. We’ll also be missing out on some of the newer OpenCV 3.0 features like text detection in natural images:

Again, make sure that you checkout the same version for opencv_contrib  that you did for opencv  above, otherwise you could run into compilation errors.

Time to setup the build:

Update (3 January 2016): In order to build OpenCV 3.1.0 , you need to set -D INSTALL_C_EXAMPLES=OFF  (rather than ON ) in the cmake  command. There is a bug in the OpenCV v3.1.0 CMake build script that can cause errors if you leave this switch on. Once you set this switch to off, CMake should run without a problem.

Notice how compared to last week our CMake command is substantially less verbose and requires less manual tweaking — this is because CMake is able to better automatically tune our install parameters (at least compared to OSX).

Now we can finally compile OpenCV:

Where you can replace the 4 with the number of available cores on your processor to speedup the compilation.

Here’s an example of OpenCV 3.0 compiling on my system:

Figure 1: OpenCV 3.0 with Python 2.7+ support compiling on my Ubuntu 14.04 system.

Figure 1: OpenCV 3.0 with Python 2.7+ support compiling on my Ubuntu 14.04 system.

Assuming that OpenCV compiled without error, you can now install it on your Ubuntu system:

Step 11:

If you’ve reached this step without an error, OpenCV should now be installed in  /usr/local/lib/python2.7/site-packages

However, our cv  virtual environment is located in our home directory — thus to use OpenCV within our cv  environment, we first need to sym-link OpenCV into the site-packages  directory of the cv  virtual environment:

Step 12:

Congratulations! You have successfully installed OpenCV 3.0 with Python 2.7+ bindings on your Ubuntu system!

To confirm your installation, simply ensure that you are in the cv  virtual environment, followed by importing cv2 :

Here’s an example of demonstrating the OpenCV 3.0 and Python 2.7+ install on my own Ubuntu machine:

Figure 2: OpenCV 3.0 with Python 2.7+ bindings has been successfully installed on Ubuntu!

Figure 2: OpenCV 3.0 with Python 2.7+ bindings has been successfully installed on Ubuntu!

Step 13:

Now that OpenCV has been configured and installed, let’s build a quick Python script to detect the red game cartridge in the image named games.jpg  below:

Figure 2: Our goal is to detect the red game cartridge (on the right) in this image.

Figure 3: Our goal is to detect the red game cartridge (on the right) in this image.

Open up your favorite editor, create a new file, name it find_game.py , and insert the following code:

You’ll also need to download the games.jpg image and place it in the same directory as your find_game.py  file. Once the games.jpg  file has been downloaded, you can execute the script via:

Assuming that you have downloaded the games.jpg  image and placed it in the same directory as our find_game.py  script, you should see the following output:

Figure 4: We have successfully detected the red game cartridge in the image!

Figure 4: We have successfully detected the red game cartridge in the image!

Notice how our script was able to successfully detect the red game cartridge in the right portion of the image, followed by drawing a green bounding box surrounding it.

Obviously this isn’t the most exciting example in the world — but it has demonstrated that we have OpenCV 3.0 with Python 2.7+ bindings up and running on our Ubuntu system!

So, what’s next?

Congrats! You have a brand new, fresh install of OpenCV on your Ubuntu system — and I’m sure you’re just itching to leverage your new install to build some awesome computer vision apps.

But I’m also willing to bet that you’re just getting started learning computer vision and OpenCV, and you’re probably feeling a bit confused and overwhelmed on where exactly to start.

Personally, I’m a big fan of learning by example, so a good first step would be to read this blog post on accessing your webcam with OpenCV. This tutorial details the exact steps you need to take to (1) capture photos from your webcam and (2) access the raw video stream.

And if you’re really interested in leveling-up your computer vision skills, you should definitely check out my book, Practical Python and OpenCV + Case Studies. My book not only covers the basics of computer vision and image processing, but also teaches you how to solve real world computer vision problems including face detection in images and video streams, object tracking in video, and handwriting recognition.

raspberry_pi_in_post

So let’s put your fresh install of OpenCV on your Ubuntu system to good use — just click here to learn more about the real-world projects you can solve using your new OpenCV install + Practical Python and OpenCV .

Summary

To celebrate the OpenCV 3.0 release, I am working my way through OpenCV 3.0 and Python 2.7/Python 3.4 installation instructions on OSXUbuntu, and the Raspberry Pi.

Last week I covered how to install OpenCV 3.0 and Python 2.7+ on OSX.

And today we covered how to install OpenCV 3.0 with Python 2.7 bindings on Ubuntu. I have personally tested these instructions on my own Ubuntu 14.04 machine, but they should work on any Debian-based system.

Next week we’ll continue the install-fest and hop back to OSX — this time installing OpenCV 3.0 and Python 3!

This will be the first time we’ve used Python 3 on the PyImageSearch blog, so you won’t want to miss it!

And please consider subscribing to the PyImageSearch Newsletter by entering your email address in the form below. As we work through the OpenCV install-fest, I’ll be sending out updates as each new OpenCV 3.0 + Python install tutorial is released!

, , ,

296 Responses to Install OpenCV 3.0 and Python 2.7+ on Ubuntu

  1. Mitch Berkson June 23, 2015 at 3:59 pm #

    So where is games.jpg?

    • Adrian Rosebrock June 23, 2015 at 6:34 pm #

      Hey Mitch, I saw your other comment, but for further clarification I’ll add the games.jpg link here to help other readers out: http://www.pyimagesearch.com/wp-content/uploads/2015/06/games.jpg

    • Albert September 24, 2015 at 6:31 am #

      Right click -> Save image as …

      • Adrian Rosebrock September 24, 2015 at 6:57 am #

        I’ll also add that the code + images is available in the source code download of the post.

  2. Mitch Berkson June 23, 2015 at 4:04 pm #

    Doh. Can just download games.jpg from the browser.

  3. Frank Frederic June 23, 2015 at 10:01 pm #

    Thank you Adrian for the guide.
    Are you able to install the combination OpenCV 3 + Python 3 on any platform?
    I’m long for this.

    • Adrian Rosebrock June 24, 2015 at 6:10 am #

      I’ve personally installed OpenCV 3.0 + Python 3 on OSX, Linux (Debian based systems), and the Raspberry Pi. I do not have a Windows system, otherwise I would probably give that a shot as well.

      • Red May 29, 2016 at 10:11 am #

        I’m constantly struggling with “Missing PythonLibraries” for both the 2.7 and 3.x using an Xubuntu 14.04 LTS. Development packages for both Python versions are installed. Funny thing is that every second time I run cmake .. in the build directory of OpenCV 3’s source directory I get that the libraries for Python 2.7 are found. When I get that I’m also able to build the Python libs for 2.7. For Python 3.x not the same luck sadly. After two months still unable to compile the damn libs hence I’m unable to properly use OpenCV 3 in Python 3.

        • Adrian Rosebrock May 29, 2016 at 1:49 pm #

          I’m sure this is a dumb question on my part, but after you looked at my OpenCV 3 + Python 3 installation tutorial?

  4. shixudongleo June 23, 2015 at 11:15 pm #

    Should I set the OPENCV_EXTRA_MODULES_PATH explicitly so that cmake can include the opencv_contrib modules?

    Or the cmake can automatically locate the opencv_contrib directory because opencv and opencv_contrib directories are download side-by-side.

    • Adrian Rosebrock June 24, 2015 at 6:12 am #

      You need to explicitly set the OPENCV_EXTRA_MODULES_PATH like this: -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules

      Also, just realized that I didn’t mention that in the original post. I have updated it, thanks for pointing it out Xudong!

  5. Sébastien Vincent June 24, 2015 at 4:33 pm #

    Hi
    It seems to me that there’s a small mistake in the build setup instruction. The two dots should not be before the OPENCV_EXTRA_MODULES_PATH option but at the very end:
    cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D BUILD_EXAMPLES=ON
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules .. <== there!

    • Adrian Rosebrock June 25, 2015 at 6:21 am #

      Thanks for pointing this out Sébastien! I have fixed the error 🙂

  6. Chris Viehoff June 25, 2015 at 1:10 am #

    I did not get any error but in step11 checking for the files in /usr/local/lib/python2.7/site-packages …there were no files created.

    • Adrian Rosebrock June 25, 2015 at 6:26 am #

      Take a look at your output from cmake, like in this screenshot and look for the packages path. This will tell you where the generated c2.so file will be stored. It’s possible that CMake had generated a different packages path on your system.

      • Refik Can June 25, 2015 at 9:33 am #

        I also did not find cv2.so file under /usr/local/lib/python2.7/site-packages and my cmake build python options are same with your screenshot.

        • Adrian Rosebrock June 25, 2015 at 12:42 pm #

          Hey Refik, just to confirm — you ran $ make install after compiling, correct? If so, can you check in your /usr/local/lib/python2.7/dist-packages directory to see if it ended up there?

          Finally, check in your build/lib directory as well. You should find the cv2.so file there after a successful compilation. The make install command just copies the cv2.so binary to the packages path.

          • Evan Zamir January 18, 2016 at 6:41 pm #

            I have a similar issue installing on a clean (Docker) version of Ubuntu 14.04. The only instructions I didn’t follow were installing the virtualenv (which doesn’t make much sense since I’m using Docker anyway).

            I can’t find cv2.so anywhere, not in /lib/build and not in site-packages or dist-packages. Any idea what might have gone wrong? No errors encountered along the way. Seems odd.

          • Adrian Rosebrock January 20, 2016 at 1:55 pm #

            After the compile did you run sudo make install? Also be sure to check the lib sub-directory of build.

            You might also want to try searching the entire Pi for the cv2.so file as well:

            $ find / -name "cv2.so"

      • Max Gordon September 8, 2015 at 7:48 am #

        Thanks for an excellent tutorial! I’m having a samilar problem on Ubuntu 14.04. I’ve solved it by doing a “sudo cp lib/cv2.so /usr/local/lib/python2.7/dist-packages/” – it does feel like a hack and it would be nice to understand why this issue appears.

        It seems that cmake doesn’t find the python2.7 libraries. I think the problem in my case is that it fails to find the python libraries, I get the:

        Could NOT find PythonLibs (missing: PYTHON_INCLUDE_DIRS) (found suitable exact version “2.7.6”)

        The part corresponding to you screenshot is just “Libraries: No” for the Python 2 section. I’ve also noticed that instead of site-packages it uses the dist-packages. From googling I’ve found a few hints (the best one is probably http://askubuntu.com/questions/479260/cmake-can-not-find-pythonlibs) but I’m not familiar enough with cmake to start changing the script and the python-dev is alreade installed.

        • Adrian Rosebrock September 9, 2015 at 6:48 am #

          It might “feel” like a hack, but in reality that’s all the sudo make install command does for the Python bindings. It just copies the cv2.so file into the specified location.

          • Vincent January 24, 2016 at 12:27 pm #

            Thanks a lot Adrian! Everything worked great for me. I could even find the red cartridge.

            As I located the cv2.so in the opencv/build/lib directory, I copied it in the dist-package with ” sudo cp lib/cv2.so /usr/local/lib/python2.7/dist-packages/ “, as Max said.

            Thanks again for all this, guys.

        • plon.io March 6, 2016 at 11:30 am #

          I have the same problem, and upgrading cmake 2.8->3.2 helps (http://askubuntu.com/questions/479260/cmake-can-not-find-pythonlibs)

          sudo apt-get install software-properties-common
          sudo add-apt-repository ppa:george-edison55/cmake-3.x
          sudo apt-get update
          sudo apt-get install –only-upgrade cmake

          for me was also crucial to install

          sudo apt-get install qt5-default libvtk6-dev

          • Red May 29, 2016 at 10:56 am #

            Kudos for the tip! I had problems for both Python 2.7 and Python 3.4 but after upgrading cmake everything was found and configured appropriately. Thanks a lot!

          • hjjoo July 24, 2016 at 9:57 pm #

            Thank you!!! so!! much!!!!

          • John August 12, 2016 at 7:41 pm #

            Thanks, sorted also! Had to remember to delete CMakeCache.txt in the build folder before running CMake again.

  7. Chris Viehoff June 25, 2015 at 1:15 am #

    when I run

    cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D BUILD_EXAMPLES=ON ..
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules

    the last command does not get executed and remains on the command line by itself:

    (cv)chris@theLab:~/opencv/build$ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules

    • Adrian Rosebrock June 25, 2015 at 6:21 am #

      Hi Chris, thanks for pointing this out, I was missing an extra “\” after the -D BUILD_EXAMPLES=ON .. part. I have updated the post and added in the extra slash, please go back up and try copying and pasting the command again.

  8. Caio Iglesias June 25, 2015 at 3:08 am #

    I installed opencv3 directly to the virtual enviroment so I can keep opencv3 on “cv” and leave opencv2 on the “gurus” enviroment as we move through the pyimagesearch gurus course. Just wanted to share the code here if someone ends up needing it.

    cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=$VIRTUAL_ENV/local/ \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D PYTHON_EXECUTABLE=$VIRTUAL_ENV/bin/python \
    -D PYTHON_PACKAGES_PATH=$VIRTUAL_ENV/lib/python2.7/site-packages \
    -D BUILD_EXAMPLES=ON ..

    Just remember to delete CMakeCache.txt if you’ve ran cmake before and to run “workon cv” before running this cmake code.

    • Adrian Rosebrock June 25, 2015 at 6:14 am #

      Very nice, thanks for sharing Caio!

    • Phil Smith September 9, 2015 at 4:29 pm #

      Thanks you – this worked perfectly on Ubuntu 14.04.

    • Manuel June 16, 2016 at 5:35 pm #

      Thank you very much, I did what you suggested and it worked.

  9. Tim Clemans June 28, 2015 at 9:57 am #

    Would be nice to have a single script version of this tutorial that one can wget and just run.

    • Adrian Rosebrock June 28, 2015 at 2:35 pm #

      Hey Tim, that’s a great suggestion. I’ll look into putting something like this together. The biggest challenge is that everyones’ system configuration is slightly different and that can lead to system/package paths being different. And if those system/packages paths are off, then you’ll get compile or install errors. I’ll look into the feasibility of this though.

  10. Sébastien Vincent June 29, 2015 at 4:12 pm #

    Hello Adrian,
    I’m not sure if it’s my install, but I performed all the steps, and it looks like cv2 can’t find the reference to the xfeatures2d package… Could you check if it’s ok on your PC? Here is what I got:

    (cv)seb@seb-VirtualBox:~/tst-opencv$ python
    Python 2.7.6 (default, Jun 22 2015, 17:58:13)
    [GCC 4.8.2] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import cv2
    >>> cv2.__version__
    ‘3.0.0’
    >>> detector = cv2.xfeatures2d.SIFT_create()
    Traceback (most recent call last):
    File “”, line 1, in
    AttributeError: ‘module’ object has no attribute ‘xfeatures2d’
    >>>

    • Adrian Rosebrock June 29, 2015 at 4:22 pm #

      Hey Sébastien, I’m actually trying to figure out this problem myself. I’ve installed OpenCV 3 with Python 3+ bindings and the xfeatures2d module is installed. However, if I perform the same process of installing OpenCV 3 with Python 2.7 bindings, then the xfeatures2d bindings are not not installed. I think it might be a bug with OpenCV as I’ve gone through the process a bunch of times, each time ensuring that the opencv_contrib package was included in the compile, but each time it cannot import xfeatures2d

      Definitely repeat the process with OpenCV 3 and Python 3 and let me know if this resolves the issue. If so, then we’ll know it’s a bug with either the cmake script/OpenCV.

      • AlexM December 24, 2015 at 11:34 am #

        Thank you Adrian! I’ve done everything exactly as it was described here ( Ubuntu 14.04, Python 2.7), and I even have libopencv_xfeatures2d installed in site-packages, but still I can’t work with xfeatures2d 🙁

        didn’t show, that xfeatures2d exist ( despite the fact, that .so object is installed properly, as I said before)

        Could you please hint me?

        • Adrian Rosebrock December 24, 2015 at 12:09 pm #

          If you’re not seeing xfeatures2d, then go back to the CMake step and ensure that xfeatures2d is listed in the “modules to be built” list. If it’s not, then it’s likely that your path to the opencv_contrib/modules directory is incorrect.

  11. Sébastien Vincent June 29, 2015 at 4:49 pm #

    Thanks for the quick answer. I’ll try with Python3.4. Coming back to Linux after ~15 years does not help, as I’m not sure if errors come from my mistakes or from something else!

    • Sébastien Vincent July 6, 2015 at 6:07 pm #

      Hello Adrian,
      Up to now, I haven’t been able to install OpenCV3 with Python3 (in a virtualenv) on my Ubuntu install. I think I’ll have to wait for your tutorial. I guess something is wrong with my cmake options.

      • Adrian Rosebrock July 6, 2015 at 6:09 pm #

        Hey Sébastien — I was planning on having the OpenCV 3 + Python 3 tutorial online today, but the bat-country post ended up taking priority. I’ll try to have it online by next Monday.

  12. Vanderson July 3, 2015 at 12:37 am #

    Thanks a lot man! God bless you.

    • Adrian Rosebrock July 3, 2015 at 6:18 am #

      No problem, I’m glad the tutorial worked for you!

  13. Larry Low July 3, 2015 at 8:04 pm #

    Thanks Adrian! This works on Linux Mint 17.

    • Adrian Rosebrock July 4, 2015 at 7:38 am #

      Awesome, glad to hear it Larry!

  14. Nick July 8, 2015 at 4:31 pm #

    Works like a charm, did not even get one error message. Your page really do look very interesting. Now I have so many ideas about what to extract.

    Thanks.

    • Adrian Rosebrock July 9, 2015 at 6:27 am #

      Fantastsic — I’m glad it worked for you Nick!

  15. tej kamal August 8, 2015 at 6:49 am #

    i am working with ubuntu 14.04. i am getting an error at step11. it is stating that

    (bash: cd: /home/kolukuluri/.virtualenvs/cv/lib/python2.7/site-pakages/: No such file or directory)

    and in the next step while import cv2 file from opencv to python it is showing an error

    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named cv2

    please guide me a way to overcome this error

    • Adrian Rosebrock August 9, 2015 at 7:06 am #

      If you are getting an error that the site-packages directory does not exist for the cv virtual environment, then I would go back to Step 8 and install virtualenv, virtualenvwrapper, and ensure that you have created the cv virtual environment — that is a critical step in getting this tutorial to work.

  16. Sulav August 8, 2015 at 1:39 pm #

    Hello,

    When I import the cv2 module from python interpreter it is imported, but when I do the same in python file, it says “importError : No cv2 module” ?
    Can you help on this?

    Thank you.

    • Adrian Rosebrock August 9, 2015 at 7:08 am #

      There are many reasons why the cv2 module could fail to import. The most common reasons are that your virtual environment was not setup correctly, the OpenCV compile failed, or the OpenCV bindings were not sym-linked into the site-packages directory of the virtual environment.

  17. Grant August 11, 2015 at 6:36 pm #

    I got through most of this tutorial fine, but I’m having a problem on the make step. I get about 40% compiled before getting this error.

    make[2]: *** No rule to make target ‘/usr/include/../lib/libavcodec.a’, needed by ‘lib/libopencv_videoio.so.3.0.0’. Stop.
    make[2]: *** Waiting for unfinished jobs….
    [ 42%] Building CXX object modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o
    CMakeFiles/Makefile2:5408: recipe for target ‘modules/videoio/CMakeFiles/opencv_videoio.dir/all’ failed
    make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
    make[1]: *** Waiting for unfinished jobs….

    It’s having problems making the libavcodec, and I’ve tried several things, including install ffmpeg and other dev packages, but none of them seem to fix the missing rule. I’ve also re-run the cmake command to see if it makes any difference, but no dice. When going through the tutorial, I did notice that libtiff4-dev is no longer available and has been replaced with libtiff5-dev. I’ve seen some boards suggesting one of the other packages removed features that libavcodec depends on, but I haven’t had any luck fixing it. Any suggestions for how to modify the make command or other packages to install to get over this hump?

    • Adrian Rosebrock August 11, 2015 at 6:47 pm #

      Based on the compile error, it seems like FFMPEG is causing some problems. I would suggest running ccmake .. (you might have to install the ccmake terminal GUI) and going through the options. Look for an option that looks like WITH_FFMPEG or something similar and make sure it’s set to OFF. Then do a make clean and recompile.

      • Grant May 26, 2016 at 6:01 pm #

        Back again almost 10 months later. I recently got back into the idea of using OpenCV, after being unsuccessful building it back in August. I’ve been able to build OpenCV 3.1.0 and use it with FFMPEG turned off, but I’m trying to do the 10-day crash course now, which requires video features. The issue is the same one as before, and despite three solid days of trying different solutions, the problem is still libavcodec.a not having a rule.

        I’ve posted the problem to the OpenCV board, but at this point, my options are to hope someone gives an answer that apparently doesn’t exist anywhere on the internet, or I find a way to compile OpenCV’s video features without FFMPEG.

        • Adrian Rosebrock May 27, 2016 at 1:30 pm #

          Hey Grant — just to confirm, did you try adding the -D WITH-FFMPEG=OFF to your CMake command?

  18. Luke August 11, 2015 at 10:22 pm #

    I tried these instructions with Anaconda. Initially cv2.so wasn’t generated, but I changed the cmake command as specified here and then it generated correctly:

    http://stackoverflow.com/questions/29381705/install-opencv-3-0-0-beta-on-ubuntu-14-04-with-anaconda-python-2-7/29422394#29422394

    then I followed the rest of this tutorial, and finally used the command
    find / -name “cv2.so”
    to locate the file.

  19. Joey August 13, 2015 at 1:04 pm #

    Hello,

    I work on Ubuntu 14.04.3. After I followed your tutorial I got no error until step 12. When I imported cv2 it gives me:
    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named cv2

    that error. Can you guess what should I do to make it work?

    • Adrian Rosebrock August 14, 2015 at 7:19 am #

      Hey Joey — unfortunately, there are a number of reasons why importing OpenCV could fail. The main reasons are not being in the cv virtual environment before compiling, not being in the virtual environment when trying to import OpenCV, and not setting up the proper sym-links into the environment. My suggestion is to go back to Step 11 and see where the cv2.so file is stored, it seems likely that the sym-link is incorrect.

  20. Andrey August 14, 2015 at 9:44 am #

    Hello Adrian,

    I wanted to update opencv version from 2.4.9 to 3.0.0 so I find your guidelines very useful, however I’ve stacked to some problem.

    I’ve followed your instructions, except for step 8 with virtualenv.
    Everything goes fine until step 11 – I am unable to find cv2.so.

    I mean, there is cv2.so which belongs to older version of CV.
    Bth I’m using python 2.7 under anaconda.

    • Adrian Rosebrock August 15, 2015 at 6:29 am #

      I’m not a fan of anaconda so I don’t have any personal experience trying to install using it. Be sure to check the lib directory of your build.

  21. Bulat Suleymanov August 17, 2015 at 4:50 pm #

    Hello Adrian!

    After installing Python 3.4 + OpenCV 3.0 on Ubuntu using virtual environment, I tried to create another one with Python 2.7 according to your tutorial. When I create new virtual environment it already consists lib/Python3.4. What should I do?

    • Adrian Rosebrock August 18, 2015 at 6:43 am #

      Hey Bulat, you’ll need to specify your Python version when using the mkvirtualenv command:

      $ mkvirtualenv foo -p python2.7

  22. Nathan McCorkle August 20, 2015 at 1:39 am #

    Thanks! I just used these to compile the MASTER branches of both, since there have been tons of bugfixes since the 3.0.0 release… so far things are great!

    • Adrian Rosebrock August 20, 2015 at 6:58 am #

      Nice — very nice Nathan. I’m sure there will be a ton more bug fixes to come. The 3.0.0 release is nice, but I don’t think things will start to settle down and become more stable until the 3.1 or even 3.2 release.

  23. MJ August 26, 2015 at 9:18 am #

    I install it on Ubuntu
    I get the below error several time. plz any one help me.
    ln: failed to create symbolic link ‘cv2.so’: File exists.

    • Adrian Rosebrock August 27, 2015 at 6:22 am #

      If the file already exists, remove it:

      $ rm cv2.so

      And then re-create the sym-link.

    • sohmod August 29, 2015 at 8:30 pm #

      The cv2.so might had gotten installed in your dist-packages folder, if so the sym-link should be ;

      cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
      $ ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so

  24. Arun Rajagopalan August 26, 2015 at 5:36 pm #

    Hi all,

    If you encounter the error “use of enum ‘AVCodecID’ without previous declaration”during make, then the following link will help you.
    http://stackoverflow.com/questions/31663498/opencv-3-0-0-make-error-with-ffmpeg

    Hello Adrian Sir,

    You deserve massive respect for making such an useful, lucid tutorial! Take a bow Sir! Continue being this awesome!

    • Adrian Rosebrock August 27, 2015 at 6:22 am #

      Nice, thanks for passing this along Arun!

  25. Ernest Chen September 6, 2015 at 10:58 pm #

    Hi Adrian,

    I have been following your instructions without any issues. I noticed that I can only vidocapture the video stream at the recorded fps, and not faster. I found a similar issue here: http://stackoverflow.com/questions/28224280/how-to-process-video-files-with-python-opencv-faster-than-file-frame-rate

    in which it mentions ffmpeg is required rather than the libavcodec library used here. How to do this with your instructions here? Thanks very much.

    Ernest

    • Adrian Rosebrock September 7, 2015 at 8:22 am #

      Hi Ernest — to get FFMPEG support for OpenCV, you’ll need to (1) install FFMPEG on your Ubuntu system, (2) compile OpenCV with the -D WITH_ffmpeg=ON flag set (you should also make sure that’s the correct flag, I’m doing this from memory since it’s been a long time since I’ve had to use FFMPEG), and then (3) re-install OpenCV once the compile has finished.

  26. Lawrence Moore September 8, 2015 at 7:52 pm #

    Hi Adrian,

    Thanks for the super clear tutorial. The normal libraries of openCV work fine, but whenever I try to use something from the opencv_contrib repository like SIFT, it says “AttributeError: ‘module’ object has no attribute ‘SIFT'”. Doing a quick dir(cv2) affirms that the functions aren’t there. Do you know what could have gone wrong? I followed all the steps as best I could. I did notice that when I ran the cmake command it couldn’t find a few packages, but I ran all the apt-get installs as instructed. Thanks!

    Lawrence

    • Adrian Rosebrock September 9, 2015 at 6:44 am #

      Hey Lawrence, to read my post on Where did SIFT and SURF go in OpenCV 3?. If you followed my install instructions exactly, then you’ll be able to find SIFT and SURF in the xfeatures2d sub-module of cv2.

  27. Adrian September 9, 2015 at 8:18 am #

    Hi Adrian, I was able to install OpenCV and Python with contrib modules but it seems that some contrib modules are missing.
    For example “text” module seems to be incomplete.

    This is the output of the command help(“cv2.text”). I was expected to find methods like “loadClassifierNM1”, etc.

    Any help is appreciated.
    Thanks
    Adrian

    Help on module cv2.text in cv2:

    NAME
    cv2.text

    FILE
    (built-in)

    DATA
    ERFILTER_NM_IHSGRAD = 1
    ERFILTER_NM_IHSGrad = 1
    ERFILTER_NM_RGBLGRAD = 0
    ERFILTER_NM_RGBLGrad = 0
    ERGROUPING_ORIENTATION_ANY = 1
    ERGROUPING_ORIENTATION_HORIZ = 0
    OCR_DECODER_VITERBI = 0
    OCR_LEVEL_TEXTLINE = 1
    OCR_LEVEL_WORD = 0

    (END)

    • Adrian Rosebrock September 10, 2015 at 6:37 am #

      Some modules may not be compiled and installed if your system does not have the appropriate dependencies installed. I haven’t explored all of the opencv_contrib modules, so I’m not sure which dependencies each of them has. However, after you run CMake, run ccmake .. and you’ll be able to easily navigate and see which modules will be compiled and which ones won’t.

  28. John Wandeto September 12, 2015 at 6:38 am #

    Hello
    May I update my earlier question. I get this at step 10:

    (cv)john@john-Precision-T1700:~/opencv_contrib/build$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    > D CMAKE_INSTALL_PREFIX=/usr/local \
    > D INSTALL_C_EXAMPLES=ON \
    > D INSTALL_PYTHON_EXAMPLES=ON \
    > D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    > D BUILD_EXAMPLES=ON ..
    CMake Error: The source directory “/home/john/opencv_contrib” does not appear to contain CMakeLists.txt.
    Specify –help for usage, or press the help button on the CMake GUI.
    (cv)john@john-Precision-T1700:~/opencv_contrib/build$

    Any idea what could be ailing me?

    • Adrian Rosebrock September 12, 2015 at 6:45 am #

      Hey John — you’re trying to compile OpenCV from the opencv_contrib directory, not the opencv directory, that’s what your problem is. Simply change directory to cd ~/opencv, create your build directory, and then run CMake.

  29. selvam September 14, 2015 at 3:16 am #

    Hi Adrian

    In step 8, after entering “source ~/.bashrc”, I am getting the following response:

    bash: /usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh: No such file or directory

    Could you please guide me

    Thanks in advance

    • Adrian Rosebrock September 14, 2015 at 6:11 am #

      It sounds like you may have forgotten to install virtualenv and virtualenvwrapper in Step 8.

  30. vasista September 16, 2015 at 8:48 am #

    ImportError: libtiff.so.4: cannot open shared object file: No such file or directory

    I got this error while compiling find_game.py

    Can you please tell how to rectify this issue @Adrian

    • Adrian Rosebrock September 16, 2015 at 11:04 am #

      It sounds like you might have missed Step 3 where you install the required packages to read various image formats from disk:

      $ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev

      I would suggest ensuring the packages above are installed, then re-compiling and re-installing OpenCV.

  31. Tanya September 16, 2015 at 10:52 pm #

    Please tell me why am I having this errors?

    tanichka@tanichka-K56CM:~/Sublime$ ./find_game.py
    ./find_game.py: line 2: import: command not found

    I dont know why, because I installed numpy as well as opencv. Now I have to link them with python again?

    Please tell me how

    • Adrian Rosebrock September 17, 2015 at 8:12 am #

      Try executing the script with python:

      $ python find_game.py

  32. Vascom September 17, 2015 at 7:06 am #

    How can I get the list of installed modules in cv2 interface. Exactly, I want to know if cuda based modules are installed in cv2.pyd

    • Adrian Rosebrock September 17, 2015 at 7:33 am #

      The easiest way is to run ccmake .. (that’s not a typo, there are two c’s in the program). This will give you a graphical version of CMake, allowing you to see what is going to be compiled and installed.

  33. Chris September 29, 2015 at 12:01 am #

    Outstanding step-by-step. Rarely do I ever find a guide that I can execute line-for-line with 0 modifications.

    • Adrian Rosebrock September 29, 2015 at 6:01 am #

      Awesome, I’m glad it worked for you Chris! And thanks so much for the kind words. 😀

  34. Paul September 29, 2015 at 8:20 pm #

    Hello, I have done this instruction yesterday. However, Im getting this error after rebooting the ubuntu server. Could you let me know which configurations Im missing? Thanks.


    ~/opencv/build$ workon cv
    workon: command not found


    Self answering to the question.. I went back to step 8 to set up virtual environment..

    • Adrian Rosebrock September 30, 2015 at 6:29 am #

      Nice, I’m glad it’s working for you now Paul 🙂

  35. Shia September 30, 2015 at 12:19 pm #

    Hi,
    I am new to python,opencv. I followed these instructions and installed opencv,python on my machine(unix). It created two main folders opencv and opencv-contrib with many files and sub dirs.
    I started saving my source files in opencv folder. Initially simple programs were executing perfectly but now i see for imports like
    import matplotlib.pyplot as plt
    import numpy as np
    import scipy.signal as signal etc, it keeps giving ImportError, “eg:ImportError: No module named matplotlib.pyplot” for many such imports. I then have to google and find the solutions.
    I then install the missing module again in the current dir. Exactly which directory should I work into, to avoid this problem?

    P.S- I understand that this might be a very basic question, but i will greatly appreciate your help. I am just a beginner student here and for now I am getting so loaded with my assignments that I am not getting time to figure out the problem and its solution by myself, trying to implement algorithms from my assignments is eating up all my time.

    • Adrian Rosebrock October 1, 2015 at 6:13 am #

      Did you follow the instructions exactly as detailed in this post? You’ll need to make sure you’re in the cv virtual environment, but afterwards, installing the extra packages is easy:

      I would suggest reading up on pip and how it allows you to install packages into Python. Also, there is no need to save your own Python scripts in the opencv folder. Provided you followed the instructions on this post, you can execute your scripts from anywhere on your machine.

  36. Michel October 4, 2015 at 10:25 pm #

    According to docs,
    http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours

    findContours returns 2 values – contours, hierarchy not 3. And when changed it to
    (_, cnts) = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    it gives error
    in
    c = max(cnts, key=cv2.contourArea)

    • Adrian Rosebrock October 5, 2015 at 6:33 am #

      You can read more about the change in cv2.findContours return signature in OpenCV 3 here — what you linked to is the OpenCV 2.4 docs, not the OpenCV 3 docs.

      A quick fix to get cv2.findContours to work in both OpenCV 2.4 and OpenCV 3, just modify the code slightly:

      And that will take care of everything.

  37. Michel October 5, 2015 at 11:11 am #

    Thanks, Adrian. The fix worked like a charm!

    • Kouki April 24, 2016 at 4:59 pm #

      I am grateful for this great tutorial and for your response I had the same error and that fixed it.

      Thanks a lot!

  38. Toby October 7, 2015 at 1:18 am #

    When I execute make -j4 it says – the source directory “home/toby/opencv/build/ -j4 does not exsist”. Any help thanks

    • Adrian Rosebrock October 7, 2015 at 6:29 am #

      You need to preface that with a “make”:

      $ make -j4

  39. Saurabh Bisht October 11, 2015 at 7:55 am #

    Everything went right before this at the last step

    • Adrian Rosebrock October 11, 2015 at 8:14 am #

      Make sure the cv2.so file is properly sym-linked into your site-packages directory.

      • Saurabh Bisht October 11, 2015 at 8:29 pm #

        can you please tell me the procedure to do that.

        • Adrian Rosebrock October 12, 2015 at 6:54 am #

          Please see Step 7 of this tutorial. The trick is to make sure the path to the site-packages directory is correct for your installation of Python.

  40. hacklavya October 17, 2015 at 3:09 am #

    I installed succ. fully with this tutorial, thanks.

    and in the installation terminal I ended up with (CV) before myname@computername and I can do import cv2
    but
    when I open a new terminal, I can not see (CV) on command prompt and also I can’t do import cv2.
    is it some environment thing?

    • Adrian Rosebrock October 17, 2015 at 6:43 am #

      Make sure to run workon cv in your new terminal and that will drop you down into the virtual environment, allowing you to import cv2 successfully.

      • hacklavya October 17, 2015 at 3:41 pm #

        Thanks for the answer
        but
        how can I go into that environment (which command to use)?
        I found following commands do it:
        source /usr/local/bin/virtualenvwrapper.sh
        source ~/.bashrc
        mkvirtualenv cv
        am I right? or there is short and easy way?

        • Adrian Rosebrock October 18, 2015 at 7:05 am #

          If you want to access that virtual environment again, all you need is:

          The mkvirtualenv command is only used to actually create the virtual environment.

          • Betty November 9, 2015 at 6:03 am #

            Hi. I also want to go into that environment but I found that it is “workon: command not found” after I followed your step.
            Thank you!

          • Adrian Rosebrock November 9, 2015 at 6:26 am #

            Take a look at the “Troubleshooting” section of this post. While it’s for the Raspberry Pi, it can help diagnose why the workon command is not working for you.

  41. Mohamed Ragab October 19, 2015 at 11:26 am #

    Hi Adrain,

    I have a problem with >>> import cv2,that not modules with this name and I am sure cv2.so is exited in package modules. Thanks for your efforts.

    • Adrian Rosebrock October 20, 2015 at 6:18 am #

      If you are sure that the cv2.so file is in the site-packages directory of the cv virtual environment, then make sure you’re typing it:

      $ workon cv

      To drop down into the cv virtual environment prior to running Python or trying to import OpenCV.

  42. Arpit Joshi October 19, 2015 at 12:42 pm #

    I followed all the steps you told above.
    After installing, in am in the cv environment and it successfully imports cv2.

    I exit the terminal and then open it again and type import cv2
    It gives me an error saying cv2 not defined

    also if i type workon cv it gives me an error saying workon not defined.

    I followed each and every step. I hope you can help me out on this one! Thanks

    Also can this be installed without the virtual env? I really dont want to type workon cv again and again. Can’t we directly install it on the ubuntu system?

    • Adrian Rosebrock October 20, 2015 at 6:16 am #

      The virtual environment is certainly optional, it’s just a best practice for Python development.

      If you are getting an error that the workon command is not found, go back and make sure your ~/.bashrc was updated correctly. If you’re still getting an error, try directly sourcing it via: source ~/.bashrc

  43. Krystof October 26, 2015 at 1:13 am #

    Nice tutorial, Adrian.

    If you’re using virtual environments (very reasonable), why do you install the library globally (‘sudo make install’)? Wouldn’t it be more sensible to keep it in the build directory and point the virtual environment there?

    Ultimately the reason I ask is because I’d like to have OpenCV 2.x side-by-side with OpenCV 3.x, and it seems to me that virtual environments are the right way to do it, but it also seems to me that installing the lib globally is not conducive to this effort.

    • Adrian Rosebrock October 26, 2015 at 6:13 am #

      If you want to avoid having OpenCV itself installed globally, just be sure to keep the build directory after the compilation has finished (everything else can be deleted). Then you can sym-link in the cv2.so file into your virtual environment and it will work just fine.

      It involves a few more steps and I didn’t want to include it since this tutorial is meant for those who are just getting started building OpenCV and installing it.

  44. Hacklavya November 8, 2015 at 2:52 pm #

    I CAN install scikit-image outside the virtual env. but I CAN’T install and use scikit-image in virtual env, and because of that I am unable to make any progress, please include the step(s) for scikit-image installation inside virtual env, so that I can use skimage on following command prompt.

    • Adrian Rosebrock November 8, 2015 at 3:18 pm #

      Installing scikit-image inside the virtual environment is just as easy as installing it outside the virtual environment, just let pip do the work for you:

  45. Ben Lansdell November 22, 2015 at 1:49 am #

    Thanks a lot for this, very useful.

    I found this guide worked perfectly on Ubuntu 14.04 on a machine without CUDA installed.

    On an Ubuntu machine with CUDA installed I needed to follow the advice here:

    http://stackoverflow.com/questions/31663498/opencv-3-0-0-make-error-with-ffmpeg

    and edit some of the source code to replace AVCodecID with CV_CODEC_ID.

    • Adrian Rosebrock November 22, 2015 at 7:15 am #

      Thanks for sharing Ben!

    • Ben Lansdell November 22, 2015 at 2:39 pm #

      With a little extra thought, it’s unlikely the AVCodecID is actually related to CUDA. Nonetheless, it may still be useful for some to know that this correction is needed for some setups.

  46. Michael December 23, 2015 at 4:21 pm #

    Hello
    and I solved the problem, but now I have another not imported into the terminal well opencv
    Now open my subleme_text editor and executor find_game.py code directly from there I get this error
      import cv2
    ImportError: No module named cv2
    is not it should
    thank you for your help

    • Adrian Rosebrock December 23, 2015 at 6:40 pm #

      Make sure you are in your Python virtual environment before executing the find_game.py script:

      workon cv

      And then execute the Python script.

  47. Beat Tödtli December 25, 2015 at 6:59 am #

    Hi,
    thanks!
    In step 10 I needed a “cd ..” before
    make -j4
    Kind regards,
    Beat

  48. Aishwarya December 25, 2015 at 12:54 pm #

    Thanks a ton! Works for me!

    • Adrian Rosebrock December 25, 2015 at 1:44 pm #

      Glad to hear it Aishwarya! 🙂

  49. Srikar December 26, 2015 at 11:27 am #

    Thank You for the post!
    I tried a lot to install on windows first but it wasn’t coming, so I did it on Ubuntu now and it works! 🙂

    • Srikar December 27, 2015 at 2:33 am #

      Also, am having this problem:
      Once it worked and I closed my terminal window and opened a new one and I type “workon cv”,”python”,”import cv2″ I get errors such as not found or no command, I realised I may not be in the correct directory, can you help me how to open it ?
      As am using Ubuntu for the first time I don’t have much idea…
      Thanks!


      Hello,

      I have found the solution to my problem, I have seen that many people are facing the same problem and thought I should share…

      Once you open the terminal window start from step 8 with “$ sudo rm -rf ~/.cache/pip”
      and follow all the commands upto ” mkvirtualenv cv”
      Then you enter cv envirnoment and you can open python and import cv.

      Thank You!

      • Adrian Rosebrock December 27, 2015 at 7:56 am #

        After you have already created the mkvirtualenv cv command once, all you need is the workon cv command to drop down into the virtual environment. From there, you’ll be able to import OpenCV.

    • Adrian Rosebrock December 27, 2015 at 7:55 am #

      Glad to hear it Srikar! 🙂

  50. Mahmoud Tolba December 31, 2015 at 9:07 am #

    Hi Adrian,

    Thanks for the great tutorials.

    I installed OpenCV 3.0 and python 2.7 on a virtual environment cv and it works well.

    Now I want to install OpenCV 2.4.11 on another virtual environment to run the old codes instead of modifying them to work with OpenCV 3.0, Should I start with creating a new virtual environment called cv2 for example and continue with the rest of steps?

    • Adrian Rosebrock December 31, 2015 at 1:47 pm #

      Yes, the first step would be to create a new virtual environment. Then, follow the same steps using this tutorial

      However, do not run make install! Simply run make and then sym-link in the cv2.so into your new virtual environment. All other files and directories besides build/lib can then be deleted.

  51. karun January 4, 2016 at 11:08 am #

    hello first of all thanks for a great tutorial
    am new to this kind of stuffs and while executing one of the programs i get an error saying that

    orb = cv2.ORB()
    AttributeError: ‘module’ object has no attribute ‘ORB’

    and

    also some errors in
    xfeatures2d while using sift

    • Adrian Rosebrock January 4, 2016 at 12:00 pm #

      I would (1) check your OpenCV 3 version and (2) read this post on accessing SIFT/SURF in OpenCV 3.

  52. Moataz January 7, 2016 at 12:30 am #

    Adrian,

    I followed every step on a clean VBox machine.
    Everything is run without errors. but there is no cv2.so file on the machine at all.
    There exist a bunch of so files related to cv (i.e. libopencv_*.so.*) in ‘/usr/local/lib/’ just setting there next to the python folder!

    Any help is much appreciated.

    • Adrian Rosebrock January 7, 2016 at 6:36 am #

      The cv2.so file should be in /usr/local/lib/python2.7/site-packages/. You should also check build/lib to see if it was compiled. If there is not a cv2.so file in your build/lib directory, I would suggest double checking the output of your CMake command to ensure Python 2.7 was properly picked up for the build.

  53. Pewe January 25, 2016 at 9:03 am #

    hi Adrian, nice tutorial btw,
    when i followed your tutorial, i already have installed OpenCV2.4.11, and your tutorial works great, there’s no error on OpenCV3. But, is there any way to use OpenCV2.4.11 back instead of OpenCV3? when i first install OpenCV3, i made no changes at the Cmakelists but it still works. thanks

    • Adrian Rosebrock January 25, 2016 at 4:09 pm #

      Running a multi-version install of OpenCV on the same system is non-trivial, but the easiest way is to install OpenCV 3 first. Then, create a new build for OpenCV 2.4. After OpenCV 2.4 successfully compiles, you can delete everything but the build/lib directory. Then create a new Python virtual environment and sym-link in the cv2.so file from the build/lib OpenCV 2.4 directory.

  54. Sridhar January 25, 2016 at 1:21 pm #

    fatal: unable to access ‘https://github.com/Itseez/opencv.git/’: Could not resolve host: github.com
    What do i do??

    • Adrian Rosebrock January 25, 2016 at 4:04 pm #

      Make sure your internet connection is working properly and can load GitHub.com.

  55. Dhivya January 27, 2016 at 1:17 am #

    Hi Adrian,
    Nice tutorial! I followed this tutorial and reached till step 12. I am able to see the cv2.so file in the site-packages. But when I import cv2 in python from the virtual environment, it says “no module named cv2”.
    I am working on Ubuntu 14.04. Can you help with troubleshooting this?

    • Adrian Rosebrock January 27, 2016 at 3:06 pm #

      I would suggest reading through the “Troubleshooting” section on this post.

  56. Ajay Yadav January 28, 2016 at 5:42 am #

    Hi Adrian,
    Very nice tutorial, I followed all the steps. It works good.

    • Adrian Rosebrock January 28, 2016 at 3:39 pm #

      Awesome, I’m glad it worked for you Ajay! 🙂

  57. Todd Brick January 28, 2016 at 4:18 pm #

    Hi Adrian, I got your package with the VM; I am learning a lot and having fun. I was installing the SITL environment from diydrones on it that has some OpenCV code to do a map and icon display. It was obviously written for 2.4.X since it can’t find cv2.cv functions like CreateMatHeader() and SetData(). Do you have any suggestions for replacements for these that will work with 3.0? I can’t seem to find any good docs on how to transition 2.4.X code to 3.X OpenCV code. Thanks, Todd

    • Adrian Rosebrock January 28, 2016 at 6:07 pm #

      Actually, those old cv2.cv bindings pre-date OpenCV 2.4! They are seriously old.

      I personally have tried this, but you should be able to compile OpenCV with the old functionality as well. Go back to the CMake command and try adding --BUILD_opencv_world=ON to he command. The “world” references the old legeacy cv2.cv bindings. If that switch doesn’t work, try running ccmake .. from the build directory and enabling anything related to the “world”.

      Otherwise, you should consider getting an OpenCV 2.4.X installation going. OpenCV 2.4 still has support for the super old bindings.

  58. Ram January 29, 2016 at 1:28 am #

    Hi Adrian,
    The build completed without errors..However when i type workon cv it says command not found..i also did the previous step involving creation of symbolic linkage

    • Adrian Rosebrock January 29, 2016 at 7:32 am #

      If the workon command is giving you trouble, it sounds like your .bashrc file was not updated correctly. Take a look at “Step 8” of this tutorial and also give the “Troubleshooting” section of this post.

  59. Christian February 1, 2016 at 8:28 am #

    Small typo in step 8: “To make the changes to our ~/bashrc file take affect,…” Dot missing in ~/.bashrc. Also it should be “effect” not “affect”.

    • Adrian Rosebrock February 2, 2016 at 10:37 am #

      Thanks for letting me know Christian!

  60. Rig February 8, 2016 at 8:36 pm #

    Hey Adrian, will the cv2.VideoCapture feature work straight away with this install or do the uv4l drivers need to be installed during this setup to allow webcam access? Thanks for another great tutorial btw!

    • Adrian Rosebrock February 9, 2016 at 4:01 pm #

      Are you accessing a USB webcam or the Raspberry Pi camera module? For the USB webcam, you don’t need the UV4L drivers and cv2.VideoCapture should work out of the box.

  61. Joshua February 19, 2016 at 7:13 am #

    Hi Adrian!

    I followed all the steps you indicate and everything has been installed without errors . On completion, check the version of your cv and find_game.py example has worked. But after reboot I get the error ” no module named numpy “, and “no module named CV2 ” . I do not know what could have happened . thanks

    • Adrian Rosebrock February 19, 2016 at 7:24 am #

      Make sure your ~/.bashrc file has been successfully updated and the changes to include virtualenv and virtualenvwrapper are still there. If so, be sure to enter the cv virtual environment before executing your Python script:

      • joshua February 22, 2016 at 6:49 am #

        thanks, i update my ~/.bashrc and now works.

        • Adrian Rosebrock February 22, 2016 at 4:19 pm #

          Congrats on resolving the issue, Joshua!

  62. suresh February 24, 2016 at 3:00 pm #

    I just followed your steps and in the first try everything went smooth and it is installed successfully. By the way I am using Linux Mint 17.3 Mate. Your installation steps are just WOW. Thanks a lot. 🙂

  63. Sandeep February 25, 2016 at 2:48 pm #

    This is a great resource! Thanks for these installation instructions. Coming from someone who has ran into all sorts of issues while compiling OpenCV + Python on Ubuntu and Mac.

  64. panini February 26, 2016 at 4:31 am #

    Thanks a million!
    Very clear, and step by step. Needed to rope in the contrib repo and thanks for getting me to do the virtual env… been dragging my feet on that 🙂

    • Adrian Rosebrock February 26, 2016 at 1:50 pm #

      No problem, happy to help 🙂

  65. Ravi Naik March 5, 2016 at 8:27 am #

    Thanks a lot Adrian…Finally I could get Opencv working on my ubuntu…Thanks for your precise instructions…

    • Adrian Rosebrock March 6, 2016 at 9:17 am #

      No problem Ravi, congrats on getting OpenCV installed!

  66. Cédric March 5, 2016 at 5:36 pm #

    So I am just wondering what the point of the virtual environment is. How do I get to use OpenCV when I open up a new terminal. I have some python scripts, that are scattered all around my computer, and they usually tell me that they do not know the module cv2. Can you tell me how to make the module recognised everywhere?

    • Adrian Rosebrock March 6, 2016 at 9:17 am #

      The point of using Python virtual environments is to have an isolated environment for each project you are working on. For example, suppose you are working on a project that requires package X v1.0. But another project you are working on requires package X v2.0. What do you do? Buy another machine? Install a VM? Nope. Instead, you use virtual environments. Each Python virtual environment is entirely isolated from the system install of Python, therefore you can install different versions of Python packages without having to worry about conflicts.

      In either case, you need to execute the workon command before running your Python script to drop you into the cv virtual environment. Inside this virtual environment, the OpenCV bindings can be found and recognized:

  67. Greg S March 9, 2016 at 3:49 pm #

    I meant this worked great 🙂 Haha sorry

    • Adrian Rosebrock March 9, 2016 at 4:36 pm #

      I’m glad it worked for you Greg! 🙂

  68. Mathys March 19, 2016 at 8:46 am #

    Hi man thanks for the tutorial(and all the other).

    I have followed all the steps and redone them as well but no luck.

    when I import cv2 it says no such file.

    so I checked with sudo find / -name “cv2.s0” and it says its in my home/mathys/.virtualenvs/cv/lib/python2.7/site-packages/cv2.so

    but when I go into that file it is empty, theres no cv2.so
    Another thing as you mentioned earlier to check my cmake output under python2 everything looks the same as the sreenshot you gave a link too but where it says libraries it says no instead of something like usr/lib/libpython2.7.so

    Many thanks
    Mathys

    • Adrian Rosebrock March 19, 2016 at 9:07 am #

      If you ran find and it returned a cv2.so file, then there is absolutely in the /home/mathys/.virtualenvs/cv/lib/python2.7/site-packages/ directory, so I would double check there again.

      • Mathys March 19, 2016 at 9:47 am #

        Man why must life be so hard haha

        I just double checked it again I go into computer/home/mathys/ and then there is no folder called virtualenvs

        • Adrian Rosebrock March 20, 2016 at 10:45 am #

          It’s a hidden directory. Note the “.” before the directory path “.virtualenvs”

  69. Felipe March 29, 2016 at 12:19 pm #

    Excellent guide! Thanks a lot.

    • Adrian Rosebrock March 29, 2016 at 3:37 pm #

      No problem Felipe, I’m happy the guide helped!

  70. mikele March 31, 2016 at 10:49 am #

    Awesome! Everything works perfectly. Thank you.

    • Adrian Rosebrock March 31, 2016 at 2:53 pm #

      Glad to hear it! 🙂

  71. Jacob S April 1, 2016 at 12:55 pm #

    Hi,

    I got everything set up alright, but when running the example script (copied into a python file), I get the error “The lower bounary is neither an array of the same size and same type as src, nor a scalar in function inRange”

    I’m using python 2.7 and openCV 3.1

    Any idea what’s causing this?

    • Adrian Rosebrock April 1, 2016 at 3:11 pm #

      Double check your path passed to cv2.imread. Typically you’ll see cv2.inRange throw that error message when the image path is invalid and thus the image is not properly loaded from disk.

  72. Paul April 4, 2016 at 4:07 am #

    Excellent installation guide! I successfully installed OpenCV 3.1.0 in Ubuntu with the help of this article.

    • Adrian Rosebrock April 4, 2016 at 9:25 am #

      Awesome job Paul! 🙂

  73. Mo Badawy April 8, 2016 at 6:10 am #

    Hi Adrian, Awesome tutorial but I ran into a snag. Regardless of what I do, querying the opencv version yields 2.4.8!!! After some digging it turned out that Linux Mint 17.3 Cinnamon have a dependency on opencv, so opencv 2.4.8 was already installed by system. Uninstalling opencv (2.4.8) => remove Cinnamon (among others as well, e.g. Blender) as well. Cinnamon is Mint’s Desktop Environment, so removing it is not an option.

    So, here is what I did and it seems to be working so far 🙂
    1. In step 10, in the cmake command, change the installation directory to a local folder, e.g. use “-D CMAKE_INSTALL_PREFIX=/home//local \”
    2. Then use “make install”, i.e. drop “sudo”.
    3. cd to the installation folder and search for “cv2.so” via:
    $ find ./ -iname cv2.so
    4. Copy the resulting folder’s path (e.g. /home//local/lib/python2.7/dist-packages) and add it to $PYTHONPATH by doing the following:
    – Use your favorite text editor to open/create ~/.profile
    – Add the line: export PYTHONPATH=$PYTHONPATH:/home//local/lib/python2.7/dist-packages
    – Save & exit, then $ source ~/.profile
    5. Loading the cv2 module in Python & checking version now yields 3.1.0, while system dependence on opencv 2.4.8 is uninterrupted.

    • Adrian Rosebrock April 8, 2016 at 12:51 pm #

      This is fantastic, thanks so much for sharing! 🙂

  74. Anagh April 10, 2016 at 1:25 am #

    Hello Adrian ,

    When i used git checkout 3.0.0 in opencv_contrib
    i m getting this error

    HEAD is now at 6123e89… Merge pull request #247 from cbalint13/daisy

    • Adrian Rosebrock April 13, 2016 at 7:09 pm #

      That’s not an error message at all — it’s just the output of running git checkout.

  75. Emre April 18, 2016 at 7:21 am #

    Hey Adrian. After installing and uninstalling for 5 times finally i installed the opencv 3.1.0 successfully. Thanks for the great tutorial .

    • Adrian Rosebrock April 18, 2016 at 4:45 pm #

      Congrats Emre!

  76. ed April 26, 2016 at 12:00 pm #

    Hi, Great tutorial! Hopefully a simple issue, for some reason the game.jpg is rendered smaller that is should be e.g I can’t see the red game. It’s not shrunk compared to the original in ratio but it looks more cropped with the top left hand corner be OK extending out to 50% of the original image.

    Thanks,

    Ed

  77. Ronald April 29, 2016 at 6:28 pm #

    Thanks a lot for taking time to write this amazing tutorial

    • Adrian Rosebrock April 30, 2016 at 3:52 pm #

      No problem Ronald, I’m happy I could help 🙂

  78. ruben May 11, 2016 at 9:49 am #

    hello adrian!!! i have this problem when compiling program!!!

    can you help me?
    thanks!!!

    • Adrian Rosebrock May 12, 2016 at 3:43 pm #

      Hi Ruben — please read the comments before posting. Michel and I have discussed the problem and solution in a comment above.

  79. ruben May 13, 2016 at 11:19 am #

    thanks a lot!!!!!

    • Adrian Rosebrock May 13, 2016 at 11:30 am #

      No problem Ruben!

  80. Swapnil May 18, 2016 at 2:19 am #

    Hey Adrian after installing opencv, writing the code and downloading the image i am continuously getting assertion failed error i.e. its not reading the image but it works fine for videos. How can I fix this ??

    I have saved the image in the same directory of the python code and tried several path name apart from “games.jpg” or “games.jpeg” but its showing the same error every time.

    • Adrian Rosebrock May 19, 2016 at 6:09 pm #

      It sounds like OpenCV is having issues loading images from disk, but it able to process videos just fine. In that case, you might have skipped over Step 3 where the image I/O libraries are installed.

  81. Vania May 18, 2016 at 8:34 pm #

    Hi Adrian!

    Could we install both OpenCV 2.4 and 3 on MacOS?

    • Adrian Rosebrock May 19, 2016 at 6:01 pm #

      Absolutely! The process is a bit more complicated, but you need to compile both of them — but don’t run make install. Instead, you’ll want to delete everything but your build directory and then sym-link the cv2.so files into their corresponding virtual environments.

  82. mayank May 20, 2016 at 4:54 pm #

    I think the order of executing these commands must be reversed.

    First sourse then export

    1: source /usr/local/bin/virtualenvwrapper.sh
    2: export WORKON_HOME=$HOME/.virtualenvs

    If i followed the the order mentioned in post and it could not recognize mkvirtualenv command.

    BTW great post adrian.

    • Adrian Rosebrock May 21, 2016 at 8:13 am #

      According to the install instructions, the export comes before the source. I’m not sure why it would need to be reversed in your case.

  83. Douglas May 25, 2016 at 3:35 pm #

    Hello Adrian, is there any good tutorials like your that you know that does not use cv virtual envs ?

    i ask this because i need to call my python scripts from another aplication (unity) but i can’t find a way to do that using opencv virtual envs, basicaly i need the command “python script.py” to run my script using opencv without any virtualenv

    • Adrian Rosebrock May 25, 2016 at 5:38 pm #

      You can still use these same instructions to install OpenCV with Python bindings without the virtual environment. Just skip Step #8 and Step #11. Also, take note of which site-packages directory the output cv2.so bindings will be stored. That will help debug any issues when it comes to import cv2.

      Alternatively, if you need to call a Python script via a separate application, you can just as easily call a shell script form a separate application. The shell script can them access the Python virtual environment and then call the Python script, just like I do in this post.

      • douglas May 29, 2016 at 3:50 pm #

        Thank you for your answer, i am just having trouble with the “source /home/pi/.profile” path, i don’t know what path should i use in my machine, whats default ?

        note: i followed the tutorial exactly.

        • Adrian Rosebrock May 29, 2016 at 4:34 pm #

          Are you trying to install on Ubuntu or a Raspberry Pi? The tutorial you’re currently following is for Ubuntu. On Ubuntu, the path should be /home//.bashrc. On the Pi it should be /home/pi/.profile

      • douglas May 29, 2016 at 4:30 pm #

        Thank you Adrian !

  84. Yash K May 30, 2016 at 3:02 am #

    Hi,
    I proceeded as mentioned in the tutorial and everything went as per the tut till step 10 .
    While using the ln command I got this error:
    ln: failed to create symbolic link ‘cv2.so’: File exists

    and cv2.so file is empty and when i start working cv2 is not found

    • Adrian Rosebrock May 31, 2016 at 4:04 pm #

      Take a look at your output of CMake, specifically the “Site packages configuration for Python. This is where your cv2.so bindings will be stored. Then, sym-link this file into the site-packages of your virtual environment.

  85. y2k May 31, 2016 at 12:22 am #

    Hi Adrian
    Amazing tutorial but I am having a problem executing the last step , it is saying

    ln: failed to access ‘cv2.so’: Too many levels of symbolic links

    So can you please suggest a work around to this or a way to resolve this , thanks in advance

    • Adrian Rosebrock May 31, 2016 at 3:43 pm #

      That’s quite strange, I’ve never encountered that error message before. It sounds like your sym-links are not valid. Can you double-check that the cv2.so file exists?

  86. Michael June 7, 2016 at 11:40 am #

    Hi Adrian,

    Thanks for providing the tutorials for this and installing openCV with python 3 I was wondering what you gain (or loose) from using python 3 over python 2 with OpenCV3 in particular and what config would you recommend?

    Also any advice for installing OpenCV3 as part of a cluster requirement (having OpenCV3 available on every machine in the cluster) I see the benefit of the virtual environments but I expect my issue will be getting 1 consistent environment and deploying it to a cluster

    • Adrian Rosebrock June 7, 2016 at 2:48 pm #

      There aren’t any real benefits to using OpenCV 3 with Python 2 or Python 3 — simply go with the Python install that you feel most comfortable with.

      As for installing OpenCV in a cluster environment, I would suggest creating a “master image” that has OpenCV installed and then replicating it across each of the systems in the cluster.

      • Michael June 7, 2016 at 3:46 pm #

        Thanks, python version would be 1 less job to worry about. The master image route have been my preference but beggars can’t be choosers.

        Is it possible to run the “make install” command from outside the build directory and should I run cmake -> make -> make install on every system in the cluster or can I get away with just “make install”

        • Adrian Rosebrock June 9, 2016 at 5:32 pm #

          Is your hardware and OS the same? Because if so, you can just compile OpenCV once, copy the build directory to each machine, and run make install.

          If the environment is different for each system, then yes, you’ll need to run cmake, make, and make install on every system.

  87. XuanToa June 9, 2016 at 9:25 pm #

    Hi Adrian,
    Thanks for your tutorials.
    To enter the cv enviroment, I must follow 4 steps
    1. export WORKON_HOME=$HOME/.virtualenvs
    2. source /usr/local/bin/virtualenvwrapper.sh
    3. source ~/.bashrc
    4. workon cv

    If i just use ” workon cv”, I always get an error: command not found.

    Can you solve this problem?

    • Adrian Rosebrock June 12, 2016 at 9:44 am #

      Hey XuanTao — it looks like your ~/.bashrc file has not been updated correctly. Make sure you open it:

      $ nano ~/.bashrc

      And ensure you have included:

      At the bottom of the file.

  88. Sagar June 13, 2016 at 11:38 am #

    Can I just delete the opencv and opencv_contrib folders once I finish installing?

    Thanks

    • Adrian Rosebrock June 15, 2016 at 12:46 pm #

      Yes, once you have finished installing OpenCV, feel free to delete both the opencv and opencv_contrib directories.

  89. Akshat Jain June 15, 2016 at 1:39 pm #

    Hey
    I need to get opencv working on Apache Storm and everytime I install opencv and import it inside I keep getting an error that the requirements for cv2 were not met. Any ideas on how I should go around this error?

    • Adrian Rosebrock June 18, 2016 at 8:31 am #

      Apache Storm is a distributed system for computation — which means that all nodes on your system need to have OpenCV installed on them. This includes all the dependency libraries, NumPy, etc. I haven’t tried to install OpenCV specifically on a Storm cluster before, so it’s pretty hard for me to diagnose this error.

  90. Dinesh June 17, 2016 at 5:05 am #

    Hey Adrian! Excellent Tutorial. But I have a small query in Step 10. I have read the comment and haven’t found anyone asking this. Should we exit the virtual environment ‘cv’ when you say ‘Our environment is now all setup — we can proceed to change to our home directory’.
    I did cd ~ but that still keeps me in the virtual environment ‘cv’.. So am I supposed to continue there or exit the virtual environment before I clone the git?

    • Adrian Rosebrock June 18, 2016 at 8:19 am #

      Once you enter the cv virtual environment, you should remain in that virtual environment for the remainder of the tutorial. Do not exit the cv virtual environment.

  91. Youssef June 18, 2016 at 8:56 pm #

    Hello Adrian!
    So I have run this whole tutorial around 5 times now and I am almost certain I did not miss any steps. I watched the other video from your tutorial with the Pi and viewed the troubleshooting section of that page. However, I cant seem to make the error go away.
    ImportError: No module named cv2
    when i go to ~/.virtualenvs/cv/lib/python2.7/site-packages/ there is a cv2.so, when I double click it it says
    “This link cannot be used because its target “/usr/local/lib/python2.7/dist-packages/cv2.so” doesn’t exist.”
    The /usr/local/lib/python2.7/site-packages directory is always empty. I think it should have the cv2.so file there or something, but it doesn’t have it.
    I am not sure what I may b doing wrong.

    I am fairly new to linux so my lingo is still new, I will try to understand your explanation.
    Thank you for your time.

    • Adrian Rosebrock June 20, 2016 at 5:32 pm #

      Hey Youssef — unfortunately, there are many reasons why you might not be able to import OpenCV. I detail the main reasons in the “Troubleshooting” section of this post.

  92. Raul June 28, 2016 at 8:41 am #

    Hello Adrian,

    Thanks for the great tutorial. I noticed the upper threshold used to retrieve the red game is practically the blue color. Could you explain the reasoning behind this? What changes should I make to retrieve the red instead? I tried messing around with the range values but was not successful.

    • Adrian Rosebrock June 28, 2016 at 10:47 am #

      RGB colors are actually stored in reverse order in OpenCV, so we actually write them in BGR order. To detect red, you’ll want to change the range values. Given the new knowledge regarding RGB versus BGR order, this should help 🙂

      • Raul July 1, 2016 at 2:46 pm #

        Hi Adrian!

        Thanks for getting back at me. In my last comment I meant to say blue instead of red!

        Taking into account your comments about the color ordering I fiddled around with the thresholds but I did not get the results I expected.

        These are the values I attempted:

        upper = np.array([255, 65, 65])
        lower = np.array([150, 0, 0])

        • Adrian Rosebrock July 1, 2016 at 2:52 pm #

          It’s a game of trial and error, and based on your lighting conditions, colors can appear differently. I would suggest using the range-detector script inside the imutils library to help you tune the threshold.

  93. Aris July 4, 2016 at 3:40 pm #

    Thanks for this easy to follow tutorial. Just to add a little on it if I could.

    After following all with no Error whatsoever I started having the following error while trying to follow the simple example at the end: (pyhton find_game.py)

    ImportError: No module named cv2

    It happened for the simple reason that, as a beginner, I failed to understand how to update the ~/.bashrc file

    The line at Step 8 are supposed to be added to the ~/.bashrc file. Not to be copy and paste in the terminal which I did in my first attempt.

    You should copy those lines

    use the following line to open the ~/.bashrc file

    $ sudo nano ~/.bashrc

    Copy and paste the code there and source the ~/.bashrc file.

    This is how I solved the “ImportError: No module named cv2”

    Thanks Adrian

    • Adrian Rosebrock July 5, 2016 at 1:46 pm #

      Hmmm, it’s strange that you would need to be sudo for this — that shouldn’t be necessary at all. Were you trying to install OpenCV as the “root” user? Or a standard, normal user?

      • Aris July 6, 2016 at 7:08 am #

        I was just trying to access the .bashrc file. May be I did not need to be sudo.

  94. San July 8, 2016 at 1:37 pm #

    Works like a charm, thanks Adrian.

    • Adrian Rosebrock July 9, 2016 at 7:24 am #

      Fantastic, glad to hear it San! 🙂

  95. Gray July 26, 2016 at 12:51 pm #

    Thank you so much for this tutorial! I used it to install OpenCV 3.1 and Python 2.7 on an Ubuntu VM on VirtualBox. It worked very smoothly and I was able to try out the demo program by the end of the day. After tearing my hair out trying to work out how to install OpenCV for Python on my Windows 8.1 machine this was so much easier. Thank you, sir.

    • Adrian Rosebrock July 27, 2016 at 2:00 pm #

      Nice job getting OpenCV installed on your Ubuntu machine, congrats!

  96. Antoine August 9, 2016 at 5:12 am #

    Many thanks Adrian for you tutorial !
    Hard time to get it working on Linux Mint 17.2 but (after many tries) using the opencv zip file its done !
    Gotta catch the red !

    • Adrian Rosebrock August 10, 2016 at 9:30 am #

      Congrats on getting OpenCV installed Antoine! 🙂

  97. Alex August 17, 2016 at 9:01 am #

    In my cmake report I have not the “packages path” neither for python 2.7 nor for python 3.4. I’ve followed all the instruction unless the use of the virtual environment. As result, i can see all the opencv modules compiled (i.e., all the .so files) in the “/usr/local/lib” directory but the cv2.so file. And I still not find such a file.

    • Adrian Rosebrock August 17, 2016 at 11:58 am #

      It sounds like your site-packages directory is not being automatically determined for some reason. Try updating your cmake command to use the following switch:

      -D PYTHON_PACKAGES_PATH=$VIRTUAL_ENV/lib/python2.7/site-packages

  98. Anushree August 28, 2016 at 8:01 am #

    Looks like i forgot to install numpy. How to go about it now?

    • Adrian Rosebrock August 29, 2016 at 1:57 pm #

      You can install NumPy by accessing your virtual environment and using pip. Go back to Step #9 and continue with the tutorial.

  99. Abder-Rahman August 28, 2016 at 7:45 pm #

    Very nice and detailed tutorial, keep it up!

    Thanks for your kind efforts.

    • Adrian Rosebrock August 29, 2016 at 1:55 pm #

      Thank you Abder-Rahman, I appreciate that 🙂

  100. Luis September 1, 2016 at 3:04 pm #

    Hi Adrian,

    When I run :
    make -j4

    CUDA causes problems because my default gcc version is 5.4, and CUDA only works with gcc version 4.9 & below. I have followed the guidance at

    http://stackoverflow.com/questions/6622454/cuda-incompatible-with-my-gcc-version

    to solve the problem, but it is not working. So I have three questions:

    1. Is it possible to eliminate the dependence on CUDA?

    2. If so, how?

    3. What functionality in openCV might I lose if I were to do so?

    • Adrian Rosebrock September 2, 2016 at 7:03 am #

      You can compile OpenCV without CUDA support. If your CMake command is automatically picking up CUDA support (likely due to NVIDIA/CUDA drivers already being installed), then just disable them within your CMake command by adding the following switch:

      -D WITH_CUDA=OFF

      You won’t lose much support, just any GPU optimizations that would have otherwise been installed.

  101. Gaurav September 5, 2016 at 6:53 am #

    After following all the steps my cv2 version is 2.4.8
    How to create virtual environment for cv2??
    Unable to run sift

    • Adrian Rosebrock September 5, 2016 at 7:58 am #

      It sounds like you may not be in the cv virtual environment. Make sure you are in the correct Python virtual environment using mkvirtualenv and the workon commands when compiling and installing OpenCV.

  102. Zack Mitkin September 13, 2016 at 6:49 pm #

    Hello, I did all the steps correctly, but python cannot find cv2 or numpy. I swear I did all the steps correctly, and I got no errors. Please tell me a way to check what the problem is

    • Adrian Rosebrock September 15, 2016 at 9:37 am #

      If you cannot find cv2 or Numpy, then you are likely not in the cv virtual environment when trying to import your libraries:

      • jai September 21, 2016 at 2:44 am #

        I am also facing similar problem n i am quite sure that i am working withing cv virtualenv.

  103. Moni Delgado September 19, 2016 at 10:02 pm #

    I can’t even run the “workon cv” command. I think it’s because I’ve already closed the terminal and log in again, so I’m not running stuff in the right place…what are the easy steps to access cv on python and run again the example you showed, for example.

    • Adrian Rosebrock September 21, 2016 at 2:17 pm #

      If you can’t run the “workon” command then it’s likely your ~/.bashrc file wasn’t updated properly. Go back to Step 8 and check the contents of the file.

  104. Nicolas October 4, 2016 at 4:21 pm #

    Thank you very much for this tutorial 😀

    • Adrian Rosebrock October 6, 2016 at 7:01 am #

      No problem Nicolas, I’m glad I could help 🙂

  105. Sai October 26, 2016 at 3:36 pm #

    Hey,

    I already have opencv 2.4.8 and tried to do the above process and when i ran:

    import cv2

    print cv2.__version__

    it printed 2.4.8

    How can i upgrade it to 3.1.0?

    Thanks,
    Sai

    • Adrian Rosebrock November 1, 2016 at 9:31 am #

      It sounds like you’re not in the proper virtual environment. Make sure you run:

      $ workon cv

      To access your Python virtual environment. You should also check the site-packages directory for the “cv” virtual environment to ensure the cv2.so bindings are there.

  106. Pedro Lorente November 4, 2016 at 5:19 pm #

    Every time I close the terminal window, I have to repeat the whole process of Chapter 8, to be in the environment (cv).
    Is there anyone who can help me?
    Thank you all!

    • Adrian Rosebrock November 7, 2016 at 2:56 pm #

      Every time you open a new terminal all you need to do is:

      $ workon cv

      No other command except for the one above needs to be executed to access the “cv” Python virtual environment.

  107. neztorig November 8, 2016 at 10:52 am #

    Great I following this step to install OpenCV and its work on ubuntu.
    many thank’s

    • Adrian Rosebrock November 10, 2016 at 8:44 am #

      Great job getting OpenCV installed! 🙂

  108. Melki November 23, 2016 at 11:49 am #

    HI

    I am trying to install OpenCV 3.1.0 with python 2.7 on linux. All the installation process went ok (I have some problems that I solved) but finally I see that the cv2.so is installed in /usr/local/python/2.7/ rather than /usr/local/lib/python2.7/site-packages. Do you have any idea why? I solced the problem by copying the cv2.so to /usr/local/lib/python2.7/site-packages manually but I guess it is not the way it should be done..

    Thanks!

    • Adrian Rosebrock November 23, 2016 at 3:17 pm #

      I’m not sure why the cv2.so file would have ended up in /usr/local/python2.7, but yes, copying it into /usr/local/lib/python2.7/site-packages is the correct solution.

  109. Namrah November 23, 2016 at 3:08 pm #

    This was aweosme, I had been struggling with opencv and opencv_contrib installation. Thank you so much, this really helped.

    • Adrian Rosebrock November 23, 2016 at 3:16 pm #

      Congrats on getting OpenCV installed on your system Namrah, nice job!

  110. DanDan December 3, 2016 at 3:12 am #

    Thanks very much for the great post. I wonder how much time I spent to try out and compile into this comprehensive post.
    Thanks again.

  111. ibrahim December 5, 2016 at 7:16 am #

    hi thanks for the great tutorial you give here,
    i have installed the opencv3.1(with the instruction above) and python 2.7 successfully, but when come to run the program example you give, it give an error that said, “OpenCV Error: Sizes of input arguments do not match (The lower boundary is neither an array of the same size and same type as src, nor a scalar) in inRange”
    i still new at this , perhaps you can give me some direction to solve this error

    thanks

    • Adrian Rosebrock December 5, 2016 at 1:25 pm #

      Hey Ibrahim — it sounds like you might have passed an invalid path to cv2.imread. Double check the path to your input image is correct.

  112. Mehta Jaldhi December 16, 2016 at 1:42 am #

    Actually I have installed opencv in different login.. now I need to login in hduser.. how can i remove from my local login?

  113. Hakan December 19, 2016 at 5:18 pm #

    Hi Adrian,

    Great tutorial. I followed all the steps installed OpenCV on Ubuntu successfully. Thanks !

    • Adrian Rosebrock December 21, 2016 at 10:30 am #

      Fantastic, nice job getting OpenCV installed Hakan!

  114. Madhu Oruganti December 20, 2016 at 5:14 am #

    Dear Sir, I am using the Ubuntu 14.04 and I installed the Opencv3 as per given steps, but I am getting two problems as follows
    1. If I close the terminal I could not open the CV directly until process the step8 and 9.
    2.ImportError: No module named matplotlib
    Please respond to my problem.

    • Adrian Rosebrock December 21, 2016 at 10:27 am #

      1. If you close your terminal you can re-access the “cv” virtual environment via the workon command:

      $ workon cv

      2. You need to install matplotlib into the “cv” virtual environment:

      • Madhu Oruganti December 22, 2016 at 11:21 pm #

        it’s working sir.Thank you so much, sir.

  115. Madhu Oruganti December 27, 2016 at 6:00 am #

    Sir, Can you please tell me the code/command to run the video in python and opencv3

    • Adrian Rosebrock December 31, 2016 at 1:35 pm #

      Are you trying to access a video stream with OpenCV? If so, please see this post.

  116. Artur December 29, 2016 at 2:11 pm #

    Using your post I’ve just installed OpenCV 3.2 with Python 3.5 on Ubuntu 16.04.
    Thanks Adrian 🙂

    • Adrian Rosebrock December 31, 2016 at 1:23 pm #

      Congrats on getting OpenCV installed Artur, nice job! 🙂

  117. Yuvraj January 8, 2017 at 2:59 am #

    Thank you Adrian!
    up to step 11 everything works perfect but i got error in step12.

    $workon cv
    workon: command not found

    can you please help me?

    • Adrian Rosebrock January 9, 2017 at 9:13 am #

      Please see the “Troubleshooting” section of this blog post for common reasons why the “workon” command may not be found.

      • yuvraj January 15, 2017 at 5:47 am #

        Getting error while importing “cv2”
        “ImportError: No module name cv2”

        even “cv2.so” present in site-packages.
        so whats going wrong?

        • Adrian Rosebrock January 15, 2017 at 12:02 pm #

          It’s hard to say without having physical access to your machine, but I think the issue is that you aren’t in the “cv” virtual environment:

          $ workon cv

  118. Alex January 19, 2017 at 10:00 pm #

    One key package which I needed to install to get this working was python-dev…without it, everything runs successfully but cv.so is never built and therefore you cannot use the library.

    • Adrian Rosebrock January 20, 2017 at 10:56 am #

      Correct, you need the Python development headers installed before trying to compile OpenCV.

  119. Mehta Jaldhi January 21, 2017 at 4:01 am #

    Thankx Adrian.

    Loved your article.

    But can you tell me how to uninstall the whole method what we just did?

    Actually I need to again install it.

    • Adrian Rosebrock January 22, 2017 at 10:17 am #

      You can delete OpenCV by removing all files that contain “opencv” in the filename from /usr/local/lib. Alternatively you can simply re-run make and make install and you will overwrite all the old files.

  120. Tony January 21, 2017 at 9:20 pm #

    I get to step 10 and type make -j4, but it doesn’t work, instead printing this error:

    make: *** No targets specified and no makefile found. Stop.

    It seems that cmake didn’t entirely work. There is a file in opencv/build/CMakeFiles/ called CMakeError.log. Scanning through that, I see a number of errors, but I don’t know how to resolve them.

    I should note I’m doing this using MinGW/msys on a Windows 10 machine.

    • Saikanam January 29, 2017 at 9:39 pm #

      I had the same issue,try fixing the -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ thing,try to fix it to wherever you have cloned opencv_contrib.. hope this helps 🙂

  121. Deepan Raj January 22, 2017 at 12:16 pm #

    Hi !!
    Love this blog. But i dont have good computer specs that can support vmware. So i started to install Opencv for python in windows, also installed numpy, scikit, matplotlib too! Can you Please tell me is there any way to install dlib in windows.. installed all other packages like a charm in windows..Only thing i cant do is how to install dlib..I have downloaded dlib package. installed cmake….
    Note: do i need visual studio to install dlib ?

  122. Huck January 24, 2017 at 1:27 pm #

    I have used these steps many times without error but this time I’ve had problems. First, I had errors with opencv_perf_stitching so I just disabled building that module so I could get through the make step. I did the install and ldconfig but there is NO cv2.so on my machine anywhere! What in tarnation am I doing wrong?

    • Adrian Rosebrock January 24, 2017 at 2:15 pm #

      Hi Huck — I’m sorry to hear about the issues with compiling OpenCV. Can you check the build/lib directory and see if the cv2.so file is there? If it’s not, there was likely a misconfiguration within CMake.

    • Huck January 24, 2017 at 2:23 pm #

      I realized that I could not import numpy either….I had to re pip install numpy but this time with sudo…

      sudo pip install numpy

      I completely wiped my build directory and started over….this time cv2.so showed up as planned.

      Note: I do NOT use the virtual environments so perhaps this is why I needed to sudo pip install?

      • Adrian Rosebrock January 24, 2017 at 2:33 pm #

        If you did not use the virtual environment then yes, you would need “sudo” to have the proper permissions to install any Python packages into your system’s site-packages directory.

      • Huck January 24, 2017 at 3:27 pm #

        Adrian…thanks for your great instructions. Is it possible to build opencv 3.1.0 for both Python 2.7 (already done and working happily) *AND* Python 3.4 (not instead of)? Can I just follow your instructions here (http://www.pyimagesearch.com/2015/07/20/install-opencv-3-0-and-python-3-4-on-ubuntu/) or is there something else that needs to be done?

        • Adrian Rosebrock January 26, 2017 at 8:28 am #

          To install OpenCV 3.1 for Python 2.7 and Python 3.4 you will need to run CMake and make twice. Once for Python 2.7. And then again for Python 3.4. Copy the cv2.so file created each respective Python version into their respective site-packages directory.

  123. Tonmoy Roy January 29, 2017 at 12:19 am #

    Hi, I didn’t read through all the comments, but someone might have asked this question already. What if OpenCV_Contrib is not the same version as OpenCV?

    • Adrian Rosebrock January 29, 2017 at 2:42 pm #

      You should be using the same opencv_contrib version as your opencv version, otherwise you’ll likely run into errors.

  124. Deepak Sharma February 2, 2017 at 10:55 am #

    in case you get an error numpy not found after pip install during make

    export PYTHON_INCLUDE=$PYTHON_INCLUDE:/home/USERNAME/.virtualenvs/cv/lib/python2.7/site-packages/numpy

  125. Siddhesh Acharekar February 12, 2017 at 1:32 pm #

    Hey I followed this step by step, everything worked great but in the last step i wasnt able to create the link. I got this error:

    ln: failed to create symbolic link ‘cv2.so’: File exists

    Not able to understand what this means but mainly how do I go about this? Also when I enter the python environment it shows version 2.4.8 which was preinstalled. I want to move onto 3.0.0 and based on your tutorial thats all but done due to this one error. Please help.

    • Adrian Rosebrock February 13, 2017 at 1:41 pm #

      If the sym-link already exists (I’m not sure why it would already) you can simply delete it and re-create it:

      rm /path/to/sym-link

Trackbacks/Pingbacks

  1. Install OpenCV 3.0 and Python 3 on Ubuntu - PyImageSearch - July 20, 2015

    […] A couple weeks ago I provided step-by-step install instructions to setup OpenCV 3.0 and Python 2.7+ on your Ubuntu machine. […]

  2. Ubuntu 16.04: How to install OpenCV - PyImageSearch - October 24, 2016

    […] Install OpenCV 3.0 and Python 2.7+ on Ubuntu 14.04 […]

Leave a Reply