imagej/fiji插件turboreg/stackreg的python实现
pystackreg的Python项目详细描述
摘要
Python/C++端口,由菲利普·戴维纳斯/ EPFL编写。
用于将源图像或堆栈(电影)自动对齐到目标图像/参考帧的python扩展。
说明
pystackreg用于将一个或多个图像与一个公共参考图像对齐(注册),这在时间分辨荧光显微镜或广域显微镜中通常是必需的。它直接从imagej插件TurboReg的源代码中移植,并提供imagej插件StackReg的功能,这两个插件都是由philippe thevenaz/epfl编写的(可在http://bigwww.epfl.ch/thevenaz/turboreg/获得)。
pystackreg提供了以下四种类型的失真:
- 翻译
- 刚体(平移+旋转)
- 缩放旋转(平移+旋转+缩放)
- 仿射(平移+旋转+缩放+剪切)
- 双线性(非线性变换;不保留直线)
pystackreg支持stackreg的全部功能以及一些附加选项,例如,使用不同的参考图像和访问实际的转换矩阵(请参见下面的示例)。
请注意:双线性变换不能传播,因为双线性变换的组合通常不会导致双线性变换。因此,当使用“上一个”图像作为参考图像时,堆栈注册/转换函数不能与双线性转换一起工作。您可以使用另一个引用(“第一”或“平均”分别用于第一个或平均图像),或者尝试将堆栈的每个图像分别注册/转换为其各自的前一个图像(并将已转换的前一个图像用作下一个图像的引用)。
安装
pypi上提供了这个包。使用:
pipinstallpystackreg
用法
下面的示例打开两个不同的文件,并使用所有不同的可能转换来注册它们
frompystackregimportStackRegfromskimageimportio#load reference and "moved" imageref=io.imread('some_original_image.tif')mov=io.imread('some_changed_image.tif')#Translational transformationsr=StackReg(StackReg.TRANSLATION)out_tra=sr.register_transform(ref,mov)#Rigid Body transformationsr=StackReg(StackReg.RIGID_BODY)out_rot=sr.register_transform(ref,mov)#Scaled Rotation transformationsr=StackReg(StackReg.SCALED_ROTATION)out_sca=sr.register_transform(ref,mov)#Affine transformationsr=StackReg(StackReg.AFFINE)out_aff=sr.register_transform(ref,mov)#Bilinear transformationsr=StackReg(StackReg.BILINEAR)out_bil=sr.register_transform(ref,mov)
下一个示例演示如何将注册与转换分离(例如,在一个颜色通道中注册,然后使用该信息转换另一个颜色通道):
frompystackregimportStackRegfromskimageimportioimg0=io.imread('some_multiframe_image.tif')img1=io.imread('another_multiframe_image.tif')# img0.shape: frames x width x height (3D)sr=StackReg(StackReg.RIGID_BODY)# register 2nd image to 1stsr.register(img0[0,:,:],img0[1,:,:])# use the transformation from the above registration to register another frameout=sr.transform(img1[1,:,:])
下面的示例演示如何注册和转换整个堆栈:
frompystackregimportStackRegfromskimageimportioimg0=io.imread('some_multiframe_image.tif')# 3 dimensions : frames x width x heightsr=StackReg(StackReg.RIGID_BODY)# register each frame to the previous (already registered) one# this is what the original StackReg ImageJ plugin usesout_previous=sr.register_transform_stack(img0,reference='previous')# register to first imageout_first=sr.register_transform_stack(img0,reference='first')# register to mean imageout_mean=sr.register_transform_stack(img0,reference='mean')# register to mean of first 10 imagesout_first10=sr.register_transform_stack(img0,reference='first',n_frames=10)# calculate a moving average of 10 images, then register the moving average to the mean of# the first 10 images and transform the original image (not the moving average)out_moving10=sr.register_transform_stack(img0,reference='first',n_frames=10,moving_average=10)
下一个示例演示如何将堆栈的注册与转换分离(例如,在一个颜色通道中注册,然后使用该信息转换另一个颜色通道):
frompystackregimportStackRegfromskimageimportioimg0=io.imread('some_multiframe_image.tif')# 3 dimensions : frames x width x heightimg1=io.imread('another_multiframe_image.tif')# same shape as img0# both stacks must have the same shapeassertimg0.shape==img1.shapesr=StackReg(StackReg.RIGID_BODY)# register each frame to the previous (already registered) one# this is what the original StackReg ImageJ plugin usestmats=sr.register_stack(img0,reference='previous')out=sr.transform_stack(img1)# tmats contains the transformation matrices -> they can be saved# and loaded at another timeimportnumpyasnpnp.save('transformation_matrices.npy',tmats)tmats_loaded=np.load('transformation_matrices.npy')# make sure you use the correct transformation here!sr=StackReg(StackReg.RIGID_BODY)# transform stack using the tmats loaded from filesr.transform_stack(img1,tmats=tmats_loaded)# with the transformation matrices at hand you can also# use the transformation algorithms from other packages:fromskimageimporttransformastfout=np.zeros(img0.shape).astype(np.float)foriinrange(tmats.shape[0]):tform=tf.AffineTransform(matrix=tmats[i,:,:])out[i,:,:]=tf.warp(img1[i,:,:],tform)
许可证
以下是TurboReg/StackReg的许可证:
/*==================================================================== | Additional help available at http://bigwww.epfl.ch/thevenaz/turboreg/ | | You'll be free to use this software for research purposes, but you | should not redistribute it without our consent. In addition, we expect | you to include a citation or acknowledgment whenever you present or | publish results that are based on it. \===================================================================*/