今天对病理图像的读写进行一个小介绍。先说结论,如果不需要读很小倍率的图像,pyvips比openslide快很多,写SVS的话用matlab。

病理图像的存储有很多种格式,可以看如下图:

常见的是.svs, .tif,.mrxs。今天就只讲.svs和.tif。.mrxs手上没有数据。但依然可以用openslide读取,以后有机会再展开。
在python上读取.svs和.tif的方式是一样的,因为.svs本质上就是一个tif,采用的数据格式和压缩方式都是一样的。示意图图下:

简单的读取可以如下:

from openslide import OpenSlide
filePath = 'file.tif'
slide   = OpenSlide(filePath)
#查看文件的金字塔结构,从中选取需要读取的层。
print(slide.level_dimensions)
# 输出结果如下:
# ((90624, 214528), (45312, 107264), (22656, 53632), (11328, 26816), (5664, 13408), (2832, 6704), (1416, 3352), (708, 1676), (354, 838), (177, 419))
#假如说需要读取40X的层,那就是第0层,代码如下:img_40X = np.array(location=slide.read_region((0,0),level=0,size=slide.level_dimensions[0]),dtype = np.uint8)[:,:,0:3]
# level表示要读的层数,我们可以从slide.level_dimensions查看需要的层
# location表示读取时图像的起始坐标,需要注意的是,不管我们读取哪个level的图像,这个点都填的是40X的坐标。
#比如说我们读取40X下起始点是(100,100)终点是(324,324)大小的patch,那location=(100,100),size=(224,224),level=0
img_40X_small = np.array(location=slide.read_region((100,100),level=0,size=(324,324)),dtype = np.uint8)[:,:,0:3]
#如果需要读取10X的一样视野的图像那个,size就应该是(224//4,224//4), level=2, 但location依然是(100,100)
img_10X_small = np.array(location=slide.read_region((100,100),level=2,size=(224//4,224//4)),dtype = np.uint8)[:,:,0:3]

location的好处是允许我们自定义读取的起始点,有时候我们需要排除wsi中的空白,就可以根据所画的ROI来决定起始点和size。
另一种方式是使用pyvips来读取,这个就简单很多:

import pyvips
svs_file = 'file.svs'
img = pyvips.Image.new_from_file(svs_file)
# crop(起始点x,起始点y,长,宽)
img2 = img.crop(x1, y1, h, w)
img2 = np.asarray(img2, dtype=np.uint8)

pyvips我没有找到如何读取其他level的方法,只能读取level=0,如果知道怎么读取其他层的朋友,请分享我一下代码,万分感谢。

pyvips的读取速度要比openslide快很多,如果不是需要读取level很高的图像,我都推荐用pyvips。我用工作站读取了一个(49821, 93298)大小的WSI,openslide花费162.94秒,pyvips花费42.79秒。越大的图像pyvips就会越快。

如果要写tif的话,我个人推荐用matlab。在python上写多层的tif文件我没有成功过,可能是因为python对tif的支持不好。写的代码如下:

function writesvs(imgdata, file_name)size2 = fix(size(imgdata));img_fist = imresize(imgdata, [size2(1) size2(2)]);size2 = fix(size(imgdata)/2);img_half = imresize(imgdata, [size2(1) size2(2)]);%size3 = fix(size(imgdata)/4);img_third = imresize(imgdata, [size3(1) size3(2)]);size4 = fix(size(imgdata)/8);img_four = imresize(imgdata, [size4(1) size4(2)]);%img_four = imgdata(1:64:end,1:64:end,:);t = Tiff(file_name,'w');%写40X的图像tagstruct.ImageDescription = "Aperio Image Library |AppMag = 40|MPP = 0.265018";tagstruct.ImageLength = size(img_fist ,1);tagstruct.ImageWidth = size(img_fist ,2);tagstruct.Photometric = Tiff.Photometric.RGB;tagstruct.BitsPerSample = 8;tagstruct.SamplesPerPixel = 3;tagstruct.RowsPerStrip = 16;tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;tagstruct.Software = 'MATLAB';tagstruct.TileWidth = 240;tagstruct.TileLength = 240;tagstruct.Compression = 7;tagstruct.JPEGQuality = 80;setTag(t,tagstruct)write(t,img_fist );writeDirectory(t);%写20X的图像tagstruct2.ImageDescription = "Aperio Image Library |AppMag = 20|MPP = 0.51";tagstruct2.ImageLength = size(img_half,1);tagstruct2.ImageWidth = size(img_half,2);tagstruct2.Photometric = Tiff.Photometric.RGB;tagstruct2.BitsPerSample = 8;tagstruct2.SamplesPerPixel = 3;tagstruct2.RowsPerStrip = 16;tagstruct2.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;tagstruct2.Software = 'MATLAB';tagstruct2.TileWidth = 240;tagstruct2.TileLength = 240;tagstruct2.Compression = 7;tagstruct2.JPEGQuality = 80;setTag(t,tagstruct2)write(t,img_half);writeDirectory(t);%写10X的图像tagstruct3.ImageDescription = "Aperio Image Library";tagstruct3.ImageLength = size(img_third,1);tagstruct3.ImageWidth = size(img_third,2);tagstruct3.Photometric = Tiff.Photometric.RGB;tagstruct3.BitsPerSample = 8;tagstruct3.SamplesPerPixel = 3;tagstruct3.RowsPerStrip = 16;tagstruct3.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;tagstruct3.Software = 'MATLAB';tagstruct3.TileWidth = 240;tagstruct3.TileLength = 240;tagstruct3.Compression = 7;tagstruct3.JPEGQuality = 80;setTag(t,tagstruct3)write(t,img_third);writeDirectory(t);%写5X的图像tagstruct4.ImageDescription = "Aperio Image Library";tagstruct4.ImageLength = size(img_four,1);tagstruct4.ImageWidth = size(img_four,2);tagstruct4.Photometric = Tiff.Photometric.RGB;tagstruct4.BitsPerSample = 8;tagstruct4.SamplesPerPixel = 3;tagstruct4.RowsPerStrip = 16;tagstruct4.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;tagstruct4.Software = 'MATLAB';tagstruct4.TileWidth = 240;tagstruct4.TileLength = 240;tagstruct4.Compression = 7;tagstruct4.JPEGQuality = 80;setTag(t,tagstruct4)write(t,img_four);close(t);
end

(病理图像读写)病理图像(whole slide images,WSI)的读写(.svs, .tiff),使用openslide,和pyvips以及matlab相关推荐

  1. VTK修炼之道13:数据读写_图像数据的读写

    1.前言 VTK应用程序所需的数据可以通过两种途径获取: 第一种是生成模型 ;第二种是从外部存储介质里导入相关的数据文件,(如vtkBMPReader读取 BMP图像) .VTK 也可以将程序中处理完 ...

  2. ITK:读写矢量图像

    ITK:读写矢量图像 内容提要 C++实现代码 内容提要 本示例说明了如何读取和写入像素类型为Vector的图像. C++实现代码 #include "itkImageFileReader. ...

  3. python 读写16bit图像的四种方法

    python 读写16bit图像的四种方法 应对超高清.HDR等图像增强类深度学习任务和专业图片处理任务时,我们需要对16bit图像进行读取.转tensor和保存等操作,这里总结出四种python实现 ...

  4. 使用OpenCV执行图像算法(加法和减法)以提亮图像或者使图像变暗

    使用OpenCV执行图像算法(加法和减法)以提亮图像或者使图像变暗 1. 效果图 2. 源码 参考 这篇博客将介绍如何使用OpenCV执行图像算术(加法和减法).可以通过俩种方法实现: 1. 使用Op ...

  5. BGR图像与HSV图像互相转换(opencv)

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  6. 【Python】函数图像绘制:二维图像、三维图像、散点图、心形图

    [Python]函数图像绘制:二维图像.三维图像.散点图.心形图 所有需要用的包 二维图像 三维图像 散点图绘制 心形图绘制 所有需要用的包 from mpl_toolkits.mplot3d imp ...

  7. 利用合成图像对热图像进行鲁棒行人检测

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 小黑导读 论文是学术研究的精华和未来发展的明灯.小黑决心每天为大家带来经典或者最新论文的解读和分享,旨 ...

  8. 滤波、漫水填充、图像金字塔、图像缩放、阈值化

      imgpro 组件是 Image 和 Process 这两个单词的缩写组合,即图像处理模块,这个模块包含了如下内容: 文章目录 1.线性滤波:方框滤波.均值滤波.高斯滤波 1.1 平滑处理 1.2 ...

  9. 高斯混合模型图像聚类、图像生成、可视化分析实战

    高斯混合模型图像聚类.图像生成.可视化分析实战 目录 高斯混合模型图像聚类.图像生成.可视化分析实战 PCA图像数据降维

最新文章

  1. js ajax 跨域问题 解决方案
  2. 怎样轻松做到SD卡照片数据恢复
  3. 安装Python3的工具包报Microsoft Visual C++ 14.0 is required的错误
  4. 利用 PGO 提升 .NET 程序性能
  5. 哈尔滨理工C语言程序设计精髓_【注意啦】哈尔滨工业大学2020考研计算机专业课调整,难度提升!...
  6. Windows 7 时代即将终结!
  7. Win10系列:C#应用控件基础12
  8. python @符号_注意!大佬提醒你python初学者这几个很难绕过的坑,附教程资料
  9. c语言 解析通信报文,基于DL/T645—2007通信规约报文的分析
  10. Kindle阅读产品体验报告-随时随地畅享阅读
  11. 秋天的第一杯奶茶,没喝到?那这个你绝对不能再错过
  12. 离散数学总复习精华版(最全 最简单易懂)已完结
  13. 什么是开发平台? 几句话就让你明白!
  14. python人工智能工程师要求_想跻身高薪的AI人工智能工程师,你需要符合哪些条件?...
  15. 每日三思:优化微信小程序中倒计时占内存较大(19-0612-1917)
  16. 怎么删除电脑服务器远程桌面连接,删除远程桌面服务客户端访问许可证
  17. php 访问 HTTP 网址
  18. 2021春 算法复习
  19. IPO败北,素士科技难解的尴尬局面
  20. wpf图表-Visifire使用教程分享

热门文章

  1. 51单片机静态数码管和动态数码管原理及实验
  2. 开发高可移植性J2ME的软件测试篇
  3. 重金属污染源matlab代码,2011重金属污染源的数学建模.docx
  4. 「三代组装」使用新版Falcon进行三代测序基因组组装
  5. BZOJ P2819 Nim
  6. 人工智能-推荐系统-模块01:离线统计模块【使用SparkSQL(基于Scala语言/Python语言)进行离线统计分析:历史热门商品统计、近期热门商品统计、商品平均评分统计...】
  7. Unity UGS官方例子BossRoom,NetCode部分的读代码笔记
  8. 春暖花开,我见六间房
  9. 是计算机病毒在危害你
  10. Vue.$nextTick你真的懂了吗?