2

我试图简单地使用 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 也很陌生!

在此先感谢您的帮助!

4

0 回答 0