python图片切面_Python Openslide 自动切割病理图像

Overview

全扫描(whole slide image, WSI)图像非常的大,处理起来比较麻烦,openslide 提供了一个很好的接口,这里介绍一个可用于处理大型病理图像的 python 库 – OpenSlide。

随着深度学习在医疗界的发展, 病理图像也越来越重要. 但是病理图像大多数在 10万x10万分辨率, 用平常的图像处理库没有办法读取. 开源的 openslide 库提供清洁便利的读取方法.

OpenSlide 本身作为一个 c 库存在,OpenSlide python 是其 python 的接口。这个库主要用于处理 whole-slide images 数据,即一般应用在数字病理上的高分辨率图片。这些图片有以下特点:

一张图片可能包含几万兆的像素值,普通的库可能无法进行处理。

whole-slide 图片可能存在多个级别的分辨率。

OpenSlide Python is a Python interface to the OpenSlide library.

OpenSlide is a C library that provides a simple interface for reading whole-slide images, also known as virtual slides, which are high-resolution images used in digital pathology. These images can occupy tens of gigabytes when uncompressed, and so cannot be easily read using standard tools or libraries, which are designed for images that can be comfortably uncompressed into RAM. Whole-slide images are typically multi-resolution; OpenSlide allows reading a small amount of image data at the resolution closest to a desired zoom level.

OpenSlide can read virtual slides in several formats:Aperio (.svs, .tif)

Hamamatsu (.ndpi, .vms, .vmu)

Leica (.scn)

MIRAX (.mrxs)

Philips (.tiff)

Sakura (.svslide)

Ventana (.bif, .tif)

Requirements

Python 2 >= 2.6 or Python 3 >= 3.3

OpenSlide >= 3.4.0

Python Imaging Library or Pillow

Installation

pip install openslide-python

Mac 下部署 openslide 环境

brew install opencv

brew install openslidepip install openslide-python

Using PIL

setup.py assumes that you want to use the Pillow fork of PIL. If you already have classic PIL installed, you can use it instead. Install OpenSlide Python with:pip install --no-deps openslide-python

or, if you are installing by hand:python setup.py install --single-version-externally-managed --record /dev/null

Documentation

Basic usage

OpenSlide objects

OpenSlide 对象表示的是一张 whole-slide 图片,其实例化方式也比较简单,直接将文件路径当做参数传入即可,而且也只有这一个参数。

其运行方式类似 python 的文件系统,即建立一个和文件的连接,在对文件进行处理的同时实时的传输数据,而不是将数据直接放入内存中。则类似的,这个类也可以作为上下文管理器使用。

所以其存在一个 close() 方法来关闭这个对象。import openslide

from openslide import deepzoom

import matplotlib.pyplot as pltslide = openslide.OpenSlide('./example.svs')

print(type(slide))

slide.close()with openslide.OpenSlide('./example.svs') as slide:

print(type(slide))

属性

level_count: 这张图片有几个级别的分辨率, 0 表示最高分辨率, 最低分辨率 (level_count - 1)

dimensions: level 为 0 时的 (width, height), 也就是最高分辨率的情况下 slide 的宽和高(元组)

level_dimensions: 每个 level 的 (width, height)

level_downsamples: 每个 level 下采样的倍数, 相对于 level 0, 即 level_dimension[k] = dimensions / level_downsamples[k]

properties: whole-slide 的 metadata, 是一个类似 dict 的对象, 其值都是 字符串

associated_images: 也是 metadata, 不过 dict 的值都是一张 pil 图片.

with openslide.OpenSlide('./example.svs') as slide:

print(slide.level_count)

print(slide.dimensions)

print(slide.level_dimensions)

print(slide.level_downsamples)

print(slide.associated_images)4

(148683, 41693)

((148683, 41693), (37170, 10423), (9292, 2605), (2323, 651))

(1.0, 4.000088325958833, 16.003087108552297, 64.02464105356638)

<_AssociatedImageMap {'label': , 'macro': , 'thumbnail': }>

with openslide.OpenSlide('./example.svs') as slide:

proper = slide.properties

print(proper['aperio.AppMag'])

print(proper['openslide.level[0].tile-height'])40

256

with openslide.OpenSlide('./example.svs') as slide:

fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(20, 6))

for i, (k, v) in enumerate(slide.associated_images.items()):

axs[i].imshow(v)

axs[i].set_title(k)

plt.show()

方法

read_region(location, level, size): 读取指定的区域其中 location 是读取区域的左上角在 level 0 中的坐标,level 表示我们要读取的是第几个 level 的图片,size 是 (width, height), 返回的是 PIL.Image。

注意:不管 level 是不是 0,location 的定位都是根据 level 0 来的,而 size 是在不同 level 上选取的。

with openslide.OpenSlide('./example.svs') as slide:

region = slide.read_region((47712, 24343), 1, (256, 256))

print(type(region))

plt.imshow(region)

plt.show()

get_best_level_for_downsample(downsample): 指定下采样倍数最好的level对于不同的下采样倍数,使用不同的 level 进行下采样会有不同的效果,比如要是下采样的多,使用更低一级的 level 效果可能更好。

with openslide.OpenSlide('./example.svs') as slide:

for i in range(10, 100, 10):

print("对于下采样%d倍,其最好的level是%d" % (i, slide.get_best_level_for_downsample(i)))对于下采样10倍,其最好的level是1

对于下采样20倍,其最好的level是2

对于下采样30倍,其最好的level是2

对于下采样40倍,其最好的level是2

对于下采样50倍,其最好的level是2

对于下采样60倍,其最好的level是2

对于下采样70倍,其最好的level是3

对于下采样80倍,其最好的level是3

对于下采样90倍,其最好的level是3

get_thumbnail(size): 缩略图size 是缩略图的 (width, height),注意到,这个缩略图是保持比例的,所以其会将 width 和 height 中最大的那个达到指定的值,另一个等比例缩放。with openslide.OpenSlide('./example.svs') as slide:

thumbnail = slide.get_thumbnail((256, 256))

plt.imshow(thumbnail)

plt.show()

类方法

detect_format(filepath),返回供应商的信息

openslide.OpenSlide.detect_format('./example.svs')'aperio'

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值