图像数据通道格式:NCHW和NHWC的区别

2021-02-06  本文已影响0人  LabVIEW_Python

在深度学习中,图像数据通道格式有两种:

import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread("data/images/bus.jpg")
img = cv2.resize(img, (5,4)) #(x, y) -> (W, H)
print(img.shape) # (H,W,C)
plt.figure()
plt.imshow(img)
plt.show()
line1 = img[1,...]
line1 = np.expand_dims(line1, axis=0)
print(line1.shape)
print(line1)
plt.figure()
plt.imshow(line1)
plt.show()
print("BGR->RGB")
rgb_line1 = cv2.cvtColor(line1, cv2.COLOR_BGR2RGB)
print(rgb_line1.shape)
print(rgb_line1)
print()
print("HWC->CHW")
CHW_line1 = np.transpose(line1, (2,0,1))
print(CHW_line1.shape)
print(CHW_line1)
运行结果如下: img.shape and imshow(img) print(line1.shape) & print(line1)
BGR->RGB & HWC->CHW
上一篇下一篇

猜你喜欢

热点阅读