yield关键字
yield关键字:定义生成器函数。
生成器函数:允许在迭代过程中逐步生成值,而不是一次性返回所有值。
yield语句会暂停函数的执行,并返回一个值给调用者。下一次调用生成器的__next()__方法,函数会从暂停的地方继续执行。
生成器函数:节省内存,按需求生成值,而不是一次性将所有值加载到内存中。
openslide库
openslide库的安装-linux
很多虚拟环境都需要额外装openslide库,所以记录一下过程:
cd /home/liusn/00apps conda activate 环境名 pip install openslide_bin-4.0.0.5-py3-none-manylinux_2_27_x86_64.whl pip install openslide-python==1.3.1
openslide的使用
- openslide库是一个读取和操作显微镜图像的python库,支持.svs,.vms和.tiff等格式。
- 支持图像金字塔格式:在不同的分辨率下访问图像数据。
- 可以从原始图像中提取特定区域,不需要加载整个图像。
- 能够访问图像的元数据,如放大倍数、图像尺寸等。
import openslide # 打开显微镜图像 slide = openslide.openslide(wsi_path) # 获取特定金字塔层级的图像尺寸 # level从0开始,0表示最高分辨率 # w, h表示指定层级的图像宽度和高度 w, h = slide.level_dimensions[level] # 获取level层的下采样比例 # 下采样比例: 特定层级的像素大小与最高分辨率像素大小的比例关系 # 如果下采样比例为(2,2), 图像的宽和高都被缩小为原来的1/2 factor = slide.level_downsamples[level] # 从显微镜图像中读取指定区域 # location: tuple, 左上角坐标; level: 金字塔层级 # size: (w, h), 要读取的区域大小; 返回一个pil对象 image = slide.read_region(location, level, size)
openslide对象的常用属性
self.level_downsamples[level]
:获取level层的下采样比例,相对于最高分辨率而言。self.level_dimension[level]
:level层的图像尺寸。
cv2库
import cv2 # 在图像上绘制多边形 # img: 要在其上绘制的图像; pts: 一个包含多边形顶点的ndarray; # color: 填充的颜色, (255)表示白色 cv2.fillpoly(img, pts, color) # 在图像上绘制文本 # img: 要绘制文本的图像; text: 要绘制的文本字符串 cv2.puttext(img, text)
numpy库
import numpy as np # 找到数组中满足条件的元组索引 # condition: bool数组, 返回所有为true的行, 列索引 x_idx, y_idx = np.where(condition) # 根据条件condition进行数组的元素选择和替换 # condition为true时, 返回value1, 否则返回value2 inst_map = np.where(condition, value1, value2) # 将数组按行的方向堆叠起来 # tup: 一个列表/元组, 返回一个新数组(总行数, 列数) # 总行数 = 所有输入数组的行数之和 np.vstack(tup) selected_x[..., 0:1] # ...表示前面所有的维度
asap库-multiresolutionimageinterface库
处理金字塔类型的数据结构。处理多分辨率图像的python库,适合医学图像和显微镜图像的分析。支持不同分辨率的访问与操作。
asap库的安装
安装asap linux(ubuntu18.04-a6000):https://www.freesion.com/article/4489476959/
安装asap linux(ubuntu22.04-4x3090)的安装步骤:
- 在asap官网下载最新版:asap 2.2,适配ubuntu2204。
- 安装asap的依赖包:用sudo apt-get install 命令。apt-get install是用于命令行操作的软件包管理工具,该命令是安装软件包。
- 离线安装asap的安装包:dpkg -i asap-2.2-ubuntu2204.deb ,手动安装本地的deb文件。
- 看asap安装的位置:dpkg -l asap 。
- 把asap放入pythonpath,然后可以import了。
pythonpath="/opt/asap/bin":"${pythonpath}" export pythonpath
asap库的使用
asap库是一个c++写的软件,所以不能读源码。少量的python调用文档见:https://academic.oup.com/gigascience/article/7/6/giy065/5026175
asap官网:https://github.com/computationalpathologygroup/asap/releases
通过python 访问tif数据:
将xml注释数据转换为tif图像,假设注释里的多边形坐标是基于图像最高分辨率级别的。
示例代码:
import multiresolutionimageinterface as mir # 创建图像接口 reader = mir.multiresolutionimagereader() # 打开和加载多分辨率图像文件 mr_image = reader.open(path) # 获取level 6的图像尺寸 level=2 w, h = mr_image.getleveldimensions(level) ds = mr_image.getleveldownsample(level) # 从level 6获取一个patch, patch左上角的坐标为(0,0), 返回的tile是一个numpy对象 tile = image.getucharpatch(0, 0, w, h, 6) # 读取一个 300 像素宽、200 像素高的图像块,从level=2 的 (568, 732) xy 坐标开始 # ds是下采样倍数, 在level=2的坐标乘以ds, 得到level=0的坐标 tile = image.getucharpatch(int(568 * ds), int(732 * ds), 300, 200, level) # 存储和管理多分辨率图像相关的注释数据 annotation_list = mir.annotationlist() # 将注释数据转换以xml格式存储 xml_repository = mir.xmlrepository(annotation_list) # 设置or更新xml文件的源路径 xml_repository.setsource(path) # 从xml文件加载数据 xml_repository.load() # 将注释数据转换为二值掩码 annotation_mask = mir.annotationtomask() # 将提供的注释annotation_list转换为二值掩码 annotation_mask.convert(annotation_list, output_path,image_dimensions, image_spacing)
concurrent.futures.threadpoolexecutor
管理线程池并高效地执行多线程任务,可以加快i/o密集型任务的处理速度。通过提交任务来执行并发操作。
from concurrent.futures import threadpoolexecutor # 创建对象, max_workers指定最大线程数, 如果没有指定, python根据系统的线程数进行调整 executor = threadpoolexecutor(max_workers=3) # 使用map()提交多个任务 executor.map(task, range(5)) # 关闭线程池 executor.shutdown(wait=true)
xml.etree.elementtree库
解析和创建xml文档,用于读取、修改和生成xml。
import xml.etree.elementtree as et # 从指定文件中读取xml数据, 并解析为一个树结构 elementtree对象 tree = et.parse(annot_path) # 获取根元素: xml文档最外层的元素 root = tree.getroot()
skimage库
import skimage # 生成多边形的像素坐标 # x: 一维数组, 多边形的列坐标; y: 一维数组, 多边形的行坐标 # shape: 指定输出坐标的图像形状 # rows, cols: 多边形内部像素的行和列坐标 # 多边形内部是指,所有的多边形都被填充好了 rows, cols = skimage.draw.polygon(x, y, shape)
pil.image库 pil.image.image
from pil import image image = image.open(path) # 查看image的mode和channel nums print(f"image mode: {image.mode}") print(f"number of channels: {len(image.getbands())}") # 转换mode mask = mask.convert("p")
detectron2库
数据增强
- 允许同时增强多种数据类型,如图像、边界框、掩码。
- 允许应用一系列静态声明的增强。
- 允许添加自定义新数据类型来增强,如旋转边界框、视频剪辑。
- 处理和操纵增强增强应用的operations。
如何在编写新的数据加载器时使用增强,如何编写新的增强。
maskformersemanticdatasetmapper类:
- 从file_name读取image
- 将几何变换应用到image和annotation
- 查找合适的cropping,将其应用于image和annotation
- 把image和annotation变成tensors
metadatacatalog类常见属性
- 从file_name读取image
- 将几何变换应用到image和annotation
- 查找合适的cropping,将其应用于image和annotation
- 把image和annotation变成tensors
faiss库-聚类
faiss库的使用:
- faiss索引包括:indexflatl2(小规模数据集)、indexivfflat(大规模数据集)、indexhnsw(高维数据)。
- 查询相似度 d, i = index.search(features, k)。对每个样本,查询与其他样本的相似度,d是距离矩阵,i是索引矩阵,返回每个样本的前k个最近邻。
到此这篇关于python库-安装与使用的文章就介绍到这了,更多相关python库安装与使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论