我想規范化我的影像并在訓練中使用它。但是在下面進行更改后,我找不到保存影像的方法。如何保存和加載它們?
files = ["/content/drive/MyDrive/Colab Notebooks/images/evre1/xyz.png",
"/content/drive/MyDrive/Colab Notebooks/images/evre1/xty.png"]
def normalize(files):
for i in files:
image = Image.open(i)
new_image =image.resize((224,224))
pixels = asarray(image)
# convert from integers to floats
pixels = pixels.astype('float32')
# calculate global mean and standard deviation
mean, std = pixels.mean(), pixels.std()
# print('Mean: %.3f, Standard Deviation: %.3f' % (mean, std))
# global standardization of pixels
pixels = (pixels - mean) / std
# confirm it had the desired effect
print(pixels)
# mean, std = pixels.mean(), pixels.std()
# print('Mean: %.3f, Standard Deviation: %.3f' % (mean, std))
normalize(files)
我試圖將它們保存為陣列:
np.save(outfile, pixels)
然后,
_ = outfile.seek(0)
t = np.load(outfile)
img_w, img_h = 224, 224
img = Image.fromarray(t, 'RGB')
img.save('test.png')
img.show()
但是沒有影像出現..
我搜索了很多檔案并花了很多時間來解決它......我正在等待任何幫助。謝謝。
uj5u.com熱心網友回復:
我認為您最好的選擇是將mean和std與每個標準化影像一起保存(在同一個.npy檔案中)。
加載標準化影像時,還加載 mean 和 std - 這允許我們恢復原始影像(在標準化之前)。
注意:
我們可能會像這樣顯示歸一化的影像(例如):
Image.fromarray(((t-t.min())/(t.max()-t.min())*255).astype(np.uint8), 'RGB').show()
順便說一句,您提出問題的方式似乎是恢復原始影像是一個更好的解決方案。
使用原始均值和標準值保存和恢復歸一化影像:
pixels使用meanand保存的示例std:
with open(outfile_name, 'wb') as f:
np.save(f, pixels) # Save the normalized image.
np.save(f, np.array([mean, std])) # Save mean and std as 2 elements numpy array after the "pixels" array (in the same file).
使用均值和標準加載歸一化影像并恢復和顯示原始影像的示例:
with open(outfile_name, 'rb') as f:
norm_t = np.load(f) # Load normalized array
mean_std = np.load(f) # Load mean and std (that were saved before).
mean, std = tuple(mean_std) # Get mean and std as two scalars
t = norm_t*std mean # Unnormalized (get the original pixels before normalizing)
t = np.round(t.clip(0, 255)).astype(np.uint8) # Convert from float to uint8 (use rounding and clipping for "cleaner" conversion).
img = Image.fromarray(t, 'RGB')
img.save('test.png')
img.show()
完整的代碼示例(包括回圈):
import numpy as np
from PIL import Image
files = ["xyz.png", "xty.png"]
def normalize(files):
for i in files:
image = Image.open(i)
new_image = image.resize((224, 224))
pixels = np.asarray(image)
# convert from integers to floats
pixels = pixels.astype('float32')
# calculate global mean and standard deviation
mean, std = pixels.mean(), pixels.std()
# print('Mean: %.3f, Standard Deviation: %.3f' % (mean, std))
# global standardization of pixels
pixels = (pixels - mean) / std
# confirm it had the desired effect
print(pixels)
# mean, std = pixels.mean(), pixels.std()
# print('Mean: %.3f, Standard Deviation: %.3f' % (mean, std))
#t = pixels
#Image.fromarray(((t-t.min())/(t.max()-t.min())*255).astype(np.uint8), 'RGB').show()
outfile_name = i.replace(".png", ".npy") # "xyz.npy" and "xty.npy"
# Save pixels, and also save np.array([mean, std]):
with open(outfile_name, 'wb') as f:
np.save(f, pixels) # Save the normalized image.
np.save(f, np.array([mean, std])) # Save mean and std as 2 elements numpy array after the "pixels" array (in the same file).
# Load outfile_name (normalized image) and [mean, std] array
with open(outfile_name, 'rb') as f:
norm_t = np.load(f) # Load normalized array
mean_std = np.load(f) # Load mean and std (that were saved before).
mean, std = tuple(mean_std) # Get mean and std as two scalars
t = norm_t*std mean # Unnormalized (get the original pixels before normalizing)
t = np.round(t.clip(0, 255)).astype(np.uint8) # Convert from float to uint8 (use rounding and clipping for "cleaner" conversion).
img = Image.fromarray(t, 'RGB')
img.save('test.png')
img.show()
normalize(files)
uj5u.com熱心網友回復:
如果要存盤帶有浮點像素的 RGB 影像,則不能使用 PNG、JPEG、TGA 或 GIF 格式。您或多或少有義務使用 TIFF 或 PFM - 盡管還有更晦澀的 HDR/EXR 格式可用。
您也不能將它們與 PIL/Pillow 一起使用,因為它不支持 32 位浮點 RGB 檔案。
所以,簡而言之,恕我直言,如果你想要浮動和 RGB,你可能想要:
- 用于存盤格式的TIFF(可能是 via
tifffile)或 PFM(可能是 EXR),以及 - OpenCV ,
scikit-image, 或libvips用于處理
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/462347.html
上一篇:govips中多個影像的并行疊加
