美文网首页python
openslide使用笔记

openslide使用笔记

作者: 井底蛙蛙呱呱呱 | 来源:发表于2019-01-10 13:40 被阅读1710次

    全扫描(whole slide image)图像非常的大,处理起来比较麻烦,openslide提供了一个很好的接口,用来处理这些非常大的图像。


    In [8]: from openslide import OpenSlide
    
    # 读入一个WSI图像,生成一个slide实例
    In [9]: slide = OpenSlide('tumor_001.tif')
    
    # 原始图像经各个level水平缩放后的长和宽
    In [10]: slide.level_dimensions
    Out[10]:
    ((97792, 221184),
     (48896, 110592),
     (24448, 55296),
     (12224, 27648),
     (6112, 13824),
     (3056, 6912),
     (1528, 3456),
     (764, 1728),
     (382, 864),
     (191, 432))
    
    # 在医学影像处理中,常见的是将图像进行level8水平的缩放
    In [11]: slide.level_dimensions[8]
    Out[11]: (382, 864)
    
    # downsamples,网上都成为下采样因子,可能不好理解,它代表的将原始WSI图像进行缩放的倍数,与level_dimentions是相对应的
    In [12]: slide.level_downsamples
    Out[12]: (1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0)
    
    # 将level_dimentions图像的长宽乘以对应的level_downsamples的缩放倍数,就可以得到原始图像大小
    In [16]: np.array(slide.level_dimensions[-1])*slide.level_downsamples[-1]
    Out[16]: array([ 97792., 221184.])
    
    # 查看WSI的level数
    In [17]: slide.level_count
    Out[17]: 10
    
    # read_region(location, level, size),Return an RGBA Image containing the contents of the specified region.
    # Parameters:  
    # location (tuple) tuple giving the top left pixel in the level 0 reference frame
    # level (int)  the level number
    # size (tuple) (width, height) tuple giving the region size
    # read_region()返回一个数组的rgba数值数组
    In [21]: slide.read_region((0,0),8,slide.level_dimensions[8])
    Out[21]: <PIL.Image.Image image mode=RGBA size=382x864 at 0x7FA92DD176D8>
    
    In [23]: np.array(img)
    Out[23]:
    array([[[255, 255, 255, 255],
            [255, 255, 255, 255],
            [251, 251, 251, 255],
            ...,
            [255, 255, 255, 255],
            [254, 255, 253, 255],
            [252, 254, 251, 255]],
            ... 
          [[250, 250, 250, 255],
            [255, 255, 255, 255],
            [251, 251, 251, 255],
            ...,
            [252, 252, 252, 255],
            [250, 250, 250, 255],
            [251, 251, 251, 255]]], dtype=uint8)
    
    In [24]: rgb_arr = np.array(img)
    
    # 查看一下图像rgb数组信息
    In [26]: rgb_arr.size
    Out[26]: 1320192
    
    In [27]: rgb_arr.shape
    Out[27]: (864, 382, 4)
    
    In [28]: rgb_arr.ndim
    Out[28]: 3
    
    In [29]: rgb_arr[0]
    Out[29]:
    array([[255, 255, 255, 255],
           [255, 255, 255, 255],
           [251, 251, 251, 255],
           ...,
           [255, 255, 255, 255],
           [254, 255, 253, 255],
           [252, 254, 251, 255]], dtype=uint8)
    
    In [30]: rgb_arr[0].shape
    Out[30]: (382, 4)
    
    

    相关文章

      网友评论

        本文标题:openslide使用笔记

        本文链接:https://www.haomeiwen.com/subject/apjwrqtx.html