nii轉png有損解決方式


Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.

還是沒有經驗的人,有什么快速的方法可以解決這個問題?

該警告是自我解釋:color.convert_colorspace(in_hsv_h, 'HSV', 'RGB')是類型float64,和imsave,轉換元素uint8

PNG圖像的像素每個分量存儲為一個字節(紅色表示一個字節,綠色表示一個字節,藍色表示一個字節)。
每個組件都是[0,255](類型uint8范圍內的整數值

color.convert_colorspace的輸出為float64,每個顏色分量的范圍為[0,1]類型float64(在內存中存儲為64位,比准確得多uint8)。

float64范圍[0,1]到uint8范圍[0,255]的轉換執行如下:uint8_val = round(float64_val*255)
舍入操作會丟失一些數據(例如:在float64_val * 255 = 132.658的情況下,結果舍入到133)。

在保存之前將圖像轉換為uint8以禁止顯示此警告

告訴您uint8在保存之前將圖像元素轉換為

解決方法很簡單。
乘以255,然后加.astype(np.uint8)

imsave('testing-sorted-hue.png', (color.convert_colorspace(in_hsv_h, 'HSV', 'RGB')*255).astype(np.uint8))

為了使代碼正常工作,您還應該.astype(np.uint8)在構建時添加newImage

newImage = np.random.randint(0, 255, (300, 300, 3)).astype(np.uint8)

完整的代碼:

from imageio import imsave from skimage import color import numpy as np newImage = np.random.randint(0, 255, (300, 300, 3)).astype(np.uint8) in_hsv_h = color.convert_colorspace(newImage, 'RGB', 'HSV') in_hsv_s = in_hsv_h.copy() in_hsv_v = in_hsv_h.copy() for i in range(newImage.shape[0]): in_hsv_h[i,:,0] = np.sort(in_hsv_h[i,:,0]) in_hsv_s[i,:,1] = np.sort(in_hsv_s[i,:,1]) in_hsv_v[i,:,2] = np.sort(in_hsv_v[i,:,2]) imsave('testing-sorted-hue.png', (color.convert_colorspace(in_hsv_h, 'HSV', 'RGB')*255).astype(np.uint8)) imsave('testing-sorted-saturation.png', (color.convert_colorspace(in_hsv_s, 'HSV', 'RGB')*255).astype(np.uint8))

摘自https://www.pythonheidong.com/blog/article/311328/468f86f8b7623f7fc591/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM