cv2.imwrite(os.path.join(gray_train_path, filename), img_train) cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:801: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'
时间: 2023-07-23 18:24:45 浏览: 43
这个错误是因为在使用cv2.imwrite()函数时,_img为空或者没有成功读取图片。你可以尝试检查以下几个方面:
1. 检查图片路径是否正确,是否能够成功读取图片。
2. 检查图片是否为空或者损坏,可以尝试用其他工具打开图片进行查看。
3. 检查cv2库是否正常安装,可以尝试重新安装cv2库。
如果以上几个方面都没有问题,你可以尝试更新OpenCV库或者查找其他解决方案。
相关问题
为什么 cv2.imwrite(os.path.join(gray_test_path, test_filename), gray_img_test)这段代码只保存了一个图片进去
这段代码只保存了一个图片进去的原因可能是因为gray_test_path和test_filename的值没有被正确地设置。假设gray_test_path是保存灰度图像的文件夹路径,test_filename是保存图像的文件名,那么这段代码应该能够成功保存多张图片,只要test_filename在每次循环迭代中被正确地设置为不同的值。
另一个可能的原因是gray_img_test变量只包含了一张灰度图像。如果您想保存多张图像,那么需要在循环迭代中获取每张需要保存的图像并将其保存到对应的文件夹中,而不是只保存gray_img_test这一张图像。
cv2.imwrite(os.path.join(save_path,prefix), out)
This line of code saves an image in the specified file path and with the specified filename prefix.
- `cv2.imwrite` is a function from the OpenCV library that saves an image to a file.
- `os.path.join(save_path,prefix)` joins the save path and filename prefix to create the full file path where the image will be saved.
- `out` is the image data that will be saved.