{"id":146106,"date":"2024-03-25T18:46:23","date_gmt":"2024-03-25T18:46:23","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=146106"},"modified":"2024-03-27T17:40:15","modified_gmt":"2024-03-27T17:40:15","slug":"install-opencv-raspberry-pi","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/install-opencv-raspberry-pi\/","title":{"rendered":"Install OpenCV on a Raspberry Pi (compatible with all RPi boards)"},"content":{"rendered":"\n<p>This is a quick guide showing how to install OpenCV (Open Computer Vision Library) software on a Raspberry Pi board. We tested this installation on Raspberry Pi OS 64-bit. OpenCV is an open-source library for computer vision and machine learning that comes with tools and algorithms for image processing, video analysis, and machine learning applications.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/Raspberry-Pi-Install-OpenCV.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install OpenCV on a Raspberry Pi compatible with all RPi boards\" class=\"wp-image-146639\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/Raspberry-Pi-Install-OpenCV.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/Raspberry-Pi-Install-OpenCV.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/Raspberry-Pi-Install-OpenCV.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/Raspberry-Pi-Install-OpenCV.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p>There are many ways for installing OpenCV on Raspberry Pi, you can install with <em>pip <\/em>on a virtual environment, install it using <em>apt install<\/em> or compile the software from source. In this guide, we&#8217;ll show you two options:<\/p>\n\n\n\n<p><a href=\"#pip-virtual-environment\" title=\"\">a) Installing OpenCV on Raspberry Pi with pip on Virtual Environment <strong>(Recommended)<\/strong><\/a><\/p>\n\n\n\n<p><a href=\"#apt-install\" title=\"\">b) Installing OpenCV on Raspberry Pi with apt<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before proceeding: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You need a <a href=\"https:\/\/makeradvisor.com\/best-raspberry-pi-starter-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi board<\/a>.<\/li>\n\n\n\n<li>You should have a <a href=\"https:\/\/randomnerdtutorials.com\/installing-raspbian-lite-enabling-and-connecting-with-ssh\/#install-raspberry-pi-os\">Raspberry Pi running Raspberry Pi OS<\/a> (32-bit or 64-bit).<\/li>\n\n\n\n<li>You should be able to<a href=\"https:\/\/randomnerdtutorials.com\/installing-raspbian-lite-enabling-and-connecting-with-ssh\/#connect-RPi-SSH\"> establish an SSH connection with your Raspberry Pi<\/a>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"pip-virtual-environment\">a) Installing OpenCV on Raspberry Pi with pip on Virtual Environment (Recommended)<\/h2>\n\n\n\n<p>Having an&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/installing-raspbian-lite-enabling-and-connecting-with-ssh\/#connect-RPi-SSH\">SSH connection established with your Raspberry Pi<\/a>, update and upgrade your Raspberry Pi, if any updates are available. Run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update &amp;&amp; sudo apt upgrade -y<\/code><\/pre>\n\n\n\n<p>Run the next command which should print in your terminal a Python version 3.X:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python --version<\/code><\/pre>\n\n\n\n<p>Install pip3 and Python 3 Virtual environment:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install -y python3-pip python3-virtualenv<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create a Virtual Environment<\/h3>\n\n\n\n<p>We&#8217;ll install the OpenCV library in a virtual environment. Creating a virtual environment will isolate the Python libraries we&#8217;re using, in this case, the OpenCV library, from the rest of the system.<\/p>\n\n\n\n<p>We&#8217;ll create our virtual environment on a directory on our <em>Desktop<\/em>. Enter the following command on a Terminal window to move to the Desktop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd ~\/Desktop<\/code><\/pre>\n\n\n\n<p>Create a folder for your project. This is where we&#8217;ll create the virtual environment and install the library. We&#8217;ll create a folder called <em>projects<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir projects<\/code><\/pre>\n\n\n\n<p>Move to the newly created folder:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd ~\/Desktop\/projects<\/code><\/pre>\n\n\n\n<p>Create a virtual environment for this directory called <span class=\"rnthl rntliteral\">myenv<\/span>. This must be the same directory where we&#8217;ll install the OpenCV library. Replace <span class=\"rnthl rntliteral\">myenv<\/span> with the desired name for your virtual environment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 -m venv projectsenv<\/code><\/pre>\n\n\n\n<p>Then, you can run the following command to check that the virtual environment is there.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -l<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"589\" height=\"162\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-virtual-environment.png?resize=589%2C162&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"create python3 virtual environment for Raspberry Pi Projects\" class=\"wp-image-146637\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-virtual-environment.png?w=589&amp;quality=100&amp;strip=all&amp;ssl=1 589w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-virtual-environment.png?resize=300%2C83&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 589px) 100vw, 589px\" \/><\/figure><\/div>\n\n\n<p>Activate the virtual environment:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>source projectsenv\/bin\/activate<\/code><\/pre>\n\n\n\n<p>Your prompt should change to indicate that you are now in the virtual environment.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"637\" height=\"114\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-activate-virtual-environment.png?resize=637%2C114&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Python 3 activate Virtual Environment\" class=\"wp-image-146634\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-activate-virtual-environment.png?w=637&amp;quality=100&amp;strip=all&amp;ssl=1 637w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-activate-virtual-environment.png?resize=300%2C54&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 637px) 100vw, 637px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Installing the OpenCV Library<\/h3>\n\n\n\n<p>Now that we are in our virtual environment, we can install the OpenCV library. Run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip3 install opencv-contrib-python<\/code><\/pre>\n\n\n\n<p>After a few seconds, the library will be installed (ignore any yellow warnings about deprecated packages).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"909\" height=\"290\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-install-openCV-pip-command.png?resize=909%2C290&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Python 3 install openCV with pip command\" class=\"wp-image-146636\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-install-openCV-pip-command.png?w=909&amp;quality=100&amp;strip=all&amp;ssl=1 909w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-install-openCV-pip-command.png?resize=300%2C96&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-install-openCV-pip-command.png?resize=768%2C245&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 909px) 100vw, 909px\" \/><\/figure><\/div>\n\n\n<p>You have everything ready to start writing your Python code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Testing OpenCV pip Installation<\/h3>\n\n\n\n<p>Now let&#8217;s ensure that OpenCV has been installed successfully on your Virtual Environment. In your terminal window, start Python:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python<\/code><\/pre>\n\n\n\n<p>Then, in the Python import the OpenCV library.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import cv2<\/code><\/pre>\n\n\n\n<p>Finally, type the next command to check the OpenCV version that you installed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cv2.__version__<\/code><\/pre>\n\n\n\n<p>If everything has been done properly, it should return your OpenCV version as illustrated in the image below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"613\" height=\"146\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-import-opencv2-test-version-pip.png?resize=613%2C146&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Python3 import opencv 2 test version with pip installation\" class=\"wp-image-146635\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-import-opencv2-test-version-pip.png?w=613&amp;quality=100&amp;strip=all&amp;ssl=1 613w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/python3-import-opencv2-test-version-pip.png?resize=300%2C71&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 613px) 100vw, 613px\" \/><\/figure><\/div>\n\n\n<p> You can exit Python prompt by typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exit()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"apt-install\">b) Installing OpenCV on Raspberry Pi with apt<\/h2>\n\n\n\n<p>Having an&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/installing-raspbian-lite-enabling-and-connecting-with-ssh\/#connect-RPi-SSH\">SSH connection established with your Raspberry Pi<\/a>, enter the next command to update and upgrade your packages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update &amp;&amp; sudo apt upgrade -y<\/code><\/pre>\n\n\n\n<p>Run the next command which should print in your terminal a Python version 3.X:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python --version<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"429\" height=\"178\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/check-python-version-on-raspberry-pi.png?resize=429%2C178&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing OpenCV on Raspberry Pi check python version\" class=\"wp-image-146111\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/check-python-version-on-raspberry-pi.png?w=429&amp;quality=100&amp;strip=all&amp;ssl=1 429w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/check-python-version-on-raspberry-pi.png?resize=300%2C124&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 429px) 100vw, 429px\" \/><\/figure><\/div>\n\n\n<p>Then, run the next command to install all the required dependencies.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install -y build-essential cmake pkg-config libjpeg-dev libtiff5-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libfontconfig1-dev libcairo2-dev libgdk-pixbuf2.0-dev libpango1.0-dev libgtk2.0-dev libgtk-3-dev libatlas-base-dev gfortran libhdf5-dev libhdf5-serial-dev libhdf5-103 libqt5gui5 libqt5webkit5 libqt5test5 python3-pyqt5 python3-dev<\/code><\/pre>\n\n\n\n<p>Install OpenCV using apt install:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install -y python3-opencv<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntcred\">This command can take up to 10 minutes, so be patience and wait for the command to complete.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Testing OpenCV apt Installation<\/h3>\n\n\n\n<p>Now let&#8217;s ensure that OpenCV has been installed successfully. In your terminal window, start Python:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python<\/code><\/pre>\n\n\n\n<p>Then, in the Python import the OpenCV library.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import cv2<\/code><\/pre>\n\n\n\n<p>Finally, type the next command to check the OpenCV version that you installed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cv2.__version__<\/code><\/pre>\n\n\n\n<p>If everything has been done properly, it should return your OpenCV version as illustrated in the image below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"717\" height=\"194\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/check-opencv-version-installation-raspberry-pi.png?resize=717%2C194&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Testing OpenCV Installation on Raspberry Pi\" class=\"wp-image-146113\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/check-opencv-version-installation-raspberry-pi.png?w=717&amp;quality=100&amp;strip=all&amp;ssl=1 717w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/check-opencv-version-installation-raspberry-pi.png?resize=300%2C81&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 717px) 100vw, 717px\" \/><\/figure><\/div>\n\n\n<p>You can exit Python prompt by typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exit()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>Congratulations! You successfully installed OpenCV on your Raspberry Pi. Now, you can start building your image and video processing projects.<\/p>\n\n\n\n<p>You may also like reading:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/set-up-usb-camera-opencv-raspberry-pi\/\" title=\"\">Set Up USB Camera for OpenCV Projects with Raspberry Pi<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/install-influxdb-2-raspberry-pi\/\">Install InfluxDB on Raspberry Pi<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-influxdb\/\">ESP32: Getting Started with InfluxDB<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/what-is-mqtt-and-how-it-works\/\">What is MQTT and How It Works<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-mosquitto-broker-on-raspberry-pi\/\">Install Mosquitto MQTT Broker on Raspberry Pi<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a quick guide showing how to install OpenCV (Open Computer Vision Library) software on a Raspberry Pi board. We tested this installation on Raspberry Pi OS 64-bit. OpenCV &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Install OpenCV on a Raspberry Pi (compatible with all RPi boards)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/install-opencv-raspberry-pi\/#more-146106\" aria-label=\"Read more about Install OpenCV on a Raspberry Pi (compatible with all RPi boards)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":146639,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301,264,190,268],"tags":[],"class_list":["post-146106","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-raspberrypi","category-project","category-raspberry-pi","category-raspberry-pi-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/01\/Raspberry-Pi-Install-OpenCV.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/146106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=146106"}],"version-history":[{"count":13,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/146106\/revisions"}],"predecessor-version":[{"id":151258,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/146106\/revisions\/151258"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/146639"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=146106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=146106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=146106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}