Install OpenCV 4 on your Raspberry Pi

Today I’m going to show you how to compile and install OpenCV 4 on your Raspberry Pi.

OpenCV 4 was officially released on November 20th, 2018.

This blog post was updated subsequently on November 28th to accommodate the changes to the install (previously these instructions linked to the alpha release source code).

It is also possible to pip install OpenCV as well! As of this update however, PyPi does not contain precompiled OpenCV 4 binaries which can be installed via pip.

Therefore, if you want OpenCV 4 then you’ll need to compile from source.

To learn how to install OpenCV 4 on your Raspberry Pi, just follow this tutorial!

Looking for the source code to this post?
Jump right to the downloads section.

Install OpenCV 4 on your Raspberry Pi

In this blog post, we’re going to install OpenCV 4 on your Raspberry Pi. OpenCV 4 has a goal of reducing bloat, adding optimizations, and updating the deep learning module.

Note: There are many install guides on my blog. Before you begin, be sure to check out the available install tutorials on my OpenCV installation guides page.

First we’ll cover assumptions that go along with this tutorial.

From there, we’ll walk through 7 steps to compile and install OpenCV 4 on your Raspberry Pi. Compiling from source allows us to have full control over the compile and build. It also allows us to grab the latest code — something that pip and apt-get don’t offer.

Finally, we’ll test our OpenCV 4 install on our Raspberry Pi with a fun project.

Let’s get started.


In this tutorial, I am going to assume that you already own a Raspberry Pi 3 B or the newer Raspberry Pi 3 B+ with Raspbian Stretch installed.

If you don’t already have the Raspbian Stretch OS, you’ll need to upgrade your OS to take advantage of Raspbian Stretch’s new features.

To upgrade your Raspberry Pi 3 to Raspbian Stretch, you may download it here and follow these upgrade instructions (or these for the NOOBS route which is recommended for beginners). The former instructions take approximately 10 minutes to download via a torrent client and about 10 minutes to flash the SD card with Etcher or another tool. At that point you can power up and proceed to the next section.

Assuming that your OS is up to date, you’ll need one of the following for the remainder of this post:

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

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.

Can’t SSH? If you see your Pi on your network, but can’t ssh to it, you may need to enable SSH. This can easily be done via the Raspberry Pi desktop preferences menu (you’ll need an HDMI cable and a keyboard/mouse) or running sudo service ssh start  from the command line of your Pi.

After you’ve changed the setting and rebooted, you can test SSH directly on the Pi with the localhost address. Open a terminal and type ssh pi@  to see if it is working. To SSH from another computer you’ll need the Pi’s IP address — you could figure it out by looking at your router’s clients page or by running ifconfig  on the Pi itself.

Keyboard layout giving you problems? Change your keyboard layout by going to the Raspberry Pi desktop preferences menu. I use the standard US Keyboard layout, but you’ll want to select the one appropriate for you.

Step #1: Expand filesystem on your Raspberry Pi

To get the OpenCV 4 party started, fire up your Raspberry Pi and open an SSH connection (alternatively use the Raspbian desktop with a keyboard + mouse and launch a terminal).

Are you using a brand new install of Raspbian Stretch?

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

And then select the “Advanced Options” menu item:

Figure 1: Selecting the “Advanced Options” from the raspi-config menu to expand the Raspbian file system on your Raspberry Pi is important before installing OpenCV 4. Next we’ll actually expand the filesystem.

Followed by selecting “Expand filesystem”:

Figure 2: The Raspberry Pi “Expand Filesystem” menu allows us to take advantage of our entire flash memory card. This will give us space necessary to install OpenCV 4 and other packages.

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.

If you are using an 8GB card you may be using close to 50% of the available space, so one simple thing to do is to delete both LibreOffice and Wolfram engine to free up some space on your Pi:

After removing the Wolfram Engine and LibreOffice, you can reclaim almost 1GB!

Step #2: Install OpenCV 4 dependencies on your Raspberry Pi

From there, let’s update our system:

And then let’s install developer tools including CMake:

Next, let’s install a selection of image and video libraries — these are critical to being able to work with image and video files:

From there, let’s install GTK, our GUI backend:

And now let’s install a package which may reduce pesky GTK warnings:

The asterisk will grab the ARM specific GTK.

Followed by installing two packages which contain numerical optimizations for OpenCV:

And finally, let’s install the Python 3 development headers:

Once you have all of these prerequisites installed you can move on to the next step.

Step #3: Download OpenCV 4 for your Raspberry Pi

Our next step is to download OpenCV.

Let’s navigate to our home folder and download both opencv and opencv_contrib. The contrib repo contains extra modules and functions which we frequently use here on the PyImageSearch blog. You should be installing the OpenCV library with the additional contrib modules as well.

When you’re ready, just follow along to download both the opencv  and opencv_contrib  code:

From there, let’s unzip the archives:

I also like to rename the directories:

If you skip renaming the directories, don’t forget to update the CMake paths.

Now that opencv  and opencv_contrib  are downloaded and ready to go, let’s set up our environment.

Step #4: Configure your Python 3 virtual environment for OpenCV 4

Let’s grab and install pip, a Python Package Manager.

To install pip, simply enter the following in your terminal:

Make use of virtual environments for Python development

If you aren’t familiar with virtual environments, please take a moment look at this article on RealPython or read the first half of the this blog post on PyImageSearch.

Virtual environments will allow you to run different versions of Python software in isolation on your system. Today we’ll be setting up just one environment, but you could easily have an environment for each project.

Let’s go ahead and install   virtualenv  and virtualenvwrapper  now — they allow for Python virtual environments:

To finish the install of these tools, we need to update our  ~/.profile  file (similar to .bashrc  or .bash_profile ).

Using a terminal text editor such as vi / vim  or nano , add the following lines to your ~/.profile :

Alternatively, you can append the lines directly via bash commands:

Next, source the ~/.profile  file:

Create a virtual environment to hold OpenCV 4 and additional packages

Now you’re at the point where you can create your OpenCV 4 + Python 3 virtual environment on your Raspberry Pi:

This line simply creates a Python 3 virtual environment named cv .

You can (and should) name your environment(s) whatever you’d like — I like to keep them short and sweet while also providing enough information so I’ll remember what they are for. For example, I like to name my environments like this:

  • py3cv4
  • py3cv3
  • py2cv2
  • etc.

Let’s verify that we’re in the cv  environment by using the workon  command:

Figure 3: The workon command is part of the virtualenvwrapper package and allows us to easily activate virtual environments. Here I’m activating the cv environment which we’ll install OpenCV 4 into on our Raspberry Pi.

Install NumPy

The first Python package and only OpenCV prerequisite we’ll install is NumPy:

