简体   繁体   中英

NSInvalidArgumentException Matplotlib OS X

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.

Relevant code:

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)

Error:

(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

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.

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

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

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

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

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

import matplotlib
matplotlib.use("Qt5Agg")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM