本文主要是介绍使用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),用OpenSlide和open_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()
其实这张图来自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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!