[英]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 读取标题,例如,代码流是:
>>> 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,但同样的事情,出现的错误是:
>>> 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
有什么方法可以将图像数据流式传输到不同类型的容器中,这样索引的数量就不是问题并且使我能够处理它?
使用幻灯片扫描仪中的文件时,我现在面临同样的问题。 我发现有用的是使用vips
和openslide
使用以下命令平铺图像:
vips dzsave image.mrxs targetdirectoryname --depth one --tile-size 2048 --overlap 0
这将输出源图像的 0 级(全分辨率)切片,您选择的切片大小和 0 的像素重叠到目标目录。
阅读大量医学图像的标准方法是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
不是很好,但如果你有耐心,它可能会起作用。
你用openslide试试吗?
import openslide
from openslide.deepzoom import DeepZoomGenerator
osr=openslide.OpenSlide('JP2.svs')
im=osr.get_thumbnail((200,200))
im.save('test.jpg')
您可以在 python 中使用 glymur 模块轻松访问全分辨率图像的每个图块
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.