Python 打开 jp2 医学影像 - Scipy, glymur

Python open jp2 medical images - Scipy, glymur

我正在尝试读取和平铺 jp2 图像文件。图像是 RGB 98176 x 80656 像素(它是医学图像数据)。

尝试使用 glymur 读取图像时出现此错误:

glymur.lib.openjp2.OpenJPEGLibraryError: OpenJPEG library error:  Prevent buffer overflow (x1: 80656, y1: 98176)

我知道图片太大了。我需要的是按图块读取图像数据并将它们保存在其他地方并以另一种格式保存。

Glymur 允许我使用 python 阅读 header,例如,代码流是:

>>> print(codestream.segment[1])
SIZ marker segment @ (87, 47)
    Profile:  no profile
    Reference Grid Height, Width:  (98176 x 80656)
    Vertical, Horizontal Reference Grid Offset:  (0 x 0)
    Reference Tile Height, Width:  (832 x 1136)
    Vertical, Horizontal Reference Tile Offset:  (0 x 0)
    Bitdepth:  (8, 8, 8)
    Signed:  (False, False, False)
    Vertical, Horizontal Subsampling:  ((1, 1), (1, 1), (1, 1))

平铺不行,读取方法不行。

编辑:

我也试过 Scipy 能够读取 header 但同样的事情,出现的错误是:

>>> import scipy.misc
>>> image=scipy.misc.imread('Sl0.jp2')
/home/user/anaconda2/lib/python2.7/site-packages/PIL/Image.py:2274: DecompressionBombWarning: Image size (7717166080 pixels) exceeds limit of 89478485 pixels, could be decompression bomb DOS attack.
  DecompressionBombWarning)
>>> scipy.misc.imwrite('/home/user/Documents/imageCfromjp2.tif',image)
/home/user/
AttributeError: 'module' object has no attribute 'imwrite'
>>> scipy.misc.imsave('/home/user/Documents/imageCfromjp2.tif',image)
/home/user/
  File "/home/user/anaconda2/lib/python2.7/site-packages/scipy/misc/pilutil.py", line 195, in imsave
    im = toimage(arr, channel_axis=2)
  File "/home/user/anaconda2/lib/python2.7/site-packages/scipy/misc/pilutil.py", line 287, in toimage
    raise ValueError("'arr' does not have a suitable array shape for "
ValueError: 'arr' does not have a suitable array shape for any mode.
>>> image2=image[0:500,0:500]
/home/user/
IndexError: too many indices for array
>>> image2=image[0:500]
/home/user/
ValueError: cannot slice a 0-d array

有没有什么方法可以将图像数据流式传输到不同类型的容器中,这样索引的数量就不是问题并使我能够处理它?

阅读大量医学图像的标准方法是 openslide,我会先尝试一下。我不确定它是否会直接读取 jp2,但假设这是来自幻灯片扫描仪,也许您可​​以保存为 openslide 支持的格式之一?

ImageMagick 将通过 OpenJPEG 加载大型 jp2 图像的部分,尽管速度不是特别快。例如,我这里有一张 10k x 10k jp2 图像,如果我转换为 JPG,我会看到:

$ time convert sekscir25.jp2 x.jpg
real    0m25.378s
user    0m24.832s
sys 0m0.544s

如果我试图裁剪出一小块,它几乎不会更快,这表明 IM 总是解码整个图像:

$ time convert sekscir25.jp2 -crop 100x100+0+0 x.png
real    0m19.887s
user    0m19.380s
sys 0m0.504s

但是如果我在加载期间进行裁剪,它确实会加速:

$ time convert sekscir25.jp2[100x100+0+0] x.png
real    0m7.026s
user    0m6.748s
sys 0m0.276s

不太好,但如果您有耐心,它可能会奏效。

您可以使用 python 中的 glymur 模块轻松访问全分辨率图像的每个图块

我在使用幻灯片扫描仪中的文件时遇到了同样的问题。 我发现有用的是通过以下命令使用 vips 和 openslide 平铺图像:

vips dzsave image.mrxs targetdirectoryname --depth one --tile-size 2048 --overlap 0

这会将源图像的级别 0(全分辨率)的图块输出到目标目录,您选择的图块大小和像素重叠为 0。

希望对您有所帮助。 马里奥

你用openslide试过吗

import openslide
from openslide.deepzoom import DeepZoomGenerator
osr=openslide.OpenSlide('JP2.svs')
im=osr.get_thumbnail((200,200))
im.save('test.jpg')