[英]Reading image files in a given directory in Python
I want to convert all images in a folder to gray. 我想将文件夹中的所有图像转换为灰色。 That's my code and I get this error:
那是我的代码,我得到这个错误:
cv2.error: OpenCV(4.1.0) C:\\projects\\opencv-python\\opencv\\modules\\imgproc\\src\\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
cv2.error:OpenCV(4.1.0)C:\\ projects \\ opencv-python \\ opencv \\ modules \\ imgproc \\ src \\ color.cpp:182:错误:(-215:断言失败)!_src.empty()在函数中'CV :: cvtColor'
import cv2
import os
path = r'C:\Users\User\PycharmProjects\computerVision\CarDetection_withOpenCV\p'
for filename in os.listdir(path):
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
How can I fix this? 我怎样才能解决这个问题?
Use full path to file 使用完整路径归档
Ex: 例如:
import cv2
import os
path = r'C:\Users\User\PycharmProjects\computerVision\CarDetection_withOpenCV\p'
for filename in os.listdir(path):
img = cv2.imread(os.path.join(path, filename))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.