我正在嘗試讀取形狀為 804 x 600 的 .pfm 影像,為此我撰寫了這樣的函式。我知道我的影像是 float16,但它們被讀取為 float32。
def read_pfm(file):
"""Method to decode .pfm files and return data as numpy array"""
f = open(file, "rb")
# read information on number of channels and shape
line1, line2, line3 = (f.readline() for _ in range(3))
width, height = (int(s) for s in line2.split())
# read data as big endian float
data = np.fromfile(f,'>f') # TODO: data is read as float32. Why? Should be float16
print(data.dtype)
print(data.shape)
data = np.reshape(data, shape)
return data
我的問題有兩個:
- 為什么我的影像在 float16 時默認讀取為 float32?
- 當我以這種方式強制將影像讀取為 float16 時
資料 = np.fromfile(f,'>f2')
輸入的形狀從 (482400,) 變為 (964800,)。為什么會這樣?
編輯: 我意識到我犯了一個錯誤,影像實際上是 float32。但是 Daweo 的回答仍然澄清了我對 16 位/32 位的困惑。
uj5u.com熱心網友回復:
當我強制將影像讀取為 float16(...) 時,輸入的形狀從 (482400,) 變為 (964800,)。為什么會這樣?
觀察482400 * 2 == 964800和32/16 == 2。考慮一個簡單的例子,假設你有以下 8 位
01101110
當您被指示使用 8 位整數時,您會認為它是單個數字 (01101110),但是當指示使用 4 位整數時,您會認為這是 2 個數字 (0110, 1110) 并且當被指示時如果使用 2 位整數,您會認為是 4 個數字(01、10、11、10)。同樣,如果假設持有 float32 的給定位元組序列確實包含 N 個數字,那么被視為持有 float16 的相同位元組序列確實包含 N*(32/16) 即 N*2 個數字。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/484207.html
