OpenCV 是一个开源计算机视觉和机器学习软件库,因其广泛的图像处理和计算机视觉功能而广受认可。从面部识别到对象跟踪,OpenCV 为开发尖端应用程序奠定了坚实的基础。但是,要充分利用 OpenCV 的功能,您需要在 Fedora 39 系统上正确安装它。在本指南中,我们将带您完成整个过程,确保您可以将 OpenCV 无缝集成到您的 Linux 环境中。
在 Fedora 上安装 OpenCV 39
步骤 1。首先,更新系统的软件包存储库并升级现有软件包。这可确保您拥有最新的软件包和依赖项。
<span class="pln">sudo dnf clean all sudo dnf update</span>
第2步。安装基本软件包和开发工具。
您需要一组软件包和开发工具才能成功编译和构建 OpenCV。使用以下命令安装它们:
<span class="pln">sudo dnf install cmake gcc</span><span class="pun">-</span><span class="pln">c</span><span class="pun">++</span><span class="pln"> python3 python3</span><span class="pun">-</span><span class="pln">pip numpy gtk2</span><span class="pun">-</span><span class="pln">devel libdc1394</span><span class="pun">-</span><span class="pln">devel libv4l</span><span class="pun">-</span><span class="pln">devel gstreamer</span><span class="pun">-</span><span class="pln">plugins</span><span class="pun">-</span><span class="kwd">base</span><span class="pun">-</span><span class="pln">devel libxine2</span><span class="pun">-</span><span class="pln">devel libvpx</span><span class="pun">-</span><span class="pln">devel libavcodec</span><span class="pun">-</span><span class="pln">devel libavformat</span><span class="pun">-</span><span class="pln">devel libswscale</span><span class="pun">-</span><span class="pln">devel libv4l</span><span class="pun">-</span><span class="pln">devel libxvidcore</span><span class="pun">-</span><span class="pln">devel libx264</span><span class="pun">-</span><span class="pln">devel tbb</span><span class="pun">-</span><span class="pln">devel eigen3</span><span class="pun">-</span><span class="pln">devel openexr</span><span class="pun">-</span><span class="pln">devel boost</span><span class="pun">-</span><span class="pln">python3</span><span class="pun">-</span><span class="pln">devel</span>
这些软件包提供了必要的工具和库来支持 OpenCV 的特性和功能。
第 3 步。设置 Python 虚拟环境。
创建 Python 虚拟环境是一种最佳实践,因为它将 OpenCV 安装与系统的默认 Python 设置隔离开来。这有助于防止冲突并确保干净的安装。
为您的项目创建一个目录,并在该目录中创建一个虚拟环境。将 ” 替换为首选环境名称:my_opencv_env
<span class="pln">mkdir my_opencv_project cd my_opencv_project python3 </span><span class="pun">-</span><span class="pln">m venv my_opencv_env</span>
若要激活虚拟环境,请使用以下命令:
<span class="pln">source my_opencv_env</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">activate</span>
您应该会在终端提示符中看到环境名称,表明您正在虚拟环境中工作。
第 4 步。在 Fedora 上安装 OpenCV 39.
现在您的系统已经准备好了,是时候下载 OpenCV 源代码了,使用 CMake 配置构建,并编译 OpenCV 库。转到要存储 OpenCV 源代码的目录,并使用 Git 克隆存储库:
<span class="pln">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>
这些存储库包含核心 OpenCV 代码和扩展其功能的附加模块。
在 OpenCV 源代码目录中创建一个 ‘build’ 目录:
<span class="pln">mkdir </span><span class="pun">-</span><span class="pln">p </span><span class="pun">~</span><span class="str">/my_opencv_project/</span><span class="pln">opencv</span><span class="pun">/</span><span class="pln">build cd </span><span class="pun">~</span><span class="str">/my_opencv_project/</span><span class="pln">opencv</span><span class="pun">/</span><span class="pln">build</span>
现在,使用 CMake 配置生成。此命令可能会根据您的具体要求而有所不同,因此请务必进行相应调整。
<span class="pln">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">/my_opencv_project/</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>
这些设置可确保 OpenCV 是使用示例和 Python 支持构建的。应该指向 opencv_contrib 存储库中的“modules”目录。OPENCV_EXTRA_MODULES_PATH
配置完成后,编译并安装 OpenCV:
<span class="pln">make </span><span class="pun">-</span><span class="pln">j4 sudo make install</span>
第 5 步。验证安装。
现在 OpenCV 已经安装完毕,验证安装以确保一切正常运行至关重要。
让我们通过运行一个简单的 Python 脚本来测试您的 OpenCV 安装。创建一个 Python 脚本,例如 ‘,’ 包含以下内容:opencv_test.py
<span class="kwd">import</span><span class="pln"> cv2 </span><span class="kwd">print</span><span class="pun">(</span><span class="pln">cv2</span><span class="pun">.</span><span class="pln">__version__</span><span class="pun">)</span>
保存脚本并运行它:
<span class="pln">python opencv_test</span><span class="pun">.</span><span class="pln">py</span>
这应该打印您安装的 OpenCV 版本。如果脚本运行没有错误,则安装成功。
要检查已安装的 OpenCV 版本和可用功能,您可以使用以下 Python 代码:
<span class="kwd">import</span><span class="pln"> cv2 </span><span class="kwd">print</span><span class="pun">(</span><span class="str">"OpenCV Version:"</span><span class="pun">,</span><span class="pln"> cv2</span><span class="pun">.</span><span class="pln">__version__</span><span class="pun">)</span> <span class="kwd">print</span><span class="pun">(</span><span class="str">"Available OpenCV functionalities:"</span><span class="pun">)</span> <span class="kwd">print</span><span class="pun">(</span><span class="pln">cv2</span><span class="pun">.</span><span class="pln">getBuildInformation</span><span class="pun">())</span>
运行此脚本将提供有关 OpenCV 安装的详细信息。
感谢您使用本教程在您的 Fedora 39 系统上安装 OpenCV。有关其他 Apache 或有用信息,我们建议您查看 OpenCV 官方网站。