当前位置:网站首页>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)
边栏推荐
猜你喜欢

【转】什么是etcd

【过一下3】卷积&图像噪音&边缘&纹理

开发一套高容错分布式系统

RL reinforcement learning summary (1)
![[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management](/img/0b/f7d9205c616f7785519cf94853d37d.png)
[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management

Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!

【cesium】元素高亮显示

逆向理论知识4

MySQL Foundation (1) - Basic Cognition and Operation

Geek卸载工具
随机推荐
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
RL reinforcement learning summary (1)
jvm 三 之堆与栈
uboot开启调试打印信息
第5讲 使用pytorch实现线性回归
【过一下16】回顾一下七月
Flutter 父子组件如何都能收到点击事件
Understanding and use of C# on set() and get() methods
LeetCode: 1403. Minimum subsequence in non-increasing order [greedy]
uva1325
Flutter real machine running and simulator running
使用二维码解决固定资产管理的难题
【过一下10】sklearn使用记录
OFDM 十六讲 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
UVA10827
2023 International Conference on Information and Communication Engineering (JCICE 2023)
电话溥功能
coppercam入门手册[6]
[cesium] 3D Tileset model is loaded and associated with the model tree
2022杭电多校第一场01