openCV imshow 随机失败

openCV imshow fails randomly

当我想显示图像时,cv2.imshow()随机失败。

import cv2

frame = cv2.imread('desk.jpg', cv2.IMREAD_COLOR)

cv2.imshow('test', frame)
cv2.waitKey(0)
cv2.destroyAllWindows()

有时候,我得到第一个,有时候得到第二个:

我确定数据已正确加载,因为当我打印帧矩阵时,一切正常。我也尝试过将图片转换成其他格式,但结果总是一样的,随机的。

>>> cv2.__version__
'4.3.0'
# opencv-contrib-python is installed

内核信息:Linux 5.3.0-62-generic #56~18.04.1-Ubuntu SMP Wed Jun 24 16:17:03 UTC 2020 GNU/Linux

编辑:正如Doch88所说,从源代码编译解决了我的问题。这是我的 cmake 命令:

mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_GENERATE_PKGCONFIG=ON \
      -D OPENCV_ENABLE_NONFREE=ON \
      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_src/opencv_contrib/modules ../opencv

还有一个question with a similar problem, and according to this issue是个bug:

This issue cannot be fixed in OpenCV, the issue is related to Qt conflicts between the bundled Qt4 in the Python wheels and system Qt4/5 installations.

解决它的一种方法可能是从源代码构建 OpenCV。