网站开发的数据比wordpress
网站开发的数据,比wordpress,仪征市企业网站建设公司,宿州网站建设开发公司哪家好一、PIL#xff0c;cv2读取数据图片以及之间的转换
cv2PIL读取acv2.imread()aImage.open()读取类型数组类型PIL类型读取尺寸排列#xff08;H,W,C#xff09;#xff08;W,H,C#xff09;显示图片cv2.imshow(“a”, a)cv2.waitKey (0)a.show()相互之间转换显示Image.from…一、PILcv2读取数据图片以及之间的转换
cv2PIL读取acv2.imread()aImage.open()读取类型数组类型PIL类型读取尺寸排列H,W,CW,H,C显示图片cv2.imshow(“a”, a)cv2.waitKey (0)a.show()相互之间转换显示Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))anumpy.array(a)#先转换为数组acv2.cvtColor(a,cv2.COLOR_RGB2BGR) #转变颜色通道转换为数组类型cv2读取的就是数组类型a numpy.array(a)
#cv2显示图像
img_pathE:\expression_recognition\\1.jpg
img_cv2 cv2.imread(img_path) # cv2读取的是数组类型 BGR H W C
cv2.imshow(img_cv2, img_cv2)
cv2.waitKey (0)#PIL显示图像
img_pathE:\expression_recognition\\1.jpg
img_PIL Image.open(img_path) # PIL读取的是PIL类型 RGB W H C
img_PIL.show()#cv2转变为PIL进行图像显示
img_pathE:\expression_recognition\\1.jpg
img_cv2 cv2.imread(img_path) # cv2读取的是数组类型 BGR H W C
img_cv2_PIL Image.fromarray(cv2.cvtColor(img_cv2,cv2.COLOR_BGR2RGB))
img_cv2_PIL.show()#PIL转变为cv2进行图像展示
img_pathE:\expression_recognition\\1.jpg
img_PIL Image.open(img_path) # PIL读取的是PIL类型 RGB W H C
img_PIL numpy.array(img_PIL)#先转换为数组 H W C
img_PIL_cv2 cv2.cvtColor(img_PIL,cv2.COLOR_RGB2BGR)
cv2.imshow(img_PIL_cv2,img_PIL_cv2)
cv2.waitKey (0)二、PIL数组类型以及tensor类型的转换
1、PIL转换为tensor类型包含CPU和GPU
主要有两种方式
transforms.ToTensor()torch.from_numpy能转换的格式PIL和数组格式只能转换数组格式具体转换过程a transforms.ToTensor() a(img_PIL) a(img_array)torch.from_numpy(img_array)
# 1、PIL转换为tensor的CPU、GPU格式
img_pathE:\expression_recognition\\1.jpg
img_tensor transforms.ToTensor()img_PIL Image.open(img_path) # PIL读取的是PIL类型 RGB W H C
img_PIL_tensor_CPU img_tensor(img_PIL)
img_PIL_tensor_GPU img_tensor(img_PIL).cuda()2、数组转换为tensor类型包含CPU和GPU
# 1、数组转换为tensor的CPU、GPU格式
img_pathE:\expression_recognition\\1.jpg
img_tensor transforms.ToTensor()img_PIL Image.open(img_path) # PIL读取的是PIL类型 RGB W H C
img_array numpy.array(img_PIL) # PIL转换为数组类型 H W C
img_array_tensor1_CPU img_tensor(img_array) # 将数组格式转换tensor格式
img_array_tensor1_GPU img_tensor(img_array).cuda()img_array_tensor2_CPU torch.from_numpy(img_array)
img_array_tensor2_GPU torch.from_numpy(img_array).cuda()
3、tensor类型转换为数组类型
注意在tensor类型转换为数组类型中tensor类型只能是cpu tensor类型
img_cpu.numpy() #cpu类型直接转换
img_gpu.cpu().numpy() #gpu类型先转换为cpu类型然后再转换为数组类型4、tensor、数组类型转换为PIL类型
tensor(可以是GPU也可以是CPU)转换为PIL格式数组转换为PIL格式第一步img_array numpy.uint8(img_array)img_tensor img_tensor.float()第二步a transforms.ToPILImage() img_PIL a(img_array)a transforms.ToPILImage() img_PIL a(img_tensor)
img_pathE:\expression_recognition\\1.jpg
img Image.open(img_path)
img_array numpy.array(img)
a1 transforms.ToTensor()
img_tensor a1(img)# 1、数组转换为PIL格式
a2 transforms.ToPILImage()
img_array numpy.uint8(img_array) # 满足类型需求
img_PIL a2(img_array)# 2、tensor转换为PIL格式
img_tensor img_tensor.float() # 满足类型需求
img_PIL a2(img_tensor)三、CPU tensor类型与GPU tensor类型的转换
img_gpu cpu_img_tensor.cuda() #cpu转换为gpu
img_cpu gpu_img_tensor.cpu() #gpu转换为cpu四、总结
CPU tensorGPU tensorPILarrayCPU tensor(cpu_img_tensor)cpu_img_tensor.cuda()a transforms.ToPILImage()img_tensor img_tensor.float()img_PIL a(img_tensor)cpu_img_tensor.numpy()GPU tensor (gpu_img_tensor)gpu_img_tensor.cpu()同cpu tensor的转换方式gpu_img_tensor.cpu().numpy()PIL (img_PIL)a transforms.ToTensor() a(img_PIL)a transforms.ToTensor() a(img_PIL).cuda()numpy.array(img_PIL)array(img_array)a transforms.ToTensor()a(img_array)或torch.from_numpy(img_array)a transforms.ToTensor()a(img_array).cuda()或torch.from_numpy(img_array).cuda()a transforms.ToPILImage()img_arraynumpy.uint8(img_array)img_PIL a2(img_array)
tips注意PIL与cv2转换要变换颜色通道。
参考https://blog.csdn.net/hjkdh/article/details/123097434
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/86826.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!