当前位置:网站首页>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)
边栏推荐
- Returned object not currently part of this pool
- Wise Force Deleter强制删除工具
- Requests库部署与常用函数讲解
- How can Flutter parent and child components receive click events
- [WeChat applet] WXML template syntax - conditional rendering
- Algorithms - ones and zeros (Kotlin)
- LAB 信号量实现细节
- 【转】什么是etcd
- 【过一下12】整整一星期没记录
- OFDM Lecture 16 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
猜你喜欢

Develop a highly fault-tolerant distributed system

u-boot调试定位手段

Flutter real machine running and simulator running

Detailed Explanation of Redis Sentinel Mode Configuration File

服务器磁盘阵列

【过一下4】09-10_经典网络解析

MySQL Foundation (1) - Basic Cognition and Operation

"Recursion" recursion concept and typical examples

基于Web的商城后台管理系统的设计与实现

Flutter 父子组件如何都能收到点击事件
随机推荐
UVA10827
【学生毕业设计】基于web学生信息管理系统网站的设计与实现(13个页面)
【转】什么是etcd
2023 International Conference on Information and Communication Engineering (JCICE 2023)
1068 Find More Coins
What field type of MySQL database table has the largest storage length?
ESP32 485 Illuminance
[Software Exam System Architect] Software Architecture Design ③ Domain-Specific Software Architecture (DSSA)
Excel Paint
Flutter学习5-集成-打包-发布
LeetCode: 1403. Minimum subsequence in non-increasing order [greedy]
for..in和for..of的区别
延迟加载js方式async与defer区别
Flutter TapGestureRecognizer 如何工作
分布式和集群
Redis - 13. Development Specifications
社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力
ESP32 485光照度
server disk array
使用二维码解决固定资产管理的难题