opencv-python imshow()时报错

本文详细记录了解决因安装两个不同版本的Qt而导致OpenCV无法正常显示图片的问题过程。通过使用brewlist命令发现并确认了冲突的存在,最终通过卸载其中一个Qt版本成功解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)
objc[25692]: Class QCocoaColorPanelDelegate is implemented in both /usr/local/lib/python2.7/site-packages/cv2/.dylibs/QtGui (0x104bd0d60) and /usr/local/Cellar/qt@4/4.8.7_5/lib/QtGui.framework/Versions/4/QtGui (0x10ec95130). One of the two will be used. Which one is undefined.
objc[25692]: Class QMacSoundDelegate is implemented in both /usr/local/lib/python2.7/site-packages/cv2/.dylibs/QtGui (0x104bd0db0) and /usr/local/Cellar/qt@4/4.8.7_5/lib/QtGui.framework/Versions/4/QtGui (0x10ec95180). One of the two will be used. Which one is undefined.
objc[25692]: Class QCocoaPanel is implemented in both /usr/local/lib/python2.7/site-packages/cv2/.dylibs/QtGui (0x104bd0e00) and /usr/local/Cellar/qt@4/4.8.7_5/lib/QtGui.framework/Versions/4/QtGui (0x10ec951d0). One of the two will be used. Which one is undefined.
objc[25692]: Class QCocoaView is implemented in both /usr/local/lib/python2.7/site-packages/cv2/.dylibs/QtGui (0x104bd0e50) and /usr/local/Cellar/qt@4/4.8.7_5/lib/QtGui.framework/Versions/4/QtGui (0x10ec95220). One of the two will be used. Which one is undefined.
objc[25692]: Class QCocoaWindow is implemented in both /usr/local/lib/python2.7/site-packages/cv2/.dylibs/QtGui (0x104bd0ea0) and /usr/local/Cellar/qt@4/4.8.7_5/lib/QtGui.framework/Versions/4/QtGui (0x10ec95270). One of the two will be used. Which one is undefined.
objc[25692]: Class QCocoaWindowDelegate is implemented in both /usr/local/lib/python2.7/site-packages/cv2/.dylibs/QtGui (0x104bd0ef0) and /usr/local/Cellar/qt@4/4.8.7_5/lib/QtGui.framework/Versions/4/QtGui (0x10ec952c0). One of the two will be used. Which one is undefined.
objc[25692]: Class QCocoaMenuLoader is implemented in both /usr/local/lib/python2.7/site-packages/cv2/.dylibs/QtGui (0x104bd0f40) and /usr/local/Cellar/qt@4/4.8.7_5/lib/QtGui.framework/Versions/4/QtGui (0x10ec95310). One of the two will be used. Which one is undefined.
objc[25692]: Class QNSApplication is implemented in both /usr/local/lib/python2.7/site-packages/cv2/.dylibs/QtGui (0x104bd0f90) and /usr/local/Cellar/qt@4/4.8.7_5/lib/QtGui.framework/Versions/4/QtGui (0x10ec95360). One of the two will be used. Which one is undefined.
objc[25692]: Class QCocoaApplicationDelegate is implemented in both /usr/local/lib/python2.7/site-packages/cv2/.dylibs/QtGui (0x104bd0fe0) and /usr/local/Cellar/qt@4/4.8.7_5/lib/QtGui.framework/Versions/4/QtGui (0x10ec953b0). One of the two will be used. Which one is undefined.
objc[25692]: Class QCocoaWindowCustomThemeFrame is implemented in both /usr/local/lib/python2.7/site-packages/cv2/.dylibs/QtGui (0x104bd1030) and /usr/local/Cellar/qt@4/4.8.7_5/lib/QtGui.framework/Versions/4/QtGui (0x10ec95400). One of the two will be used. Which one is undefined.
鼓捣电脑不知道装了什么东西,利用opencv无法正常显示图片,差一点就打算重做系统了,最后抱着试一试的态度没想到解决了。
根据提示,大致是和Qt相关的,网上有说是装了两个qt版本导致出现冲突,于是就是用brew list 查了一下

localhost:PycharmProjects han$ brew list
adwaita-icon-theme		libpeas
aspell				libpng
atk				librsvg
autoconf			libtiff
automake			libtool
automoc4			libunistring
boost				libvorbis
cairo				libvpx
cmake				libxml2
doxygen				mpfr
eigen				mysql
enchant				mysql-connector-c
erlang				numpy
ffmpeg				open-scene-graph
fontconfig			openblas
freetype			openexr
fribidi				openssl
gcc				opus
gd				pango
gdbm				pcre
gdk-pixbuf			pixman
gedit				pkg-config
gettext				py2cairo
glib				pygobject
gmp				pygtk
gobject-introspection		pyqt
graphite2			pyqt@4
graphviz			python
gsettings-desktop-schemas	python3
gspell				python@2
gtk+				qt
gtk+3				qt-webkit@2.3
gtk-mac-integration		qt@4
gtkglext			rabbitmq
gtksourceview3			readline
harfbuzz			rename
hicolor-icon-theme		sdl
hiredis				sdl2
icu4c				sip
ilmbase				snappy
inetutils			sqlite
isl				tbb
iso-codes			telnet
jpeg				theora
lame				tmux
libcroco			vala
libepoxy			webp
libevent			wget
libffi				wxmac
libidn				x264
libidn2				x265
libmpc				xvid
libogg				xz

果真是装了两个Qt,于是卸载其中一个

localhost:PycharmProjects han$ brew uninstall qt@4

这样执行会有个报错

Error: Refusing to uninstall /usr/local/Cellar/qt@4/4.8.7_5
because it is required by automoc4, pyqt@4 and qt-webkit@2.3, which are currently installed.
You can override this and force removal with:
  brew uninstall --ignore-dependencies qt@4
localhost:PycharmProjects han$ brew uninstal --ignore-dependences qt@4
Usage: brew uninstall, rm, remove [options] formula

不要气馁,根据提示执行就Ok了。

### 解决方案 当在使用 `cv2.cvtColor` 函数遇到 `_src.empty()` 断言失败的错误,通常是因为输入图像为空。以下是可能的原因以及对应的解决方案: #### 原因分析 1. **文件路径不正确** 如果指定的图片路径不存在或者拼写错误,则 `cv2.imread` 返回的结果会是一个空对象 (`None`)。这会导致后续调用 `cv2.cvtColor` 报错[^2]。 2. **路径中的字符编码问题** 路径中如果包含中文或其他特殊字符,可能会导致无法正常加载图片。建议避免使用含有中文或特殊字符的路径[^2]。 3. **路径分隔符问题** Windows 系统默认使用的反斜杠 `\` 可能会被解释为转义字符。因此,在 Python 中应使用正斜杠 `/` 或双反斜杠 `\\` 来表示路径[^2]。 4. **文件损坏或缺失** 即使路径正确,但如果目标文件已损坏或被删除,也会返回一个空的对象。 --- #### 实现代码示例 以下提供了一个完整的实现方法来处理该问题并防止程序崩溃: ```python import cv2 try: # 使用绝对路径读取图像 image_path = 'path/to/image.jpg' # 替换为实际路径 # 加载图像 image = cv2.imread(image_path) # 检查图像是否成功加载 if image is None: raise ValueError(f"Failed to load the image from {image_path}. Please check the file path and ensure it exists.") # 将BGR格式转换为灰度图 gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 显示原图和灰度图 cv2.imshow('Original Image', image) cv2.imshow('Gray Image', gray_image) # 等待按键关闭窗口 cv2.waitKey(0) cv2.destroyAllWindows() except Exception as e: print("Error:", str(e)) ``` --- #### 关键点说明 1. **检查图像是否为空** 在执行任何操作之前,先验证 `cv2.imread` 的返回值是否为 `None`。如果是 `None`,则表明未能成功加载图像[^3]。 2. **调试路径问题** 打印出当前尝试访问的路径,确认其是否存在语法错误或指向无效位置[^2]。 3. **异常捕获机制** 利用 `try-except` 结构可以有效捕捉潜在的运行错误,并给出更友好的提示信息[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值