提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
1.pytroch 的transform中图像增强方法都是基于PIL图像做的
例如:
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
a = Image.open('path') #读进来的图片是PIL格式,转化为np数组后是RGB格式
a = transform(a)
2.numpy图片格式转化为PIL格式
可以通过PIL中Image中的fromarray,直接将一个数组对象转换成为PIL中的图片对象
a = cv2.imread('path') #以BGR的顺序读进来,是np数组
b =imgplt.imread(('path')) #以BGR的顺序读进来,是np数组
c = plt.imread('path') #以BGR的顺序读进来,是np数组
d = Image.fromarray(img) #img为a,b,c
3.PIL图片格式转化为numpy格式
image = Image.open('path') #读进来的图片是PIL格式,转化为np数组后是RGB格
img = np.array(image)
4.对视频帧的处理(skvideo.io.vread())
import skvideo
skvideo.setFFmpegPath('/opt/conda/bin/')
import skvideo.io
video_data = skvideo.io.vread('视频路径')
#对视频帧进行transform操作
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
video_length = video_data.shape[0]
video_height = video_data.shape[1]
video_width = video_data.shape[2]
video_channel = video_data.shape[3]
transformed_video = torch.zeros([video_length, video_channel, video_height, video_width])
for frame_idx in range(video_length):
frame = video_data[frame_idx]
frame = Image.fromarray(frame)
frame = transform(frame) #对视频的帧进行transform
transformed_video[frame_idx] = frame
|