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/

 

posted @ 2021-11-09 23:02  编程coding小白  阅读(1251)  评论(0编辑  收藏  举报