Python 3中同时使用MatplotLib和OpenCV显示图像,发生异常

35 篇文章 1 订阅
13 篇文章 0 订阅

问题

运行如下代码,不能正常执行,并报以下异常。

代码:

from matplotlib import pyplot as plt

BLUE = [255,0,0]
img1 = cv.imread(img_file)
cv.imshow("img",img1)
cv.waitKey(0)
cv.destroyAllWindows()

replicate = cv.copyMakeBorder(img1,100,10,10,10,cv.BORDER_REPLICATE)
reflect = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT)
reflect101 = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT_101)
wrap = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_WRAP)
constant= cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_CONSTANT,value=BLUE)
plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')

plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()

异常日志:

2019-09-21 16:19:31.173 python[23097:6053633] -[QNSApplication _setup:]: unrecognized selector sent to instance 0x7ff0f9087230
2019-09-21 16:19:31.175 python[23097:6053633] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[QNSApplication _setup:]: unrecognized selector sent to instance 0x7ff0f9087230'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff2e82c2fd __exceptionPreprocess + 256
	1   libobjc.A.dylib                     0x00007fff58f00a17 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff2e8a6106 -[NSObject(NSObject) __retain_OA] + 0
	3   CoreFoundation                      0x00007fff2e7ce18f ___forwarding___ + 1485
	4   CoreFoundation                      0x00007fff2e7cdb38 _CF_forwarding_prep_0 + 120
	5   Tk                                  0x00007fff3a5ae7d8 TkpInit + 466
	6   Tk                                  0x00007fff3a52db86 Tk_Init + 1706
	7   _tkinter.cpython-37m-darwin.so      0x000000011b9d8dc4 Tcl_AppInit + 84
	8   _tkinter.cpython-37m-darwin.so      0x000000011b9d3cc8 _tkinter_create + 1080
	9   python                              0x000000010d4fe14e _PyMethodDef_RawFastCallKeywords + 686
	10  python                              0x000000010d4fd51c _PyCFunction_FastCallKeywords + 44
	11  python                              0x000000010d5c866c call_function + 476
	12  python                              0x000000010d5c0f99 _PyEval_EvalFrameDefault + 5065
	13  python                              0x000000010d5c93f1 _PyEval_EvalCodeWithName + 2769
	14  python                              0x000000010d4fcfb2 _PyFunction_FastCallDict + 450
	15  python                              0x000000010d4fe476 _PyObject_Call_Prepend + 150
	16  python                              0x000000010d54f300 slot_tp_init + 176
	17  python                              0x000000010d54acb9 type_call + 297
	18  python                              0x000000010d4fd33f _PyObject_FastCallKeywords + 559
	19  python                              0x000000010d5c86d4 call_function + 580
	20  python                              0x000000010d5c10c3 _PyEval_EvalFrameDefault + 5363
	21  python                              0x000000010d4fd9e2 function_code_fastcall + 210
	22  python                              0x000000010d5c86db call_function + 587
	23  python                              0x000000010d5c0f99 _PyEval_EvalFrameDefault + 5065
	24  python                              0x000000010d5c93f1 _PyEval_EvalCodeWithName + 2769
	25  python                              0x000000010d4fcfb2 _PyFunction_FastCallDict + 450
	26  python                              0x000000010d4fe476 _PyObject_Call_Prepend + 150
	27  python                              0x000000010d4fd638 PyObject_Call + 136
	28  python                              0x000000010d5c1341 _PyEval_EvalFrameDefault + 6001
	29  python                              0x000000010d5c93f1 _PyEval_EvalCodeWithName + 2769
	30  python                              0x000000010d4fd4d6 _PyFunction_FastCallKeywords + 230
	31  python                              0x000000010d5c86db call_function + 587
	32  python                              0x000000010d5c102a _PyEval_EvalFrameDefault + 5210
	33  python                              0x000000010d4fd9e2 function_code_fastcall + 210
	34  python                              0x000000010d5c86db call_function + 587
	35  python                              0x000000010d5c102a _PyEval_EvalFrameDefault + 5210
	36  python                              0x000000010d5c93f1 _PyEval_EvalCodeWithName + 2769
	37  python                              0x000000010d4fd4d6 _PyFunction_FastCallKeywords + 230
	38  python                              0x000000010d5c86db call_function + 587
	39  python                              0x000000010d5c0f99 _PyEval_EvalFrameDefault + 5065
	40  python                              0x000000010d5c93f1 _PyEval_EvalCodeWithName + 2769
	41  python                              0x000000010d5bfb30 PyEval_EvalCode + 48
	42  python                              0x000000010d5fbe09 PyRun_FileExFlags + 185
	43  python                              0x000000010d5fb42d PyRun_SimpleFileExFlags + 285
	44  python                              0x000000010d619907 pymain_main + 6951
	45  python                              0x000000010d61a2e0 _Py_UnixMain + 128
	46  libdyld.dylib                       0x00007fff5a6ce3d5 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

原因分析

同时使用matplotlib和OpenCV显示图像时,会发生错误,具体什么原因还不清楚。可以通过修改matplotlib的backend解决该问题

matplotlib默认使用的backend是:TkAgg
可以通过:import matplotlib;print(matplotlib.get_backend())查看当前使用的backend是什么。

解决方法

我是在Mac环境下执行,所以使用MacOSX作为backend饥渴解决该问题

import matplotlib
print(matplotlib.get_backend())
matplotlib.use("MacOSX")

参考:MatplotLib (TKinter) + OpenCV crashing in Python 3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值