PIL图像库加载并保存更改的像素值
我目前正在研究图像细分问题。作为预处理的一部分,我正在尝试为2类[0,1]创建掩码值。
虽然保存加工的张量并将其加载回去产生不同的掩码值。
我目前的猜测是在引擎盖PIL归一化像素值的下方。
如果是这样,我该如何阻止它做?
我在下面创建了一个简单的示例来解释相同的内容。
tensor_img = torch.where(torch.Tensor(250,250,3) > 0, 1, 0)
img_arr = tensor_img.numpy().astype(np.uint8)
np.unique(img_arr, return_counts=True)
(array([[0,1],dtype = uint8),阵列([[148148,39352])))
img = Image.fromarray(img_arr)
img.save("tmp.jpg")
#read saved image
img = PIL.create("tmp.jpg")
tensor(img).unique(return_counts=True)
(张量([0,1],dtype = Type = Torch.uint8),张量([62288,212]))))))))))))))))))))))
I am currently working on an Image segmentation problem. As part of preprocessing, I'm trying to create mask values for 2 classes [0, 1].
While, saving the processed tensor and loading them back produces different mask values.
My current guess is under the hood PIL normalizing pixel values.
If so how do I stop it from doing?
I have created below a simple example explaining the same.
tensor_img = torch.where(torch.Tensor(250,250,3) > 0, 1, 0)
img_arr = tensor_img.numpy().astype(np.uint8)
np.unique(img_arr, return_counts=True)
(array([0, 1], dtype=uint8), array([148148, 39352]))
img = Image.fromarray(img_arr)
img.save("tmp.jpg")
#read saved image
img = PIL.create("tmp.jpg")
tensor(img).unique(return_counts=True)
(tensor([0, 1], dtype=torch.uint8), tensor([62288, 212]))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于此简单情况(只有2个类),您需要使用
png
而不是jpeg
,因为jpeg
是一种有损的压缩和PNG
是无损的。(array([[0,1],dtype = uint8),阵列([[159189,28311]))(张量([[0,1
,dtype = type = Torch.uint8),张量([159189,28311]))
] 类是使用颜色地图。
For this simple case (only 2 classes), you need to work with
png
and notjpeg
sincejpeg
is a lossy compression andpng
is lossless.(array([0, 1], dtype=uint8), array([159189, 28311]))
(tensor([0, 1], dtype=torch.uint8), tensor([159189, 28311]))
For more classes it is preferred to work with color map.