python - OpenCV(4.1.2)错误!_src.empty()在函数'cvtColor'中
问题描述
每个人。我正在尝试通过 Google Colab 运行 Unet 脚本进行培训。但我对 cv2 函数有一个错误。这是发生错误的代码部分:
def prepare_data(img_shape, ids, reader):
img_count = len(ids)
x_data = np.empty((img_count,) + img_shape,
dtype='uint8')
y_data = np.empty((img_count, img_shape[0], img_shape[1], 1),
dtype='uint8')
for i, idx in enumerate(ids):
path = reader.get_image_path_by_id(idx)
img = cv2.imread(path, cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, dsize=(img_shape[1], img_shape[0]))
mask = []
错误消息如下:
Traceback (most recent call last):
File "/content/gdrive/My Drive/Unet/unet/train_unet.py", line 247, in <module> train(train_ids=fold4_train, val_ids=fold4_test, fold_num=fold_num, restart=args.restart)
File "/content/gdrive/My Drive/Unet/unet/train_unet.py", line 162, in train train_data, train_masks = prepare_data(img_shape, train_ids, dataset_reader)
File "/content/gdrive/My Drive/Unet/unet/train_unet.py", line 110, in prepare_data
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
我曾尝试用from skimage import io
img = io.imread(file_path)
cv2 代替,但没有奏效。
解决方案
尝试打开并读取图像后,检查它是否有效。
img = cv2.imread(path, cv2.IMREAD_COLOR)
if img is not None:
# Didn't work; do something here.
推荐阅读
- html - 如何将文本保存在图像容器下方,以免被掩盖?
- filter - 超集:filter_immune_slice_fields 是如何工作的?
- android - Id 是正确的,但是 - java.lang.NullPointerException: android.widget.TextView.setText(java.lang.CharSequence)' 在空对象引用上
- javascript - 使用新数据集更新 highcharts
- ms-access - Microsoft Access 查询搜索具有特殊字符的单词
- react-native - 如何在 react-native 中读取大文件
- c# - 向 Oracle 存储过程添加参数时出现问题
- python - 无法遍历列表以创建密钥对
- ocaml - 按字母顺序排序
- docusignapi - Docusign API 对象库是否公开?