yolov8的默认代码会将用opencv读取的bgr数据,在数据到达网络模型之前,转成rgb的!!!!!
在ultralytics/data/augment.py中
_format_img函数是数据输入网络模型之前的最后一步处理,图像数据格式组装。
def _format_img(self, img): """Format the image for YOLOv5 from Numpy array to PyTorch tensor.""" if len(img.shape) < 3: img = np.expand_dims(img, -1) img = np.ascontiguousarray(img.transpose(2, 0, 1)[::-1]) img = torch.from_numpy(img) return img
img = np.ascontiguousarray(img.transpose(2, 0, 1)[::-1]) 这一语句中,
transpose会将图像从h*w*c转成c*w*c,
'[::-1]'这部分会将bgr通道顺序转成rgb,
ascontiguousarray将矩阵变成内存连续的