NSInvalidArgumentException Matplotlib OS X [英] NSInvalidArgumentException Matplotlib OS X

查看:87
本文介绍了NSInvalidArgumentException Matplotlib OS X的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While experimenting with OpenCV and trying to create a histogram (and plot it using Matplotlib) I've run into an error that I can't solve. I have already tried specifying TkAgg as the backend to no avail. All of this is in a virtual environment, running Python 3.7 and 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,失败

Things I've tried: - Setting TkAgg as the backend for Matplotlib, unsuccessful - Creating a new virtual environment and re-linking openCV, unsuccessful

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

I'm following along in a book so I don't believe this is any kind of syntax error, surely one of configuration.

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

I have also tried specifying TkAgg as the backend in ~/.matplotlib/matplotlibrc

推荐答案

我设法通过将后端更改为pyQT5使其在virtualenv中得以实现

I managed to get it going in the virtualenv by changing the backend to pyQT5

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

Install matplotlib and pyqt and update the rendering backend to qt. Either symlink it or pip or pip3 directly into your virtualenv

pip3 install matplotlib
pip3 install PyQt5

要选择后端,您可以尝试

To choose the backend you could try this

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

或者在您的源代码中,始终在使用其他matplotlib之前添加此内容

Or in your source always add this before any other matplotlib usage

import matplotlib
matplotlib.use("Qt5Agg")

这篇关于NSInvalidArgumentException Matplotlib OS X的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