当前位置:网站首页>Opencv中,imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) 报错:error:!_src.empty() in function ‘cv::cvtColor‘
Opencv中,imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) 报错:error:!_src.empty() in function ‘cv::cvtColor‘
2022-08-05 05:13:00 【吃饱了就很强】
Opencv,imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) 报错:error:!_src.empty() in function ‘cv::cvtColor’
熟练使用Ctrl+C和Ctrl+V大法后
import cv2
path=r"296059.png"
imag=cv2.imdecode(np.fromfile(path),-1)
imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY)
运行报错:cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’
此处:原因有很多种,可能有以下几个原因:
1.图片路径错误,可能图片路径不存在
2.要写图片后缀名
3.图片中有中文路径
如果排除以上原因,可以尝试以下方法:
解决方法1:
图像分为单通道和多通道
单通道图像,直接cv2.imread(path, 0)以单通道模式读。
import cv2
path=r"296059.png"
imag=cv2.imdecode(np.fromfile(path),-1)
imag=cv2.imread(path,0)
解决方法2:
解决方案来源于(-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor‘
#读取图片
import cv2
imag = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), 1)
imag = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), -1) # 读入完整图片,见下面解释
imag = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), 0) # 读成灰度
imag = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), 1) # 读成彩图
在cv2.imdecode中补充:dtype=np.uint8,
如下解决:
import cv2
path=r"296059.png"
imag=cv2.imdecode(np.fromfile(path,dtype=np.uint8),1)
imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY)
边栏推荐
- RL强化学习总结(一)
- Algorithms - ones and zeros (Kotlin)
- jvm 三 之堆与栈
- Requests the library deployment and common function
- Dephi reverse tool Dede exports function name MAP and imports it into IDA
- Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
- redis 缓存清除策略
- Develop a highly fault-tolerant distributed system
- [Decoding tools] Some online tools for Bitcoin
- uboot enable debug printing information
猜你喜欢
随机推荐
"PHP8 Beginner's Guide" A brief introduction to PHP
Excel画图
第四讲 反向传播随笔
Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!
第5讲 使用pytorch实现线性回归
LeetCode: 1403. Minimum subsequence in non-increasing order [greedy]
Wise Force Deleter强制删除工具
Flutter learning - the beginning
一篇博客通关Redis技术栈
【过一下16】回顾一下七月
Judgment statement _switch and case
Analysis of Mvi Architecture
The difference between span tag and p
结构光三维重建(一)条纹结构光三维重建
【过一下12】整整一星期没记录
MySQL基础(一)---基础认知及操作
【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
【cesium】元素高亮显示
入口点注入
类的底层机制