NSInvalidArgumentException Matplotlib OS X

时间:2019-02-01 02:00:30

标签: python macos opencv matplotlib

尝试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,失败

我会继续学习,因此我不认为这是任何一种语法错误,肯定是配置错误之一。

编辑:我也尝试过将TkAgg指定为〜/ .matplotlib / matplotlibrc

中的后端。

2 个答案:

答案 0 :(得分:0)

经过一番测试后,我删除了虚拟环境,并使用Python3内置的-m venv命令创建了一个新环境,重新链接了OpenCV,通过Homebrew安装了pyqt --with-python3,并指定了Matplotlib.use( “ MacOSX”)导入到我的行之后。

答案 1 :(得分:0)

通过将后端更改为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")