We can now prepare OpenCV 4 for compilation on our Raspberry Pi.

Step #5: CMake and compile OpenCV 4 for your Raspberry Pi

For this step, we’re going to setup our compile with CMake followed by running make  to actually compile OpenCV. This is the most time-consuming step of today’s blog post.

Navigate back to your OpenCV repo and create + enter a build  directory:

Run CMake for OpenCV 4

Now let’s run CMake to configure the OpenCV 4 build:

Update 2018-11-27: Notice the -D OPENCV_ENABLE_NONFREE=ON  flag. Setting this flag with OpenCV 4 ensures that you’ll have access to SIFT/SURF and other patented algorithms.

Be sure to update the above command to use the correct OPENCV_EXTRA_MODULES_PATH  path. If you’re following along with this tutorial exactly you should not need to update the path.

Once CMake is finished, it’s important that you inspect the output. Your output should look similar to mine below:

Figure 4: Ensure that “Non-free algorithms” is set to “YES”. This will allow you to use patented algorithms such as SIFT/SURF for educational purposes.

Figure 5: The CMake command allows us to generate build files for compiling OpenCV 4 on the Raspberry Pi. Since we’re using virtual environments, you should inspect the output to make sure that the compile will use the proper interpreter and NumPy.

Take a second now to ensure that the Interpreter  points to the correct Python 3 binary. Also check that numpy  points to our NumPy package which is installed inside the virtual environment.

Increase the SWAP on the Raspberry Pi

Before you begin the compile I would suggest increasing your swap space. This will enable you to compile OpenCV with all four cores of the Raspberry Pi without the compile hanging due to memory exhausting.

Open up your /etc/dphys-swapfile  file:

…and then edit the CONF_SWAPSIZE  variable:

Notice that I’m increasing the swap from 100MB to 2048MB.

If you do not perform this step it’s very likely that your Pi will hang.

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 have 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 OpenCV 4

Now we’re ready to compile OpenCV 4:

Note: In the make  command above, the -j4  argument specifies that I have 4 cores for compilation. If you have compile errors or your Raspberry Pi hangs/freezes you can try without the -j4  switch which can eliminate race conditions.

Here you can see OpenCV 4 has compiled without any errors:

Figure 6: I’ve compiled OpenCV 4 on my Raspberry Pi successfully (the make command has reached 100%). So now I’ll issue the sudo make install command.

And from there, let’s install OpenCV 4 with two additional commands:

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

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

Step #6: Link OpenCV 4 into your Python 3 virtual environment

Let’s create a symbolic link from the OpenCV install in the system site-packages  directory to our virtual environment:

I cannot stress this step enough — this step is critical. If you don’t create a symbolic link, you won’t be able to import OpenCV in your scripts. Also, ensure that the paths and filenames in the above commands are correct for your Raspberry Pi.  I suggest tab-completion.

Step #7: Test your OpenCV 4 install on your Raspberry Pi

Let’s do a quick sanity test to see if OpenCV 4 is ready to go.

Open a terminal and perform the following:

The first command activates our virtual environment. Then we run the Python interpreter associated with the environment.

If you see that you have version 4.0.0 installed, then you’re now fully armed and dangerous + ready to perform computer vision and image processing.

A Raspberry Pi + OpenCV 4 project to get your feet wet

A while back, I was working hard on responding to PyImageSearch readers via comments, emails, and Twitter/Facebook/LinkedIn. I make a point of responding to as many incoming questions and comments as I can.

It was 4:30 in the afternoon and I was cranking away at the keyboard in a “flow” state.

But while I was typing away, something triggered in my brain that I was thirsty. Very thirsty.

So I took a quick break from the keyboard and went to the fridge to grab a tasty beer ?.


All my beers were gone!

Who stole my beer?!

I grabbed some water instead and went back to the computer. I closed all correspondence windows and fired up a code editor/IDE (I like PyCharm).

I started tapping away at the keyboard again and sipping some water.

What was I building?

I was building a security cam with my Raspberry Pi which will catch people leaving/entering my apartment and opening/closing my fridge. I’ll catch that SOB stealing my beer next time!

Figure 7: Examples of the Raspberry Pi home surveillance system detecting motion in video frames and uploading them to my personal Dropbox account.

If you want to learn how to build a security camera with your Raspberry Pi and OpenCV 4, then I suggest you read the original blog post.

This project is relatively simple and will accomplish the following:

  • Detects motion via background subtraction.
  • Uploads images of intruders and other motion to Dropbox so you can review events later. All images are timestamped so you’ll know when someone was in view of your Raspberry Pi security camera.

Or if you’re savvy and want to grab the code right now, you can be up and running in a few moments after you paste your Dropbox API key in the config file.

To download this project, scroll to the “Downloads” section of this blog post and fill out the form.

You can download the zip to your ~/Downloads  folder and then fire up a terminal:

Once your SSH connection is established let’s install a couple packages, namely the Dropbox API:

From there, unzip the files and change working directory:

You’ll be presented with a directory structure that looks like this:

Before you’ll be able to deploy the project, you need to edit the config file, conf.json . Let’s quickly inspect it in our terminal using the nano  text editor (or vim / emacs  if you prefer):

You’ll be presented with a JSON dictionary that looks like this:

Figure 8: The Raspberry Pi security camera configuration file. An API key must be pasted in, and a base path must be configured. For headless mode, “show_video” should be set to “false”.

At this point it is important to edit the configuration file with your API key and Path. To find your API key, you can create an app on the app creation page. Once you have an app created, the API key may be generated under the OAuth section of the app’s page on the App Console (simply click the “Generate” button and copy/paste the key into the configuration file). The Path needs to be a valid path in your Dropbox file structure.

Note: Don’t share your API key with anyone unless you trust them!

For testing, you can leave "show_video": true  and connect an HDMI screen + keyboard/mouse to your Pi. Eventually you’ll want to set the value to false  and have your Pi run headless with few cables connected to it so you can hide it in the inconspicuously.

Once you get your project working, you can monitor the Dropbox folder in your web browser (you might need to refresh to check for images), or if you are syncing the folder to your laptop/desktop, you can monitor the folder there.

highly recommend that you read the entire blog post as well.

Happy hacking with your Raspberry Pi and OpenCV 4!

Troubleshooting and Frequently Asked Questions (FAQ)

Did you encounter an error installing OpenCV 4 on your Raspberry Pi?

Don’t throw the little gadget across the room yet. The first time you install OpenCV on your Raspberry Pi, it can be very frustrating and the last thing I want for you to do is to end the learning process here.

