问题:
在python3中使用import cv2后使用cv2.imshow() 报错(python3:6706): Gtk-CRITICAL **: IA__gtk_widget_style_get: assertion ‘GTK_IS_WIDGET (widget)’ failed
分析:
1. 通过cv2.__version__打印发现python3中导入的cv2是4.3.0
2. 通过pkg-config --modversion opencv,输出opencv 版本是3.4.8
因此是python3中与本机opencv版本不对问题,通过conda list 查看conda 安装的软件,发现opencv-python是4.3.0版本,所以需要安装与opencv3.4.8相对应的opencv-python.
解决:
1. pkg-config --modversion opencv 查询opencv版本号,例如:‘3.4.8’
2. 在https://pypi.org/project/opencv-python/#history查询对应的版本号并安装,或输入
pip3 install opencv-python==3.4.8
$ pip3 install opencv-python==3.4.8
ERROR: Could not find a version that satisfies the requirement opencv-python==3.4.8 (from versions: 3.4.8.29, 3.4.9.31, 3.4.9.33, 3.4.10.35, 3.4.10.37, 3.4.11.39, 3.4.11.41, 3.4.11.43, 3.4.11.45, 4.1.2.30, 4.2.0.32, 4.2.0.34, 4.3.0.36, 4.3.0.38, 4.4.0.40, 4.4.0.42, 4.4.0.44, 4.4.0.46)
ERROR: No matching distribution found for opencv-python==3.4.8
根据提示安装对应版本号:
pip3 install opencv-python==3.4.8.29
指定源快速安装:
pip3 install --default-timeout=1000 --upgrade opencv-python==3.4.8.29 -i https://pypi.mirrors.ustc.edu.cn/simple --user
$ pip3 install --default-timeout=1000 --upgrade opencv-python==3.4.8.29 -i https://pypi.mirrors.ustc.edu.cn/simple --user
Looking in indexes: https://pypi.mirrors.ustc.edu.cn/simple
Collecting opencv-python==3.4.8.29
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/bd/95/f06beabb5e1fa319f02278004efce19e7ebe7a67e69dd7d1820d64b2dab8/opencv_python-3.4.8.29-cp38-cp38-manylinux1_x86_64.whl (28.3 MB)
|████████████████████████████████| 28.3 MB 469 kB/s
Requirement already satisfied, skipping upgrade: numpy>=1.17.3 in ./anaconda3/lib/python3.8/site-packages (from opencv-python==3.4.8.29) (1.18.5)
Installing collected packages: opencv-python
Successfully installed opencv-python-3.4.8.29
3. 验证imshow()不再报错。