OpenCV(开源计算机视觉库)是一个强大的开源库,用于计算机视觉、机器学习和图像处理。它在实时操作中发挥着重要作用,这在当今的系统中至关重要。通过使用 OpenCV,可以处理图像和视频来识别物体、人脸甚至人类笔迹。当与各种库(如 Numpy)集成时,Python 可以处理 OpenCV 数组结构进行分析。
在 Linux Mint 上安装 OpenCV 21
步骤 1。在安装任何新软件之前, 最好先更新系统。这可确保您拥有最新的安全补丁和软件更新。使用以下命令更新系统:
<span class="pln">sudo apt update sudo apt upgrade</span>
第 2 步安装 Python 和 PIP。
在安装 OpenCV 之前,请确保系统上预装了 Python 和 PIP。您可以通过打开终端并运行以下命令来检查是否已安装 Python:
<span class="pln">python </span><span class="pun">--</span><span class="pln">version</span>
如果 Python 不存在,则需要安装它。同样,若要检查是否安装了 PIP,请运行以下命令:
<span class="pln">pip3 </span><span class="pun">--</span><span class="pln">version</span>
第 3 步。安装所需的依赖项。
OpenCV 依赖于其他库才能正常运行。安装所需的依赖项:
<span class="pln">sudo apt install libgtk</span><span class="pun">-</span><span class="lit">3</span><span class="pun">-</span><span class="pln">dev libboost</span><span class="pun">-</span><span class="pln">all</span><span class="pun">-</span><span class="pln">dev libatlas</span><span class="pun">-</span><span class="kwd">base</span><span class="pun">-</span><span class="pln">dev libavcodec</span><span class="pun">-</span><span class="pln">dev libavformat</span><span class="pun">-</span><span class="pln">dev libswscale</span><span class="pun">-</span><span class="pln">dev libv4l</span><span class="pun">-</span><span class="pln">dev libxvidcore</span><span class="pun">-</span><span class="pln">dev libx264</span><span class="pun">-</span><span class="pln">dev libjpeg</span><span class="pun">-</span><span class="pln">dev libpng</span><span class="pun">-</span><span class="pln">dev libtiff</span><span class="pun">-</span><span class="pln">dev gfortran openexr libopenexr</span><span class="pun">-</span><span class="pln">dev python3</span><span class="pun">-</span><span class="pln">dev python3</span><span class="pun">-</span><span class="pln">numpy libtbb2 libtbb</span><span class="pun">-</span><span class="pln">dev libdc1394</span><span class="pun">-</span><span class="lit">22</span><span class="pun">-</span><span class="pln">dev</span>
第 4 步。在 Linux Mint 上安装 OpenCV 21.
克隆 OpenCV 和 OpenCV 贡献存储库以获取最新的源文件:
<span class="pln">mkdir </span><span class="pun">~</span><span class="str">/opencv_build && cd ~/</span><span class="pln">opencv_build git clone https</span><span class="pun">:</span><span class="com">//github.com/opencv/opencv.git</span><span class="pln"> git clone https</span><span class="pun">:</span><span class="com">//github.com/opencv/opencv_contrib.git</span>
使用 CMake 构建 OpenCV 并使用 make 编译源代码:
<span class="pln">cd </span><span class="pun">~</span><span class="str">/opencv_build/</span><span class="pln">opencv mkdir build </span><span class="pun">&&</span><span class="pln"> cd build cmake </span><span class="pun">-</span><span class="pln">D CMAKE_BUILD_TYPE</span><span class="pun">=</span><span class="pln">RELEASE \ </span><span class="pun">-</span><span class="pln">D CMAKE_INSTALL_PREFIX</span><span class="pun">=</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pln"> \ </span><span class="pun">-</span><span class="pln">D INSTALL_C_EXAMPLES</span><span class="pun">=</span><span class="pln">ON \ </span><span class="pun">-</span><span class="pln">D INSTALL_PYTHON_EXAMPLES</span><span class="pun">=</span><span class="pln">ON \ </span><span class="pun">-</span><span class="pln">D OPENCV_GENERATE_PKGCONFIG</span><span class="pun">=</span><span class="pln">ON \ </span><span class="pun">-</span><span class="pln">D OPENCV_EXTRA_MODULES_PATH</span><span class="pun">=~</span><span class="str">/opencv_build/</span><span class="pln">opencv_contrib</span><span class="pun">/</span><span class="pln">modules \ </span><span class="pun">-</span><span class="pln">D BUILD_EXAMPLES</span><span class="pun">=</span><span class="pln">ON </span><span class="pun">..</span><span class="pln"> make </span><span class="pun">-</span><span class="pln">j$</span><span class="pun">(</span><span class="pln">nproc</span><span class="pun">)</span><span class="pln"> sudo make install</span>
默认情况下,这会将 OpenCV 安装到 /usr/local/
。
通过在 Python 中导入 cv2 来检查 OpenCV 是否已正确安装:
<span class="pln">python3 </span><span class="pun">-</span><span class="pln">c </span><span class="str">"import cv2; print(cv2.__version__)"</span>
这应该打印出安装的 OpenCV 版本。
您还可以运行 /usr/local/share/opencv4/samples/python
中的一些示例 来验证 OpenCV 是否正常工作。
感谢您使用本教程在 Linux Mint 系统上安装最新版本的 OpenCV。如需更多帮助或有用信息,我们建议您查看 OpenCV 官方网站。