Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenCV-Python does not work with matplotlib.pyplot #404

Closed
4 tasks done
lewjiayi opened this issue Oct 19, 2020 · 2 comments
Closed
4 tasks done

OpenCV-Python does not work with matplotlib.pyplot #404

lewjiayi opened this issue Oct 19, 2020 · 2 comments

Comments

@lewjiayi
Copy link

lewjiayi commented Oct 19, 2020

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()
        if not ret:
                break
	cv2.imshow("Processed Output", frame)
	if cv2.waitKey(1) & 0xFF == ord('q'):
		break
cv2.destroyAllWindows()
cap.release()
print("End")

When the video ends byitself, End will be printed. When I press any key excpet spacebar and Esc while video playing, End will not be printed. I had tried with other code replacing print("End") and they will not work as well. So I pretty sure it crashes.

Issue submission checklist
  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues,
    answers.opencv.org, Stack Overflow, etc and have not found solution
  • I updated to latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc
@skvark
Copy link
Member

skvark commented Oct 19, 2020

Probably a duplicate of this: #386

Try matplotlib.use('tkagg'). If that solves the issue, then the problem is the same as in the linked issue (PyQt5 and OpenCV Qt5 have issues when used together).

@lewjiayi
Copy link
Author

Probably a duplicate of this: #386

Try matplotlib.use('tkagg'). If that solves the issue, then the problem is the same as in the linked issue (PyQt5 and OpenCV Qt5 have issues when used together).

It works! Thank you!

For future reference, your code must follow the sequnce as below in order to solve the issue.

import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants