当前位置:网站首页>In Opencv, imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) error: error:!_src.empty() in function 'cv::cvtColor'
In Opencv, imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) error: error:!_src.empty() in function 'cv::cvtColor'
2022-08-05 05:24:00 【When you are full, you will be strong】
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.图片路径错误,Maybe the image path does not exist
2.To write image suffix
3.There is a Chinese path in the picture
If the above reasons are excluded,可以尝试以下方法:
解决方法1:
Images are divided into single-channel and multi-channel
单通道图像,直接cv2.imread(path, 0)Read in single channel mode.
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)
边栏推荐
猜你喜欢
Using QR codes to solve fixed asset management challenges
coppercam入门手册[6]
ESP32 485 Illuminance
2022 Hangzhou Electric Multi-School 1st Session 01
OFDM 十六讲 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
Flutter learning 5-integration-packaging-publish
Database experiment five backup and recovery
DOM and its applications
【过一下 17】pytorch 改写 keras
vscode+pytorch use experience record (personal record + irregular update)
随机推荐
uva1325
Flutter learning 2-dart learning
span标签和p标签的区别
【过一下14】自习室的一天
Analysis of Mvi Architecture
LeetCode: 1403. Minimum subsequence in non-increasing order [greedy]
coppercam入门手册[6]
How can Flutter parent and child components receive click events
The role of DataContext in WPF
【过一下9】卷积
Requests the library deployment and common function
2022 The 4th C.Easy Counting Problem (EGF+NTT)
Basic properties of binary tree + oj problem analysis
Qt制作18帧丘比特表白意中人、是你的丘比特嘛!!!
[Go through 3] Convolution & Image Noise & Edge & Texture
1.3 mysql批量插入数据
DOM and its applications
Flex layout frog game clearance strategy
RL强化学习总结(一)
UVA10827