glichtner / pystackreg

A python extension for the automatic alignment of a source image or a stack (movie) to a target image/reference frame.
Other
82 stars 17 forks source link

Registration with translation fails on simple 2-cell images #20

Closed Helena-todd closed 2 years ago

Helena-todd commented 2 years ago

Hi Gregor,

I wish to align (register) an image to its previous timepoint in a timelapse using a StackReg.TRANSLATION. For some reason, the translated image does not seem to be aligned at all, it's rather th opposite: the translation moved the image even further from where it should have been.

import matplotlib.pyplot as plt
import numpy as np
from skimage import io
from pystackreg import StackReg

input_dir = '/Users/ht/Desktop/test_folder_ht/'
img0 = io.imread(input_dir + 'denoised_Lys_C100_Pos0_img_000000003_02-BF_000_xy5_cp_masks.tif')
img1 = io.imread(input_dir + 'denoised_Lys_C100_Pos0_img_000000004_02-BF_000_xy5_cp_masks.tif')

# Binarize the 2 images for alignment:
img0_tmp = np.where(img0 != 0, 1, img0)
img1_tmp = np.where(img1 != 0, 1, img1)

# Register (align) the 2 images and plot superimposed images before and after alignment.
# alignement to the previous image
sr = StackReg(StackReg.TRANSLATION)
translated_img = sr.register_transform(img0_tmp, img1_tmp)

# plot the superimposed images before algnment
plt.figure(figsize=(10, 10))
ax = plt.gca()
im = ax.imshow(img0_tmp+img1_tmp)
plt.suptitle('Superimposed images before alignment', fontsize=16)

# plot the superimposed images after algnment
plt.figure(figsize=(10, 10))
ax = plt.gca()
im = ax.imshow(img0_tmp + translated_img)
plt.suptitle('Superimposed images after alignment', fontsize=16)
before_alignment after_alignment

test_folder_ht.zip

georgeoshardo commented 2 years ago

What happens if you try to align the images before binarising?

glichtner commented 2 years ago

Dear Helena,

sorry for the very late reply.

I'm unable to reproduce your problem with your data and code (plotting code slightly altered), it actually works perfectly for me:

import matplotlib.pyplot as plt
import numpy as np
from skimage import io
from pystackreg import StackReg

input_dir='./'
img0 = io.imread(input_dir + 'denoised_Lys_C100_Pos0_img_000000003_02-BF_000_xy5_cp_masks.tif')
img1 = io.imread(input_dir + 'denoised_Lys_C100_Pos0_img_000000004_02-BF_000_xy5_cp_masks.tif')

# Binarize the 2 images for alignment:
img0_tmp = np.where(img0 != 0, 1, img0)
img1_tmp = np.where(img1 != 0, 1, img1)

# Register (align) the 2 images and plot superimposed images before and after alignment.
# alignement to the previous image
sr = StackReg(StackReg.TRANSLATION)
translated_img = sr.register_transform(img0_tmp, img1_tmp)

# plot the superimposed images before algnment
f, ax = plt.subplots(1, 2, figsize=(20, 10))
im = ax[0].imshow(img0_tmp+img1_tmp)
ax[0].set_title('Superimposed images before alignment', fontsize=16)

# plot the superimposed images after algnment
ax[1].imshow(img0_tmp + translated_img)
ax[1].set_title('Superimposed images after alignment', fontsize=16)
plt.savefig("result.png")

Result: result

Could you try again with your (or my) code and if it still doesn't work, let me know which operating system and python version you are using?

Best, Gregor

glichtner commented 2 years ago

Closing this issue as I haven't heard back and it works for me (see post above). Feel free to re-open in case you still encounter this issue.