Install guide: Raspberry Pi 3 + Raspbian Jessie + OpenCV 3

rpi3_board

Can you believe it’s been over four years since the original Raspberry Pi model B was released? Back then the Pi Model B shipped with only 256MB of RAM and a 700MHz single core processor.

Just over one year ago the Raspberry Pi 2 was unleashed on the world. And man, for something called a “Pi”, this beast made an impact on the computer world like an asteroid. This board sported 1GB of RAM and a 900MHz quad-core processor — quite the upgrade from the original single core, 700MHz system!

In my opinion, the Raspberry Pi 2 is what made computer vision possible on the Pi platform (at least from a Python + OpenCV perspective). The original model B simply didn’t have the processing capacity (or the RAM) to be powerful enough to process images video streams for anything more than trivial operations — the Pi 2 changed all that.

In fact, the Raspberry Pi 2 had such a meaningful impact on the computer vision space, that I even took the time to make a all code examples in Practical Python and OpenCV compatible with the Pi.

And now we have the Raspberry Pi 3:

  • 1.2Ghz 64-bit quad-core processor.
  • 1GB RAM.
  • Integrated 802.11n wireless and bluetooth.

Personally, I was hoping for a bit more RAM (perhaps in the range of 1.5-2GB). But upgrading to a 64-bit processor with 33% increased performance is well worth it.

Just as I have done in previous blog posts, I’ll be demonstrating how to install OpenCV 3 with Python bindings on Raspbian Jessie.

If you are looking for previous installation instructions for different platforms, please consult this list:

Otherwise, let’s proceed with getting OpenCV 3 installed on your brand new Raspberry Pi 3!

Assumptions

In this tutorial, I am going to assume that you already own a Raspberry Pi 3 with Raspbian Jessie installed.

You should also have either:

  • Physical access to your Raspberry Pi 3 so that you can open up a terminal and execute commands.
  • Remote access via SSH.

I’ll be doing the majority of this tutorial via SSH, but as long as you have access to a terminal, you can easily follow along.

Installing OpenCV 3 on a Raspberry Pi 3 running Raspbian Jessie

If you’ve ever installed OpenCV on a Raspberry Pi (or any other platform before), you know that the process can be quite time consuming with many dependencies and pre-requisites that have to be installed. The goal of this tutorial is to thus guide you step-by-step through the compile and installation process.

In order to make the installation process go more smoothly, I’ve included timings for each step so you know when to take a break, grab a cup of coffee, and checkup on email while the Pi compiles OpenCV. That said, the Pi 3 is substantially faster than the Pi 2, so the time it takes to compile OpenCV has decreased dramatically.

Anyway, let’s go ahead and get started installing OpenCV 3 on your brand new Raspberry Pi 3 running Raspbian Jessie.

Step #1: Expand filesystem

Are you using a brand new install of Raspbian Jessie?

If so, the first thing you should do is expand your filesystem to include all available space on your micro-SD card:

Figure 1: Expanding the filesystem on your Raspberry Pi 3.

Figure 1: Expanding the filesystem on your Raspberry Pi 3.

Once prompted, you should select the first option, “1. Expand File System”, hit Enter on your keyboard, arrow down to the “<Finish>” button, and then reboot your Pi:

After rebooting, your file system should have been expanded to include all available space on your micro-SD card. You can verify that the disk has been expanded by executing df -h  and examining the output:

As you can see, my Raspbian filesystem has been expanded to include all 8GB of the micro-SD card.

However, even with my filesystem expanded, I have already used 48% of my 8GB card!

OpenCV, along with all its dependencies, will need a few gigabytes during the compile, so you should delete the Wolfram engine to free up some space on your Pi:

After removing the Wolfram Engine, you can reclaim almost 700mb!

Step #2: Install dependencies

This isn’t the first time I’ve discussed how to install OpenCV on the Raspberry Pi, so I’ll keep these instructions on the briefer side, allowing you to work through the installation process: I’ve also included the amount of time it takes to execute each command so you can plan your OpenCV + Raspberry Pi 3 install accordingly (OpenCV itself takes 1h 12m to compile).

The first step is to update and upgrade any existing packages:

Timing: 1m 26s

We then need to install some developer tools, including CMake, which helps us configure the OpenCV build process:

Timing: 40s

Next, we need to install some image I/O packages that allow us to load various image file formats from disk. Examples of such file formats include JPEG, PNG, TIFF, etc.:

Timing: 32s

Just as we need image I/O packages, we also need video I/O packages. These libraries allow us to read various video file formats from disk as well as work directly with video streams:

Timing: 34s

The OpenCV library comes with a sub-module named highgui  which is used to display images to our screen and build basic GUIs. In order to compile the highgui  module, we need to install the GTK development library:

Timing: 3m 6s

Many operations inside of OpenCV (namely matrix operations) can be optimized further by installing a few extra dependencies:

Timing: 46s

These optimization libraries are especially important for resource constrained devices such as the Raspberry Pi.

Lastly, let’s install both the Python 2.7 and Python 3 header files so we can compile OpenCV with Python bindings:

Timing: 45s

If you skip this step, you may notice an error related to the Python.h  header file not being found when running make  to compile OpenCV.

Step #3: Download the OpenCV source code

Now that we have our dependencies installed, let’s grab the 3.1.0  archive of OpenCV from the official OpenCV repository. (Note: As future versions of openCV are released, you can replace 3.1.0  with the latest version number):

Timing: 1m 26s

We’ll want the full install of OpenCV 3 (to have access to features such as SIFT and SURF, for instance), so we also need to grab the opencv_contrib repository as well:

Timing: 43s

You might need to expand the command above using the “<=>” button during your copy and paste. The .zip  in the 3.1.0.zip  may appear to be cutoff in some browsers. The full URL of the OpenCV 3.1.0 archive is:

https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip

Note: Make sure your opencv  and opencv_contrib  versions are the same (in this case, 3.1.0 ). If the versions numbers do not match up, then you’ll likely run into either compile-time or runtime.

Step #4: Python 2.7 or Python 3?

Before we can start compiling OpenCV on our Raspberry Pi 3, we first need to install pip , a Python package manager:

Timing: 20s

If you’re a longtime PyImageSearch reader, then you’ll know that I’m a huge fan of both virtualenv and virtualenvwrapper. Installing these packages is not a requirement and you can absolutely get OpenCV installed without them, but that said, I highly recommend you install them as other PyImageSearch tutorials in the future will also leverage Python virtual environments. I’ll also be assuming that you have both virtualenv  and virtualenvwrapper  installed throughout the remainder of this guide.

So, given that, what’s the point of using virtualenv  and virtualenvwrapper ?

First, it’s important to understand that a virtual environment is a special tool used to keep the dependencies required by different projects in separate places by creating isolated, independent Python environments for each of them.

In short, it solves the “Project X depends on version 1.x, but Project Y needs 4.x” dilemma. It also keeps your global site-packages  neat, tidy, and free from clutter.

If you would like a full explanation on why Python virtual environments are good practice, absolutely give this excellent blog post on RealPython a read.

It’s standard practice in the Python community to be using virtual environments of some sort, so I highly recommend that you do the same:

Timing: 9s

Now that both virtualenv  and virtualenvwrapper  have been installed, we need to update our ~/.profile  file to include the following lines at the bottom of the file:

In previous tutorials, I’ve recommended using your favorite terminal-based text editor such as vim , emacs , or nano  to update the ~/.profile  file. If you’re comfortable with these editors, go ahead and update the file to reflect the changes mentioned above.

Otherwise, you should simply use cat  and output redirection to handle updating ~/.profile :

Now that we have our ~/.profile  updated, we need to reload it to make sure the changes take affect. You can force a reload of your ~/.profile  file by:

  1. Logging out and then logging back in.
  2. Closing a terminal instance and opening up a new one
  3. Or my personal favorite, just use the source  command:

Note: I recommend running the source ~/.profile  file each time you open up a new terminal to ensure your system variables have been setup correctly.

Creating your Python virtual environment

Next, let’s create the Python virtual environment that we’ll use for computer vision development:

This command will create a new Python virtual environment named cv  using Python 2.7.

If you instead want to use Python 3, you’ll want to use this command instead:

Again, I can’t stress this point enough: the cv  Python virtual environment is entirely independent and sequestered from the default Python version included in the download of Raspbian Jessie. Any Python packages in the global site-packages  directory will not be available to the cv  virtual environment. Similarly, any Python packages installed in site-packages  of cv  will not be available to the global install of Python. Keep this in mind when you’re working in your Python virtual environment and it will help avoid a lot of confusion and headaches.

How to check if you’re in the “cv” virtual environment

If you ever reboot your Raspberry Pi; log out and log back in; or open up a new terminal, you’ll need to use the workon  command to re-access the cv  virtual environment. In previous blog posts, I’ve seen readers use the mkvirtualenv  command — this is entirely unneeded! The mkvirtualenv  command is meant to be executed only once: to actually create the virtual environment.

After that, you can use workon  and you’ll be dropped down into your virtual environment:

To validate and ensure you are in the cv  virtual environment, examine your command line — if you see the text (cv)  preceding your prompt, then you are in the cv  virtual environment:

Figure 2: Make sure you see the "(cv)" text on your prompt, indicating that you are in the cv virtual environment.

Figure 2: Make sure you see the “(cv)” text on your prompt, indicating that you are in the cv virtual environment.

Otherwise, if you do not see the (cv)  text, then you are not in the cv  virtual environment:

Figure 3: If you do not see the "(cv)" text on your prompt, then you are not in the cv virtual environment and need to run "source" and "workon" to resolve this issue.

Figure 3: If you do not see the “(cv)” text on your prompt, then you are not in the cv virtual environment and need to run “source” and “workon” to resolve this issue.

To fix this, simply execute the source  and workon  commands mentioned above.

Installing NumPy on your Raspberry Pi

Assuming you’ve made it this far, you should now be in the cv  virtual environment (which you should stay in for the rest of this tutorial). Our only Python dependency is NumPy, a Python package used for numerical processing:

Timing: 9m 39s

Be sure to grab a cup of coffee or go for a nice walk, the NumPy installation can take a bit of time.

Note: Another question I’ve often seen is “Help, my NumPy installation has hung and it’s not installing!” Actually, it is installing, it just takes time to pull down the sources and compile. Be patient. The Raspberry Pi isn’t as fast as your laptop/desktop.

Step #5: Compile and Install OpenCV

We are now ready to compile and install OpenCV! Double-check that you are in the cv  virtual environment by examining your prompt (you should see the (cv)  text preceding it), and if not, simply execute workon :

Once you have ensured you are in the cv  virtual environment, we can setup our build using CMake:

Timing: 1m 57s

Now, before we move on to the actual compilation step, make sure you examine the output of CMake!

Start by scrolling down the section titled Python 2  and Python 3 .

If you are compiling OpenCV 3 for Python 2.7, then make sure your Python 2  section includes valid paths to the Interpreter , Libraries , numpy  and packages path , similar to my screenshot below:

Figure 4: Ensuring that Python 2.7 will be used when compiling OpenCV 3 for Raspbian Jessie on the Raspberry Pi 3.

Figure 4: Ensuring that Python 2.7 will be used when compiling OpenCV 3 for Raspbian Jessie on the Raspberry Pi 3.

Notice how the Interpreter  points to our python2.7  binary located in the cv  virtual environment. The numpy  variable also points to the NumPy installation in the cv  environment.

Similarly, if you’re compiling OpenCV for Python 3, make sure the Python 3  section looks like the figure below:

Figure 5: Checking that Python 3 will be used when compiling OpenCV 3 for Raspbian Jessie on the Raspberry Pi 3.

Figure 5: Checking that Python 3 will be used when compiling OpenCV 3 for Raspbian Jessie on the Raspberry Pi 3.

Again, the Interpreter  points to our python3.4  binary located in the cv  virtual environment while numpy  points to our NumPy install.

In either case, if you do not see the cv  virtual environment in these variables paths, it’s almost certainly because you are NOT in the cv  virtual environment prior to running CMake! 

If this is the case, access the cv  virtual environment using workon cv  and re-run the cmake  command outlined above.

Finally, we are now ready to compile OpenCV:

Timing: 1h 12m

Note: Compiling OpenCV in 72 minutes on the Raspberry Pi 3 is a 24% improvement over the previous 95 minutes for the Raspberry Pi 2. That extra 300MHz makes a big difference!

The -j4  command controls the number of cores to leverage when compiling OpenCV 3. The Raspberry Pi 3 has four cores, thus we supply a value of 4  to allow OpenCV to compile faster.

However, due to race conditions, there are times when make  errors out when using multiple cores. If this happens to you, I suggest starting the compilation over again and using only one core:

Once OpenCV 3 has finished compiling, your output should look similar to mine below:

Figure 5: Our OpenCV 3 compile on Raspbian Jessie has completed successfully.

Figure 5: Our OpenCV 3 compile on Raspbian Jessie has completed successfully.

From there, all you need to do is install OpenCV 3 on your Raspberry Pi 3:

Timing: 52s

Step #6: Finish installing OpenCV on your Pi

We’re almost done — just a few more steps to go and you’ll be ready to use your Raspberry Pi 3 with OpenCV 3.

For Python 2.7:

Provided your Step #5 finished without error, OpenCV should now be installed in /usr/local/lib/python2.7/site-pacakges . You can verify this using the ls  command:

Note: In some cases, OpenCV can be installed in /usr/local/lib/python2.7/dist-packages  (note the dist-packages  rather than site-packages . If you do not find the cv2.so  bindings in site-packages , we be sure to check dist-packages .

Our final step is to sym-link the OpenCV bindings into our cv  virtual environment for Python 2.7:

For Python 3:

After running make install , your OpenCV + Python bindings should be installed in /usr/local/lib/python3.4/site-packages . Again, you can verify this with the ls  command:

I honestly don’t know why, perhaps it’s a bug in the CMake script, but when compiling OpenCV 3 bindings for Python 3+, the output .so  file is named cv2.cpython-34m.so  (or some variant of) rather than simply cv2.so  (like in the Python 2.7 bindings).

Again, I’m not sure exactly why this happens, but it’s an easy fix. All we need to do is rename the file:

After renaming to cv2.so , we can sym-link our OpenCV bindings into the cv  virtual environment for Python 3.4:

Step #7: Testing your OpenCV 3 install

Congratulations, you now have OpenCV 3 installed on your Raspberry Pi 3 running Raspbian Jessie!

But before we pop the champagne and get drunk on our victory, let’s first verify that your OpenCV installation is working properly.

Open up a new terminal, execute the source  and workon  commands, and then finally attempt to import the Python + OpenCV bindings:

As you can see from the screenshot of my own terminal, OpenCV 3 has been successfully installed on my Raspberry Pi 3 + Python 2.7 environment:

Figure 5: Confirming OpenCV 3 has been successfully installed on my Raspberry Pi 3 running Raspbian Jessie.

Figure 5: Confirming OpenCV 3 has been successfully installed on my Raspberry Pi 3 running Raspbian Jessie.

Once OpenCV has been installed, you can remove both the opencv-3.1.0  and opencv_contrib-3.1.0  directories to free up a bunch of space on your disk:

However, be cautious with this command! Make sure OpenCV has been properly installed on your system before blowing away these directories. A mistake here could cost you hours in compile time.

Troubleshooting and FAQ

Q. When I try to execute mkvirtualenv  and workon , I get a “command not found error”.

A. There are three reasons why this could be happening, all of them related to Step #4:

  1. Make certain that you have installed virtualenv  and virtualenvwrapper  via pip . You can check this by running pip freeze  and then examining the output, ensuring you see occurrences of both virtualenv  and virtualenvwrapper .
  2. You might not have updated your ~/.profile  correctly. Use a text editor such as nano  to view your ~/.profile  file and ensure that the proper export  and source  commands are present (again, check Step #4 for the contents that should be appended to ~/.profile .
  3. You did not source  your ~/.profile  after editing it, rebooting, opening a new terminal, etc. Any time you open a new terminal and want to use a virtual environment, make sure you execute source ~/.profile  to load the contents — this will give you access to the mkvirtualenv  and workon  commands.

Q. After I open a new terminal, logout, or reboot my Pi, I cannot execute mkvirtualenv  or workon .

A. See reason #3 from the previous question.

Q. When I (1) open up a Python shell that imports OpenCV or (2) execute a Python script that calls OpenCV, I get an error: ImportError: No module named cv2 .

A. Unfortunately, this error is extremely hard to diagnose, mainly because there are multiple issues that could be causing the problem. To start, make sure you are in the cv  virtual environment by using workon cv . If the workon  command fails, then see the first question in this FAQ. If you’re still getting an error, investigate the contents of the site-packages  directory for your cv  virtual environment. You can find the site-packages  directory in ~/.virtualenvs/cv/lib/python2.7/site-packages/  or ~/.virtualenvs/cv/lib/python3.4/site-packages/  (depending on which Python version you used for the install). Make sure that your sym-link to the cv2.so  file is valid and points to an existing file.

So, what’s next?

Congrats! You have a brand new, fresh install of OpenCV on your Raspberry Pi — and I’m sure you’re just itching to leverage your Raspberry Pi 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 Raspberry Pi Camera with the picamera module. This tutorial details the exact steps you need to take to (1) capture photos from the camera module 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

All code examples covered in the book are guaranteed to run on the Raspberry Pi 2 and Pi 3 as well! Most programs will also run on the B+ and Zero models, but might be a bit slow due to the limited computing power of the B+ and Zero.

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

Summary

In this blog post, we learned how to install OpenCV 3 with either Python 2.7 or Python 3 bindings on your Raspberry Pi 3 running Raspbian Jessie.

If you are running a different version of Raspbian (such as Raspbian Wheezy) or want to install a different version of OpenCV (such as OpenCV 2.4), please consult the following tutorials:

But before you go…

I tend to utilize the Raspberry Pi quite a bit on this blog, so if you’re interested in learning more about the Raspberry Pi + computer vision, enter your email address in the form below to be notified when these posts go live!

, , , , , ,

304 Responses to Install guide: Raspberry Pi 3 + Raspbian Jessie + OpenCV 3

  1. dtbaker April 18, 2016 at 11:09 am #

    Is there any advantage to compiling OpenBLAS as well (I did that on my Pi3 without really knowing what it was, because it was mentioned as missing during config)

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

      Since all Raspberry Pi models within the same family have the same architecture, no, I can’t really see a reason for compiling OpenBLAS for source unless there is some specific feature inside OpenBLAS that you wanted to optimize. Within every library there are various knobs you can tweak and levers to pull — so unless you know exactly what you wanted to optimize, I’m not sure I would go down that rabbit hole.

      • M.AurangzaibKhan October 21, 2016 at 2:26 am #

        I have a 8gb card and when i reached to sudo make install process it shows “No space left on device”.. so is 8 gb card is enough???

        • Adrian Rosebrock October 23, 2016 at 10:22 am #

          8GB should be more than sufficient. Make sure you have ran raspi-config and expanded the disk to use the entire card.

          • M.AurangzaibKhan November 16, 2016 at 8:35 am #

            I am having a problem on step : 5 i.e: make
            i stuck on 91% it doesn’t go to 100%………. ?

          • Adrian Rosebrock November 16, 2016 at 1:34 pm #

            When you say “stuck” how long is stuck there? 5 minutes? 60 minutes? If you are compiling with multiple cores then it’s possible that a race condition was hit. Instead, try using only a single core:

            The compile will take longer but you will ideally not be caught in a situation where the compile gets stuck.

        • Steve January 26, 2017 at 12:08 am #

          Same issue here. My 8G card only had about 7 usable though. Switched to a 16G card.

          BR,
          Steve

  2. Alessio Michelini April 18, 2016 at 12:06 pm #

    Did you try to use autoenv instead of manually load the environment every time?

    https://github.com/kennethreitz/autoenv

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

      I’ve never heard of autoenv before, thanks for bringing it to my attention!

  3. Simon April 18, 2016 at 12:14 pm #

    Hi Adran, always a pleasure to read your blog/tutorials.

    Could you post some comparisons between RPI2 and PRI3 timings when solving different problems. I am wondering what is the real acceleration factor between the two RPIs.

    Personally, I am using them for motion detection.

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

      Sure, I can look into doing some benchmark posts. But keep in mind that the major difference between the Pi 2 and Pi 3 (at least in terms of speed) is that the Pi 3 is 300Mhz faster than the Pi 2. If you’re working with motion detection, then absolutely go with the Pi 3 — that extra 300Mhz is important.

  4. Am!N April 18, 2016 at 2:15 pm #

    Thanks for your tutorial
    python2 or python3 , which one is faster ?
    is there a significant difference between them in speed when run a opencv example?

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

      I don’t have any formal benchmarks to verify this, but there really aren’t any true speed differences between Python 2.7 and Python 3. Both versions are pretty optimized (at least as far as scripting languages are concerned). I would use whichever Python version you are more comfortable with.

  5. Pablo Rogina April 18, 2016 at 3:52 pm #

    Adrian, not to be picky but to reinforce your point, Raspberry Pi model B shipped originally with just 256MB of RAM. One of them is working nicely with OpenELEC in my bedroom…
    Thank you for all your interesting posts!

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

      Thanks for pointing this out Pablo! I have updated the blog post to reflect the change.

  6. dtbaker April 18, 2016 at 6:26 pm #

    Hey thank for your tutorials! Much easier than most others. Pulling bits of tutorials and examples together from around the web I’ve been able to create this: https://www.youtube.com/watch?v=hvzohGuw8XI

    Once it’s working I’ll send publish all the python/c++/html code.

    • Adrian Rosebrock April 19, 2016 at 6:52 am #

      Very cool, thanks for sharing! 🙂

  7. Blaine April 18, 2016 at 11:05 pm #

    Adrian, thanks ! I got my Pi3 up and running with OpenCV 3.1 with no problems.

    • Adrian Rosebrock April 19, 2016 at 6:52 am #

      Fantastic, glad to hear it Blaine!

  8. Eoin April 19, 2016 at 6:18 pm #

    Hey just a noob and I’m wondering will this add all the opencv headers and libraries to the “path” (sorry for using windows terms for use when compiling native cpp code?

    • Adrian Rosebrock April 19, 2016 at 6:49 pm #

      It’s been a long time since I’ve had to compile C++ code for OpenCV, but I’ve always manually specified the include directories, like in this StackOverflow question.

  9. Amin April 20, 2016 at 3:59 am #

    Hi Adrian,
    I have a few question
    1.if 64 bit OS released for Pi 3 so what is 64 bit advantages?
    i read somewhere that you can use int_64 but your program need more RAM to run
    but i cant find anything that show clearly how much improve OpenCV programs speed?

    2.in previous tutorial you say : “In order to build OpenCV 3.1.0 , you need to set -D INSTALL_C_EXAMPLES=OFF (rather than ON ) in the cmake command. ”
    but in this tutorial you not mention it.when i run make command i get error in 93% so i add this to cmake and run again .finished without error but im not sure the reason is for that.

    another thing i want to say ,the Pi3 has the same RAM and GPU but the clock frequency of them increased so i think the 33% increasing in CPU clock is not the only improvement
    and i test a several code with both Pi3 and Pi2
    the FPS increase about 45 to 50% for me
    for example,a python script with only imshow (+ your perfect imutils library) from 40 in Pi2 reached to 80 FPS ! (use raspberry camera)

    thanks Adrian

    • Adrian Rosebrock April 20, 2016 at 6:03 pm #

      I don’t have any formal benchmarks related to speed and 64-bit versus 32-bit. By definition, 64-bit value will require more memory than a 32-bit. However, the benefit is more precision.

      I also left out -D INSTALL_C_EXAMPLES=OFF in this tutorial because this bug seems to be resolved in the latest release of OpenCV.

  10. George Hill April 23, 2016 at 4:46 am #

    Hi Adrian
    Your post is invaluable – thank you. I could not have installed OpenCV on my Raspberry Pi 3 without your post.

    I did encounter one problem as I followed it through. I got a certificate error for the command

    wget https://bootstrap.pypa.io/get-pip.py

    which I worked around easily using

    wget –no-check-certificate https://bootstrap.pypa.io/get-pip.py

    kind regards
    George

    • Adrian Rosebrock April 25, 2016 at 2:12 pm #

      Awesome, thanks for sharing George!

  11. Abkul April 28, 2016 at 8:54 am #

    Hi Adrian.

    Your posts and the Book are excellent resources.

    Will you be having posts on Deep Learning soon?

    Thank You

    Abkul Orto

  12. Bob Currier April 28, 2016 at 12:44 pm #

    What a difference in timings between the PI models. I dusted off my B+ a few days ago and after adding a snazzy new 32GB SSD and Jessie I decided to compile OpenCV. 14 hours later I was done. I’m not sure where these quoted times came from as it takes several hours to compile on my MacBook Air running a Vagrant CentOS-7 box. I don’t see how it’s possible that PI can compile the full OpenCV install in just over an hour. Enlighten me, Sensei. 🙂

    • Adrian Rosebrock April 28, 2016 at 2:21 pm #

      The B+ has only one core. Meanwhile, the Pi 2 and Pi 3 have four cores. This means that you can distribute the compile time over the 4 cores and dramatically reduce your compile time!

  13. Jan May 1, 2016 at 7:27 am #

    Hi,
    Thanks for the tutorial. I would like to instal opencv 3 on rip 3 without virtual environment as I might use Python 2.7 only or no Python at all (C++) What is the sequence of installation then to skip virtual environment and it’s complexity?

    • Adrian Rosebrock May 1, 2016 at 3:06 pm #

      To clarify: You can use this same procedure to install OpenCV on your Raspberry Pi and simply never use the Python bindings. By installing OpenCV, you’ll still have full access to the C++ OpenCV API. The easiest method to skip Python support is to use CMake and add the flag: -D BUILD_opencv_python=OFF

      • Jan May 19, 2016 at 11:40 pm #

        Adrian,
        Is it possible to implement a colour tracking project (ball on the plate) using RPi3 with a resolution of 640×480 and a frame rate better than 30fps? I have Logitech C920 webcam. Thanks for your time and sharing the knowledge.

        • Adrian Rosebrock May 21, 2016 at 6:44 am #

          At 640×480 you’ll struggle to get a frame processing rate > 30 FPS, between the I/O latency and the general power of the Pi. In order to milk every last bit of speed out of the Pi, you might want to implement the color tracking in C/C++, that should give you a bit of a speed boost.

          • Jan May 22, 2016 at 10:00 pm #

            Thanks Adrian,

            Following your response, I’m trying to investigate how to use multicore and multithreading on RPi with opencv to increase the performance. Some sources say that it has to be recompiled with WITH_TBB or WITH_OPENMP flag enabled. My guess is the your installation procedure doesn’t do that, correct?

            When I started with running guvcview on my RPi3, it gives me a discouraging 2fps with 480 x 320 resolution, yet to see what I can get with opencv.
            If anyone did a similar project already and can give some pointers, it would be highly appreciated.

          • Adrian Rosebrock May 23, 2016 at 7:25 pm #

            Correct, this tutorial does not take into account TBB or OpenMP. 2 FPS sounds extremely, extremely low. How are you executing your Python script? Natively on the Pi via a keyboard and HDMI monitor? Or SSH? VNC?

          • Jeff November 17, 2016 at 3:01 am #

            can I add the openmp features if I have already installed opencv following this thread. and how?

          • Adrian Rosebrock November 18, 2016 at 9:00 am #

            If you’ve already compiled + installed OpenCV then any additional features you may want will require a re-compile and re-install.

  14. Jeff Constantine May 5, 2016 at 11:01 pm #

    Adrian,

    First a thanks…awesome install instructions. Awesome books! Awesome videos! I’m completely new to python and opencv and you have made it dead simple. I’m up and running in virtualbox on my Macbook Air and my Pi3 as well.

    Only problem is I’m having trouble with matplotlib on the Pi3. I used pip to install it and it appears to run…i.e. the histogram programs from Chapter 7 run but the windows with the plots of the histograms don’t show up. Since there doesn’t seem to be any error I haven’t been able to track down what I’m missing….

    Any thoughts?

    Regards,
    Jeff

    • Adrian Rosebrock May 6, 2016 at 4:33 pm #

      Luckily this is an easy fix! Just use this tutorial.

    • Jtaw December 2, 2016 at 3:32 am #

      Hi, Ive been planning to use openmp, tbb, and ipp for my opencv3 but I am having problems, when I try to compile IPP by including WITH_IPP=ON every time i compile, it won’t still recognize ipp. tbb was succesfully built with opencv but everytime I run my program the cores being utilize still the same. can you give me some ideas on how to work with these 3rd party software and how to use/add them in opencv3. been search all over the internet and I can’t find a good guide/answers.

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

        IPP is Intel’s asynchronous C/C++ library. The Raspberry Pi is an ARM (non-Intel) processor so IPP isn’t relevant here.

  15. Tony May 8, 2016 at 4:11 pm #

    Hi Adrian,

    Thanks for the detailed install instruction, based on these I also purchased your book which i’m working through. Could I suggest you also add pip install “picamera[array]” to the installation instructions.

    • Adrian Rosebrock May 9, 2016 at 7:00 pm #

      Hi Tony — thanks for picking up a copy of Practical Python and OpenCV. I normally wouldn’t include installation of the picamera library in a general OpenCV install tutorial, mainly because some readers may not be using the Raspberry Pi camera module — they may be using the USB webcam instead. But that’s a good suggestion, thanks for the comment.

  16. atomek May 15, 2016 at 5:00 pm #

    is it only my lack of understanding or is the virtualenv approach a total waste of time if I’m preparing a device for production? In other words, I developed an opencv app that runs on RaspberryPi and I found a client for it. The app runs on system startup (crontab pointing to a launcher script)… Did I just make a bloody mess on a brand new RPi3’s SD disk by creating a virtualenv there and installing opencv in it? Apparently now I cant access opencv from outside that virtualenv so I need to compile it again, this time outside

    • Adrian Rosebrock May 16, 2016 at 9:16 am #

      In my opinion, it’s never a waste of time to use Python virtual environments, especially for production environments. I detail my reasoning in the first few sections this blog post. As running a Python + OpenCV script on boot, keep an eye on the PyImageSearch blog. The next post (which should publish in ~30 minutes) details exactly how to do this.

  17. Kyle May 19, 2016 at 12:37 pm #

    I’ve followed your setup and am having problems. Whenever I import cv2 and run

    cam = cv2.VideoCapture(0)
    print cam.isOpened()

    I am always getting false. Whatever I do I cannot seem to be able to access the camera features through openCV.

    Any pointers?

    • Adrian Rosebrock May 19, 2016 at 5:57 pm #

      Are you using the Raspberry Pi camera module? Or a USB webcam? If you’re using the Pi camera module, A target=”blank” href=”http://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/”>follow this tutorial on how to access it. You might also be interested in this blog post where I demonstrate how to access the picamera module and cv2.VideoCapture using the same function.

  18. Brian May 21, 2016 at 2:35 pm #

    Will this install also work with Raspbian Jessie Lite?

    • Adrian Rosebrock May 23, 2016 at 7:31 pm #

      I personally haven’t tried this on tutorial on Raspbian Lite. I would be very curious to see if it works. If you gave it a try, can you let me know the result?

  19. Brian May 21, 2016 at 4:36 pm #

    When I run this command:
    pip install numpy

    I get this response:
    Requirement already satisfied (use –upgrade to upgrade): numpy in /user/lib/python2.7/dist-packages

    I’m starting from a fresh install of Jessie. So, is numpy already installed at this point, or is there something else going on?
    Thanks!

    • Adrian Rosebrock May 23, 2016 at 7:30 pm #

      Have you already used the mkvirtualenv/workon commands to enter the cv Python virtual environment? If so, then yes, you can proceed okay. Otherwise, make sure you create the cv virtual environment first and then install NumPy.

  20. Ebad Syed May 23, 2016 at 2:06 am #

    If I open up the idle then I can not import cv2 or when I don’t do workon cv even then I can not import cv2.

    I want to write my program directly in IDLE and run from there what do I do ?

    • Adrian Rosebrock May 23, 2016 at 7:21 pm #

      Are you referring to the GUI version of IDLE? Or the command line version of IDLE? Unfortunately, the GUI version of IDLE does not respect the workon command. You’ll instead need to use the command line IDLE or something like IPython Notebooks.

      • Matt July 16, 2016 at 11:30 pm #

        I think I’m having the same problem. I can import cv2 from python3 command line, but not if I try to import it when I open idle3.

        Is there no way to have it import into the Idle GUI? How do you write a program that you can save if you have to do everything from the command line?

        • Adrian Rosebrock July 18, 2016 at 5:15 pm #

          Hey Matt — you actually just write the code in a .py file and then execute it via:

          $ python my_script.py

          Keep in mind that the command line IS NOT IDLE. IDLE is a separate program. The command line just calls other programs.

  21. Jake Tucker May 23, 2016 at 5:14 pm #

    Thanks so much! Massive help for a beginner.

    • Adrian Rosebrock May 23, 2016 at 7:16 pm #

      No problem Jake, I’m happy I could help!

  22. Colin Rose May 27, 2016 at 9:14 am #

    I have installed everything following the procedures in your tutorial and everything appears to have worked well, thank you, except for one thing. When I try to use cv2.SURF I get an error that there is no such module.
    Looking through the contents and readme’s in the installed and unzipped folder there does not appear to be a SURF module in the list of modules. Am I missing something?
    I am currently using opencv 2.9.1 in my robot and use SURF to recognise images which guide it. I would like to upgrade to a later version to see if its “hit rate” can be improved.
    I have made the installation on a Pi2 with the very latest (as of yesterday) upgraded copy of Jessie but I can’t see that this would be an issue.

  23. Melrck May 31, 2016 at 7:02 am #

    sir i installed opencv3 correctly and nothing wrong up to the last command but when i run your sample code drawing.py from your pyimagesearch book the error says “No module named cv2” .. what do you think is the error?

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

      Make sure you access your Python virtual environment before executing the Python script:

  24. Melrck June 1, 2016 at 5:14 am #

    everytime that i will run python drawing.py i will run first workon cv?

    • Adrian Rosebrock June 1, 2016 at 3:21 pm #

      You don’t have to execute workon every time — just anytime you open up a terminal window and want to execute Python code. I would suggest this blog post to better understand Python virtual environments.

      • Melrick Nicolas October 19, 2016 at 3:09 am #

        sir i followed this steps correctly without errors but when i am inside the cv environment i cant import other modules like Serial and rpio.gpio .. how can i use other modules with opencv?

        but when i am inside the cv environment i cant import other modules like Serial and rpio.gpio .. how can i use other modules with opencv?

        • Adrian Rosebrock October 20, 2016 at 8:46 am #

          Please see this blog post on utilizing OpenCV + RPi.GPIO together.

  25. ramin assadollahi June 6, 2016 at 3:12 am #

    thank you again for you sensational installation guide. the whole world should have this quality in terms of documentation and explanation!
    did you also measure the speed of recognising stuff with the pi 3 vs p 2 using opencv3? it would be very interesting to know from an expert how much speed gains one gets and possibly in what analysis scenarios.

    • Adrian Rosebrock June 7, 2016 at 3:27 pm #

      I don’t have any recognition timings — I’ll keep that in mind for future blog posts. Thanks for the suggestion.

      • Melrick October 12, 2016 at 2:05 am #

        is there a way that i dont have to execute source and work on command to to execute python code .. for example just direct type python drawing.py to the terminal

        • Adrian Rosebrock October 13, 2016 at 9:20 am #

          You only have to execute the source and workon commands once when you open up a new terminal. They don’t need to be executed every time.

  26. Jack June 11, 2016 at 4:59 pm #

    Please allow me to echo what others have said — THANK YOU for this fine tutorial!

    How I was dreading the process of installing CV; yet that worry was ill placed. Your tutorial worked perfectly and the entire process was (dare I say it?) easy!

    The clarity of these instructions certainly lends confidence to the quality of your book. I’ll have to give it a look.

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

      Thanks so much for the kind words Jack, I’m happy I could help out 🙂

  27. Charles Huff June 14, 2016 at 3:26 pm #

    I installed this on my pi 3 twice. NumPy was already in Jessie. No errors found. looked ok for python 2.7 but ALWAYS after source and workon cv python import cv2 it says no module found.

    i verified cv2.so was in /home/pi/.virtualenvs/cv/lib/python2.7/site-packages

    second time, I did none of the steps with 3.4 still won’t import cv2

    Please help

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

      It’s hard to tell what the exact error is without seeing your output from CMake, but from what I can tell, CMake was not automatically detecting the correct Python interpreter, site-packages directory, etc. I’m not sure why that would be. But again, without direct access to the machine, I can’t give a definitive answer.

  28. Charles Huff June 15, 2016 at 6:32 am #

    when installing on the latest Jessie image, the process breaks because Numpy and Python are already installed and don’t get added to the virtual environment correctly. Leaving out the virtual environment seems to fix this and avoids the complexity of source ~/.profile and workon cv…

    Took me three tries but now I am well on the way to success! thank you!

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

      Hey Charles — for what it’s worth, you can install NumPy into the virtual environment:

      Since the cv virtual environment is independent from the system Python install, you won’t get an error this way.

      • Francis Tse June 20, 2016 at 5:41 pm #

        Hello Adrian, I am having the same problem as Charles. Unfortunate, all this is new to me and I could not really understand how Charles solved the problem nor your response. I followed all the steps and checked the output on each step. I checked /home/pi/.virtualenvs/cv/lib/python2.7/site-packages to make sure that I have the cv2.so file there. I also tried doing the symbolic link again and got a message that the link already exists. I am really stuck. Any further pointer you can provide is much appreciated.

        • Adrian Rosebrock June 23, 2016 at 1:36 pm #

          If the sym-link already exists, you can simply delete it and re-create it. However, I would check your build/lib directory and ensure that your cv2.so bindings have been compiled and stored there. If not, there was likely an error in your compilation OR a problem with the CMake configuration and the Python bindings were never compiled. To start, be sure to refer to the “Troubleshooting” section of this blog post.

  29. Sebastien June 18, 2016 at 5:10 pm #

    Hi Adrian,

    As requested, i made a try on Jessie Lite.
    It runs like a charm by following your tutorial (very well and sufficiently documented) step by step. (on Pi2 & py27)

    No information returned about compiling time. approximately ~2h (~3h from the beginning)

    The purpose of Jessie lite is to build a headless version of Pi.
    So the result is that direct video stream thru X11 forwarding or VNC is not efficient, as exactly i expected 🙂

    Now I will give a look on your home monitoring and motion detection tuto
    (include or add secure remote access to a real realtime stream thru http and messaging alert)

    Many Thanks

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

      Fantastic, thanks for sharing Sebastien! 🙂

  30. Alexey June 22, 2016 at 8:45 am #

    Hi Adrian,

    OpenCV has been successfully installed on my Raspberry Pi 3!

    Thank you so much!!!

    • Adrian Rosebrock June 23, 2016 at 1:18 pm #

      Nice, congratulations Alexey!

  31. Gil June 24, 2016 at 1:43 pm #

    is there any function i can use other than “cv2.boxPoints()” ? because i can’t use it in cv2 🙁

    • Adrian Rosebrock June 25, 2016 at 1:27 pm #

      Are you using the older version of OpenCV prior to OpenCV 2.4? If so, the original old function is called: cv.BoxPoints. Although I would highly recommend that you install OpenCV 2.4.X or OpenCV 3 for the newer features.

  32. Richard July 1, 2016 at 9:06 pm #

    When I type “import cv2” in my virtual environment I get the following error:

    Traceback (most recent call last):
    File “”, line 1,
    ImportError: numpy.core.multiarray failed to import

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

      If you are getting that error, then you did not install NumPy into your cv virtual environment. You can resolve the issue by installing NumPy into your virtual environment:

  33. Max July 6, 2016 at 4:39 am #

    Thanks for the post!
    Could you please advice how me to run python in cv environment for apache web server?
    I mean how to implement workon template for these script before running?

  34. Jimmy July 6, 2016 at 2:56 pm #

    Hi,
    Thank you for the tutorial. I would like to install Opencv 3 on Raspberry Pi 3. But I want to use C++ instead of Python. What is the steps of installation I should follow or which steps above might be different? Thank you!

    • Adrian Rosebrock July 6, 2016 at 4:09 pm #

      The tutorial doesn’t change at all for C++. You can skip the virtual environment steps if you would like, but otherwise, the steps are identical.

  35. Renaldy July 7, 2016 at 10:16 pm #

    thanks adrian!!.
    can i make command source ~./profile and workon cv automatic on startup ?

    • Adrian Rosebrock July 8, 2016 at 9:49 am #

      Sure. Anytime you open a new terminal, the contents of your ~/.bashrc should execute. If your ~/.bashrc file does not exist, create it, and then add the contents of ~/.profile to it.

  36. JEHL Julien July 8, 2016 at 6:17 am #

    Hello,

    Thank you for your tuto. It works to install opencv 3.1 with python 3.4. I have only a trouble with the python command :
    cam = cv2.VideoCapture(0)
    print cam.isOpened()

    Always return False.
    Do you know more about VideoCapture() drivers ?

    • Adrian Rosebrock July 8, 2016 at 9:45 am #

      If you’re using the Raspberry Pi camera module, then you’ll want to activate the V4L drivers. Otherwise, I would recommend using the picamera module to access your camera on the Raspberry Pi.

  37. Surya Poudel July 8, 2016 at 11:16 am #

    I can’t import other rpi library modules while I’m inside cv environment. why is it so??

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

      This is because you are using virtual environments. Virtual environments are sequestered and independent from your default Python install. We use them to avoid versioning issues and to keep your projects tidy. You can read more about virtual environments in the first half of this blog post.

  38. Tyler Gubala July 8, 2016 at 11:37 am #

    I cloned the latest OpenCV and OpenCV_Contrib gits and performed a make -j4. It’s been going for over 12 hours at 100% CPU Load. Is this normal? Instructables says that for a Pi model b (the original) it should take 10hrs.

    Could the kernel I am using be nit-so-good? Or maybe I ran out of RAM and it is now performing the switching operations? (I have 256mb allocated to the GPU in my current setup)

    Thanks for the tutorial! Otherwise very easy to follow.

    Funny thing about your RC post and Cat detection posts the other day: I would like to do exactly that, have a cheap rc toy to interact with my cats!

    Shoot, sorry, I should add that this is an RPI 3 Model b. Thanks in advance!

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

      It should definitely not take 12 hours to compile OpenCV on the Raspberry Pi 3. In fact, it should take less than 2 hours. I think there might be an issue with the multi-processing compile and somewhere a race-condition has been caused, making the compile infinitely spin.

      Instead, I would suggest stopping the compile, deleting your “build” directory, re-creating it, and re-running CMake. From there, just compile with a single thread using:

      $ make

      This will take longer to compile, but it should ideally avoid any type of threading issues you may have encountered.

  39. Manu Mahalingam July 13, 2016 at 10:12 pm #

    Hello Mr.Adrian,

    After executing make -j4 my raspberry pi 3 restarted and didn’t do anything after. Can you please explain how to fix this?

    Thank you,
    Manu Mahalingam

    • Adrian Rosebrock July 14, 2016 at 1:10 pm #

      That sounds quite strange. It should not have restarted during the reboot. Perhaps it overheated? I would suggest deleting your build directory, re-creating it, and then re-running make and CMake to see if the problem happens again.

      • Manu Mahalingam July 14, 2016 at 10:38 pm #

        Hello again Mr.Adrian,

        I am not very experienced with raspberry pi yet so could you tell me how to delete the build directory?

        Thanks,
        Manu Mahalingam

        • Adrian Rosebrock July 15, 2016 at 6:18 am #

          You can just use the rm -r command, for example:

          rm -r build

          I would suggest also running man rm to learn more about the command.

  40. Manu Mahalingam July 15, 2016 at 12:36 pm #

    Hi Mr.Adrian,

    When rm -r build
    it says the file is not found yet when I look into the folder in open.cv I find the build file. So could you please explain how to fix this? Sorry for so many questions I hope I am not bothering you.

    Thanks,
    Manu Mahalingam

    • Adrian Rosebrock July 15, 2016 at 1:27 pm #

      Hey Manu — before continuing, I would suggest reading up on the basics of utilizing the command line and the Linux filesystem. This will better help you understand how to resolve these types of errors. This tutorial should help you get started.

  41. Hariharan July 18, 2016 at 1:19 pm #

    Hello Mr Adrian,

    I have followed the entire tutorial and everything works great, but im having trouble with how to autorun my opencv code on start up since my code will only work inside the cv virtual env. Can u please tell me which command should be used to do the above .Thanks in advance.

  42. Milan July 19, 2016 at 3:08 am #

    Hi Adrian,

    I’ve spent a couple of days trying to complete this tutorial, even went as far as creating a clean Raspbian install when I couldn’t get it working.

    I am continually hitting an error just before I run the ¨make -j4¨.

    http://pastebin.com/xus0QxWM

    I have tried deleting the build directory and re-running cmake multiple times.

    Any suggests as to what I might be doing wrong would be appreciated.

    Thanks

    • Adrian Rosebrock July 19, 2016 at 12:28 pm #

      Take a look at your CMake output — the error is clearly stated on Line 60:

      Basically, you need to double-check your path to the opencv_contrib directory. You may have forgotten to download it or placed it in a different directory.

      • Milan July 19, 2016 at 11:58 pm #

        Perfect!! Thank you so much, had a look and the contrib folder was empty so something must have gone wrong there.

        I found your post on finding the distance of a known object to the camera extremely useful. I was wondering If you have any similar posts or resources for finding the x,y,z distance from the camera? I understand this involves camera calibration?

        I’m trying to find a way to do this accurately with real world distances rather than just in the image frame perspective.

        • Adrian Rosebrock July 20, 2016 at 2:38 pm #

          If you want a true representation of the (x, y, z)-coordinate space you’ll need to perform camera calibration first. I don’t have a blog post on that, but I’ll try to do one in the future.

          • Milan July 21, 2016 at 8:08 am #

            Thanks. I will definitely be keeping my eye out for one.

  43. Kevin July 19, 2016 at 7:22 pm #

    Hello Mr. Adrian,

    I was following your tutorial when I made an error during step 4 updating the raspberry pi profile. Instead of putting the ~/.profile prefix, I left it out and ran the commands without it. When I realized my mistake, I tried to run the commands again with ~/.profile but it gave me an “access denied” error.

    What did the commands do without the ~/.profile prefix and how do I stop getting the “access denied” error?

    Thanks.

    • Adrian Rosebrock July 20, 2016 at 2:39 pm #

      Hey Kevin — I’m not sure why you would be getting a access denied permissions error on your ~/.profile file, but try editing the file as sudo instead:

      $ sudo nano ~/.profile

      • Kevin July 22, 2016 at 4:08 pm #

        Mr. Adrian,

        Firstly I want to apologize for the late response. I have been busy with other projects lately and haven’t had time to reply.

        Anyway, I took your suggestion and the nano editor gave me access to the profile folder. However, I know almost nothing about Linux C programming, and am a little lost at what to do. Do I just add the lines after the last if statement, or within the last if statement? And can you explain what the commands do?

        Thanks again.

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

          After you access the ~/.profile file, scroll to the bottom of the file, add a new line, and then copy and paste the virtualenv statements from this tutorial. From there, save and exit the editor.

  44. Stephen July 20, 2016 at 5:59 pm #

    Hi!

    I’m sure you know the answer to this:

    When i do not enter “workon cv” and do not see the (cv) i cannot import cv2 into python3 but can in python 2.

    As soon as I get the cv virtual environment I again have cv2 in python3. Why is this?

    • Adrian Rosebrock July 21, 2016 at 12:44 pm #

      Hey Stephen — I would suggest reading up on Python virtual environments to better understand how they work and why we use them. Specifically, you’ll want to pay attention to the first half of the blog post I linked to.

  45. Manu Mahalingam July 21, 2016 at 3:37 pm #

    Hello Mr.Adrian,

    My raspberry pi 3 crashed again but this time did not restart, even after I deleted the build directory, recreated it and ran cmake and make. Could you please help?

    Thanks for all the tips so far,
    Manu Mahalingam

    • Adrian Rosebrock July 22, 2016 at 10:56 am #

      I’m not sure why your Raspberry Pi 3 would crash. Perhaps it overheated during the compile? Next time try using only a single core when compiling. The compile will take longer, but ideally the Pi won’t get as hot.

      • Manu Mahalingam July 22, 2016 at 7:06 pm #

        Hello Mr.Adrian,

        By using one core I was able to get it running and it is working. Thanks for the great tip, by the way your tutorials rock! I am planning to use this image recognition software for a robotic project and i found this very useful. However I would like to know why in rare conditons does a raspberry pi overheat because of 4 cores did it work for you when you did ran the make command? Just wondering…

        Thanks,
        Manu Mahalingama

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

          Hey Manu — race conditions can handle in any multi-threaded environment. As for exactly why this happened with OpenCV, I’m not sure. I would have to look at the error logs. Also, the Raspberry Pi can overheat. It happens. Especially if you are already in a warm environment.

  46. pradumna July 24, 2016 at 12:09 am #

    can we use open cv on raspberry pi .take video form rasberry pi process it and send the data of the processed video over bluetooth to mobile.i want to create a entire standalone system of pi.without using a laptop

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

      I have a tutorial here on accessing the Raspberry Pi camera. As for sending the photo of bluetooth, I personally haven’t done that and don’t have any experience in that area. I would suggest starting with simply accessing the Raspberry Pi video stream though. From there, you can focus on frame serialization.

  47. Rohan Khosla July 28, 2016 at 1:28 am #

    Hey Adrian, thanks for such a great tutorial.
    Am new to Raspberry pi world and So I was wandering if I use Noobs to install jessie on my RPI 3, am I still able to remove wolfram engine from the memory and continue with your tutorial or I have to completely follow your tutorial from expanding file system and rebooting and all??

    • Adrian Rosebrock July 29, 2016 at 8:38 am #

      I’m not positive, but I think newer versions of NOOBS automatically expands the filesystem? In either case, I would run raspi-config to ensure that the filesystem is expanded. It will only take a few seconds to do it and this will ensure that your filesystem is expanded.

      • Mike October 29, 2016 at 2:15 pm #

        Hi I am using noobs. When I run sudo raspi-config and try to expand partition get message that:” your partition layout is not currently supported by this tool. You are probably using noobs, in which case your root file system is already expanded anyway. ”
        However when I run sudo df -h it tells me Size of root is 6gb. As a result I’m running out of room following your tutorial for opencv installation. Any idea where the other 2gb has gone?

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

          That is quite strange, I’m not sure why that would happen. Can you instead try using strictly Raspbian without NOOBS? If the issue persist I would post on the official Raspberry Pi forums regarding the error.

  48. Gray July 28, 2016 at 10:57 am #

    Hi there,

    I’ve been trying to install OpenCV on my Pi 3 according to these instructions and ran out of space on the Pi’s 8GB SD card. I’m guessing this isn’t normal. I’m using standard NOOBS and there’s some pre-installed stuff I’ve now purged (libreoffice*, penguinpuzzle, etc) to make more room. From df -h I now see the card has 3.2G available – is that enough? Or should I mount something externally to store the build files in? (Or get a bigger SD card, sigh…)


    Success! Looks like removing those extra unecessary packages did the trick. I imagine in future it would be easiest to start out with a ‘light’ copy rather than full NOOBS which is what I was using.

    For reference, I ended up purging:
    libreoffice*
    smartsim
    penguinpuzzle
    dillo
    minecraft-pi
    python-minecraft-pi
    sonic-pi
    wolfram-engine

    Hope this helps someone else.

    • Adrian Rosebrock July 29, 2016 at 8:30 am #

      I agree, I’m actually pretty frustrated with how “bloated” Raspbian has become. There is Raspbian Lite, which as the name suggests is a much smaller version of Raspbian, but it’s also “headless” so if you needed a GUI, you would need to install it.

  49. Xiaolei Tan July 28, 2016 at 11:36 am #

    Removing opencv directory did not work. my image file still has 14GB worth of stuff. Any ways you could help? Thanks in advance!

    $ rm -rf opencv-3.1.0 opencv_contrib-3.1.0

    • Adrian Rosebrock July 29, 2016 at 8:31 am #

      See the comment by “Gray” on packages that can be removed to reduce your .img size.

  50. Mark Salacinski August 3, 2016 at 3:35 pm #

    Thanks for the great guide!
    I ran into a problem while installing NumPy which took some time to figure out, so I’ll share here in case anyone else runs into the same issue.
    The installation for numpy was failing with an error code 1

    What I finally realized was that my /tmp directory was running out of space since I had previously turned it into a tmpfs RAM filesystem with a 80mb limit.
    To fix I just updated my “/etc/fstab” file to remove the line I added that defined /tmp as a RAM filesystem and rebooted my Pi. The numpy install went smoothly after that.

    • Adrian Rosebrock August 4, 2016 at 10:11 am #

      Thanks for sharing Mark!

  51. Aulia Khilmi August 13, 2016 at 11:14 am #

    Hi Adrian, thanks for this tutorial. But I have some problem that I didn’t know the solution.
    I have followed your tutorial step by step, I have done all step correctly. But can’t use import cv2 in python. The dist-package and site-package folder is empty. I think thats because my Step 5 not run correctly and impact to the next step.

    I prefer create python2 virtual environment but just interpreter of python2 that shows.
    http://imgbox.com/ZgLLCjMM

    Here this the full log of my cmake installation of opencv:
    http://pastebin.com/hgZ4tTPZ

    Thanks.

    • Adrian Rosebrock August 14, 2016 at 9:22 am #

      Hey Aulia — it’s hard to to tell what the exact issue is without seeing all of your previous commands, but I would double-check which Python version your virtual environment is using. It seems that you may be using Python 3 by accident.

      • Aulia Khilmi August 14, 2016 at 11:49 pm #

        How to use both of them in a virtual environment cz i see that your screenshot, python 2 and 3 detected sucessfully..

        • Adrian Rosebrock August 16, 2016 at 1:06 pm #

          If you want to use OpenCV in both virtual environments, then you would need to create two separate compiles of OpenCV — one for Python 2.7 and another for Python 3.

  52. JAYANTH August 14, 2016 at 4:04 pm #

    Hi Adrian, thanks for the tutorial, but i have a problem.
    After the cmake command is exectuted, in its output under Python2 and Python3 there is only the path of the interpreter present and nothing else, which is leading to improper installation.
    PLEASE HELP : )
    Thank you

    • Adrian Rosebrock August 16, 2016 at 1:09 pm #

      Please see the previous comments to this post as this issue has been discussed multiple times.

  53. Chris August 14, 2016 at 10:48 pm #

    Hi Adian! tks for your tutorial. i have folowed your tut step by step. i use terminal to test version of opencv in my raspberry pi, it show “3.0.0”, yeah, it work.
    but when i use a file *.py to compiler, it show ” import error cv2″. can you help me? look forward to hearing from you. tks so much 😀

  54. Jianbin August 15, 2016 at 12:57 am #

    Hi Adrian, I do make sure it’s in cv environment in step 5 but the cmake result still are still not in the cv environment. I have tried multiple times and same result happens every time. Where do you think I could be doing wrong? Thanks for your help.

  55. vivek borse August 16, 2016 at 8:30 am #

    hey adrian thanks for tutorial ….
    how to add matplotlib to vertual environment of cv?
    i already install matplot in pi but when i start using cv mode it never import matplotlib so please hepl me on that

    • Adrian Rosebrock August 16, 2016 at 12:57 pm #

      Hey Vivek — take a look at this post for information installing matplotlib.

  56. Joanna August 16, 2016 at 4:24 pm #

    Hi,

    I followed your tutorial and at the last step it says no module named cv2. at this point i was in (cv) environment as you mentioned. but when I’m not in (cv) environment and if i run “import cv2” it works. What does that mean? I think inside virtual environment python cant see openCV. am I right? Please help.

    • Adrian Rosebrock August 17, 2016 at 12:05 pm #

      If you can import OpenCV outside the virtual environment, then OpenCV is installed correctly — you just need to symlink the file into your virtual environment. See Step #6 for more information.

  57. daniel August 16, 2016 at 6:34 pm #

    hi adrian

    How to install OPENCV but outside the virtual environment? I have sensors installed outside , I need to work outside the virtual environment with sensors

    • Adrian Rosebrock August 17, 2016 at 12:04 pm #

      You can certainly use OpenCV outside the virtual environment — just skip all steps related to virtualenv, virtualenvwrapper, and workon and you can install OpenCV without Python virtual environments.

      However, based on your comment, I get the impression that you can simply install your required sensor packages into the Python virtual environment. See this blog post for more information.

      • daniel August 18, 2016 at 11:25 am #

        the opencv without virtual environment was installed , but when testing the example of the cameras , opencv error indicates that libgtk missing , I followed all the steps minus the virtual environment, although not well how to do the last part in step 6

        • Adrian Rosebrock August 22, 2016 at 1:42 pm #

          It sounds like you might have installed OpenCV without GTK installed. You’ll need to install libgtk and then re-compile and re-install OpenCV.

  58. Pierre August 21, 2016 at 7:27 pm #

    Thank you very much Adrian, this is a really well done tutorial! Worked perfectly first time and I have got openCV installed on my RPi 3. Can’t wait to start your tutorials and then I should be ready to start with my project. You have done a fantastic job. Nice one.

    • Adrian Rosebrock August 22, 2016 at 1:27 pm #

      Congrats on getting OpenCV installed on your Pi, Pierre — nice job!

  59. Ken August 25, 2016 at 12:30 am #

    Hi Adrian, thanks a lot for your clear instructions, I have successfully installed OpenCV. My question is on the samples/examples. I saw printout they got compiled and installed along the steps, but I couldn’t find their binaries get installed in the file system. You suggested to rm -rf the two folders at the last step. That includes the sample source code. Do we need them no more? Perhaps there is another way to run those samples? Thanks.

    • Adrian Rosebrock August 25, 2016 at 7:15 am #

      Hey Ken, congrats on getting OpenCV installed. I honestly can’t remember where the samples are stored after you run make install. That would be a good question for the OpenCV forums. Before deleting the opencv and opencv_contrib directories, I would check in there and see if the samples were compiled. But after you are done with them, you can delete those directories.

  60. Ricardo August 25, 2016 at 9:50 pm #

    Hi Adrian!

    Nice walkthrough, thanks! 🙂

    BTW, any idea if we can build OpenCV to use the Raspberry Pi GPU (QPU)?

  61. Luis August 26, 2016 at 12:58 pm #

    Excellent tutorial!
    Got up and running, and completed a proof of concept really quickly! Thanks for the generosity.

    • Adrian Rosebrock August 29, 2016 at 2:06 pm #

      Congrats on getting OpenCV installed and configured on your Pi, Luis!

  62. vivek borse August 29, 2016 at 8:37 am #

    hi Adrian in this tutorial can we install scipy and matplotlib ?
    if yes when i can do that?

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

      Of course! You just need to install them into your Python virtual environment:

      If you run into any issues with matplotlib, refer to this post.

  63. amrosik September 4, 2016 at 5:15 pm #

    There is something strange:

    At the end of the tutorial you say one can delete the opencv-3.1.0 and opencv_contrib directories with rm -rf [folders]. Where is then OpenCV installed? We built Opencv into the build-folder. But after having deleted the folders, there is no build-folder left. How can it be, that Opencv still works? Where is opencv located?

    If I would want to make little customizations to the opencv source, I have no idea where to find the files, and how to remake them.

    kind regards from germany,
    amrosik

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

      After you run:

      $ sudo make install

      The compiled libraries and binaries are copied into their respective places. The original build directories are no longer needed after running make install. You can see exactly where the OpenCV has been copied to by examining your CMake output. Normally you can find the files in /usr/local/lib.

  64. Deepesh Padala September 5, 2016 at 11:09 am #

    i have installed everything successfully but every time i try running a program (after reboot) that includes the opencv library i have to enter these two commands:

    ‘workon cv’
    ‘source ~/.profile’

    Is there any way by which i don’t need to do that every time i run a script after reboot
    Please reply as soon as possible

    • Adrian Rosebrock September 5, 2016 at 12:44 pm #

      Yes, both of those scripts need to be executed each time you reboot your system or open a new shell. It’s part of configuring your development environment properly. If you do not want to run workon after reboot, just update your .profile file to include the workon command as well.

      • Deepesh Padala September 6, 2016 at 7:01 am #

        So i’ll have to run source ~/.profile every time?

        • Deepesh Padala September 6, 2016 at 7:12 am #

          I have another doubt: How do i run it on GUI

          • Adrian Rosebrock September 6, 2016 at 3:36 pm #

            What do you mean by “GUI”? I presume you mean IDE?

        • Adrian Rosebrock September 6, 2016 at 3:35 pm #

          Not every time — just each time that you open up a new shell.

  65. Deepesh Padala September 6, 2016 at 11:17 am #

    Can you please tell me how to execute the ‘workon cv’ command in python shell

    • Adrian Rosebrock September 6, 2016 at 3:35 pm #

      You actually execute the workon cv< ?code> command BEFORE you open a Python shell. For example:

  66. Tkp September 10, 2016 at 6:40 am #

    Hi when I enter this command “wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
    I get the error failed:Connection timed out. Retrying..I’ve done this multiple times but no success.

    • Adrian Rosebrock September 12, 2016 at 12:54 pm #

      It sounds like you might have a problem with your internet connection or there was a hiccup in the GitHub connection. I’ve manually tried to download the file just now and it worked fine. My suggestion would be to ensure your internet connection is working properly and try again.

      • Jason Braverman November 10, 2016 at 12:14 pm #

        I am not able to get this either… seems like it was moved. Can you tell us where to locate it now?

        • Adrian Rosebrock November 14, 2016 at 12:21 pm #

          The GitHub link is still working. Make sure you are copying and pasting the URL correctly.

  67. Tom September 12, 2016 at 5:01 pm #

    Thanks for the thorough, clear, and well-paced tutorial!

    I followed everything to a T but failed at Step 6. My Python 3.4 directory only has dist-packages, no site-packages. As such, it doesn’t seem like any of the Python bindings have been installed after running ‘make’… Any idea? Fresh RPi3, fresh SD card, started from the top, worked my way down…

    Thanks!!

    • Adrian Rosebrock September 13, 2016 at 12:50 pm #

      Check in the build/lib directory for any sort of cv2.so file. If you’re using Python 3, the filename may by slightly different. Also, after running sudo make install, be sure to check your dist-packages directory. Finally, manually investigate your CMake output to see if CMake has displayed where the bindings would be installed.

      • Tom September 17, 2016 at 12:44 pm #

        That did it! Much thanks, seems to work 🙂

        It had put it in the build/lib/python3 directory. I simply moved it over to the site-packages folder and followed the rest of the steps.

        • Adrian Rosebrock September 19, 2016 at 1:11 pm #

          Great job resolving the issue Tom, congrats!

  68. Md. Fahimul Islam September 17, 2016 at 8:11 am #

    Your tutorial are the best in this world.

    I have a simple question on my mind. You said:
    “Any Python packages in the global site-packages directory will not be available to the cv virtual environment. Similarly, any Python packages installed in site-packages of cv will not be available to the global install of Python.”

    I didn’t install opnenCV on my Global environment but on the environment named “cv” i installed python 3 and open cv. But when I open a fresh terminal and type (without the workon command):

    $ python
    python 2.7.9 (default, Mar 8 2015, 00:52:26)

    which makes sense, cause my global python is 2.7.9 where as the one in the virtual environment is 3.4.2

    >>> import cv2
    >>> cv2.version
    ‘3.1.0’

    NO ERRO!! Why is that? Shouldn’t it pop an error message saying “cv2 not understood” as we are working on global environment?

    no error occurs. Why is that?

    • Adrian Rosebrock September 19, 2016 at 1:17 pm #

      When you installed OpenCV, it was installed in /usr/local/lib/python2.7/site-packages — this directory is the global directory for your Python install. It does indeed work outside the virtual environment. We then use the sym-link command to move the cv2.so bindings into the “cv” virtual environment.

      That said, I’m not sure how you have OpenCV bindings for both Python 2.7 and Python 3 installed on the same system without explicitly doing so. Compiling OpenCV against a different version of Python than it’s imported into would cause an error, so I’m not particularly sure how your system is setup.

  69. Shehan September 22, 2016 at 6:07 am #

    http://www.pyimagesearch.com/wp-content/uploads/2015/10/raspbian_jessie_py3_example.jpg

    When I try the above code at terminal, it is working .But When I open Python3IDLE and type #import cv2# then a error occurs saying #ImportError: No module named ‘cv2’# .

    How to solve this???????????

    • Adrian Rosebrock September 23, 2016 at 6:57 am #

      Python virtual environments will work with the shell version of IDLE but not the GUI version of IDLE. Instead, try using Jupyter notebooks.

  70. Eric Lai October 7, 2016 at 9:00 am #

    After I enter the Cmake command, there were no paths for libraries, numpy, packages path for Python 2, however there is for Python 3. Does this mean, I won’t be able to use OpenCV in Python 2?

    • Adrian Rosebrock October 11, 2016 at 1:17 pm #

      If you want to use OpenCV with both Python 2.7 and Python 3 then you’ll need to compile OpenCV twice, one for each respective version. The bindings are not cross-compatible between Python versions.

  71. Eric Lai October 7, 2016 at 9:49 pm #

    Hi Adrian,
    I completed the tutorial, and its awesome, I got it working on Python 3. However, I can only run cv2 on ssh terminal. Once I enter the pi via vnc server, it doesn’t work anymore, I can’t enter the virtual environment through ther terminal either.

    I’m still new to raspi, and also how linux works. So how do you run the virtual environment in the Raspi GUI via VNC server?

    Thank you in advance Adrian

    • Adrian Rosebrock October 11, 2016 at 1:13 pm #

      If you are accessing your Pi via the GUI (VNC doesn’t have anything to do with it) make sure you are source‘ing your .profile file before importing the cv2 bindings:

  72. Michael Deitcher October 10, 2016 at 11:12 pm #

    Thanks so much! Your instructions worked like a charm. Although most of the steps took a lot longer than the amount of time you stated.

    • Adrian Rosebrock October 11, 2016 at 12:52 pm #

      Thanks for sharing Michael, I’m happy the tutorial worked for you.

  73. Peter Litvak October 13, 2016 at 7:07 pm #

    Anyone had issue with building opencv because of the FFMpeg dependencies problem?
    I’m getting a compilation error about avformat_get_mov_video_tags.
    Any help is much appreciated.

    • Riven February 5, 2017 at 9:26 pm #

      This happens to me before I correct typing the opencv contrib path.

  74. Olivier October 18, 2016 at 3:30 am #

    Awesome guide! The only one that worked for me.

    Do you have any new insights on how to include TBB to the mix since Jan asked in May?

    • Adrian Rosebrock October 20, 2016 at 9:01 am #

      I honestly haven’t tried since then. I’ll try to discuss it in a future post but I’m not sure when that might be.

  75. Imena October 19, 2016 at 10:50 am #

    Thank you so much , it was very useful 🙂

  76. David Park October 21, 2016 at 3:05 am #

    hi~
    i’m done until Step#3 and installed pip.
    but i don’t get how to edit ‘.profile’ .
    would you tell me where it is and how to edit?

    • Adrian Rosebrock October 23, 2016 at 10:21 am #

      You can use a simple text editor such as nano:

      $ nano ~/.profile

  77. Charles Linquist October 21, 2016 at 6:57 pm #

    About halfway through the tutorial, you mention modifying the ~/.profile

    I can’t figure out where the .profile file is. If I type ~/.profile, I get nothing. If I try nano ~/.profile, it creates a new file. How do I get to this file to edit it?

    See below
    ————————————————————————————————————–
    Now that both virtualenv and virtualenvwrapper have been installed, we need to update our ~/.profile file to include the following lines at the bottom of the file:

    # virtualenv and virtualenvwrapper
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh

    • Adrian Rosebrock October 23, 2016 at 10:18 am #

      If the ~/.profile does not already exist, create it.

  78. Steven Luong October 24, 2016 at 6:10 pm #

    Thanks a lot for sharing,
    Very useful !

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

      No problem, I’m happy I could help Steven!

  79. John Ellinas October 26, 2016 at 1:31 am #

    Adrian congratulations for your posts and your book!

    I have installed opencv with python2.7 correctly and everything is fine. Then, I tried to make the same steps for python3 from the point: mkvirtualenv cv -p python3 onwards in the same or in a different terminal window. The installation cannot be done. When I am trying to import cv2, it says it cannot find numpy. Why numpy is not in the site-packages although I have followed exactly the same steps as for python2.7?

    Thank you anyway for your valuable presence!

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

      Hey John — it sounds like you are not inside the cv virtual environment. Every time you open up a new terminal (after running mkvirtualenv to create the actual virtual environment) you should be using the workon command to access it:

      $ workon cv

  80. Andy October 27, 2016 at 10:07 am #

    hi Adrian,

    Appreciated with your post, it really helped plenty of people who wants to install opencv on their Raspberry Pi, however there is one question would like to ask:

    Although my Pi 3 has SUCCESSFULLY installed the opencv 3.1.0 and also showed the correct version in Python 3 as Step #7 illustrated; however, when i verify with the ls command, by type in:

    ls -l /usr/local/lib/python3.4/site-packages/

    It returns the following:
    -rw-r--r-- 1 root staff 1480296 Oct 27 06:08 cv2.cpython-34m.so

    Knowing some files are missing as for the fact, but will opencv operate properly with missing files? Please advice, thank you very much.

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

      I actually address this question inside the blog post. Take a look at the “For Python 3:” sub-section. All you need to do is rename the file to simply cv2.so

  81. tommie jones October 29, 2016 at 10:22 pm #

    this must have been a race condition. I did a make without the j4 and it worked.

  82. Imran Ibrahimi November 2, 2016 at 6:13 am #

    Hello Adrian,

    I read a comment above indicating that the steps would remain the same for a c++ installing however I’m unsure how or where to obtain the c++ header files from in order to compile with c++ bindings?

    Hope you can get back to me!

    Thanks for always replying to my emails by the way, your dedication is inspirational!

    • Adrian Rosebrock November 3, 2016 at 9:44 am #

      I don’t cover C++ on this blog, but for what it’s worth, your answer can be found here. Basically, you need to use either pkg-config or CMake.

  83. Jack November 6, 2016 at 6:03 pm #

    Hi Adrian,

    I’m reasonably new to python (and raspberry pi) and am feeling in the dark a little bit with the virtual environments stuff.

    I need to use opencv 3 on my raspberry pi 3, preferably with python 3, and I also need to install wiringpi:
    https://github.com/WiringPi/WiringPi-Python#manual-build

    If I try to follow your instructions without the virtual environments bit then I don’t know how to get cmake to set the ‘python (for build)’ version to python3.

    Conversely, if I try to follow the instructions on the wiringpi page whilst in my newly created python3 virtual environment, and then run
    python -c ‘import wiringpi; print(“Hello”);’
    I get an error that it cannot find the ‘_wiringpi’ module.

    What is my quickest way out?

    • Jack November 7, 2016 at 1:52 am #

      UPDATE:

      I set the build going for what I assumed would be only python2.7 last night, only do my delight discovered that it created the cv2.so files for both python versions!!

      So my problem was really a non-problem (though others may benefit from knowing this too!!).

      Thank you very much for the excellent tutorial!

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

        Hi Jack — I would double-check that the cv2.so files were created for both versions by importing OpenCV into both Python versions. I have only seen circumstances where the bindings are build for Python 2.7 OR Python 3 — never both.

        I also don’t have any experience with WiringPi, but provided you can copy the install files to the virtual environment you’ll be all set 🙂

  84. Carlos November 8, 2016 at 4:03 pm #

    Hi, is it possible to install Opencv in a Raspberry 2 with Ubuntu mate?

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

      I have not tried using this tutorial with Ubuntu Mate but it should work just fine.

  85. Nura November 9, 2016 at 7:52 am #

    Hi Adrian, I have different problems every time I try to compile. such problems are os / hardware related problem (this happened when I run with core4). then I changed to make -j1 as I believed to compile with 1core, it took me couple of hours until it terminated and give message as below:

    /usr/include/c++/4.9/limits:1777:1: fatal error: can’t write PCH file: No space left on device

    Im using 16GB memory sd card.
    Any advise? Thank you.

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

      16GB is more an sufficient to compile and install OpenCV. If you are running out of space my guess is that you forgot to run raspi-config => Expand Filesystem to ensure the entire disk is being utilized.

      • NURA November 11, 2016 at 9:36 am #

        I just found out that Raspberry built some partitions in SD card that only left 2GB for me. I reformat my SD card (delete the unnecessary partitions) and finally have 8GB volume left. Thank you for your tutorial. Now everything work as I hope it to be.

  86. Filippo November 15, 2016 at 11:25 am #

    Hi!
    Thanks for your great tutorial! Everything is working fine except when I’m in the virtual env (python2.7) I get import error of other modules installed with apt-get outside it.

    The module pigpio is imported without any problems outside cv env.

    How can I solve this?

    Thanks again, keep up the good work!

    • Adrian Rosebrock November 16, 2016 at 1:44 pm #

      You need to install pigpio into the “cv” virtual environment. I haven’t used pigpio before, but this blog post should help get you started.

  87. Mike November 15, 2016 at 7:00 pm #

    When I got to step 5 and type in:
    $ cmake …

    it says:
    bash: cmake: command not found

    Any suggestions?

    • Adrian Rosebrock November 16, 2016 at 1:37 pm #

      Hey Mike — make sure you follow the instructions in this tutorial exactly. You forgot to execute this command in Step #2:

      $ sudo apt-get install build-essential cmake pkg-config

  88. Amit Jain November 16, 2016 at 8:08 am #

    Is there any ready to burn RasPi image with integrated OpenCV libraries?

    • Adrian Rosebrock November 16, 2016 at 8:09 am #

      Great question, thanks for asking.

      Yes, there is a pre-configured Raspbian .img with OpenCV pre-installed as part of the Quickstart Bundle and Hardcopy Bundle of my book, Practical Python and OpenCV.

  89. Mike November 21, 2016 at 11:51 am #

    Hello
    I have gotten all the way through the the tutorial and can import cv2 in the LX terminal when in the virtual environment running python 3, but when I open up python 3 and try to import cv2 it gives me an undefined error. What do I need to do to fix this?

    • Adrian Rosebrock November 21, 2016 at 12:24 pm #

      So you can import OpenCV into your Python 3 virtual environment, correct? And you now want to import OpenCV outside your virtual environment? And I understanding your question correctly?

  90. Atilla November 22, 2016 at 5:44 am #

    Hi Adrian,

    Thanks for the post its amazing I like to ask one question that is it possible to install opencv and c++ only. Can i skip entire python installation process. I just need opencv and C++ compiler in raspberry pi 3 model B with raspbaian jesse. please suggest what can be the steps.

    Thanks and Take care 🙂

    • Adrian Rosebrock November 22, 2016 at 12:30 pm #

      You can skip the Python steps if you want and you’ll still have the C++ version of OpenCV installed. That said, if you skip steps I can’t provide any help or suggestions if you run into errors.

  91. JBeale November 24, 2016 at 3:30 pm #

    Great and detailed instructions! Just FYI, I tried the compile starting with 2.6 GB free space, but that didn’t work. Turns out after compiling, the opencv-3.1.0/ folder ends up taking 3.0 GB of space.

    • Adrian Rosebrock November 28, 2016 at 10:43 am #

      I would suggest having an 8GB card for the install process.

  92. Vaibhav Jain November 27, 2016 at 6:21 am #

    Hi sir,I created a local webserver that streams the video from my Logitech camera, and the webpage also has the buttons through which I control my robot.Can i make open cv work on the video that my camera is streaming to my local server?

    • Adrian Rosebrock November 28, 2016 at 10:26 am #

      You would need to use cv2.VidepCapture to read the video stream URL from your camera. However, this would be separate from the video that you see in your web browser. Without a significant amount of hacking I don’t see how you could modify the direct URL of the stream loaded into your browser.

  93. Edgar November 28, 2016 at 8:33 pm #

    hey hi, i have a scool project with raspberry, but i am a beginner in phyton, ok i say what is the project, fist i need to take a picture of a plate of car, and need to read this picture and then activate a 2 step motors of this motors i have the code of programation, i have problems whit take a capture of the plate can u helpme please?

    • Adrian Rosebrock November 29, 2016 at 7:59 am #

      By “plate” do you mean “license plate”? If so, I cover Automatic License Plate Recognition inside the PyImageSearch Gurus course.

  94. Omer November 30, 2016 at 2:25 am #

    Hi Adrian,

    Thanks for your tutorial and demonstration I appreciated! I did everything as you did. But there is a problem about video streaming. Everything worked well when I was trying to take a photo. But I got only black screen when I was streaming to video. I will be very happy if you can help me. Thank you very much ..

    • Adrian Rosebrock December 1, 2016 at 7:37 am #

      Hey Omer — I discuss the black screen issue in this post where I also provide the solution.

  95. Anthony December 2, 2016 at 12:38 am #

    Hi Adrian,

    I followed your guide to install opencv on a RPi 3, but with Jessie Lite and with no virtual environment. Cmake failed in that opencv only pointed to python2.7, when I want to be using python3.

    Therefore, I started again and only installed the python3-dev, and also used sudo pip3 for numpy. However, the packages path points to lib/python3.4/dist-packages, rather than site packages ….. is this a problem?

    I can import cv2 and numpy into python3 and they seem to be working fine, but will future packages be installed into site-packages and will having two locations for packages cause an issue?

    I don’t really understand it, but it is discussed here https://leemendelowitz.github.io/blog/how-does-python-find-packages.html

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

      Hey Anthony — the solution in your case was to use “python3-dev”. You need the Python development headers installed so you can compile the OpenCV bindings.

      You will still be able to use OpenCV with your Python 3 install. Python will check dist-packages and site-packages.

  96. Edgar December 2, 2016 at 12:21 pm #

    hi sorry but i have problems when tray to put this make -j4 because the instalation is in 91% or 92% and the raspberry freeze do you know why do this?

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

      It sounds like this could be a race case condition. I would stop the compile, run make clean and then compile with just make.

  97. Thaddeus December 4, 2016 at 10:02 am #

    I wish to use I2C with my opencv project, but I can’t seem to get the “smbus” module working in the cv environment.

    “import smbus” results in: “ImportError: No module named smbus”

    I tried both “sudo apt-get install python-smbus” and “sudo pip install smbus” with cv activated, but it returns that it’s already up to date. Yet “pip freeze” does not show smbus unless I’m outside cv.

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

      You need to install smbus into the Python virtual environment.

      From there your script should work.

      • Thaddeus December 8, 2016 at 2:06 am #

        Sorry, I meant to add that I was already working on the cv environment when trying to install it!

        • Adrian Rosebrock December 10, 2016 at 7:28 am #

          I’m not familiar with the smbus library. Does it require root permissions to run? I would also utilize pip freeze to ensure it is installed inside the “cv” virtual environment.

  98. Hamed December 11, 2016 at 1:42 pm #

    Hi Adrian
    I followed the exact steps that you’ve written nad the I execute the cmake in step 5 my numpy and interpreter paths are not in the cv environment :
    Interpreter : /usr/bin/python2.7
    I am sure that I’m in cv env and I have installed all the packages and libraries from the second chapter of your book.
    what can I do to fix this?
    Thanks Hamed

    • Adrian Rosebrock December 12, 2016 at 10:37 am #

      Hey Hamed — it’s hard to tell without having access to your machine, but I would suggest taking a look at the “Troubleshooting” section of this tutorial. You should also delete your “build” directory, re-create it, and re-run CMake.

      • Hamed December 15, 2016 at 3:23 pm #

        Hi again
        I tried opencv 3.1.0 three times with a new raspbian every time but it didnt work so I tried your other tutorial for opencv 3.0.0 and it worked perfectly.
        when I was trying the fifth step (cmake command) it needed the sudo command to work but on opencv 3.0.0 it worked without the sudo and I think that was the only difference.Thanks for the tutorials you are the best

        • Adrian Rosebrock December 18, 2016 at 8:49 am #

          Congrats on getting OpenCV installed on your Pi, Hamed — nice job!

  99. brunno December 13, 2016 at 7:39 am #

    Great tutorial, thank you!

    • Adrian Rosebrock December 14, 2016 at 8:31 am #

      I’m happy I could help Brunno! Have an awesome day. 🙂

  100. leon December 18, 2016 at 6:33 pm #

    I used NOOBS, and there isn’t sufficient space on there for that.
    The total build uses 3.24GB.

    Here is what I did:

    1) grabbed an old thumb drive – 4GB and plugged it in
    2) typed “df” and identified it from that as /dev/sdb1
    3) umount /dev/sdb1
    4) mkfs.ext3 /dev/sdb1 (this annihilates everything but hey, thats what a 4GB drive deserves!
    5) mkdir /home/cv (make a directory called /home/cv, for our new user “cv” -see later)

    6) put an entry into /etc/fstab:
    /dev/sdb1 /home/cv ext3 defaults,noatime 0 1

    7) sudo mount /dev/sda1 /home/cv (or sudo mount -a )

    8) sudo adduser cv (to add the user cv, which will compile the opencv)

    9) sudo chown cv:cv /home/cv

    And from there on, follow step 2 from above!

    I must say, I am coming back to linux after nearly a decade of absence, and this was one of the easiest and most nicely documented compiles I have seen. Well done!

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

      Thanks for sharing Leon. As long as you have an 8GB or greater SD card you should have enough room to compile and install OpenCV without additional storage.

  101. ghanendra December 19, 2016 at 6:47 am #

    Hey Adrian, can you help me out,
    how to install dlib library??
    how to use all 4 cores of processor in Pi 3 for heavy motion tracking.?

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

      I will write an updated tutorial on how to install dlib, but you should be using the official dlib documentation in the meantime.

      For the Python bindings, you can also use:

      $ pip install dlib

  102. Mike December 19, 2016 at 4:58 pm #

    Hi Adrain thanks for the great tutorials, I’ve found many of them to be quite helpful.

    I am having some trouble on step 5 of this tutorial where we configure the cmake options and confirm that the output shows the build is using the correct python interpreters. Currently I have a python 3.5 virtualenv setup, have installed numpy and can confirm that “python” and “import numpy” pull the 3.5 interpreter and numpy successfully. However when I run cmake with your options it chooses the python 3 interpreter in /usr/bin/python3.4

    I tried adding these more explicit options into cmake:
    -D PYTHON3_PACKAGES_PATH=/.virtualenvs/py35-cv/lib/python3.5/site-packages \
    -D PYTHON3_INCLUDE_DIR=/.virtualenvs/py35-cv/include/python3.5m \

    But they didn’t seem to have an effect.

    Any advice?
    Note: I am working inside my py35-cv virtual environment when running the cmake commands.

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

      Whenever you make a change to your CMake command, make sure you delete the build directory, re-create it, and then re-run CMake. This will ensure that any previous CMake configurations have been removed. If you have explicitly pointed CMake to your Python 3.5 install then it should work.

  103. Supra December 28, 2016 at 3:14 pm #

    OpenCV 3.2.0 has been released

  104. Harikishore December 31, 2016 at 12:52 am #

    Hi,
    Is it possible to run an opencv python program in raspberry pi without using a display module? Because I don’t want any visual outputs,so using a display will be a waste of cost.

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

      Absolutely. You can SSH into your Pi with X11 forwarding and you’ll be able to see the results of your program on your screen. You could also use VNC as well.

  105. Roshan Sivakumar January 4, 2017 at 4:55 am #

    Hi Adrian,

    Am I supposed to write the following:

    $ sudo make install
    $ sudo ldconfig

    in the build folder??

    • Adrian Rosebrock January 4, 2017 at 10:40 am #

      Yes, you run sudo make install after successfully compiling OpenCV via make. The sudo ldconfig command can technically be ran from anywhere.

      • Roshan Sivakumar January 4, 2017 at 4:09 pm #

        thanks

      • Justin January 18, 2017 at 3:51 pm #

        I’m at the step “Finally, we are now ready to compile OpenCV:” and get error message “make: *** No targets specified and no makefile found. Stop.” I have the (cv) pi@raspberrypi: so am in the cv environment. Any ideas? Thanks, Justin

        • Justin January 18, 2017 at 3:56 pm #

          I looked at the results from cmake .. and see “Configuring incomplete, errors occurred!” I have to look through the error log.

          • Yunshen January 21, 2017 at 10:59 pm #

            i have the exact same error as yours last night, then I made a new system and follow the instructions again.

            This time i got rid of the “Configuring incomplete”. I thought this would be caused by the version, i used the 3.1 instead of 3.2 this time.

            When running the “make -j4”, make sure that you are currently in the /build direction.

  106. Forrest L January 8, 2017 at 12:58 pm #

    Very high quality tutorial. I’m sure I will be accessing your paid content soon. Thanks!

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

      Thanks Forrest! Have a great day.

  107. veeresh January 17, 2017 at 11:12 pm #

    thanks a lot for this in-depth tutorial….was very helpful for a noob like me. keep up the good work!

    • Adrian Rosebrock January 18, 2017 at 7:10 am #

      Congrats on getting OpenCV installed, Veeresh — nice work!

  108. Ashish January 18, 2017 at 8:36 am #

    I got the solution…..

    the error was related to make -j4

    initially i tried make -j4 command for compilation

    after using just make command opencv compiled successfully

    • Adrian Rosebrock January 18, 2017 at 12:21 pm #

      Congrats on getting OpenCV installed Ashish!

  109. Yunshen January 22, 2017 at 1:10 am #

    Hi, many thanks for your really helpful tutorial let me finally get the opencv work on my Pi.

    But i do have a question here. This tutorial teaches how to install the Opencv in the virtual environment. Therefore does it means, for every time i have to go into the VE to import the cv2? I found which could be very tedious, so how can i install it in the real system instead of the virtual one?

    Many thanks

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

      If you would like to use OpenCV outside the virtual environment just ensure you have copied the cv2.so file into the site-packages directory of your system install. Take care to ensure that your system install of Python is the same as the one you compiled OpenCV against, otherwise you will get an error.

      • Finn January 27, 2017 at 9:30 am #

        Where can I locate the cv2.so file?

        • Adrian Rosebrock January 28, 2017 at 6:55 am #

          If you have already compiled OpenCV it should be in build/lib. If you have already installed OpenCV it will be in the site-packages directory of the Python location you provided ruing the CMake step.

  110. Gal January 22, 2017 at 8:35 pm #

    I tried following steps with download of up-to-date OpenCv3.2.0 and got stuck at step 5. I then deleted these and started from step 1 again, this time using download links provided to OpenCv3.1.0 and works perfectly. Thanks Adrian!

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

      Congrats on getting OpenCV installed, Gal — nice job!

  111. Jesse January 29, 2017 at 1:58 am #

    Thanks for the in-depth tutorial Adrian.
    I too have had an issue with version 3.2.0 not working. Could you please update this so it will work with 3.2.0 so we can utilise the new features and bug fixes
    Thanks!

    UPDATE

    I just figured out what my problem was. I hope this is the same problem others were having also as it is a simple fix. I did not see the 3.1.0 in the cmake part, here -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \

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

      This compile will certainly work with OpenCV 3.2. As you suggested, just make sure you update all 3.1 paths to 3.2.

  112. Bipin Paudel January 29, 2017 at 9:35 am #

    Thankyou so much for this amazing tutorial. worked perfectly 😀

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

      Awesome, I’m glad to hear it Bipin 🙂 Have a great day.

  113. Sam January 30, 2017 at 12:58 am #

    Adrian, This is a great tutorial and a must-have resource for starting with OpenCV and Python on Raspberry Pi ! I have subscribed to both your 10 day and 21 day crash courses. I have been having trouble trying to get my setup working on Raspberry Pi. I had two unused webcams (Phillips SPC900NC and Logitech Quickcam 400). I am trying to get OpenCV working on Raspberry Pi with an intention to use these two for stereo vision. Do you think its something worth attempting? I know people typically use same cameras – but theoretically I should be able to get this working? Your advice?

    • Adrian Rosebrock January 30, 2017 at 4:21 pm #

      If at all possible I would highly recommend that you use the same cameras. Theoretically, it’s possible — but practically it’s more of a headache.

  114. Johannes February 6, 2017 at 4:01 pm #

    Hi Adrian, great tutorial! It works also on my RPi2B with full Jessy-OS on 16GByte with opencv3.2.0. I got also Jupyter on my virtual Environment. It works fine. Next i try to use the picamera. The cv2.videoCapture with camera is not easy2use but You also habe a solution 🙂

    • Adrian Rosebrock February 7, 2017 at 9:04 am #

      Congrats on getting OpenCV installed Johannes! And excellent usage of Jupyter Notebooks 🙂

  115. Silvia February 7, 2017 at 10:49 am #

    Thanks so much for this wonderful step-by-step post! 🙂 Very useful and easy to follow!

    • Adrian Rosebrock February 10, 2017 at 2:18 pm #

      Thanks Silvia!

  116. Bud February 10, 2017 at 5:03 pm #

    Awesome post!!

    Sadly though, I am having some trouble integrating it with my current project.

    I am able to install OpenCV3 fully and import/test it (step 7) successfully. However, when I exit out of python back to the normal command line and attempt to run my program I get “ImportErrorr: no module named cv2”. I ran through all of the trouble shooting steps and can’t determine the problem. My “~/.profile” is correct, it looks like the smy-link is correct, and I am able to run “workon cv”. I’m not sure where to go from here and any help would be appreciated.

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

      Hey Bud — are you sure you’re inside your Python virtual environment when trying to execute your script?

  117. Jesse February 11, 2017 at 12:59 pm #

    Great instructions and everything works. Unfortunately my unfamiliarity with virtual environments is causing me a lot of issues. Looking up explanations of virtualenv elsewhere, they use different terminology/steps to install it (for instance they don’t use .profile etc). Therefore, I’m unable to understand what I did in this tutorial and I am having trouble adding packages to my virtual environment or creating new virtual environments.

    If I wanted to delete this virtual environment and install opencv the regular way, how do I go about uninstalling the cv virtual environment/ what folders do I delete?

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

      Hey Jesse, you can delete a virtual environment with:

      $ rmvirtualenv your_env_name

      If you are struggling to get OpenCV + Python installed on your system, be sure to take a look at the Quickstart Bundle and Hardcopy Bundle of Practical Python and OpenCV which come with a Raspbian .img file with OpenCV pre-configured and pre-installed. Simply flash the .img file to your SD card and boot!

  118. N Sruthi February 15, 2017 at 8:12 am #

    i have a error on installation the ” importerror: no module named cv2” pls how to diagnose the error pls help me

    • Adrian Rosebrock February 15, 2017 at 8:57 am #

      Please take a look at the “Troubleshooting and FAQ” section of this tutorial where I discuss common errors related to importing the “cv2” bindings.

  119. Matt February 18, 2017 at 2:58 pm #

    Hi Adrian – great install guide! I’m having trouble setting up the OpenCv build. It seems to be telling me that cmake is not available ..? I may have screwed this up myself, by first setting up the python virtualenv for Py2.7, then for Py3. Should I wipe the card and start over?

    (cv) pi@raspberrypi:~/opencv-3.1.0/build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \

    bash: cmake: command not found

    I’m new to Linux and sure could use some advice.
    Thanks!

    • Adrian Rosebrock February 20, 2017 at 7:52 am #

      I thin you forgot this command from Step #2:

      $ sudo apt-get install build-essential cmake pkg-config

      • Matt February 21, 2017 at 4:06 pm #

        well, that’s just embarrassing … thanks, Adrian.

        • Adrian Rosebrock February 22, 2017 at 1:32 pm #

          No problem, I’m happy I could help 🙂

  120. abhik February 18, 2017 at 3:22 pm #

    Amazing Post!!
    Had no snags whatsoever and got a good idea of what I’m doing thanks to the embedded links.

    You’re a real lifesaver. Keep it up and all the best!

    • Adrian Rosebrock February 20, 2017 at 7:51 am #

      Nice job getting OpenCV installed Abhik, congrats!

  121. linson February 19, 2017 at 1:26 am #

    $ sudo pip install virtualenv virtualenvwrapper this step not working it says could not find a version that satisfies the requirment virtualenwrapper( from versions:)
    sir how can i solve it

  122. Mustakim February 19, 2017 at 12:18 pm #

    hi Adrian, can i follow this steps for raspberry pi 2 model B? Thanks.

    • Adrian Rosebrock February 20, 2017 at 7:43 am #

      Yes, these steps will work for the Pi Model B provided that you are running Raspbian Jessie.

  123. Matt February 20, 2017 at 4:52 pm #

    Thanks for the tutorial, Adrian! Got it all done and backed up, too.

    One question:

    Could the install have affected my RPi3 wifi? I had trouble staying connected during some of the longer gets yesterday, and read that I could use ‘sudo wconfig lan0 power off’ to fix it, and it did- the rest of the installs went off without a hitch. But today, after a fresh boot, the same command (its also in rc.local) has no apparent affect. The wifi fails every time I use the internet. Each time I restart the wifi it goes down within a minute, which is much more frequently than when I was doing the installs. And I think it takes my router down with it, too.

    Any ideas?

    • Adrian Rosebrock February 22, 2017 at 1:43 pm #

      Hey Matt — that is a very strange error, I haven’t encountered that before. The OpenCV install should not affect your WiFi at all.

  124. Breanne February 22, 2017 at 11:59 am #

    Hi Adrian,
    Thanks for the great tutorial. I currently have opencv working on my pi 3, but when I try to run a python file that imports cv2 as sudo it cant find it.

    To be more specific when I run:
    python cvTest.py

    everything works and cv is properly imported and such, but when I run:
    sudo python cvTest.py

    I get the error: ImportError: No module named cv2

    Do you have any idea why this is happening or how to solve it? I have to run my final program as sudo to use the pigpio that the project also uses.

    Thanks for the help!

    • Adrian Rosebrock February 22, 2017 at 1:28 pm #

      Hey Breanne — since you are trying to execute the script as the root user, you need to specify the full path to your Python binary to ensure the environment variables are set:

      $ sudo /home/pi/.virtualenvs/cv/bin/python your_script.py/code>

  125. Ray February 23, 2017 at 8:41 am #

    Hi again; it doesn’t have the import cv2 problem when in python IDLE 2.7.9 or IDLE 3.4.2 when running in the GUI or in a terminal while in the GUI… seems like it was just a Putty problem! Woohoo!

    • Adrian Rosebrock February 24, 2017 at 11:30 am #

      Congrats on resolving the issue Ray!

Leave a Reply