Install OpenCV 4 on Raspberry Pi 4 and Raspbian Buster

In this tutorial you will learn how to install OpenCV 4 on the Raspberry Pi 4 and Raspbian Buster.

You will learn how to install OpenCV 4 on Raspbian Buster via both:

  1. A simple pip-install method (which can be completed in a matter of minutes)
  2. Compiling from source (which will take longer but will give you access to the full, optimized install of OpenCV)

To learn more about installing OpenCV 4 on the Raspberry Pi 4 and Raspbian Buster, just keep reading.

Install OpenCV 4 on Raspberry Pi 4 and Raspbian Buster

In this tutorial we will install and test OpenCV 4 on Raspbian Buster in five simple, easy-to-follow steps.

If you’ve ever compiled OpenCV from scratch before, you know that the process is especially time-consuming and even painstakingly frustrating if you miss a key step or if you are new to Linux and Bash.

In Q4 2018, a new, faster method for installing OpenCV on the Raspberry Pi (i.e., a pip install) was made possible thanks to the hard work of the following people:

Installing OpenCV via pip is easier than ever. In fact, you can be up and running (Step #1 – Step #4a) in less than 10 minutes.

But what’s the catch?

Using pip to install OpenCV is great, but for some projects (including many educational projects on PyImageSearch.com and in my books/courses) you might want the complete install of OpenCV (which the pip install won’t give you).

Don’t worry, I’ve got you covered in Step #4b below — you’ll learn to use CMake and Make to compile OpenCV 4 on BusterOS from scratch.

Let’s dive in!

Before we begin: Grab your Raspberry Pi and flash BusterOS to your microSD

Let’s review the hardware requirements for this tutorial:

  • Raspberry Pi: Despite the title of this tutorial, you may use the Raspberry Pi hardware including 3B, 3B+, or 4B to install OpenCV 4. These instructions only apply to Raspbian Buster, however.
  • 32GB microSD: I recommend the high-quality SanDisk 32GB 98Mb/s cards. Here’s an example on Amazon (however you can purchase them on your favorite online distributor).
  • microSD adapter: You’ll need to purchase a microSD to USB adapter so you can flash the memory card from your laptop.

If you don’t already have a Raspberry Pi 4, I highly recommend CanaKits (which are available on Amazon) and directly through Canakit’s website. Most of their kits come with a Raspberry Pi, power adapter, microSD, microSD adapter, heatsinks, and more!

Figure 1: Hardware for installing OpenCV 4 on your Raspberry Pi 4 running Raspbian Buster.

Once you have the hardware ready, you’ll need to flash a fresh copy of the Raspbian Buster operating system to the microSD card.

  1. Head on over to the official BusterOS download page (Figure 2), and start your download. I recommend the “Raspbian Buster with Desktop and recommended software”.
  2. Download Balena Etcher — software for flashing memory cards. It works on every major OS.
  3. Use Etcher to flash BusterOS to your memory card (Figure 3).

Figure 2: Download Raspbian Buster for your Raspberry Pi and OpenCV 4.

After downloading the Raspbian Buster .img file you can flash it to your micro-SD card using Etcher:

Figure 3: Flash Raspbian Buster with Etcher. We will use BusterOS to install OpenCV 4 on our Raspberry Pi 4.

After a few minutes the flashing process should be complete — slot the micro-SD card into your Raspberry Pi 4 and then boot.

From there you can move on to the rest of the OpenCV install steps in this guide.

Step #1: Expand filesystem and reclaim space

For the remainder of this tutorial I’ll be making the following assumptions:

  1. You are working with a brand new, fresh install of Raspbian Buster (see the previous section to learn how to flash Buster to your microSD).
  2. You are comfortable with the command line and Unix environments.
  3. You have an SSH or VNC connection established with your Pi. Alternatively, you could use a keyboard + mouse + screen.

Go ahead and insert your microSD into your Raspberry Pi and boot it up with a screen attached.

Once booted, configure your WiFi/ethernet settings to connect to the internet (you’ll need an internet connection to download and install required packages for OpenCV).

From there you can use SSH as I have done, or go ahead and open a terminal.

The first step is to run, raspi-config and expand your filesystem:

And then select the “7 Advanced Options” menu item:

Figure 4: The raspi-config configuration screen for Raspbian Buster. Select 7 Advanced Options so that we can expand our filesystem.

Followed by selecting “A1 Expand filesystem”:

Figure 5: The A1 Expand Filesystem menu item allows you to expand the filesystem on your microSD card containing the Raspberry Pi Buster operating system. Then we can proceed to install OpenCV 4.

Once prompted, you should select the first option, “A1 Expand File System”, hit enter  on your keyboard, arrow down to the “<Finish>” button, and then reboot your Pi — you may be prompted to reboot, but if you aren’t you can execute:

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 32GB of the micro-SD card.

However, even with my filesystem expanded, I have already used 15% of my 32GB card.

While it’s not required, I would suggest deleting both Wolfram Engine and LibreOffice to reclaim ~1GB of space on your Raspberry Pi:

Step #2: Install dependencies

The following commands will update and upgrade any existing packages, followed by installing dependencies, I/O libraries, and optimization packages for OpenCV:

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

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

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.:

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:

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 and prerequisites:

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

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

The following pre-requisites are for Step #4a and they certainly won’t hurt for Step #4b either. They are for HDF5 datasets and Qt GUIs:

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

If you’re working with a fresh install of the OS, it is possible that these versions of Python are already at the newest version (you’ll see a terminal message stating this).

Step #3: Create your Python virtual environment and install NumPy

We’ll be using Python virtual environments, a best practice when working with Python.

A Python virtual environment is an isolated development/testing/production environment on your system — it is fully sequestered from other environments. Best of all, you can manage the Python packages inside your your virtual environment inside with pip (Python’s package manager).

Of course, there are alternatives for managing virtual environments and packages (namely Anaconda/conda). I’ve used/tried them all, but have settled on pip, virtualenv, and virtualenvwrapper as the preferred tools that I install on all of my systems. If you use the same tools as me, you’ll receive the best support from me.

You can install pip using the following commands:

Let’s install  virtualenv  and virtualenvwrapper  now:

Once both virtualenv  and virtualenvwrapper  have been installed, open up your ~/.bashrc  file:

…and append the following lines to the bottom of the file:

Figure 6: Using the nano editor to update ~/.bashrc with virtualenvwrapper settings.

Save and exit via ctrl + x , y , enter .

From there, reload your ~/.bashrc  file to apply the changes to your current bash session:

Next, create your Python 3 virtual environment:

Here we are creating a Python virtual environment named cv  using Python 3. Going forward, I recommend Python 3 with OpenCV 4+.

Note: Python 2.7 will reach end of its life on January 1st, 2020 so I do not recommend using Python 2.7.

You can name the virtual environment whatever you want, but I use cv  as the standard naming convention here on PyImageSearch.

If you have a Raspberry Pi Camera Module attached to your RPi, you should install the PiCamera API now as well:

Step #4(a or b): Decide if you want the 1-minute quick install or the 2-hour complete install

From here you need to make a decision about the rest of your install. There are two options.

  1. Step #4a: pip install OpenCV 4: If you decide to pip install OpenCV, you will be done in a matter of seconds. It is by far the fastest, easiest method to install OpenCV. It is the method I recommend for 90% of people — especially beginners. After this step, you will skip to Step #5 to test your install.
  2. Step #4b: Compile OpenCV 4 from source: This method gives you the full install of OpenCV 4. It will take 2-4 hours depending on the processor in your Raspberry Pi.

As stated, I highly encourage you to use the pip instructions. They are faster and will work for 90% of your projects. Additionally, the patented algorithms can only be used for educational purposes (there are plenty of great alternatives to the patented algorithms too).

Step #4a: pip install OpenCV 4

In a matter of seconds, you can pip install OpenCV into the   cv virtual environment:

Figure 7: To quickly install OpenCV 4 on your Raspberry Pi 4 running Raspbian Buster, I recommend using pip as shown.

If you watch the terminal output of the above screenshot carefully you’ll see that OpenCV 3.4 rather than OpenCV 4 was installed?

What gives?

At the time of this writing, PiWheels has not been updated with pre-complied OpenCV 4 binaries for Raspbian Buster. PiWheels normally lags slightly behind the latest version of OpenCV, likely to ensure compatibility across the major Raspbian releases. Once OpenCV 4 has been released for PiWheels I will update this section.

That’s really all there is to it. You may skip to Step #5 now to test your install.

Step #4b: Compile OpenCV 4 from source

This option installs the full install of OpenCV including patented (“Non-free”) algorithms.

Note: Do not follow Step #4b if you followed Step #4a.

Let’s go ahead and download the OpenCV source code for both the opencv and opencv_contrib repositories, followed by unarchiving them:

For this blog post, we’ll be using OpenCV 4.1.1; however, as newer versions of OpenCV are released you can update the corresponding version numbers.

Increasing your SWAP space

Before you start the compile you must increase your SWAP space. Increasing the SWAP will enable you to compile OpenCV with all four cores of the Raspberry Pi (and without the compile hanging due to memory exhausting).

Go ahead and open up your /etc/dphys-swapfile  file:

…and then edit the CONF_SWAPSIZE  variable:

Notice that I’m increasing the swap from 100MB to 1024MB. This is critical to compiling OpenCV with multiple cores on Raspbian Buster.

Save and exit via ctrl + x , y , enter .

If you do not increase SWAP it’s very likely that your Pi will hang during the compile.

From there, restart the swap service:

Note: Increasing swap size is a great way to burn out your Raspberry Pi microSD card. Flash-based storage has a limited number of writes you can perform until the card is essentially unable to hold the 1’s and 0’s anymore. We’ll only be enabling large swap for a short period of time, so it’s not a big deal. Regardless, be sure to backup your  .img  file after installing OpenCV + Python just in case your card dies unexpectedly early. You can read more about large swap sizes corrupting memory cards on this page.

Compile and install OpenCV 4 on Raspbian Buster

We’re now ready to compile and install the full, optimized OpenCV library on the Raspberry Pi 4.

Ensure you are in the cv  virtual environment using the workon  command:

Then, go ahead and install NumPy (an OpenCV dependency) into the Python virtual environment:

And from there configure your build:

There are four CMake flags I’d like to bring to your attention:

  • (1) NEON and (2) VFPv3 optimization flags have been enabled. These lines ensure that you compile the fastest and most optimized OpenCV for the ARM processor on the Raspberry Pi (Lines 7 and 8).
    • Note: The Raspberry Pi Zero W hardware is not compatible with NEON or VFPv3. Be sure to remove Lines 7 and 8 if you are compiling for a Raspberry Pi Zero W.
  • (3) Patented “NonFree” algorithms give you the full install of OpenCV (Line 11).
  • And by drilling into OpenCV’s source, it was determined that we need the (4) -latomic  shared linker flag (Line 12).

I’d like to take a second now to bring awareness to a common pitfall for beginners:

  • In the terminal block above, you change directories into ~/opencv/ .
  • You then create a build/  directory therein and change directories into it.
  • If you try to execute CMake without being in the ~/opencv/build  directory, CMake will fail. Try running pwd  to see which working directory you are in before running cmake .

The cmake  command will take about 3-5 minutes to run as it prepares and configures OpenCV for the compile.

When CMake finishes, be sure to inspect the output of CMake under the Python 3 section:

Figure 8: CMake configures your OpenCV 4 compilation from source on your Raspberry Pi 4 running Buster.

Notice how the Interpreter , Libraries , numpy , and packages  path variables have been properly set. Each of these refers to our cv  virtual environment.

Now go ahead and scroll up to ensure that the “Non-Free algorithms” are set to be installed:

Figure 9: Installing OpenCV 4 with “Non-free algorithms” on Raspbian Buster.

As you can see, “Non-free algorithms” for OpenCV 4 will be compiled + installed.

Now that we’ve prepared for our OpenCV 4 compilation, it is time to launch the compile process using all four cores:

Figure 10: We used Make to compile OpenCV 4 on a Raspberry Pi 4 running Raspbian Buster.

Running make  could take anywhere from 1-4 hours depending on your Raspberry Pi hardware (this tutorial is compatible with the Raspberry Pi 3B, 3B+, and 4). The Raspberry Pi 4 is the fastest at the time of this writing.

Assuming OpenCV compiled without error (as in my screenshot above), you can install your optimized version of OpenCV on your Raspberry Pi:

Reset your SWAP

Don’t forget to go back to your /etc/dphys-swapfile  file and:

  1. Reset CONF_SWAPSIZE  to 100MB.
  2. Restart the swap service.

Sym-link your OpenCV 4 on the Raspberry Pi

Symbolic links are a way of pointing from one directory to a file or folder elsewhere on your system. For this sub-step, we will sym-link the cv2.so  bindings into your cv  virtual environment.

Let’s proceed to create our sym-link. Be sure to use “tab-completion” for all paths below (rather than copying these commands blindly):

Keep in mind that the exact paths may change and you should use “tab-completion”.

Step 5: Testing your OpenCV 4 Raspberry Pi BusterOS install

As a quick sanity check, access the cv  virtual environment, fire up a Python shell, and try to import the OpenCV library:

Congratulations! You’ve just installed an OpenCV 4 on your Raspberry Pi.

If you are looking for some fun projects to work on with OpenCV 4, be sure to checkout my Raspberry Pi archives.

What’s next?

Ready to put your Raspberry Pi and OpenCV install to work?

My brand new book, Raspberry Pi for Computer Vision, has over 40 Computer Vision and Deep Learning projects for embedded computer vision and Internet of Things (IoT) applications. You can build upon the projects in the book to solve problems around your home, business, and even for your clients. Each of these projects place an emphasis on:

  • Learning by doing
  • Rolling up your sleeves
  • Getting your hands dirty in code and implementation
  • Building actual, real-world projects using the Raspberry Pi

A handful of the highlighted projects include:

  • Daytime and nightime wildlife monitoring
  • Traffic counting and vehicle speed detection
  • Deep Learning classification, object detection, and instance segmentation on resource constrained devices
  • Hand gesture recognition
  • Basic robot navigation
  • Security applications
  • Classroom attendance
  • …and many more!

The book also covers deep learning using the Google Coral and Intel Movidius NCS coprocessors along with NVIDIA Jetson Nano board.

If you’re interested in studying Computer Vision and Deep Learning on embedded devices, you won’t find a better book than this one!

Pick up your copy of Raspberry Pi for Computer Vision today!

Summary

In today’s tutorial, you learned how to install OpenCV 4 on your Raspberry Pi 4 running the Raspbian Buster operating system via two methods:

  • A simple pip install (fast and easy)
  • Compiling from source (takes longer, but gives you the full OpenCV install/optimizations)

The pip method to install OpenCV 4 is by far the easiest way to install OpenCV (and the method I recommend for 90% of projects). It is especially great for beginners too.

If you need the full install of OpenCV, you must compile from source. Compiling from source ensures that you have the full install including the “contrib” module with patented (“NonFree”) algorithms.

While compiling from source is both (1) more complicated, and (2) more time-consuming, it is currently the only way to access all features of OpenCV.

I hope you enjoyed today’s tutorial!

And if you’re ready to put your RPi and OpenCV install to work, be sure to check out my book, Raspberry Pi for Computer Vision — inside the book you’ll learn how to build practical, real-world Computer Vision and Deep Learning applications on the Raspberry Pi, Google Coral, Movidius NCS, and NVIDIA Jetson Nano.

Be be notified when future tutorials are published on the PyImageSearch blog (and download my free 17-page CV and DL Resource Guide PDF), just enter your email address in the form below!

, , , , ,

70 Responses to Install OpenCV 4 on Raspberry Pi 4 and Raspbian Buster

  1. hamze60 September 16, 2019 at 10:58 am #

    I found direct compiling problematic with NCS2:
    https://github.com/opencv/opencv/issues/15446

  2. Flynn September 16, 2019 at 11:28 am #

    Thank you! Just started playing with opencv this weekend on an old model b, you have been an invaluable resource.
    One question though: how much faster would you say the new model 4 is compared to the b or b+?
    Trying to decide if it’s worth the upgrade or if I should switch to a laptop.
    I was following your dlib tutorials last night and when modified for a live camera feed I was getting 3-7fps.
    I can’t wait to get your book. Thanks again, this is really cool stuff.

    • Adrian Rosebrock September 16, 2019 at 12:21 pm #

      You mean how fast is the Pi 4 compared to the Pi 3? It depends on which RAM model you get (1GB, 2GB, or 4GB) model, but many of our tests have seen 1.5-3x speedup on the RPi 4 for most book chapters. Some chapters are even 5x or more faster on the RPi 4.

  3. Sriram Baradwaj September 16, 2019 at 11:49 am #

    i wanted this tutorial before a month ago.at that time i used your last year’s tutorial for installing opencv in raspberry pi 4.i see some major changes in this tutorial.how do you come to know these changes.is there any technical or logical reason to figure this out?

    • Adrian Rosebrock September 16, 2019 at 12:22 pm #

      We figure it out through a combination of:

      1. Experience
      2. Trial and error

      Over the years I’ve sort of developed a “sixth sense” as to how to debug any errors and resolve them. I then collect my tips, suggestions, and best practices and then publish them here on the blog.

  4. Daniel Krotov September 16, 2019 at 11:55 am #

    Thanks for this sir!! Can’t wait to see the chapters!

    • Adrian Rosebrock September 16, 2019 at 12:22 pm #

      Thanks Daniel 🙂

  5. Ken Walker September 16, 2019 at 1:47 pm #

    Thanks for the tutorial. I just attempted to follow using the pip method. Have tried two separate times and get an error on Step 5. Get the error msg below:


    ModuleNotFoundError: No module named ‘cv2’

    Any ideas?

    Thanks,

    Ken Walker

    • Adrian Rosebrock September 16, 2019 at 4:24 pm #

      The most likely reason for that is not being inside the Python virtual environment when trying to import cv2. Make sure you access the Python virtual environment first:

      • Nityananda Hazarika September 20, 2019 at 8:39 am #

        by following this commands, when I run cv2.__version__, it shows ‘3.4.3’. Why is it so? Thankyou.

        • Adrian Rosebrock September 25, 2019 at 10:40 am #

          See my note regarding installing via pip and why it will install 3.4.3 instead of OpenCV 4 until the PiWheels are updated.

  6. Jon Lucenius September 16, 2019 at 3:08 pm #

    Adrian,

    Thanks for producing this guide, top-notch as always. However, one small item, I went with the quick install, and in your animation and on my Pi4, I have OpenCV 3.4.3 installed, and not 4.1.1. This is probably the result of the piwheels download using “opencv_contrib_python-3.4.3.18” . Is there a way to force the 4.1.1 version to be used instead?

    Thanks!
    Jon

    • Adrian Rosebrock September 16, 2019 at 4:23 pm #

      Hey Jon, thanks for the comment.

      I meant to include a note in the blog post regarding OpenCV 4 and the pip install (but I forgot about it, thanks for the reminder) — PiWheels does not currently have a pre-compiled OpenCV 4 distribution. They will but it hasn’t been released yet. I included it in the post so that it will work once it becomes available but forgot to include that note. I’ll be sure to do that now 🙂

      • Jon Lucenius September 17, 2019 at 11:16 pm #

        Thanks Adrian – Just a quick follow-up to the install of OpenCV 4.1.1 on the Raspberry Pi 4b. 100% success on getting it installed. Took maybe 1.5 hours in all, thanks again for the help.

        • Adrian Rosebrock September 18, 2019 at 6:45 am #

          Congrats on getting OpenCV installed on your RPi!

  7. Martin Evans September 16, 2019 at 3:49 pm #

    Hi isn’t pip install opencv-contrib-python
    still installing version 3.4 and not 4.1

    • Adrian Rosebrock September 16, 2019 at 4:23 pm #

      Hey Martin — see my reply to Jon Lucenius.

  8. Philip Jarvis September 16, 2019 at 4:20 pm #

    After following your pip install method I end up with version 3.4.3 of cv2 – not version 4 ?

    • Adrian Rosebrock September 16, 2019 at 4:23 pm #

      See my reply to Jon Lucenius.

  9. Steven Griset September 16, 2019 at 4:30 pm #

    Adrian

    Most likely a dump questions (apologies for that), but not sure why you are linking the C++ atomic library (CMAKE_SHARED_LINKER_FLAGS=-latomic \), what in the opencv source encouraged you do that?

    Thanks

    Steve

    • David Hoffman September 19, 2019 at 10:41 am #

      Hi Steven. We found that -latomic is required for OpenCV 3.4.7. It doesn’t appear to hurt OpenCV 4.1.1 either, so we left it in there. That said, there is a related OpenCV issue which says “merged” at the bottom, so it may no longer be required. We’ll look into it.

      • Steven Griset September 20, 2019 at 10:33 am #

        David

        Thank you, particularly for that link to the Issue very helpful. I have compiled OpenCV 4.1.1 without that switch on both ARM32 and ARM64 boards all running buster and not seeing any errors. So I think you are right it might not be required. Thank you again for your help.

    • Martin Green September 20, 2019 at 3:27 pm #

      Hello Steven
      I have been following the post about issues with the raspi pi and Buster (mine is pi4). The post , as advised by David earlier, has some changes that have been added to the current master (and possibly 3.7) 15278/15353 and a small correction 15540 near the bottom of that post (merged in 3.7 but not yet in 4 master. With these the atomic flag references are now under the bonnet for my pi4 at least.
      Martin

  10. Supavit September 16, 2019 at 11:10 pm #

    If I don’t want to use virtual environment, which step should I skip (I am newbie in opencv). Thanks in advance.

    • Adrian Rosebrock September 17, 2019 at 5:29 am #

      If you want to skip the virtual environment I would recommend you just install OpenCV via pip and sudo:

      $ pip install opencv-contrib-python

      That will install OpenCV into your global site-packages directory.

      Secondly, if you are new to OpenCV, you should read Practical Python and OpenCV to learn the fundamentals quickly.

  11. Andre September 17, 2019 at 5:31 am #

    Adrian – Could you provide some insight regarding the performance improvement achieved between non-compiled vs compiled OpenCV?

    Thanks

  12. Curt Wuollet September 17, 2019 at 8:36 am #

    OK, I get it that the virtual environment thing is a best practice for development.
    What about deployment, where you would have to use shell scripts, etc., make sure you are in
    the right spot with the right environment. That’s after you ensured that you have the exact same
    VE set up on all targets. Is there an easier way? For an embedded application where you will
    only ever run one this one application, perhaps on multiple units, doesn’t that get a bit sticky?

    • Adrian Rosebrock September 19, 2019 at 10:00 am #

      Deployment is really up to you. I would suggest creating a .img file for your environment and then deploying that to every device, that way you have a consistent environment across all devices.

  13. Mitesh September 18, 2019 at 4:42 am #

    Dear Sir,
    I want to play a pi camera using raspberry pi 4 models, but the pi camera is not operating at that time. I have used commands like to display a video as vlc rtsp://192.168.0.100…at this time vlc is open but not live to respond.

    Also, another option to create a .sh file and add a raspivid commands with its width & height, but sill not responding.

    Also use : raspivid -o vid.h264..failed!!

    • Adrian Rosebrock September 18, 2019 at 6:45 am #

      RTSP streams with OpenCV can be a bit of a bear to deal with. I recommend using ImageZMQ instead.

  14. CK September 18, 2019 at 5:59 am #

    Hi Adrian,

    as I finished pip install on my own, I saw you post. Great post again!
    I purchased your course few months ago and one of the reason why I did it was the images you offered in the course!
    Would you consider building an image with BusterOS and OpenCV 4 precompiled for people like me, who would like to save time and nerve to compile on our own? We would be more than happy if we can purchase a prebuilt image from you!
    Thanks!

    • Adrian Rosebrock September 18, 2019 at 6:46 am #

      Hi CK — both Practical Python and OpenCV and Raspberry Pi for Computer Vision include a Raspbian .img file that has Buster OS and OpenCV pre-compiled/pre-installed. It sounds like that’s exactly what you need 🙂

      • CK September 18, 2019 at 7:35 am #

        Thanks for the quick reply!
        So you mean if I go to the purchase page again and download the zipped raspian image file I’ll get the latest image for my Pi 4? Or I’ve to buy the course again?

        • Adrian Rosebrock September 18, 2019 at 2:36 pm #

          Do you already own a copy of Practical Python and OpenCV or Raspberry Pi for Computer Vision? If so, just download the latest .img file and it will (1) work with your RPi 4 and (2) include OpenCV 4.

          If you don’t own a copy of either book, purchase a copy — you will have have access to the latest Raspbian .img file.

          • CK September 20, 2019 at 8:46 am #

            I purchased the Quickstart Bundle…So I am not sure if it counts as “own” a copy…
            I downloaded the “Raspbian.zip” again yesterday and it seems like the image is not updated.

          • Adrian Rosebrock September 20, 2019 at 12:32 pm #

            Hey CK — if you purchased a copy of the Quickstart Bundle then you certainly own a copy. Can you send me an email (or my contact form) so we can chat more about it?

          • Mark September 25, 2019 at 1:34 pm #

            Do you have a raspbian buster image that supports pi zero w? I assume with the NEON and VFPv3 flags set to true it will cause errors?

            Attempted to compile with flags set to false using Pi 3B+ but failed at ~100% and it now refuses to boot.

          • Adrian Rosebrock October 3, 2019 at 12:48 pm #

            I do! It can be found inside Practical Python and OpenCV as well as Raspberry Pi for Computer Vision.

  15. doni mart September 18, 2019 at 11:12 am #

    Thank you it works installing openCV 4 on rasp pi 4. my question. if using rasp pi camera via CSI port does the main processor(bcm2711) alone will handle the computation? is this the same with smartphone camera system? same question with screen of rasp pi via DSI port. thank you so much from indonesia

    • Adrian Rosebrock September 19, 2019 at 9:56 am #

      If you’re using the CSI port the graphics chip on the card handles grabbing/processing the frames. That’s one of the benefits of using the CSI camera vs. a USB camera (in which the CPU would be used).

  16. khan September 19, 2019 at 6:40 am #

    hey Adrian Rosebrock, hope you will be fine, we are trying to classifying cat_vs_dog on raspberry pi connected NCS2, using our own trained keras model . please make a tutorial on how to run our own trained model on NCS2.

  17. Niles September 19, 2019 at 9:48 am #

    Thank you for updating all of this information to Buster, and more importantly for me, for giving the hints about installing on a Raspberry Pi Zero. The changes to installing NEON and VFPv3 made for a successful compile of OpenCV 4. The make step took about 16 hours, but it got there!

    • Adrian Rosebrock September 19, 2019 at 9:52 am #

      Congrats on getting OpenCV installed on your Pi, Niles!

  18. Sriram Baradwaj September 19, 2019 at 11:46 am #

    i tried running a face recognition program in startup after installing opencv using the above mentioned steps.i get ‘ImportError: no module named imutils.video’.(P.S. i have already installed imutils package using pip install)

    • Adrian Rosebrock September 25, 2019 at 10:39 am #

      It sounds like “imutils” was not installed properly. Try again:

      $ pip install --upgrade imutils

      Also make sure that you do not have a file named “imutils” in your working project directory.

  19. Zorawar September 22, 2019 at 2:27 am #

    Hi Adrian!

    I tried installing OpenCv 4 on Raspian Buster before this post came out. I successfully installed OpenCv compiling from Source but here’s the catch.

    The Python 3 IDLE isn’t pre-installed for Buster therefore I have to spearately install that. So whenever I install the IDLE

    Run this command →

    source ~/.profile
    workon cv
    idle

    I am not able to ge the opencv in the idle however it works in the terminal shell

    source ~/.profile
    workon cv
    python
    cv2.__version__
    »version 4.0.2

    Does this method solve the IDLE problem? Also do you recommend using Anaconda on Pi?

    • Adrian Rosebrock September 25, 2019 at 10:38 am #

      Python’s IDLE does not respect Python virtual environments. You should either:

      1. Use the Python shell
      2. Execute your scripts via the command line or use
      3. Use Jupyter Notebooks if you want an IDLE-like environment

  20. Isaac September 23, 2019 at 11:55 am #

    I finished everything as stated. It works like a charm. But when it came to the last step, i got 3.4.3 instead of 4.1.1 when checking cv2 version. That’s normal right?

    Or am I missing something out.

    (Completely beginner, sorry)

    • Adrian Rosebrock September 25, 2019 at 10:36 am #

      Which method did you use? Compile from source? Or did you install via pip? If you installed via pip see my note regarding OpenCV 3.4 vs. OpenCV 4.

  21. Steve Gale September 24, 2019 at 4:13 am #

    Another great blog, just installed it on a PI zero for a simple burglar alarm, based on one of your earlier blogs of course!

    Tried to be clever and put all of the install commands in a bash script which meant I missed some errors downloading some files, mainly to do with the gui, but got there in the end.

    • Adrian Rosebrock September 25, 2019 at 8:54 am #

      Congrats on getting OpenCV installed on your Pi Zero, Steve!

  22. Dave September 24, 2019 at 3:34 pm #

    Many thanks Adrian.

    The pip install on a Raspberry Pi 3 Model B gave me ERROR: Could not find a version that satisfies the requirement opencv-contrib-python (from versions: none).

    However, compiling from source worked fine and only took a couple of hours.

    • Adrian Rosebrock September 25, 2019 at 8:54 am #

      Congrats on getting OpenCV installed on your RPi!

  23. Nick October 1, 2019 at 9:15 pm #

    Hi Adrian,
    Thanks so much for putting this together. I got through all the steps, but I kept getting stuck at the CMAKE command. It kept having trouble finding the python3 libraries. I’m not totally sure why. I found the solution was to just delete everything in the build folder and then run the following command:
    sudo apt-get install libatlas-base-dev
    Hopefully this save someone else’s time.

    • Adrian Rosebrock October 3, 2019 at 12:22 pm #

      Thanks for sharing, Nick!

  24. Abuthahir October 2, 2019 at 11:33 am #

    Will the same method work for aarch64 arm devices?

  25. Ali October 7, 2019 at 7:14 pm #

    Thanks! been trying for a while and finally got OpenCV to compile after going through your tutorial.

    • Adrian Rosebrock October 10, 2019 at 10:19 am #

      Congrats on getting OpenCV installed on your RPi, Ali!

  26. Wim Nelis October 10, 2019 at 9:29 am #

    Hello Adrian,

    using your step-by-step guide OpenCV 4 was compiled and installed on a RPi3B. It was not difficult, but very time-consuming. OpenCV needs to be installed on 3 other RPi3B’s as well. Is there an overview of files and configuration settings which can be copied from one RPi3B to another in order to install OpenCV 4? That might be much faster than compiling it on each RPi individually. Moreover, such an overview can (will) be used to create an ansible-script (playbook) to automate installation (and update) of the set of RPi’s.

    • Adrian Rosebrock October 10, 2019 at 10:06 am #

      Hey Wim — have you taken a look at Practical Python and OpenCV and Raspberry Pi for Computer Vision? Those books include a pre-configured Raspbian .img file that you can use and shortcut the install process.

      • Wim Nelis October 10, 2019 at 4:28 pm #

        Hello Adrian — thanks for your quick reply. I take it that your answer implies “no” to my question. In this case maintenance of the RPi’s is based on ansible, allowing for updates with preservation of configuration and data files of other installed packages. Updating OpenCV by starting with a new image implies saving and restoring those configuration and data files.

  27. Acapo October 10, 2019 at 4:53 pm #

    Hello Adrian thanks for the tutorial. The installation of make is stuck at build target opencv_perf_stereo for almost 10 hours now on my second attempt. the day before it got stuck at the build target opencv_perf_stereo also for 6 hours before I lose my patience (haha) and decided to flash the OS again. Is this normal, should i just wait?

    I’m using Raspberry Pi 3B+ with Raspbian Buster.

    • Adrian Rosebrock October 17, 2019 at 7:07 am #

      Try compiling with a single core (versus all four). It will take longer but should avoid any race conditions with the compile. Additionally you should try increasing your swap size.

  28. Manuel October 11, 2019 at 4:26 am #

    Hi,

    Thanks for such a complete and comprehensive tutorial! I am trying to instal openCV in my raspberry pi 3 b+, and then I am in the cmake part, when I do the check that you make in Figure 9, for the non-free programs I have ano (instead of a Yes). I did copy-paste all the lines and all the rest looks as in your tutorial, what could be the cause?

    Thank you in advance

    • Adrian Rosebrock October 17, 2019 at 7:06 am #

      Hey Manuel — it’s hard to say what the exact issue is without seeing the full “cmake” log. If you’re having trouble installing OpenCV on your RPi I would recommend picking up a copy of Practical Python and OpenCV or Raspberry Pi for Computer Vision. Both of those books include a Raspbian .img file with OpenCV pre-installed. All you need to do is download the .img file, flash it to your RPi, and boot.

  29. DEV October 15, 2019 at 4:03 am #

    Hi,

    I have a raspberry pi3 and have been following your tutorial to install OpenCV4 with Raspbian Buster. when I compile, all seems to work well until I get to this line

    “[100%] Built target opencv_perf_stereo”

    It then just hangs. We at this point for 5hrs now.

    Suggestions?

    • Adrian Rosebrock October 17, 2019 at 7:05 am #

      Try compiling with just a single core rather than all four cores. Additionally you may want to increase your swap size.

Before you leave a comment...

Hey, Adrian here, author of the PyImageSearch blog. I'd love to hear from you, but before you submit a comment, please follow these guidelines:

  1. If you have a question, read the comments first. You should also search this page (i.e., ctrl + f) for keywords related to your question. It's likely that I have already addressed your question in the comments.
  2. If you are copying and pasting code/terminal output, please don't. Reviewing another programmers’ code is a very time consuming and tedious task, and due to the volume of emails and contact requests I receive, I simply cannot do it.
  3. Be respectful of the space. I put a lot of my own personal time into creating these free weekly tutorials. On average, each tutorial takes me 15-20 hours to put together. I love offering these guides to you and I take pride in the content I create. Therefore, I will not approve comments that include large code blocks/terminal output as it destroys the formatting of the page. Kindly be respectful of this space.
  4. Be patient. I receive 200+ comments and emails per day. Due to spam, and my desire to personally answer as many questions as I can, I hand moderate all new comments (typically once per week). I try to answer as many questions as I can, but I'm only one person. Please don't be offended if I cannot get to your question
  5. Do you need priority support? Consider purchasing one of my books and courses. I place customer questions and emails in a separate, special priority queue and answer them first. If you are a customer of mine you will receive a guaranteed response from me. If there's any time left over, I focus on the community at large and attempt to answer as many of those questions as I possibly can.

Thank you for keeping these guidelines in mind before submitting your comment.

Leave a Reply

[email]
[email]