If you really get stuck, don’t forget that the QuickStart Bundle of Practical Python and OpenCV + Case Studies comes with Raspberry Pi images which are pre-configured and ready to go. Images for RPi 3B/3B+ and RPi Zero W are included. These images can save you hours and days (yes it took me about 6 days to set up the RPi Zero W) of frustration.

If you have your heart set on figuring this out on your own, I’ve put together a short list of frequently asked questions (FAQs) and I suggest that you familiarize yourself with them.

Q. How do I flash an operating system on to my Raspberry Pi memory card?

A. I recommend that you:

  • Grab a 16GB or 32GB memory card.
  • Flash Raspbian Stretch with Etcher to the card. Etcher is supported by all 3 major OSes.
  • Insert the card into your Raspberry Pi and begin with “Assumptions” and “Step 1” in this blog post.

Q. Can I use Python 2.7?

A. I don’t recommend using Python 2.7 as it’s rapidly approaching its end of life. Python 3 is the standard now. But if you insist…

Here’s how to get up and running with Python 2.7:

Then, before you create your virtual environment in Step #4, first install pip for Python 2.7:

Also in Step #4: when you create your virtual environment, simply use the relevant Python version flag:

From there everything should be the same.

Q. Can I just pip to install OpenCV 4?

A. In the future, yes. Currently you’ll need to compile from source until piwheels has an OpenCV 4 binary available.

Q. Why can’t I just apt-get install OpenCV?

A. Avoid this “solution” at all costs even though it might work. First, this method likely won’t install OpenCV 4 for a while. Secondly, apt-get doesn’t play nice with virtual environments and you won’t have control over your compile and build.

Q. The   mkvirtualenv  and  workon commands yield a “command not found error”. I’m not sure what to do next.

