我试图用OpenCV简单地显示图像并使用MatPlotLib绘制图形,不一定同时。相反,我在奇怪的情况下收到一条错误消息,我无法破译。我的代码如下:
# These top two import statements were recommended in a related post,
# but do not fix my issue
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
import cv2
import numpy as np
# Section 1 (showing an image file using cv2):
image = cv2.imread("beach.png")
cv2.imshow("Original", image)
cv2.waitKey(0)
# Section 2 (displaying a simple plt plot):
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
plt.savefig("test.png")
plt.show()
运行此代码时出现的错误是在关闭cv映像后发生的,如下所示(以及弹出的Python已崩溃):
2017-06-23 11:02:12.408 python[38929:17539609] -[NSApplication _setup:]: unrecognized selector sent to instance 0x100674320
2017-06-23 11:02:12.411 python[38929:17539609] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x100674320'
*** First throw call stack:
(
0 CoreFoundation 0x00007fffa9a062cb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffbe81148d objc_exception_throw + 48
2 CoreFoundation 0x00007fffa9a87f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fffa9978755 ___forwarding___ + 1061
4 CoreFoundation 0x00007fffa99782a8 _CF_forwarding_prep_0 + 120
5 Tk 0x0000000104ee0c02 TkpInit + 471
6 Tk 0x0000000104e5c2a9 Tk_Init + 1794
7 _tkinter.cpython-36m-darwin.so 0x0000000104beedfd Tcl_AppInit + 77
8 _tkinter.cpython-36m-darwin.so 0x0000000104bec849 _tkinter_create + 889
9 Python 0x000000010006a688 _PyCFunction_FastCallDict + 568
10 Python 0x00000001000f33e4 call_function + 612
11 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
12 Python 0x00000001000f27a0 _PyEval_EvalCodeWithName + 2720
13 Python 0x00000001000f3636 _PyFunction_FastCallDict + 422
14 Python 0x000000010000e984 _PyObject_FastCallDict + 356
15 Python 0x000000010000eaa0 _PyObject_Call_Prepend + 208
16 Python 0x000000010000e5b3 PyObject_Call + 99
17 Python 0x0000000100089871 slot_tp_init + 81
18 Python 0x0000000100080144 type_call + 212
19 Python 0x000000010000e8d4 _PyObject_FastCallDict + 180
20 Python 0x00000001000f3225 call_function + 165
21 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
22 Python 0x00000001000f312e fast_function + 606
23 Python 0x00000001000f33cb call_function + 587
24 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
25 Python 0x00000001000f27a0 _PyEval_EvalCodeWithName + 2720
26 Python 0x00000001000f28cf PyEval_EvalCodeEx + 95
27 Python 0x000000010004244a function_call + 186
28 Python 0x000000010000e5b3 PyObject_Call + 99
29 Python 0x00000001000f7c31 _PyEval_EvalFrameDefault + 17457
30 Python 0x00000001000f27a0 _PyEval_EvalCodeWithName + 2720
31 Python 0x00000001000f2fab fast_function + 219
32 Python 0x00000001000f33cb call_function + 587
33 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
34 Python 0x00000001000f312e fast_function + 606
35 Python 0x00000001000f33cb call_function + 587
36 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
37 Python 0x00000001000f27a0 _PyEval_EvalCodeWithName + 2720
38 Python 0x00000001000f2fab fast_function + 219
39 Python 0x00000001000f33cb call_function + 587
40 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
41 Python 0x00000001000f27a0 _PyEval_EvalCodeWithName + 2720
42 Python 0x00000001000f2fab fast_function + 219
43 Python 0x00000001000f33cb call_function + 587
44 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
45 Python 0x00000001000f27a0 _PyEval_EvalCodeWithName + 2720
46 Python 0x00000001000f2944 PyEval_EvalCode + 100
47 Python 0x000000010012f21e PyRun_FileExFlags + 206
48 Python 0x000000010012f4bf PyRun_SimpleFileExFlags + 447
49 Python 0x0000000100148ada Py_Main + 3914
50 python 0x0000000100000dfe python + 3582
51 python 0x0000000100000c34 python + 3124
)
libc++abi.dylib: terminating with uncaught exception of type NSException
如果我在代码中注释掉第1部分或第2部分,那么剩余的数字会正确显示。奇怪的是,如果我复制第2节并在第一部分之前再次粘贴它,代码运行正常,我看到所有三个数字。
我已经梳理了大量有类似错误消息的帖子,但他们使用'TkAgg'的常见解决方案并没有解决我的问题。我正在使用MacOS Sierra和OpenCV 3.2.0。我非常感谢任何其他建议,我是OpenCV的新手,对MatPlotLib来说还是新手!
提前感谢您的帮助!