MatplotLib (TKinter) + OpenCV crash in Python 3

I am trying to run some Python 3 code that first shows the OpenCV window and when it closes it should draw something using MatplotLib. However, when called, the plt.show()

program crashes: python3.6 exits unexpectedly.

import numpy as np

import matplotlib
matplotlib.use("TkAgg")     # <== Doesn't solve the problem
import matplotlib.pyplot as plt

import cv2
#cv2.ocl.setUseOpenCL(False)  <== Doesn't solve the problem

im = np.zeros((100,100,3))
cv2.imshow('test', im)
cv2.waitKey(0)

plt.plot(np.sin(np.linspace(0,20,200)))
plt.show()                  # <== Crashes on this call

      

In other StackOverflow posts I found that setting up the backend for MatplotLib TkAgg

shouldn't fix this or install setUseOpenCL(false)

. However, both of these potential fixes don't work on my MacBook. I've tried several things like changing the import order while trying to replace the MatplotLib database. Anyone have an idea how to fix this problem?

MacOS Sierra 10.12.3
Python 3.6.0
TKinter Version 8.5
OpenCV Version 3.2.0
MatplotLib Version 2.0.0

Stop code of how it crashes:

libc ++ abi.dylib: exit with an uncaught exception of type NSException

2017-03-28 11: 30: 45.639 python3.6 [5208: 78361] - [NSApplication _setup:]: unrecognized selector posted to instance 0x7f997ec98190 2017-03-28 11: 30: 45.641 python3.6 [5208: 78361] * Application termination due to uncaught exception "NSInvalidArgumentException", reason: '- [NSApplication _setup:]: unrecognized selector posted to instance 0x7f997ec98190' * First throw call stack: (0 CoreFoundation 0x00007fffbeb80e7b exceptionPreobdc.607fffbeb80e7b exceptionPreobdc60xA 1710000 1 objc_exception_throw + 48 2 CoreFoundation 0x00007fffbec02cb4 - [NSObject (NSObject) doesNotRecognizeSelector:] + 132 3 CoreFoundation 0x00007fffbeaf2fb5 ___ forwarding _+ 1061 4 CoreFoundation 0x00007fffbeaf2b08 _CF_forwarding_prep_0 + 120 5 Tk 0x00000001032c45e6 TkpInit + 471 6 Tk 0x000000010323fc8d Tk_Init + 1794 7 _tkinter.cpython 82m-darwarding_prep_0 +so 0x00000001031183a3 _tkinter_create + 1094 9 Python 0x0000000100b28ea2 _PyCFunction_FastCallDict + 172 10 Python 0x0000000100b927a2 call_function + 584 11 Python 0x0000000100b8fbbb _PyEval_EvalFrameDefault + 23930 12 Python 0x0000000100b92fbf _PyEval_EvalCodeWithName + 1973 13 Python 0x0000000100b938c4 _PyFunction_FastCallDict + 477 14 Python 0x0000000100aeea73 _PyObject_FastCallDict + 231 15 Python 0x0000000100aeeb93 _PyObject_Call_Prepend + 149 16 Python 0x0000000100aee8cb PyObject_Call + 102 17 Python 0x0000000100b3cd24 slot_tp_init + 61 18 Python 0x0000000100b39ac3 type_call + 184 19 Python 0x0000000100aeea34 _PyObject_FastCallDict + 168 20 Python 0x0000000100b9263b call_function + 225bFrom9 Python0x0000000_PyFunction_FastCall + 122 23 Python 0x0000000100b92775 call_function + 539 24 Python 0x0000000100b8fbbb _PyEval_EvalFrameDefault + 23930 25 Python 0x0000000100b92fbf _PyEval_EvalCodeWithName + 1973 26 Python 0x0000000100b89e02 PyEval_EvalCodeEx + 100 27 Python 0x0000000100b101e3 function_call + 363 28 Python 0x0000000100aee8cb PyObject_Call + 102 29 Python 0x0000000100b8fe16 _PyEval_EvalFrameDefault + 24533 30 Python 0x0000000100b92fbf _PyEval_EvalCodeWithName + 1973 31 Python 0x0000000100b936da fast_function + 241 32 Python 0x0000000100b92775 call_function + 539 33 Python 0x0000000100b8fbbb _PyEval_EvalFrameDefault + 23930 34 Python 0x0000000100b93976 _PyFunction_FastCall + 122 35 Python 0x0000000100b9270000 +122 35 Python 0x0000000100b927000037 Python 0x0000000100b92fbf _PyEval_EvalCodeWithName + 1973 38 Python 0x0000000100b936da fast_function + 241 39 Python 0x0000000100b92775 call_function + 539 40 Python 0x0000000100b8fbbb _PyEval_EvalFrameDefault + 23930 41 Python 0x0000000100b92fbf _PyEval_EvalCodeWithName + 1973 42 Python 0x0000000100b936da fast_function + 241 43 Python 0x0000000100b92775 call_function + 539 44 Python 0x0000000100b8fbbb _PyEval_EvalFrameDefault + 23930 45 Python 0x0000000100b92fbf _PyEval_EvalCodeWithName + 1973 46 Python 0x0000000100b89d98 PyEval_EvalCode + 100 47 Python 0x0000000100bb33aa run_mod + 58 48 Python 0x0000000100bb36bf PyRun_FileExFlags + 178 49 Python 0x0000000100bb2d58 PyRun_SimpleFileExFlags + 676 50 Python 0x0000000100bc74d0 Py_Main + 3472 51 python3.6 0x0000000100adfe17 python3.6 + 7703 52 libdyld.dylib 0x00007fffd4044255 start + 1 53 ??? 0x0000000000000002 0x0 + 2)

+3


source to share


2 answers


Not sure if this helps you guys / girls, but I ran into similar problems while trying to insert matplotlib into a kivy app (also on Mac), after hours of browsing the web, rewriting classes, deleting and reinstalling and at some point even tried to use a different library entirely. I found the simplest solution was to disable the backend (TkAgg) completely as it doesn't seem to play well and then explicitly announces OSX one

import matplotlib
matplotlib.use("MacOSX")
import matplotlib.pyplot as plt

      



Worked for me, Oksams Shaver and everything I suppose, good luck!

+1


source


Don't know about matplotlib, but I can imagine there might be a window management problem. Try the following:

cv2.imshow('test', im)
cv2.waitKey(1)
cv2.destroyWindow('test')

      



At least this is the correct way to handle already open cv2.imshow () windows when they are no longer in use.

0


source







All Articles