A. There a number of reasons why you would be seeing this error message, all of come from to Step #4:

  1. First, ensure you have installed virtualenv  and virtualenvwrapper  properly using the pip  package manager. Verify by running pip freeze , and ensure that you see both virtualenv  and virtualenvwrapper  in the list of installed packages.
  2. Your ~/.profile  file may have mistakes. View the contents of your ~/.profile  file to see the proper export  and source  commands are present (check Step #4 for the commands that should be appended to ~/.profile ).
  3. You might have forgotten to source  your ~/.profile . Make sure you run  source ~/.profile  after editing it to ensure you have access to the mkvirtualenv  and workon  commands.

Q. When I open a new terminal, logout, or reboot my Raspberry Pi, I cannot execute the mkvirtualenv  or workon  commands.

A. If you’re on the Raspbian desktop, this will likely occur. The default profile that is loaded when you launch a terminal, for some reason, doesn’t source the ~/.profile  file. Please refer to #2 from the previous question. Over SSH, you probably won’t run into this.

Q. When I try to import OpenCV, I encounter this message:  Import Error: No module named cv2 .

A. There are several reasons this could be happening and unfortunately, it is hard to diagnose. I recommend the following suggestions to help diagnose and resolve the error:

  1. Ensure your  cv  virtual environment is active by using the workon cv  command. If this command gives you an error, then verify that virtualenv  and virtualenvwrapper  are properly installed.
  2. Try investigating the contents of the site-packages  directory in your cv  virtual environment. You can find the site-packages  directory in ~/.virtualenvs/cv/lib/python3.5/site-packages/  depending on your Python version. Make sure (1) there is a cv2  sym-link directory in the  site-packages  directory and (2) it’s properly sym-linked.
  3. Be sure to check the site-packages  (and even dist-packages ) directory for the system install of Python located in /usr/local/python/ , respectively. Ideally, you should have a cv2  directory there.
  4. As a last resort, check in your build/lib  directory of your OpenCV build. There should be a cv2  directory there (if both cmake  and make  executed without error). If the cv2  directory is present, manually copy it into /usr/local/python  as well as the site-packages  directory for the cv  virtual environment.

Q: Why do I encounter a message about “Non-free modules” not being installed? How can I get the OpenCV non-free modules?

A: New in OpenCV 4, a special CMake flag must be set to obtain the non-free modules.  Refer to Step #5 above and pay attention to the flag in the CMake command.

Q. What if my question isn’t listed here?

A. Please leave a comment below or send me an email. If you post a comment below, just be aware that code doesn’t format well in the comment form and I may have to respond to you via email instead.


Today we installed OpenCV 4 on our Raspberry Pi.

Compiling from source was paramount so that we could obtain OpenCV 4 since it isn’t possible yet to install OpenCV 4 via pip.

We then tested our install and deployed a Raspberry Pi as a security camera. This security camera will detect motion via background subtraction and upload pictures of intruders to Dropbox. You can read the full Raspberry Pi + home surveillance post here.

To stay up to date with PyImageSearch, be sure to drop your email in the form below!


If you would like to download the code and images used in this post, please enter your email address in the form below. Not only will you get a .zip of the code, I’ll also send you a FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL! Sound good? If so, enter your email address and I’ll send you the code immediately!

, , , ,

108 Responses to Install OpenCV 4 on your Raspberry Pi

  1. Sudeep September 26, 2018 at 11:09 am #

    Hey Adrian, How come we dis not need to change the Swap size from the default 100 to the 1024 like we did in the other OpenCV installs on the Rpi

    • David Hoffman September 26, 2018 at 12:49 pm #

      Hi Sudeep, good catch. It is very important to change the SWAP to 1024 before starting the compile. I made this change for Adrian while he is on vacation.

  2. marco September 26, 2018 at 12:47 pm #

    Hello Adrian, I followed step by step your beautiful post and everything went smoothly up to the Cmake -D where in the end I received an error, thinking it was not very important I continued the installation but when I typed Make -j4 not the He found … where did I go wrong?

    (cv) pi@raspberrypi: ~/opencv-4.0.0-alpha/build $ make -j4
    make: *** No specified target and no makefiles found. Stop."

    • David Hoffman September 26, 2018 at 1:10 pm #

      Hi Marco, this is because you did not rename your directories as specified in Step #3. It isn’t your fault — due to OpenCV 4 Alpha releasing on September 20th while Adrian was on vacation, and considering Adrian wrote this post about 3 weeks ago, this post needed a last minute update (from cloning the Git repo to downloading the zip archives). I updated the post to show the need for renaming the directories. Simply rename your opencv-4.0.0-alpha directory to opencv and opencv_contrib-4.0.0-alpha to opencv_contrib. Then delete the build directory and re-run CMake and continue to follow the instructions. Sorry for the mixup and thanks for bringing it to our attention!

      • Filippo Pesavento December 1, 2018 at 8:24 am #

        Hi 🙂 I got the same error, though I renamed correctly all folders. Seems to be an error from the previous step, using cmake.
        Cmake does not say “Configuring done” and “Generating done”, but it throws an error log file and I cannot figure out why. Any suggestion?
        PS. I followed the tutorial step by step, even if I am very familiar with Linux and bash. I’m running the latest version of Raspbian Lite, if that can help.

  3. Linus Goh September 26, 2018 at 3:25 pm #

    Hi Adrian, David,

    I’m an active member of the Raspberry Pi community so I think I can say a word or two about that 🙂

    1. Step #1 should NOT be necessary, as Raspbian will do this step *automatically* on the first boot and reboot afterwards, which is since a long time BTW. Even though it might not yield any errors doing it a second time, I see no improvement in it, whatsoever.

    2. Even though recommended by the foundation itself, in the German Raspberry Pi forum we have commonly reports of strange issues caused by NOOBS installs of Raspbian which DO NOT OCCUR when doing a direct flashing of Raspbian onto the SD card, with the same Pi, PSU etc.
    It’s not entirely clear what causes this, but IMO NOOBS is to be recommended with caution, especially to completely new users. They should be able to use Raspbian installed directly as it’s the next step after NOOBS for most people anyway.


    • Abhishek Thanki September 27, 2018 at 10:17 am #

      Hi Linus,

      Interesting on Raspbian automatically expanding the filesystem. I recently did an install and had to do it manually.

      Also, I think the only reason why Adrian recommends NOOBS for beginners is since it’s recommended by the foundation itself. But thanks for your input on it!

      • Alexander Holsgrove September 28, 2018 at 1:06 pm #

        I think if you’re interested in learning about OpenCV and other articles by Adrian, then you’re probably more likely to be comfortable working on Raspbian rather than NOOBS!

  4. Johan Smit September 27, 2018 at 12:46 am #

    I have done embedded work on Arm processors (STM32F1 and F4) in C without operating system, but no Raspberry Pi.
    Please help me with information.
    I would like to use the unit as a type of trail camera, with animal recognition and discarding some photos but saving others according to type of animal.
    This means that the processor must mostly sleep, and be woken up by passive infrared detector.
    The question is: Will the Raspbian operating system be able to handle this wakeup to take photos in a short time, eg., milliseconds?
    I have seen Raspberry Pis boot up, and that is totally useless, minutes rather than milliseconds.
    Any information will be greatly appreciated, thank you
    Johan Smit

    • Denis Brion September 28, 2018 at 5:55 am #

      w/r to RPi starting in minutes : consider a RPi as … a PC (libreoffice, wolfram are x86 ports to ARMs) and notice it boots about 10 100 times faster than Windows (part of Windows booting is hidden : there is a desktop for marketing “reasons”, but difficult to use).
      There is a thing RPi is very bad: it is what PC have, named ACPI (ask wikipedia). RPi has no -or very lousy and buggy- sleep options. Do not worry : nanoPi (chinese “clones”, with better HW design, octo cores – are worse in this domain,… I am af asking a raid you asking a feature where RPis are at their worst.

  5. Denis Brion September 27, 2018 at 3:51 am #

    Three things worry me :

    a) make -j’ is unnecessary; make (without j) will compile 4 times slower… and keep RPi somewhat cool (BTW, at least in hot countries… or in summer, an heatsink+ fan are useful/necessary when compiling for hours)

    b) one can remove the tiny swap partition and add an external swap file on a USB disk.
    Drawbacks :it is slightly slower; one needs to compile linux-utils (IIRC) for a classical manipulation)
    Advantages : repeated writes on a flash device ruins it…. and swap is made to allow repeated writes. No need to remove libreoffice (suppose one needs it) or wolfram. One can get rid of the 100M nanoflash (eats a little disk swap).
    AFAIK (from reading last year dphys git) , dphys is a warpper around sawpon and swapoff… (parts of linux-utils: I prefer using century old commands to 1) make a swap file (not partition); 2) desctivate the 100 M builtin partition; 3) actvate the new swap file)

    c) alpha versions seem … very new. What is the advantage over a ocv3-x version (btw: pip installing a 3.x version worked very fast and comfortably, as you showed it last week)

    • Abhishek Thanki September 27, 2018 at 10:08 am #

      Hi Denis,

      a) I think using make -jx is a personal choice and I don’t really see any issues with it, most people would go for speed on any given day. (Also, having lived in a country where temperatures go upto 50C in summer, we use A/Cs, and it’s pretty cool inside. 🙂 )

      b) Interesting and theoretically speaking it makes sense. Definitely something worth a try.

      c) Advantages: optimizations, C++11 support, more compact modules, and many improvements to the Deep Neural Network (DNN) module.

      • Denis Brion September 29, 2018 at 8:56 am #

        Hi Abishek:
        heat issues are not a worry for computers between Syria and North India: as people know one can reach 50C, one has an air conditioner. OTOH, in West Europe, heath spells are rare enough, thus most people do not have air conditioners.
        I have a nanoPi m3 (octocore) and just an airshield -no fan : is noisy- and tried with 8 cores (compiling xtensa-gcc : does not need swap extension: it lasted 15 minuts with heatsink before crushing: using 3 cores was OK).

        nanoPi m3 are very much like RPi (less support; use debian stable instead of debian unstable ; software compiled on a nanoPi is likely to work -ocv does- once newer (but compatible) libraries are installed. same RAM -1G-: worse temperature scaling, IMO).
        I managed to have ocv4 alpha installed (had to remove _xfeatures2d and stiching, to avoid issues) using your script the following way:

        a) I made a huge (1G) file in ~/ext/catalogue/gros using dd.
        597 sudo mount /dev/sdb2 ext # ~ext is mount point /directory
        598 ls ext
        599 ls ext/catalogue

        609 dd if=/dev/zero of=ext/catalogue/gros bs=1k count=1024k
        610 sync
        611 ls -lh ext/catalogue/gros

        b) as “gros” cannot be used as a swap file as it is, it must be preapaired. single issue is that some parts of util-linux (ask wikipedia for util-linux)are not packaged , as, among them, the one I neded, mkswap, was not packaged -according to Murphys laws- and I had to compile them from sources (got from the following way:

        619 tar xvf cle/utilLinux.tar.gz
        620 cd util-linux-2.26-rc1/
        621 ./configure
        622 make -j4 # is short to compile… wonot heat that much
        623 ls # what did I do?
        624 ./mkswap ~/ext/catalogue/gros # no need to install, it is just to prepare a swap file

        Then, it is straight forwards (on a RPi, one should remove existing wap partition with suso swapoff -a )

        625 sudo swapon ~/ext/catalogue/gros
        626 history >~/swapstoria # to remember … for next time or pyimagesearch…

        Then, compilation (with one core enabled) was comfortable (almost no library needed swap : makes a difference with ocv3 IIRC and seems a nice side of ocv4; only one was python bindings which needed about 400 M swap + exiting RAM -but desktop remains usable for reading docs)

  6. Alexander Holsgrove September 28, 2018 at 1:03 pm #

    After running the build and then the make on a Raspberry Pi 3, I can’t link the .so file as it’s not there. I do have /usr/local/lib/python3.5/dist-packages/ and also /usr/local/lib/python3.5/dist-packages/

    What have I done wrong please?

    • Adrian Rosebrock October 8, 2018 at 12:21 pm #

      You have not done anything wrong. Use the filename that you have found.

  7. Matteo September 29, 2018 at 1:45 am #

    Hello to all,
    I am a beginner and I have some problems with the translations, I tried to document before writing but probably it did not happen to anyone that after making make -j4 without errors and verified the correct directories of the python and Numpy interpreter launching the python command then gave “importError: no module named CV2”
    I think it’s a path problem, but I do not know how to get it back, can you help me?

    • Adrian Rosebrock October 8, 2018 at 12:18 pm #

      Hey Matteo, I would recommend you read the “Troubleshooting and Frequently Asked Questions (FAQ)” section of this tutorial as I provide answers to the most common reasons why you may not be able to import the OPenCV bindings.

  8. Alexander Holsgrove September 29, 2018 at 12:18 pm #

    The symlink was wrong for me. The file is “” and not “”. The whole command therefor “ln -s /usr/local/lib/python3.5/site-packages/

  9. Pranav Lal September 30, 2018 at 12:42 am #

    I completed the installation. I had to interrupt make -j4 once when it was stuck at 99%. I ran make again without any parameters. I have followed the rest of the guide including creating the symbolic link. However, when I import cv2 python 3.5 is unable to find the file. I have created a virtual environment named cv4 and have followed the rest of your instructions without problems. What data should I share to help us diagnose the problem?

    • Adrian Rosebrock October 8, 2018 at 12:11 pm #

      Take a look at the “Troubleshooting and Frequently Asked Questions (FAQ)” section of this tutorial as I discuss the most common reasons why the import will fail.

  10. Cameron October 2, 2018 at 2:42 am #


    I have tried following this tutorial to compile opencv 4 for the orangepi zero board, however when I run the cmake command it does not complete due to errors. Looking in the error log it says:
    Regex: ‘command line option .* is valid for .* but not for C\+\+’

    Any ideas why this may be happening?

  11. marco October 2, 2018 at 2:44 am #

    hello Adrian, I would like to understand one thing, why on the Raspberry even if you follow the post to the letter then OpenCv does not install as it should, even repeating the installation several times then you always find yourself with the same problem! You know I thought maybe if you put a video with all the steps of the installation maybe it could help beginners like me to understand where have wrong or at least see where there are differences in the installation.

    However, I thank you because you are doing a great jo

    • Adrian Rosebrock October 8, 2018 at 10:40 am #

      I’ve done a few Raspberry Pi + OpenCV install guide videos. I’m considering doing one for this post as well but I wanted to wait until OpenCV 4 is officially released.

  12. Lee October 2, 2018 at 6:27 am #

    hi , have anyone of you here faced this problem?
    the build of opencv hang/not responding when come to 98% progress.
    any suggestion/comment?

    • Adrian Rosebrock October 8, 2018 at 10:35 am #

      Did you increase your swap size as recommended? If so, try:

      1. Deleting your “build” directory
      2. Re-creating your “build” directory
      3. Re-run “cmake”
      4. Start the compile but this time only compile with a single core via “make -j1”

      It sounds like your Pi is running into a threading/race condition which is causing the problem.

    • Chris Pinkenburg October 28, 2018 at 2:11 pm #

      Hi Lee,
      at the end of the compilation using -j4 you run two linking processes – each taking 80% of the available memory and the raspberry pi swaps itself to death achieving nothing (run “top” in a terminal window, hit M to sort by memory and you’ll see two large compilation processes which do not get any significant cpu).
      What worked for me is to ctrl-c the ongoing build and then run make without -j4. There is enough memory for running one process. You do not need to run a make clean, make will pick up where you left it. This way you still get the benefit of running -j4 which is a lot faster

  13. Andrew Baker October 2, 2018 at 6:49 pm #

    I was able to successfully install OpenCV 4.4.0-alpha. However I had some initial issues.
    1. The first attempt failed, RPI locked up. I had the swap space at 1024MB and used the make -j4 command. While running the RPI was very hot (80 – 85C).

    2. I deleted my build folder and tried again. This time I was met with the error:
    cmake fails to determine the bitness of the target platform.

    1. Within an old post I found a slightly different cmake command. This was the modified command I used:
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.0.0-alpha/modules \
    -D BUILD_opencv_python3=TRUE \
    -D PYTHON3_LIBRARY=/usr/lib/arm-linux-gnueabihf/ \

    2. I executed this with only cmake. I didn’t use -j4.

    3. It took approximately 4 hours, but the build complied and installed successfully.

    • Adrian Rosebrock October 8, 2018 at 10:31 am #

      Thanks for sharing Andrew! And congrats on getting OpenCV 4 installed on your Pi.

  14. Gaurav Shirke October 3, 2018 at 1:27 am #

    [ 95%] Building CXX object modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o

    stucked at 95%
    tried with make -j4 which stuck at same line but with 98%
    with makt -j1 at 95%
    and with make at 95%

    • Gaurav Shirke October 3, 2018 at 4:31 am #

      problem solved

      • Adrian Rosebrock October 8, 2018 at 9:15 am #

        Congrats on resolving the issue, Gaurav!

      • devashish October 27, 2018 at 10:14 am #

        same here . How did you do it?

    • Adrian Rosebrock October 8, 2018 at 10:28 am #

      Did you increase your swap size like I recommended in the tutorial? Make sure you do so. Alternatively, you can pip install opencv.

  15. Gaurav Shirke October 3, 2018 at 4:30 am #

    eveything is okay but then this error occured
    ln: failed to create symbolic link ‘’: File exists

    Followed all steps open file is not there in ”/usr/local/lib/python3.5/site-packages/”

    another file is present their which is named as ””

  16. Arul October 3, 2018 at 7:36 am #

    Step # 6 references incorrect lib? I see only ARM version, not x86. I had to do this instead.

    ln -s /usr/local/lib/python3.5/site-packages/

    • Adrian Rosebrock October 8, 2018 at 10:25 am #

      Hey Arul, I’ve updated the tutorial with the path most readers are getting. I suggest other readers use tab completion to derive the correct file name.

  17. murksiuke October 5, 2018 at 3:25 am #

    hello, after the cmake the output shows the interpreter as python2.7, even though i followed the commands on the guide. not sure what to do

    • Adrian Rosebrock October 8, 2018 at 9:52 am #

      Are you referring to the interpreter used for the compile? If so, ignore it — that’s a bug in the “cmake” output. As long as your “Python 3” section looks like mine you can proceed.

      • murksiuke October 11, 2018 at 2:27 pm #

        Thank you for the reply, i redid the whole thing (actually got a larger sd because of space problems) and everything went fine. Thanks for the guide!

        • Adrian Rosebrock October 12, 2018 at 8:56 am #

          Fantastic, I’m glad it worked for you! 🙂

  18. Ahmed Raza October 5, 2018 at 5:04 am #

    having an error while cmake

    Run CMake for OpenCV 4

    configuration in complete, errors occured!

    • Adrian Rosebrock October 8, 2018 at 9:51 am #

      Take a look at your output of “cmake”. It will tell you what the error is.

  19. Francisco October 5, 2018 at 8:07 pm #

    Hi, thanks for the detailed tutorial.

    I faced an issue when creating the link in step #6, the output .so file had a different name “” instead of “”, so I had to rename it (this was already pointed out in your previous tutorial for installing OpenCV 3).

    • Adrian Rosebrock October 8, 2018 at 9:48 am #

      Thanks Francisco. I’ve updated the post but I would encourage readers to use tab completion to correctly determine their output filename.

  20. Adrian Rosebrock October 8, 2018 at 9:16 am #

    Thanks for sharing, Victor. I’ve updated the blog post. I also recommend readers perform tab completion so correctly determine the filename.

  21. Charlie October 8, 2018 at 1:24 pm #

    Hi Dr. Adrian. As a beginner, I wonder if it’s possible to have both versions of OpenCV (3.x and 4.x) installed on a Pi. Can I use a virtual environment (VE) for each version? If so, how can I do that? Is it covered in any blog post? Besides, how can I update an Environment with a more recently Python version when one becomes available? Can I do the same with a new OpenCV release in a VE? Thank you.

    • Adrian Rosebrock October 8, 2018 at 1:40 pm #

      Yes, you can have multiple OpenCV versions on your system. I like to:

      1. Create a new Python virtual environment for each version of OpenCV
      2. Compile OpenCV but only sym-link in the OpenCV bindings from the “build/lib” directory (I keep a “build” directory for each version of OpenCV I compile)

  22. Steven October 9, 2018 at 7:31 pm #


    Thank you for the post really appreciate it. I built OpenCV 4.0 on new Pi3 Model B+ booting off mSATA drive with you can do with the new Model B+. I was monitoring the build with -J4 switch with free -h and the process actually used up the swap file and stalled in latter stages of the build, (99% stage). Increased the swap size and was able to make it through that late build stage and complete. Its possible that some of the others reading the comments hit the same situation. Quick question why does your version show 4.0 and mine show 4.0 alpha on the check version?

    import cv2
    >>> cv2.__version__
    >>> exit()

    • Adrian Rosebrock October 12, 2018 at 9:22 am #

      Hey Steven, thanks for the tip on increasing swap size further. Compiling only only a single core just should resolve the issue as well.

      Regarding your question, it looks like you compiled OpenCV using the official alpha version of the library. When I compiled OpenCV it was with the pre-release which did not have the “alpha” tag, hence the discrepancy.

      • Denis Brion October 17, 2018 at 7:03 am #

        AFAIK :
        a) “only” last stage needs swaps, if one uses a single core (else, one has to add memory consumptions of each process + some RAM to have parallel processes to communicate)
        b) last stage is a single process build (makes a huge *.so with a lot of libraries, ready to be called by python): it needs less than 1G swap (total less than 2G RAM+ virtual RAM : if RPi had 2 G RAM, … no swap would be needed.

        Is it possible to … cross compile for RPi (I sometimes use nanoPi -debian stable- to make binaries for RPi -debian unstable-: *.so are compatible (the other way, it would not), though certain optimisations cannot be caught. Cross compiling goes further : one uses an x86 PC -some have tons of RAM, lets us say 4 G- to make *.so -and executables; they are sent to a RPi (arduino cross compiles for … avrs, say) : hertaville used it for <=512 M RPis
        It might be difficult to install on the first time, but much faster (and one often needs a PC … to put on a SD disk the Rapsbian image : at least, once…). (processes using swap have unpredictable delays : depends on the nature and state of the swap partition/file ; a process can be seen as hanged and continue at a very slow pace…. leading people to kill it : cross compiling is likely, once setup -this is a difficult task- to avoid all of these troubles)

        • Adrian Rosebrock October 20, 2018 at 7:56 am #

          Cross-compiling OpenCV for the Pi has been a royal pain in my ass. It’s not a nut I’ve been able to crack yet, unfortunately 🙁

  23. m October 11, 2018 at 2:24 pm #

    Hello! Is there a reason for installing inside the “cv” environment? I have seen guides that skip that step, and in the end you can call “import cv2” without a “workon cv” command beforehand.

    • Adrian Rosebrock October 12, 2018 at 8:56 am #

      Using Python virtual environments is a best practice in Python development. They are certainly optional but heavily recommended. If you’re new to Python virtual environments or how they work be sure to read this post.

  24. Phil Desrosiers October 16, 2018 at 1:25 am #

    I’m getting “zsh: no matches found: libcanberra-gtk*” when I try to apt-get libcanberra-gtk* .

    As far as I know, my sources are up to date. Do I need to add another apt source somewhere?

  25. Phil Desrosiers October 16, 2018 at 10:02 pm #

    I’m getting an error while attempting to make. Do I need an EVEN LARGER swap file?:

    virtual memory exhausted: Cannot allocate memory

    • Adrian Rosebrock October 20, 2018 at 8:00 am #

      Unfortunately it seems like you may need a larger swap file.

  26. Ahmed Raza October 19, 2018 at 10:40 am #

    Dear Adrian
    kindly resolve my issue i am having an error “ImportError: No module named ‘picamera’ ”
    while running a python file.

    • Adrian Rosebrock October 20, 2018 at 7:29 am #

      You need to install the picamera Python library:

      $ pip install "picamera[array]"

  27. Rion Ahl October 21, 2018 at 4:31 am #

    Adrian, I have a problem, with the import cv2 as described in FAQ, but as I am a beginner, I dont really understand what to do? Could you please help?

    • Adrian Rosebrock October 22, 2018 at 8:05 am #

      Hey Rion, unfortunately without physical access to your Pi it is hard to determine where you would have made a mistake. It’s okay if you are a beginner, but you need to take the time to educate yourself on how the command line argument works, including file paths. Again, it’s impossible for me to guess what step may have tripped you up without more information so I suggest you take it slow and work through the FAQ.

  28. wenkang October 22, 2018 at 5:31 am #

    Hi Adrian,

    Can the OpenCV 3 and 4 coexist in Raspberry pi ? Or do I have to remove one if want to install the other?

    And if not, how to configure and specify the one that I want when running one python script ?


    • Adrian Rosebrock October 22, 2018 at 7:47 am #

      Both can coexist on the same machine, just create a separate Python virtual environment for your OpenCV 4 install. Other than that, no other changes are required.

  29. Jim October 24, 2018 at 7:26 pm #

    Hey Adrian, I made it most of the way through this tutorial…while compiling opencv, my Pi stops at [ 98% ] Built target opencv_perf_tracking. I expanded the swapfile size to 1024…there’s no error message, just no more progress. Should I close the terminal and start from the compiling step again? Should I make the swapfile bigger?

  30. elroy October 24, 2018 at 10:53 pm #

    Did you ever find out who stole your beer?

  31. jim421616 October 25, 2018 at 6:59 pm #

    Hi, Adrian, I posted a comment yesterday about my Pi not responding during the compile. I left it running overnight and when I came back to it, it had run out of memory. I increased the swapfile size to 2048 Mb, ran the compile again, and it works now. Thanks for your outstanding tutorials!

    • Adrian Rosebrock October 29, 2018 at 1:48 pm #

      Awesome, congratulations on getting OpenCV installed on your Pi!

  32. devashish October 26, 2018 at 8:36 pm #

    Hi adrian . i have been trying to install opencv from past 4 days. I have gone through all versions from 3.3.0 to the 4.0 . Currently in this tutorial the cmake compiled till 98% but has been like this since 1.5 hrs . This opencv thing has made be furious . Please suggest something x(.

    • Adrian Rosebrock October 29, 2018 at 1:38 pm #

      Have you tried (1) increasing your swap size and (2) compiling using just a single core? What about doing a pip install of opencv?

  33. Emilio Meza October 27, 2018 at 1:26 pm #

    Hi Adrian! I followed the steps and everything worked well on the virtual environment but, what if i want to use opencv 4 on python 3(IDLE)? there’s an easy way to do it without starting again with a different process?

    • Adrian Rosebrock October 29, 2018 at 1:32 pm #

      If you are using the same Python version for a different environment you can simply copy your bindings into the new site-packages directory for the environment. Otherwise you will need to recompile OpenCV for that specific Python version number. You may also be able to pip install opencv.

  34. Aly Rofie November 3, 2018 at 12:45 am #

    Hi Adrian, Thanks for your tutorial. But i got some problem when i’m on step 5, i can’t do “make” it says (cannot find makefile. stop), and also i can’t move into opencv directory, can you tell how to fix this?

    thank you

    • Adrian Rosebrock November 6, 2018 at 1:32 pm #

      Your “cmake” directory likely excited with an error. Go back to the “cmake” step and investigate the output to find the error. Once “cmake” executes without error then you can run “make”.

  35. David Monk November 4, 2018 at 11:51 am #

    I installed opencv-4.0.0-beta last night; I noticed the beta version was available so I downloaded the source and compiled it instead of the alpha. Seems to have compiled successfully.

    • Adrian Rosebrock November 6, 2018 at 1:24 pm #

      Awesome, congrats on getting OpenCV 4 installed on your Pi, David!

  36. Darcy November 10, 2018 at 10:57 am #

    Hey Adrian,

    Love the tutorials. You are really the main resource for opencv on raspberry pi. I’m hoping you can help me with a problem I have. I’ve gone through the tutorial 3 times now, and no matter what I do during step 5, I cannot get the interpreter, or numpy to point inside the virtual environment. What am I doing wrong?

    • Adrian Rosebrock November 13, 2018 at 5:00 pm #

      Hey Darcy, are you sure you are in the Python virtual environment when running “cmake”? Unfortunately without having physical access to your Pi I’m not sure what the issue may be. Don’t worry though, OpenCV 4 will soon be released and then you’ll be able to use pip in install opencv.

  37. balaji November 11, 2018 at 11:11 am #

    hi adrian,

    Thanks for your post,,,i follow your steps but when make -j4 command running my code gets hanged at 98% ,,i tried 3 times even it doesn’t cross 98%,,,how to resolve this issues,,,,,

    • Adrian Rosebrock November 13, 2018 at 4:53 pm #

      It sounds like your Raspberry Pi is frozen. Delete the “build” directory, re-create it, re-run “cmake”, but this time run “make” without the “-j4” option. The compile will take longer but your Pi shouldn’t lock up.

  38. Mark November 18, 2018 at 2:55 pm #

    This code does not run “as is” with the current Raspberian
    Version:November 2018
    Release date:2018-11-13
    Kernel version:4.14

    and with OpenCV 4

    The problem seemed to be two fold: It installed for Python2.7 and never could find Python 3 (per the instructions) and was missing:
    sudo apt-get install libatlas-base-dev
    sudo pip install setuptools


    • Adrian Rosebrock November 19, 2018 at 12:28 pm #

      It sounds like you created a Python 2.7 virtual environment instead of a Python 3 virtual environment. Can you check your Python virtual environment version?

  39. Austin B November 23, 2018 at 8:30 am #

    Thank you so much for this tutorial! I was able to get OpenCV 4.0.0 (official release now, no longer at pre-release) built against Python 3.6.7 on the 3 B+ in about half a day (using only “make -j2”, because I was afraid of overheating, and I was going to let it run overnight). Your instructions worked like a charm 🙂

    • Adrian Rosebrock November 25, 2018 at 9:12 am #

      Congrats on getting OpenCV 4 installed on your Pi, Austin!

  40. Chris Pinkenburg November 25, 2018 at 9:39 am #

    Hi Adrian,

    great tutorial, made installing opencv a cut and paste operation. opencv 4.0.0 is now out and I just installed it. One thing which has changed compared to the alpha version is the location of the python cv2 library. I found it in /usr/local/python/cv2/python-3.5/, not as your tutorial suggests in /usr/local/lib/python3.5/site-packages/

    • Adrian Rosebrock November 25, 2018 at 9:45 am #

      Thanks Chris — this guide was written before OpenCV 4 was officially released. OpenCV 4 was officially released last week so I’m testing out all the install instructions and updating as necessary 🙂

  41. Tom Foltz November 27, 2018 at 11:36 am #

    When I get to this point, I’m confused on what to do. I’m a relative newbie

    Using a terminal text editor such as vi / vim or nano , add the following lines to your ~/.profile :

    When I enter the following: sudo nano ~/.profile
    I seem to have a problem. Maybe I don’t need sudo. I think the ~/ takes me to home, so what is the file I’m editing, “.profile” is not valid.

    • Tom Foltz November 27, 2018 at 12:21 pm #

      I’m in this section:

      Make use of virtual environments for Python development

      • Adrian Rosebrock November 30, 2018 at 9:29 am #

        No, you do not need “sudo”. Use just “nano ~/.profile” so you are editing the .profile file of the “pi” user.

  42. Ziad Alexander November 27, 2018 at 1:36 pm #

    Awesome tutorial. I am building a drone with an IR camera using openCV. But PLEASE for the love of all things holy. REMOVE the dollar signs ‘$’ from your shell lines. They are beyond annoying and serve no purpose. I still have no idea why people still do that.

    • Adrian Rosebrock November 30, 2018 at 9:26 am #

      They do serve a purpose. The “$” indicates that you are at the command line executing a command versus opened up a text file for editing, etc.

  43. Roy Georgius November 29, 2018 at 4:13 pm #

    hi, im stuck while in process make -j4 in the line 100% building CXX object module/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2/cpp.o can you help me??

    • Adrian Rosebrock November 30, 2018 at 8:51 am #

      It sounds like your Raspberry Pi has hung. Stop the compile, delete your “build” directory, re-create it, and re-run “cmake” and “make”, but this time increase your swap size to 2048MB.

  44. Chris Foley December 2, 2018 at 8:18 pm #

    I think my pi has stalled. I am at mostly 100% but the command prompt won’t come back. This is a B 3+ with swap file expanded to 2048 as specified. The compile was done with the -j4 switch. This is a BRAND NEW Pi and a brand new install. I got a bunch of warnings about clock skew and some about parameter passing being changed in GCC 7.1.

    After a lot of retries, I found that I was having some intermittant network issues. So I went bakc to a physical connection. I had some trouble figuring out why I could not switch to my cv environment locally. After editing my .bashrc file, I was up and recompiling.

    I would like to ask about the GCC 7.1 warnings. Will this be updated soon? GCC is now at 8.5?

    • Adrian Rosebrock December 4, 2018 at 10:06 am #

      I can’t comment on the GCC warnings without seeing what they were but regarding the Pi looking up, try using only a single core when compiling:

      $ make -j1

      That should, ideally prevent the Pi from locking up.

  45. Chris Foley December 3, 2018 at 7:00 am #

    Clearly I am having issues. I keep missing steps. Forgetting to change to CV and so forth. I have removed the build directory several times now and finally have a new compilation running under CV directly and not limited by my sporadic network issues. I will let this run this morning and hope that I can come home to a new compile. I still worry about “failed” errors in the build cycle. Several items were marked this way. Do I need to post those are is it normal to see a few items marked this way? Seems fishy to me but I do not know any of details of this build.

    • Adrian Rosebrock December 4, 2018 at 9:51 am #

      It’s totally normal to see the “failed” message in the “cmake” output. OpenCV has A LOT of different configurations. The “cmake” script is just checking for various configurations and reporting “failed” if it cannot find a given configuration — it’s nothing to worry about.

  46. Naveen December 3, 2018 at 11:30 pm #

    Hi Adrian,
    First of all a pretty comprehensive and well compiled tutorial.
    OpenCv Intsalltion on my pi3 B+ has been a nightmare for me. First try it got hung up , Then i had to restore my pi from backup . The Second time i tried from some other method .This time it went thru’ successful but my got listed under /home/pi and i could’t do the sym-link . I am now going to try it for the 3rd time from this link . Hopefully it works out fine . Any tips that could help me out .

    • Adrian Rosebrock December 4, 2018 at 9:41 am #

      Here are my suggestions:

      1. Increase your swap size from 1024MB to 2048MB
      2. Compile with only a single core (make -j1)

      Both of those will help the Pi from locking up.

      As for your sym-link issue that just sounds like you were in the incorrect directory when creating the sym-link. Try again but this time make sure you’re in the “site-packages” directory.

  47. Leandro Boari December 7, 2018 at 12:14 pm #

    I followed the steps and on my Raspberry Pi 3B is stopped here:

    [100%] Built target opencv_perf_stitching

    Can someone help me?

    • Adrian Rosebrock December 11, 2018 at 1:01 pm #

      It sounds like you Pi has locked up. Try increasing your swap size to 2048MB and compiling with only a single core (i.e., “make -j1”). The compile will take longer but it should prevent the Pi from locking up.

  48. galile December 9, 2018 at 6:20 am #

    Hi Adrian,

    When I compile opencv, it’s being 100 percent and I got no error, but the command line doesn’t come to console,its not understood it compiled or not. I mean I can’t enter new command. How can I figure out?

    • Adrian Rosebrock December 11, 2018 at 12:53 pm #

      Can you still type via your keyboard or use your mouse? Is your Pi locked up?

  49. Florin December 10, 2018 at 6:24 am #

    Hi Adrian, I have a probelm with this error

    ln: failed to create symbolic link ‘cv2/cv2’: Permission denied

    • Adrian Rosebrock December 11, 2018 at 12:45 pm #

      What directory are you when you try to create the sym-link?

  50. paul December 10, 2018 at 11:54 am #

    In “Run CMake for OpenCV 4” run those commands 1-at-a-time. THey do not work in one big copy/paste.

  51. Shashank December 10, 2018 at 11:22 pm #

    How do I install matplotlib, scikit, imutils etc after doing this procedure? Can anyone please help. And after installing this there is only 300MB left , I have uninstalled libre office , Wolfram engine etc

    • Adrian Rosebrock December 11, 2018 at 12:38 pm #

      You can use pip to install them:

      $ pip install imutils matplotlib scikit-learn

  52. Calum December 11, 2018 at 11:56 am #

    Hi Adrian,

    I am trying to install openCV without a virtual environment and for python 2.7. This is due to some additional equipment that I’m using that requires installation outside of a virtual environment and is only compatible with python 2.7. I have previously installed openCV with python 2.7 following your tutorial, but this was done in a virtual environment.
    Now that I am trying both python 2.7 and no virtual environment, I am struggling to successfully install openCV. I am able to complete the build and install steps successfully, but when I try to import cv2 i get the error ‘ImportError: No module named cv2’.
    Is there anything different I must do to install openCV in this way? How do I go about successfully importing cv2?


    • Adrian Rosebrock December 11, 2018 at 12:29 pm #

      If you can skip the Python virtual environment, that’s not a problem. The same goes for the “Troubleshooting and Frequently Asked Questions (FAQ)” section of the guide, just be sure to check the “site-packages” directory of your system install of Python 2.7. Please keep in mind that the PyImageSearch blog is now officially Python 3+ so my Python 2.7 support moving forward will be fairly limited.

Leave a Reply