闲话:
opencv是个非常工科男思路的包,不是那种完美的包,体现在几个点上:
1.opencv的接口没有文件夹自动创建功能,当你准备输出结果的时候,目标路径却没有,
也不报错,而你却看不到保存的图片,这很麻烦。
2.opencv始终不集成图片中文路径识别不了的问题,用cv2.imread会有报错。
本来都挺容易解决的,可能涉及到大的架构,所以官方一直不改。可能他们兴趣点不在这上面。
闲话休提,怎么解决,在这:
#英文路径读取图片代码
dirPath= "xxxxx" for file in os.listdir(dirPath): if file=="t1.png": img = cv2.imread(dirPath + "/"+file) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#中文路径读取图片代码
dirPath= "xxxxx" for file in os.listdir(dirPath): if file!="t1.png": img = cv2.imdecode(np.fromfile(dirPath + "/"+file, dtype=np.uint8), flags=cv2.IMREAD_COLOR) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)