-1

System information (version)

  • OpenCV => 4.4.0.44
  • Operating System / Platform => Ubuntu 18.04.5
  • Compiler => Visual Studio Code

Detailed description

When I import matplotlib.pyplot and uses cv2.imshow the window will just close on any key I press except for spacebar and Esc. The following code will not run but it returns no error, it simply stop running. It started after I upgrade my opencv from 4.2 to 4.4. I had tried to import matplotlib only and other matplotlib modules and they does not cause any problem.

Matplotlib version 3.3.2

Steps to reproduce

import cv2
import matplotlib.pyplot

cap = cv2.VideoCapture("video/7.mp4")
while True:
    (ret, frame) = cap.read()
    cv2.imshow("Processed Output", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()
cap.release()
print("End")
2
  • 1
    Since you are reading from a video, what happens if frame is empty, e.g. cap.read() fails or at the end of the video? Commented Oct 19, 2020 at 2:04
  • It will work perfectly End will be printed Commented Oct 19, 2020 at 12:14

1 Answer 1

6

I found a solution for it. It appears the problem iswith PyQt5 and OpenCV Qt5 when used together You can read more about the problems here. To solve the problem you need to import matplotlib as below.

import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt
Sign up to request clarification or add additional context in comments.

1 Comment

The following are needed for tkagg backend to work. sudo apt-get install python3-pil python3-pil.imagetk.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.