NSInvalidArgumentException Matplotlib OS X

乔霍华德

在尝试使用 OpenCV 并尝试创建直方图(并使用 Matplotlib 绘制它)时,我遇到了一个无法解决的错误。我已经尝试将 TkAgg 指定为后端无济于事。所有这些都在一个虚拟环境中,运行 Python 3.7 和 openCV4。

相关代码:

import matplotlib
matplotlib.use("TkAgg")

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
        help = "Path to image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])

image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)

hist = cv2.calcHist([image], [0], None, [256], [0,256])

plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
plt.plot(hist)
plt.xlim([0, 256])
plt.show()
cv2.waitKey(0)

错误:

(cv-py3) [joseph:~/Python/OpenCV]$ python grayscale_histogram.py -i image.JPG                                  (master✱) 
2019-01-31 19:52:52.721 python[54162:11266555] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fcb3a47c430
2019-01-31 19:52:52.722 python[54162:11266555] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7fcb3a47c430'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff4ffdd43d __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff7beea720 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff5005a255 -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff4ff7cad0 ___forwarding___ + 1486
    4   CoreFoundation                      0x00007fff4ff7c478 _CF_forwarding_prep_0 + 120
    5   Tk                                  0x00007fff5c2dd777 TkpInit + 467
    6   Tk                                  0x00007fff5c25c2ce Tk_Init + 1697
    7   _tkinter.cpython-37m-darwin.so      0x0000000110376dd9 Tcl_AppInit + 84
    8   _tkinter.cpython-37m-darwin.so      0x0000000110372486 _tkinter_create + 1059
    9   Python                              0x0000000103ca52d1 _PyMethodDef_RawFastCallKeywords + 492
    10  Python                              0x0000000103ca4836 _PyCFunction_FastCallKeywords + 44
    11  Python                              0x0000000103d3f7b4 call_function + 556
    12  Python                              0x0000000103d36de8 _PyEval_EvalFrameDefault + 3662
    13  Python                              0x0000000103d400e4 _PyEval_EvalCodeWithName + 1749
    14  Python                              0x0000000103ca4435 _PyFunction_FastCallDict + 441
    15  Python                              0x0000000103ca55d5 _PyObject_Call_Prepend + 150
    16  Python                              0x0000000103ce4d47 slot_tp_init + 80
    17  Python                              0x0000000103ce180e type_call + 178
    18  Python                              0x0000000103ca468b _PyObject_FastCallKeywords + 381
    19  Python                              0x0000000103d3f818 call_function + 656
    20  Python                              0x0000000103d3791b _PyEval_EvalFrameDefault + 6529
    21  Python                              0x0000000103ca4c24 function_code_fastcall + 117
    22  Python                              0x0000000103d3f81f call_function + 663
    23  Python                              0x0000000103d36de8 _PyEval_EvalFrameDefault + 3662
    24  Python                              0x0000000103d400e4 _PyEval_EvalCodeWithName + 1749
    25  Python                              0x0000000103ca4435 _PyFunction_FastCallDict + 441
    26  Python                              0x0000000103ca55d5 _PyObject_Call_Prepend + 150
    27  Python                              0x0000000103ca494d PyObject_Call + 137
    28  Python                              0x0000000103d37b3d _PyEval_EvalFrameDefault + 7075
    29  Python                              0x0000000103d400e4 _PyEval_EvalCodeWithName + 1749
    30  Python                              0x0000000103ca47fb _PyFunction_FastCallKeywords + 225
    31  Python                              0x0000000103d3f81f call_function + 663
    32  Python                              0x0000000103d36de8 _PyEval_EvalFrameDefault + 3662
    33  Python                              0x0000000103d400e4 _PyEval_EvalCodeWithName + 1749
    34  Python                              0x0000000103d35ef3 PyEval_EvalCode + 57
    35  Python                              0x0000000103d66368 run_mod + 54
    36  Python                              0x0000000103d652b9 PyRun_FileExFlags + 164
    37  Python                              0x0000000103d64900 PyRun_SimpleFileExFlags + 282
    38  Python                              0x0000000103d7c821 pymain_main + 5202
    39  Python                              0x0000000103d7cff1 _Py_UnixMain + 149
    40  libdyld.dylib                       0x00007fff7cfb8085 start + 1
    41  ???                                 0x0000000000000004 0x0 + 4
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我尝试过的事情: - 将 TkAgg 设置为 Matplotlib 的后端,未成功 - 创建新的虚拟环境并重新链接 openCV,未成功

我正在跟进一本书,所以我不相信这是任何类型的语法错误,肯定是配置之一。

编辑:我也尝试在 ~/.matplotlib/matplotlibrc 中指定 TkAgg 作为后端

约翰牛顿

我设法通过将后端更改为 pyQT5 使其在 virtualenv 中运行

安装 matplotlib 和 pyqt 并将渲染后端更新为 qt。将它或 pip 或 pip3 直接符号链接到您的 virtualenv

pip3 install matplotlib
pip3 install PyQt5

要选择后端,你可以试试这个

touch ~/.matplotlib/matplotlibrc
echo "backend: PyQt5" >> ~/.matplotlib/matplotlibrc

或者在您的源代码中始终在任何其他 matplotlib 使用之前添加它

import matplotlib
matplotlib.use("Qt5Agg")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章