Well, all the above answers are great. But I needed to do some additional tweaking to make it run. I believe an end-to-end description of which worked for me will be helpful.
- Clone/Download
opencv
and opencv-contrib
. Contrib contains additional functionality you most likely want. Make sure both of the versions are the same. In my case:
git clone https://github.com/opencv/opencv.git -b 4.8.0 --single-branch
git clone https://github.com/opencv/opencv_contrib.git -b 4.8.0 --single-branch
Change the version pointer from 4.8.0 to your desired version according to your need. Keep these two clones inside the same parent directory.
- Go inside of
opencv
folder and make a folder called build
and go inside of build
:
cd opencv && mkdir build && cd build
- Open up the terminal and activate your desired Anaconda environment. Activating Anaconda might not be required, but I did this anyway.
- Enter the following in the terminal:
CONDA_PREFIX=~/anaconda3/envs/YOUR_ENV_NAME
PYTHON_VERSION=3.11
Here use your anaconda environment name instead.
export CPLUS_INCLUDE_PATH=$CONDA_PREFIX/lib/python$PYTHON_VERSION
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=$CONDA_PREFIX/ \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D PYTHON3_LIBRARY=$CONDA_PREFIX/lib/libpython$PYTHON_VERSION \
-D PYTHON3_INCLUDE_DIR=$CONDA_PREFIX/include/python$PYTHON_VERSION \
-D PYTHON3_EXECUTABLE=$CONDA_PREFIX/bin/python$PYTHON_VERSION \
-D PYTHON3_PACKAGES_PATH=$CONDA_PREFIX/lib/python$PYTHON_VERSION/site-packages \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_EXAMPLES=ON ..
Here I am assuming you are only interested to install opencv
for Python3 and not for Python2
.
After building is done, now make it. -j$(nproc)
ensures we are using all CPU cores to make the process faster:
make -j$(nproc)
sudo make install
- After installation is done, inside your anaconda environment, open Python shell and try to import
cv2
. You may get the following similar to it:
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/hafiz031/anaconda3/envs/CV_mod/lib/python3.11/site-packages/cv2/__init__.py", line 181, in <module>
bootstrap()
File "/home/hafiz031/anaconda3/envs/CV_mod/lib/python3.11/site-packages/cv2/__init__.py", line 153, in bootstrap
native_module = importlib.import_module("cv2")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/hafiz031/anaconda3/envs/CV_mod/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: /home/hafiz031/anaconda3/envs/CV_mod/bin/../lib/libstdc++.so.6: version `GLIBCXX_3.4.30' not found (required by /home/hafiz031/anaconda3/envs/CV_mod/lib/libopencv_gapi.so.408)
- If so, then a little bit of work is yet to be done:
conda install -c conda-forge gcc=12.1.0
- Now let's try one more time:
>>> import cv2
>>> cv2.__version__
'4.8.0'
If so, then you are done with your installation .