【问题标题】:NSInvalidArgumentException Matplotlib OS XNSInvalidArgumentException Matplotlib OS X
【发布时间】:2019-06-25 13:54:39
【问题描述】:

在尝试使用 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 作为后端

【问题讨论】:

    标签: python macos opencv matplotlib


    【解决方案1】:

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

    【讨论】:

    • 应该是 echo "backend: Qt5Agg" >> ~/.matplotlib/matplotlibrc,但是效果很好!
    【解决方案2】:

    玩了一会儿,我删除了虚拟环境,使用Python3内置的-m venv命令新建了一个,重新链接OpenCV,通过Homebrew安装了pyqt --with-python3,并指定了Matplotlib.use的后端( "MacOSX") 在我的导入行之后。

    【讨论】:

      猜你喜欢
      • 2012-10-17
      • 2013-05-06
      • 2014-03-23
      • 2014-02-12
      • 2012-04-23
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      相关资源
      最近更新 更多