首页 > 解决方案 > 如何让 OpenCV 在 Raspberry Pi 4 (Raspbian Buster) 上完全运行?

问题描述

我已经尝试了很多天使用 Raspbian Buster 在我的 Raspberry Pi 4 上安装 OpenCV,但我无法完成。在大多数情况下,安装都有效,但在导入或使用 cv2 方法(如 cv2.imshow())时,会出现错误(见下文)。

有人让 OpenCV 在 Raspberry Pi 4 上工作,或者知道如何让它工作吗?预先感谢您的帮助 :-)!


使用 pip 安装 OpenCV 后出错:

pip install opencv-python
pip install opencv-contrib-python

python
>>> import cv2

ImportError: libImath-2_2.so.12: cannot open shared object file: no such file or directory

我无法安装这个库“libImath-2_2.so.12”。错误仍然存​​在。


使用 conda 安装 OpenCV 后出错:

conda install -c conda-forge opencv
conda install -c conda-forge opencv=4.1.0
conda install -c menpo opencv

python
>>> import cv2
>>> img = cv2.imread("image.png", 0)
>>> cv2.imshow("Frame", img)

OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /home/jhelmus/conda/conda-bld/opencv_1512174356192/work/modules/highgui/src/window.cpp, line 605
Traceback (most recent call last):
  File "detect_video.py", line 113, in <module>
    cv2.imshow("Frame", img_main)
cv2.error: /home/jhelmus/conda/conda-bld/opencv_1512174356192/work/modules/highgui/src/window.cpp:605: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage

我检查并已经安装了库 libgtk2.0-dev 和 pkg-config。我不知道如何以使其工作的方式“在函数 cvShowImage 中配置脚本”。


通过编译安装 OpenCV 后出错

我经历了这两个指南中描述的编译过程。这些指南适用于我的 Raspberry Pi 3b,但不适用于我的 Raspberry Pi 4:

  1. https://www.pyimagesearch.com/2018/09/26/install-opencv-4-on-your-raspberry-pi/
  2. https://www.learnopencv.com/install-opencv-4-on-raspberry-pi/

我能够使用 make、sudo make install 等完成编译。之后,尽管我现在仍然必须将 openCV 安装链接到我的 Python 绑定。使用cmake命令我设置以下路径:

我现在如何访问 OpenCV,如何将其链接到 Python,以便将其作为 cv2 导入?

预先感谢您的帮助 :-)!

保罗

标签: pythonopencvraspberry-pi

解决方案


在@Ingo 的帮助下,我终于能够在 Raspbian Buster 上安装 OpenCV,其解决方案比预期的简单得多:

只需运行sudo apt install python3-opencv它就可以工作,还可以使用 cv2.imshow() 创建窗口。


推荐阅读