<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="https://rodosingh.github.io/feed.xml" rel="self" type="application/atom+xml"/><link href="https://rodosingh.github.io/" rel="alternate" type="text/html" hreflang="en"/><updated>2025-02-24T09:56:08+00:00</updated><id>https://rodosingh.github.io/feed.xml</id><title type="html">blank</title><subtitle></subtitle><entry><title type="html">Using CMake to build and install OpenCV for Python and C++ in Ubuntu 20.04</title><link href="https://rodosingh.github.io/blog/2022/using-cmake-to-build-and-install-opencv-for-python-and-c-in-ubuntu-2004/" rel="alternate" type="text/html" title="Using CMake to build and install OpenCV for Python and C++ in Ubuntu 20.04"/><published>2022-02-15T14:39:25+00:00</published><updated>2022-02-15T14:39:25+00:00</updated><id>https://rodosingh.github.io/blog/2022/using-cmake-to-build-and-install-opencv-for-python-and-c-in-ubuntu-2004</id><content type="html" xml:base="https://rodosingh.github.io/blog/2022/using-cmake-to-build-and-install-opencv-for-python-and-c-in-ubuntu-2004/"><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*nUb4StrdDlibGaHuSehKWQ.png"/><figcaption>Source: <a href="http://mspoweruser.com/wp-content/uploads/2014/07/CMake-logo.png">http://mspoweruser.com/wp-content/uploads/2014/07/CMake-logo.png</a></figcaption></figure> <p><a href="https://cmake.org/"><strong>CMake</strong></a> is an open-source, cross-platform family of tools designed to build, test, and package software, mainly used to control the software compilation process using simple platform and compiler independent configuration files and generate native makefiles and workspaces that can be used in the compiler environment of your choice.</p> <figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M0QQM3DX5ZfS1MWkIqcBaA.png"/><figcaption>Image Source: <a href="https://opencv.org/wp-content/uploads/2021/02/1_HfZmZayUqnYioPC9qTfd4A.png">https://opencv.org/wp-content/uploads/2021/02/1_HfZmZayUqnYioPC9qTfd4A.png</a></figcaption></figure> <p><a href="https://opencv.org/"><strong>OpenCV</strong></a> (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library for image processing and machine learning tasks that support Tensorflow, Torch/Pytorch, and Caffe. It has C++, Python, Java, and MATLAB interfaces and supports Windows, Linux, Android, and Mac OS. The library is cross-platform, and you can pip install it (where you are using it with Python) with CPU support. Alternatively, where you want to use it with <strong>GPU support</strong>, you can build it from the source, which is more involved. This article is a walk-through on how to build OpenCV from a source (by cloning OpenCV repo) on a <strong>Linux Ubuntu machine</strong>. There are a lot of articles and Q&amp;A, but all they have is scattered information that may not work in general, for which I thought to document this so that we can handle each edge case and try to install it perfectly on your Linux Ubuntu.</p> <p>In this article, we’ll discuss on following points:</p> <ul><li>Install OpenCV from a source within a pythonic environment (e.g., conda environment) using CMake. Also, we’ll show you how to do it in Homefolder (i.e., in root).</li><li>What all essential libraries and packages do we need to pre-install so that OpenCV works for both C++ and Python 3.0 and above.</li><li>And at the end, how to configure if some known errors we face.</li></ul> <p>Since my machine consists of miniconda3, I prefer to build and install my OpenCV in a separate environment with Python3.8 (other than base). Again, it’s not only using <strong>conda</strong>; one can also use the <strong>virtualenv </strong>package to create virtual env. Also, one can install it without requiring any virtual environment (i.e., in the Home directory). Don’t worry; we’ll cover all the cases one by one in each step.</p> <h3><strong>Step 0: Warm-Up</strong></h3> <ul><li>If you want to install it in an environment, then activate that environment first. For conda, write the following in a terminal (if you wish to install miniconda3, see <a href="https://docs.conda.io/projects/conda/en/latest/user-guide/install/linux.html">this</a>):</li></ul> <pre>$ conda activate &lt;your_env_name&gt;</pre> <ul><li>Or if you have your virtual env created in the Home folder then (<a href="https://virtualenv.pypa.io/en/latest/installation.html">Guide for virtualenv package</a>),</li></ul> <pre>$ source &lt;your_env_name&gt;/bin/activate</pre> <ul><li>If you don’t have any specific environment to start with, there is no need to write anything mentioned above.</li><li>Ensure that Python3.x is installed in your system (or your env). For mine, it was OpenCV 4.5.5, which requires a minimal version of Python3.2. Plus, I would suggest going with a conda or virtualenv<strong> </strong>virtual environment. You install your Python3.x there and do the following.</li></ul> <h3>Step 1: Install required libraries and packages</h3> <ul><li>Update and upgrade yourapt followed by installing some required packages like CMake, Git, python3.8-dev (make sure to change it to your required version), python3-numpy (specific to Python3). Here -y<strong> </strong>flag is for ‘yes’ response while installing packages.</li></ul> <pre>$ sudo apt-get update &amp;&amp; sudo apt-get upgrade</pre> <pre>$ sudo apt-get install -y build-essential cmake git unzip pkg-config make</pre> <pre>$ sudo apt-get install -y python3.8-dev python3-numpy libtbb2 libtbb-dev</pre> <ul><li>OpenCV packages that aid in reading images and videos.</li></ul> <pre>$ sudo apt-get install -y  libjpeg-dev libpng-dev libtiff-dev libgtk2.0-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev</pre> <h3>Step 2: Clone official OpenCV and OpenCV_contrib repo</h3> <ul><li>First, create a folder named <strong>opencv_build</strong>, and then clone your required repositories into that folder as mentioned below:</li></ul> <pre>$ mkdir ~/opencv_build &amp;&amp; cd ~/opencv_build<br />$ git clone <a href="https://github.com/opencv/opencv">https://github.com/opencv/opencv</a><br />$ git clone <a href="https://github.com/opencv/opencv_contrib">https://github.com/opencv/opencv_contrib</a><br />$ cd ~/opencv_build/opencv<br />$ mkdir -p build &amp;&amp; cd build</pre> <h3>Step 3: Compile and Install OpenCV with CMake</h3> <ul><li>It’s time to install all the build files of OpenCV into the <strong>build</strong> folder while compiling and installing it using CMake. We should also configure CMake so that it would also install nonfree modules by mentioning the flag value of OPENCV_ENABLE_NONFREE=ON. Before executing the following code, we have two options:<br/>1. CMAKE_INSTALL_PREFIX=/usr/local or <br/>2. CMAKE_INSTALL_PREFIX=$(python3 -c “import sys; print(sys.prefix)”)<br/>Since we intend to make OpenCV compatible with both C++ and Python3.x going with the first option is more agreeable. Because /usr/local contains aninclude folder that aids in the successful execution of#include &lt;package&gt;statements (header statements) in our C++ code. But if we want to have OpenCV installation only for Python3, then going with option 2 is more favorable as we might want our packages to be in the desired environment (if any). Plus, if you want <strong>GPU</strong> support, putWITH_CUDA=ON ;else WITH_CUDA=OFF.</li></ul> <pre>$ cmake -D WITH_CUDA=OFF -D BUILD_TIFF=ON -D BUILD_opencv_java=OFF -D WITH_OPENGL=ON -D WITH_OPENCL=ON -D WITH_IPP=ON -D WITH_TBB=ON -D WITH_EIGEN=ON -D WITH_V4L=ON -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_opencv_python2=OFF -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON3_INCLUDE_DIR=$(python3 -c &quot;from distutils.sysconfig import get_python_inc; print(get_python_inc())&quot;) -D PYTHON3_PACKAGES_PATH=$(python3 -c &quot;from distutils.sysconfig import get_python_lib; print(get_python_lib())&quot;) -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D PYTHON3_EXECUTABLE=$(which python3) -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules -D BUILD_EXAMPLES=ON ..</pre> <ul><li>After the build files were generated, we used maketo install the build files as shown below. This compilation approximately takes 15 to 20 minutes, depending on the number of processors you have in your system. Note thatmake -j&lt;# of cores&gt;. And sudo ldconfig create the necessary links and cache to the most recent shared libraries found in the directories specified.</li></ul> <pre>$ make -j8<br />$ sudo make install<br />$ sudo ldconfig</pre> <h3>Step 4: Verify your installation</h3> <p>To verify the installation for both C++ and Python3, type the following commands:</p> <ul><li>For C++ bindings:</li></ul> <pre>$ pkg-config --modversion opencv4</pre> <pre>Output:<br />4.5.5</pre> <ul><li>Further, we may get errors like <a href="https://stackoverflow.com/questions/63455427/fatal-error-opencv2-opencv-modules-hpp-no-such-file-or-directory-include-ope">fatal error: opencv2/opencv_modules.hpp: No such file or directory #include “opencv2/opencv_modules.hpp</a>&quot;. We need to mention the correct path for OpenCV header files to handle this. Since /usr/local/includehandle all such imports, either we:<br/>1. create a <strong>symlink</strong> in /usr/local/includefor opencv2 located at /usr/local/include/opencv4 by entering the following:<br/>$ sudo ln -s /usr/local/include/opencv4/opencv2 /usr/local/include/opencv2<br/>2. Or simply change your line for header file to:<br/>$ #include &quot;opencv4/opencv2/&lt;your_file_name&gt;.hpp&quot;</li><li>For Python3:<br/>If you <strong>haven’t</strong> installed OpenCV, keeping your virtual environment active, and have used the system’s Python3, you are good to go. Just enter the following:</li></ul> <pre>$ python3 -c &quot;import cv2; print(cv2.__version__)&quot;</pre> <pre>Output:<br />&#39;4.5.5-dev&#39;</pre> <ul><li>Otherwise, we need to link the OpenCV package in cv2folder at/usr/local/lib/python3.8/site-packages/ to site-packagesfolder of our virtual env with a .sofile as shown below (type the following with the required env ON):</li></ul> <pre>$ cd ~/miniconda3/envs/rodo_env/lib/python3.8/site-packages/<br />$ ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-x86_64-linux-gnu.so cv2.so<br />$ python3 -c &quot;import cv2; print(cv2.__version__)&quot;</pre> <pre>Output:<br />&#39;4.5.5-dev&#39;</pre> <h3>Conclusion</h3> <p>We have shown a more involved way to install OpenCV both for C++ and Python3 on your Ubuntu 20.04 machine, which guarantees to run given all the requirements and preferences are satisfied. Even though installing the packaged version from the Ubuntu repository is easier, building OpenCV from the source gives you more flexibility. It should be your first option when installing OpenCV.</p> <p>Here is the bashfile I used to install it one go:</p> <iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca89aa52c40eee2ca4a865f02418c4ce/href">https://medium.com/media/ca89aa52c40eee2ca4a865f02418c4ce/href</a></iframe> <h3>References:</h3> <ul><li><a href="https://stackoverflow.com/questions/50816241/compile-opencv-with-cmake-to-integrate-it-within-a-conda-env">compile-OpenCV-with-CMake-to-integrate-it-within-a-conda-env</a></li><li><a href="https://stackoverflow.com/questions/47315645/unable-to-import-cv2-after-building-from-source">unable-to-import-cv2-after-building-from-source</a></li><li><a href="https://linuxize.com/post/how-to-install-opencv-on-ubuntu-20-04/">How to Install OpenCV on Ubuntu 20.04</a></li><li><a href="https://medium.com/@pokhrelsuruchi/setting-up-opencv-for-python-and-c-in-ubuntu-20-04-6b0331e37437">Setting up OpenCV for Python and C++ in Ubuntu 20.04</a></li></ul> <p>If you have any questions or feedback, feel free to comment below. And if you liked it or it does come out helpful, do give a clap. Till then, Happy Coding.</p> <p><img src="https://medium.com/_/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=6c5881eebd9a" width="1" height="1" alt=""/></p>]]></content><author><name></name></author></entry></feed>