1. 程式人生 > >使用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.lever_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=1022, 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) tuple

tile_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 + 2*overlap = 2^n

此處,1022+1*2=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