问题
When I want to display an image, the cv2.imshow()
fails randomly.
import cv2
frame = cv2.imread('desk.jpg', cv2.IMREAD_COLOR)
cv2.imshow('test', frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sometimes, I got the first and sometimes the second:
I'm sure that the data is correctly loaded because when I print the frame matrix, everything is ok. I also tried to convert picture to other format but the result is always the same, random.
>>> cv2.__version__
'4.3.0'
# opencv-contrib-python is installed
Kernel informations: Linux 5.3.0-62-generic #56~18.04.1-Ubuntu SMP Wed Jun 24 16:17:03 UTC 2020 GNU/Linux
Edit: As Doch88 said, compiling from source solve my problem. Here my cmake command:
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
回答1:
There is another question with a similar problem, and according to this issue it is a 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.
A way to solve it is probably to build OpenCV from source code.
来源:https://stackoverflow.com/questions/63077771/opencv-imshow-fails-randomly