【问题标题】:Problems Converting Images from 8-bit to 10-bit将图像从 8 位转换为 10 位的问题
【发布时间】:2020-10-26 16:53:35
【问题描述】:

我正在尝试将 8 位图像转换为 10 位。我认为这就像更改 bin 值一样简单。我试过枕头和 cv-python:

from PIL import Image
from numpy import asarray
import cv2

path = 'path/to/image'
img = Image.open(path)
data = asarray(img)

newdata = (data/255)*1023 #2^10 is 1024
img2 = Image.fromarray(newdata) #this fails

cv2.imwrite('path/newimage.png, newdata)

虽然cv2.imwrite 成功写入新文件,但即使 bin 上升到 1023,它仍被编码为 8 位图像。

$ file newimage.png
newimage.png: PNG Image data, 640 x 480, 8-bit/color RGB, non-interlaced

在 python 或 linux 中是否有另一种方法可以将 8 位转换为 10 位?

【问题讨论】:

  • 为什么需要 10 位图像?这是非常罕见和低效的(在性能的情况下)。您的处理器和内存可能无法很好地处理 10 位数据。

标签: python opencv ubuntu python-imaging-library bit-depth


【解决方案1】:

这里出了很多问题。

  1. 您无缘无故地将 OpenCV (cv2.imwrite) 与 PIL (Image.open) 混合在一起。不要那样做,因为它们使用不同的 RGB/BGR 排序和约定,您会感到困惑,

  2. 您正在尝试将 10 位数字存储在 8 位向量中,

  3. 您正试图在 PIL 图像中保存 3 个 16 位 RGB 像素,will not work 因为 RGB 图像在 PIL 中必须是 8 位。


我建议:

import cv2
import numpy as np

# Load image
im = cv2.imread(IMAGE, cv2.IMREAD_COLOR)

res = im.astype(np.uint16) * 4
cv2.imwrite('result.png', res)

【讨论】:

  • 感谢您纠正我对混合 openCV 和枕头的使用。当我$ file image.png 时,它现在显示的是 16 位图像,而不是 10 位。我还没有找到任何在 openCV 中工作的 10 位图像示例
  • @theastronomist PNG does not support 10 bits per channel。使用内置的压缩​​,没有必要 - 16 位将编码 10 位,而不会创建过大的文件。
  • 对不起,我应该更清楚。正如 Mark 所说,PNG 不支持每个通道 10 位,您或多或少必须迁移到 16 位。 OpenCV 本身也不真正支持 10 位。它也将使用 16 位类型来保存 10 位值。
【解决方案2】:

我找到了一个使用pgmagick wrapper for python 的解决方案

import pgmagick as pgm

imagePath = 'path/to/image.png'
saveDir = '/path/to/save'

img = pgm.Image(imagePath)
img.depth(10) #sets to 10 bit

save_path = os.path.join(saveDir,'.'.join([filename,'dpx']))
img.write(save_path)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多