Problem location: 在训练的图片集中,混入了几张4通道的图片,将这几张图片删除后问题解除。
使用cv2中img.shape列出非3通道图片的文件名:
import os
import numpy as np
import cv2
if __name__ == '__main__':
root_dir = r'D:\WData\Workspace\springdemo\images\table_images\1.0\all_table'
name = root_dir.split('\\')[-1]
file_names = sorted(os.listdir(root_dir))
for file in file_names:
if file.endswith('.png'):
img = cv2.imdecode(np.fromfile(os.path.join(root_dir, file), dtype=np.uint8), -1)
size = img.shape
if size[2] != 3:
print("通道"+ str(size[2]) + file)
# os.remove(file)
supplementary knowledge
CV2
cv2 is OpenCV-python implements.
Win10 + anaconda安装cv2
pip install opencv-python
Images and Channels
Images are often referred to as RGB images that use channels. Channels are a very simple idea that is easy to use.
RGB Explained