Use openslide-python to read and display the whole slide image (WSI), build pyramids, and generate tiles

Use openslide-python to read and display the whole slide image (WSI), build pyramids, and generate tiles

How to read H&E stained pathology sections

The characteristic is: too big, each 600Mb~10Gb, the general software cannot open.
Based on python development, temporarily think of 3 ways to open:

#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])

Output:

(68046, 80933)

The pixel of this picture is (68046, 80933), it is no problem to open with OpenSlide and open_slide , but the memory overflows with ImageSlide .
After opening, you can look at the image information that openslide can parse and implement image segmentation and other operations. For details, please refer to the official website: https://openslide.org/api/python/
Here are some of the things I think may need to be used Operation (python3.6):

from openslide . deepzoom import DeepZoomGenerator #Image Scanner Manufacturer print( slide . Detect_format ( img_path )) #Slide's various properties print( slide . properties ) #Down sampling factor downsamples = slide . Level_downsamples #Image size (width, height) [ w , h ] = slide . Level_dimensions [0] print( w , h ) #Get the thumbnail of the original image (206X400) simg = slide . Get_thumbnail ((206,400)) #Display thumbnail plt . Imshow ( simg ) plt . Show () #Implement the function of 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'))

The output is:

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 >

Display 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 ()

View Image
View Image

In fact, this picture is from level11, its size is (1064, 1265), and the split size is 1024, so the picture is split into 4 sub-pictures, and we show the first row, first column and second row, Two subgraphs in one column.

Note: The setting principle of tile_size is: tile_size + overlap = 2^n

Here, 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)))

Output:

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

Reference : https://blog.csdn.net/MerryYoung/article/details/84071091