opencv cv2.imread()报错: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor‘

Problem:
When using opencv in pycharm, an error is reported when executing cv2.imread(filepath) to read pictures:
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src \color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
insert image description here
Solution:
Try the following methods:
1. Check whether there is Chinese in the picture path, try to Do not include Chinese, if there is any, modify it
2. Change the "\" in the picture path to "/";

# filepath = 'D:\flower\picture\yzm1.bmp' # 这么写会报错
filepath = 'D:/flower/picture/yzm1.bmp'
#  1.图片处理
# 1)先读入图片,并将图片转成灰度图
im = cv2.imread(filepath)
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

3. Check the picture path, not to the folder, but to the specific file name
4. Check whether the file name suffix of the picture is correct (.jpg, .bmp, etc.)

If you still report an error, you can leave a message.

Guess you like

Origin blog.csdn.net/weixin_48415452/article/details/129204528