如何使用python脚本正确校正XY图像漂移?

时间:2019-03-26 09:44:47

标签: python image-processing fft scikit-image cross-correlation

我是编码新手,这是我的第一篇文章! 作为首要任务,我试图使用skimage功能(例如register_translation和fourier_shift)在python中实现一个简单的图像漂移校正例程(因此,我不需要依赖ImageJ插件)。

下面您可以找到我到目前为止所做的事情, 但这是关于此方法的主要问题:

是否正确应用了位移校正?

。当我们通过FFT应用互相关峰以识别相对位移时,我不清楚的一件事是,该方法如何区分“伪像”图像漂移和真实物体运动? (即实际像素强度偏移)。

。我测量了每两个连续图像的漂移,并相应地校正了延时。有更好的方法吗?

。到目前为止,我认为我至少可以部分纠正电影中的漂移,但是最终输出仍然显示随机方向上有1个像素的漂移,而且我的tiff电影看起来像是“闪烁”(由于像素)。但是我应该以其他方式应用漂移校正!?

不仅对我的特定问题,而且对于本主题的一般性,都期待有所见识。

# import the basics

import numpy as np
import matplotlib.pyplot as plt
from skimage import data
from skimage.feature import register_translation
from scipy.ndimage import fourier_shift
from skimage import io

'''  register translation estimates the cross-correlation peak by an FFT
i.e, identifies the relative shift between two similar-sized images
using cross-correlation in Fourier space '''

movie = mymovie
shifts = []
corrected_shift_movie = []

for img in range(0,movie.shape[0]):
    if img < movie.shape[0] - 1:

        shift, error, diffphase = register_translation(movie[0], movie[img + 1])

        img_corr = fourier_shift(np.fft.fftn(movie[img + 1]), shift)
        img_corr = np.fft.ifftn(img_corr)

        shifts.append(shift)
        corrected_shift_movie.append(img_corr.real)


# for plotting the xy shifts over time

shifts = np.array(shifts)
corrected_shift_movie = np.array(corrected_shift_movie)

x_drift = [shifts[i][0] for i in range(0,shifts.shape[0])]
y_drift = [shifts[i][1] for i in range(0,shifts.shape[0])]

plt.plot(x_drift, '--g' , label = ' X drift')
plt.plot(y_drift, '--r' , label = ' Y drfit')
plt.legend()

# checking drift for the new corrected movie

movie = corrected_shift_movie
shifts_corr = []

for img in range(0,movie.shape[0]):
    if img < movie.shape[0] - 1:

        shift, error, diffphase = register_translation(movie[0], movie[img + 1])
        shifts_corr.append(shift)

shifts_corr = np.array(shifts_corr)

x_drift = [shifts_corr[i][0] for i in range(0,shifts_corr.shape[0])]
y_drift = [shifts_corr[i][1] for i in range(0,shifts_corr.shape[0])]

plt.plot(x_drift, '--g' , label = ' X drift')
plt.plot(y_drift, '--r' , label = ' Y drfit')
plt.legend()

# saving the new corrected movie

import tifffile as tiff
movie_to_save = corrected_shift_movie

with tiff.TiffWriter('drift_correction.tif', bigtiff=True) as tif:
        for new_image in range(movie_to_save.shape[0]):
            tif.save(movie_to_save[new_image], compress=0)

0 个答案:

没有答案