{"id":213,"date":"2022-09-07T20:00:00","date_gmt":"2022-09-07T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=213"},"modified":"2022-08-21T20:09:41","modified_gmt":"2022-08-21T11:09:41","slug":"raspberrypi-opencv","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/raspberrypi-opencv\/","title":{"rendered":"How to install OpenCV on Raspberry Pi"},"content":{"rendered":"\n<p>OpenCV is an image processing library developed by Intel.<\/p>\n\n\n\n<p>This article shows you <span class=\"st-mymarker-s\">how to install OpenCV on Raspberry Pi<\/span>.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Contents<\/span><\/p><div class=\"free-inbox\">\n<p><\/p>\n\n\n\n<ul><li>Overview of OpenCV<\/li><li>Overview of OpenCV_contrib, an extension module of OpenCV<\/li><li>How to install OpenCV on Raspberry Pi<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Overview of OpenCV<\/h2>\n\n\n\n<p>I will show you an <span class=\"st-mymarker-s\">overview of OpenCV<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>With OpenCV, you can mainly use the following functions.<\/p>\n\n\n\n<ul><li>Image Processing<\/li><li>Transforms<\/li><li>Fitting<\/li><li>Optical Flow Tracking<\/li><li>Segmentation<\/li><li>Calibration<\/li><li>Features VSLAM<\/li><li>Depth, Pose Normals, Planes, 3D Features<\/li><li>Object recognition Machine learning<\/li><li>Computational Photography<\/li><\/ul>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>I quote from the explanation on <a href=\"https:\/\/atmarkit.itmedia.co.jp\/ait\/articles\/1507\/27\/news154.html\" target=\"_blank\" rel=\"noreferrer noopener\">this site<\/a>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote has-text-align-center\"><p><img decoding=\"async\" width=\"920\" height=\"655\" class=\"wp-image-345\" style=\"width: 700px;\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/opencv.png\" alt=\"\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/opencv.png 920w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/opencv-300x214.png 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/opencv-768x547.png 768w\" sizes=\"(max-width: 920px) 100vw, 920px\" \/><\/p><\/blockquote>\n\n\n\n<div style=\"height:80px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>An extension module called OpenCV_contrib allows the use of new algorithms and experimental functions.<\/p>\n\n\n\n<p>In the next section, I will briefly show an overview of OpenCV_contrib.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Overview of OpenCV_contrib<\/h3>\n\n\n\n<p>I will briefly show you <span class=\"st-mymarker-s\">an overview of OpenCV_contrib<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><a href=\"https:\/\/docs.opencv.org\/3.3.0\/d3\/d81\/tutorial_contrib_root.html\" target=\"_blank\" rel=\"noreferrer noopener\">The official OpenCV tutorial<\/a> is helpful in learning about functions of OpenCV_contrib.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote has-text-align-center\"><p><img decoding=\"async\" width=\"567\" height=\"506\" class=\"wp-image-359\" style=\"width: 600px;\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberypi-opencv_1-1.jpg\" alt=\"\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberypi-opencv_1-1.jpg 567w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberypi-opencv_1-1-300x268.jpg 300w\" sizes=\"(max-width: 567px) 100vw, 567px\" \/><\/p><\/blockquote>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>For example, a module called &#8220;aruco&#8221; can detect 2D marks and extract their coordinates.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote has-text-align-center\"><p><img decoding=\"async\" width=\"640\" height=\"480\" class=\"wp-image-349\" style=\"width: 700px;\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/aruco.jpg\" alt=\"\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/aruco.jpg 640w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/aruco-300x225.jpg 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p><cite>quotation : <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.opencv.org\/3.3.0\/d3\/d81\/tutorial_contrib_root.html\" target=\"_blank\">The official OpenCV tutorial<\/a><\/cite><\/blockquote>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>I installed OpenCV_contrib because I wanted to detect AR marker by using &#8220;aruco&#8221;.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">How to install OpenCV on Raspberry Pi<\/h2>\n\n\n\n<p>I will show you <span class=\"st-mymarker-s\">how to install OpenCV on Raspberry Pi<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>There are two ways to install OpenCV as shown below, but I will show method No.2 because it is easier.<\/p>\n\n\n\n<ol><li>Build from OpenCV source code<\/li><li>Install pre-built package with &#8220;pip&#8221; command<\/li><\/ol>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Follow the steps below to install OpenCV.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Procedure<\/span><\/p><div class=\"free-inbox\">\n<p><\/p>\n\n\n\n<ul><li>Update the RaspberryPi package<\/li><li>Install libraries that depend on OpenCV<\/li><li>Install OpenCV with &#8220;pip&#8221; command<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Update the RaspberryPi package<\/h3>\n\n\n\n<p>First,<span class=\"st-mymarker-s\"> update the &#8220;list&#8221; of installable packages<\/span> and <span class=\"st-mymarker-s\">update the installed packages<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Enter the following command in the terminal.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ sudo apt-get update<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ sudo apt-get upgrade<\/code><\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Update &#8220;pip&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ sudo pip install --upgrade pip<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Install libraries that depend on OpenCV<\/h3>\n\n\n\n<p>Next, <span class=\"st-mymarker-s\">install libraries that depend on OpenCV<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Enter the following command in the terminal.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-100\n$ sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5\n$ sudo apt-get install libatlas-base-dev\n$ sudo apt-get install libjasper-dev<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If the installation of &#8220;libhdf5-100&#8221; does not work, try &#8220;libhdf5-103&#8221; as well.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Install OpenCV with &#8220;pip&#8221; command<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">Install OpenCV with &#8220;pip&#8221; command<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Note that there are several different types of pip commands.<\/p>\n\n\n\n<p>In a coexisting environment of Python 2 and Python 3, &#8220;pip&#8221; command is an installation for Python 2 and &#8220;pip3&#8221; command is an installation for Python 3.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Since Python2 and Python3 are installed on Raspberry Pi, mistaking &#8220;pip&#8221; and &#8220;pip3&#8221; may result in unexpected behavior.<\/p>\n\n\n\n<p>It is recommended to check the python version before installation.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\"><a href=\"https:\/\/python-academia.com\/en\/raspberry-pi-python-version\/\">How to check the python version of Raspberry Pi and change to python3<\/a><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If you only need to install the main OpenCV module, use the following command to install it.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ sudo pip3 --default-timeout=1000 install opencv-python<\/code><\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If you want to install extension modules, install &#8220;opencv-contrib&#8221; after installing &#8220;opencv-python&#8221;.<\/p>\n\n\n\n<p>In this case, make sure that <span class=\"st-mymarker-s\">the versions of &#8220;opencv-python&#8221; and &#8220;opencv-contrib-python&#8221; are the same<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>How to specify the version is explained in the next section.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h3 class=\"wp-block-heading\">If an error occurs during OpenCV installation<\/h3>\n\n\n\n<p>If the installation does not work, the OS version may be too old to support the latest version of OpenCV.<\/p>\n\n\n\n<p>Enter the following command to check the version of OpenCV that can be installed.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ sudo pip3 install opencv-python==<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ sudo pip3 install opencv-contrib-python==<\/code><\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Install with the version specified command.<\/p>\n\n\n\n<p>Here is an example of installing version 3.4.14.51.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ sudo pip3 --default-timeout=1000 install opencv-contrib-python==3.4.14.51<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The latest version of &#8220;opencv-contrib-python&#8221; was 4.5.2.52, but the author was able to install it with version 3.4.14.51.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If this does not work, try uninstalling and then reinstalling OpenCV using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ sudo pip3 uninstall opencv-contrib-python<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h3 class=\"wp-block-heading\">Check that OpenCV is properly installed<\/h3>\n\n\n\n<p>To check that OpenCV is installed properly, run Python and import OpenCV.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Enter the following command to run Python.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ python3<\/code><\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Import OpenCV.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>&gt;&gt;&gt; import cv2<\/code><\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Check the version of OpenCV.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>&gt;&gt;&gt; cv2.__version__<\/code><\/pre>\n\n\n\n<p>If the version is displayed, the installation was successful.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In the author&#8217;s environment, the following error occurred<\/p>\n\n\n\n<p>&#8221; ImportError: numpy.core.multiarray failed to import &#8220;<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is an error that occurs when the version of numpy is old, so update numpy with the following command.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ sudo pip3 install -U numpy<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-matched-content-rows-num=\"4,2\"\n     data-matched-content-columns-num=\"1,4\"\n     data-matched-content-ui-type=\"image_stacked,image_stacked\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"2243394422\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>OpenCV is an image processing library developed by Intel. This article shows you how to install Open &#8230; <\/p>\n","protected":false},"author":1,"featured_media":433,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,7,3],"tags":[20,22,24],"_links":{"self":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/213"}],"collection":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":99,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":432,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/213\/revisions\/432"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/433"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}