激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - python、PyTorch圖像讀取與numpy轉(zhuǎn)換實例

python、PyTorch圖像讀取與numpy轉(zhuǎn)換實例

2020-04-27 10:19PoemK Python

今天小編就為大家分享一篇python、PyTorch圖像讀取與numpy轉(zhuǎn)換實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

Tensor轉(zhuǎn)為numpy

np.array(Tensor)

numpy轉(zhuǎn)換為Tensor

torch.Tensor(numpy.darray)

PIL.Image.Image轉(zhuǎn)換成numpy

np.array(PIL.Image.Image)

numpy 轉(zhuǎn)換成PIL.Image.Image

Image.fromarray(numpy.ndarray)

首先需要保證numpy.ndarray 轉(zhuǎn)換成np.uint8型

numpy.astype(np.uint8),像素值[0,255]。

同時灰度圖像保證numpy.shape為(H,W),不能出現(xiàn)channels

這里需要np.squeeze()。彩色圖象保證numpy.shape為(H,W,3)

之后Image.fromarray(numpy.ndarray)

PIL.Image.Image轉(zhuǎn)換成Tensor

torchvision.transfrom

?
1
2
3
4
5
img=Image.open('00381fa010_940422.tif').convert('L')
 
import torchvision.transforms as transforms trans=transforms.Compose([transforms.ToTensor()])
 
a=trans(img)

Tensor轉(zhuǎn)化成PIL.Image.Image

先轉(zhuǎn)換成numpy,再轉(zhuǎn)換成PIL.Image.Image

灰度圖像

?
1
2
3
4
5
6
7
8
9
10
11
12
13
img=Image.open('00381fa010_940422.tif').convert('L')
 
import torchvision.transforms as transforms
trans=transforms.Compose([transforms.ToTensor()])
 
a=trans(img)
b=np.array(a) #b.shape (1,64,64)
maxi=b.max()
b=b*255./maxi
b=b.transpose(1,2,0).astype(np.uint8)
b=np.squeeze(b,axis=2)
xx=Image.fromarray(b)
xx

彩色圖象

?
1
2
3
4
5
6
7
8
9
10
img2=Image.open('00381fa010_940422.tif').convert('RGB')
import torchvision.transforms as transforms
trans=transforms.Compose([transforms.ToTensor()])
a=trans(img2)
a=np.array(a)
maxi=a.max()
a=a/maxi*255
a=a.transpose(1,2,0).astype(np.uint8)
b=Image.fromarray(a)
b

python-opencv

?
1
2
3
4
5
6
import cv2
a=cv2.imread('00381fa010_940422.tif') #a.shape (64,64,3)
cv2.imwrite('asd.jpg',a)
Image.fromarray(a)
b=cv2.imread('00381fa010_940422.tif',0)#b.shape (64,64)
Image.fromarray(b)

cv2.imread()返回numpy.darray, 讀取灰度圖像之后shape為(64,64),RGB圖像的shape為(64,64,3),可直接用Image.fromarray()轉(zhuǎn)換成Image。

cv寫圖像時,灰度圖像shape可以為(H,W)或(H,W,1)。彩色圖像(H,W,3)

要從numpy.ndarray得到PIL.Image.Image,灰度圖的shape必須為(H,W),彩色為(H,W,3)

對于Variable類型不能直接轉(zhuǎn)換成numpy.ndarray,需要用.data轉(zhuǎn)換

np.array(a.data)

以上這篇python、PyTorch圖像讀取與numpy轉(zhuǎn)換實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/yskyskyer123/article/details/80707038

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 性感美女一级毛片 | 中国洗澡偷拍在线播放 | 成人男女啪啪免费观看网站四虎 | 久久国产精品电影 | 日日操夜夜透 | 人人看人人舔 | 99精品视频一区二区三区 | 国产精品av久久久久久无 | 91短视频在线视频 | 色999久久久精品人人澡69 | 99视频在线观看视频 | 欧美一区在线观看视频 | 国产精品久久久久久久亚洲按摩 | 真人一级毛片免费 | 国产成人综合在线 | 国产毛片毛片毛片 | 日本在线免费观看 | 国产一区二区三区四区五区在线 | 蜜桃传媒视频麻豆第一区免费观看 | 在线免费观看毛片视频 | sesee99| 成人毛片100部免费观看 | www.777含羞草 | 国产精品二区高清在线 | 2023av在线视频 | 少妇的肉体2无删减版 | 亚洲尻逼视频 | 成人福利在线免费观看 | 久久久久久片 | 色日本视频 | 免费在线性爱视频 | 午夜视频在线免费播放 | 毛片在线免费观看网址 | 关键词 | 日本黄色免费片 | 国产一级爱c视频 | 久久久青 | 欧美日韩国产中文字幕 | 五月天堂婷婷 | 亚洲乱妇19p | 免费观看高清视频网站 |