使用openslide-python对whole slide image(WSI)进行读取、显示和金字塔构建、生成tiles

本文主要是介绍使用openslide-python对whole slide image(WSI)进行读取、显示和金字塔构建、生成tiles,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

H&E染色的病理切片怎么读取

特点是:太大,每张600Mb~10Gb,一般软件打不开。
基于python开发,暂时想到3种打开方式:


#coding:utf-8
import openslide
import matplotlib.pyplot as plt
#image file
img_path = 'path/to/img/1.tif'#method 1
slide1 = openslide.OpenSlide(img_path)
#method 2
slide2 = openslide.open_slide(img_path)
#method 3
slide3 = openslide.ImageSlide(img_path)#size of the image
print(slide.level_dimensions[0])

输出:

(68046, 80933)

这张图的像素是(68046, 80933),用OpenSlideopen_slide打开没问题,但是用ImageSlide就内存溢出了。
打开之后,就可以看看openslide能够解析的图像信息了,以及实现图像切分等操作,具体可参见官网:https://openslide.org/api/python/
下面是部分我认为可能需要用到的操作(python3.6):

from openslide.deepzoom import DeepZoomGenerator#图像扫描仪制造商
print(slide.detect_format(img_path))#幻灯片的各种属性
print(slide.properties)#下采样因子
downsamples = slide.level_downsamples#图像大小(宽,高)
[w, h] = slide.level_dimensions[0]
print(w,h)#得到原图的缩略图(206X400)
simg = slide.get_thumbnail((206,400))
#显示缩略图
plt.imshow(simg)
plt.show()#实现DeepZoomGenerator的功能
data_gen = DeepZoomGenerator(slide2, tile_size=1023, overlap=1, limit_bounds=False)#The number of Deep Zoom levels in the image
print(data_gen.level_count)#The total number of Deep Zoom tiles in the image
print(data_gen.tile_count)#A list of (tiles_x, tiles_y) tuples for each Deep Zoom level. level_tiles[k] are the tile counts of level k
print(data_gen.level_tiles)#A list of (pixels_x, pixels_y) tuples for each Deep Zoom level. level_dimensions[k] are the dimensions of level k
print(data_gen.level_dimensions)#Return a string containing the XML metadata for the Deep Zoom .dzi file
#Parameters:format (str)  the delivery format of the individual tiles (png or jpeg)
print(data_gen.get_dzi('png'))

输出是:

18
7199
((1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (2, 2), (3, 3), (5, 5), (9, 10), (17, 20), (34, 40), (67, 80))
((1, 1), (2, 2), (3, 3), (5, 5), (9, 10), (17, 20), (34, 40), (67, 80), (133, 159), (266, 317), (532, 633), (1064, 1265), (2127, 2530), (4253, 5059), (8506, 10117), (17012, 20234), (34023, 40467), (68046, 80933))
<Image Format="png" Overlap="1" TileSize="1022" xmlns="http://schemas.microsoft.com/deepzoom/2008"><Size Height="80933" Width="68046" /></Image>

显示tiles:

#Return an RGB Image for a tile.
#level (int):the Deep Zoom level
#address (tuple):  the address of the tile within the level as a (column, row) tupletile_img1 = data_gen.get_tile(11,(0,0))
tile_img2 = data_gen.get_tile(11,(0,1))
plt.imshow(tile_img1)
plt.show()
plt.imshow(tile_img2)
plt.show()

tile_img1
tile_img2

其实这张图来自level11,它的大小是(1064,1265),切分大小是1024,所以该图片被切分成了4个子图,而我们显示的是第一行第一列和第二行第一列的两张子图。

注意:tile_size的设置原则是:tile_size + overlap = 2^n

此处,1023+1=1024(2^10)

# Return the OpenSlide.read_region() arguments corresponding to the specified tile.
# Most applications should use get_tile() instead.
# level (int)  the Deep Zoom level
# address (tuple)  the address of the tile within the level as a (column, row) tuple
read_region = data_gen.get_tile_coordinates(11, (0,0))
print(read_region)#Return a (pixels_x, pixels_y) tuple for the specified tile.
print(data_gen.get_tile_dimensions(12, (0,0)))

输出:

((0, 0), 2, (4092, 4092))
(1024, 1024)
Authors
Merry Young

这篇关于使用openslide-python对whole slide image(WSI)进行读取、显示和金字塔构建、生成tiles的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/560221

相关文章

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二