Hi Serge


Hhere you go (attached).



Notes:



- The transform is very sensitive *outside of any coordinates specified
  in the source coordinates*, so make sure it covers your whole image
- The process I follow here is as follows: estimate the transform,
  calculate where the corners of the image will go, then estimate a new
  transform that includes a shift that positions the result on a new,
  larger output image
- Estimate `affine` (forward) and warp on `affine.inverse` (because warp
  expects an inverse transformation to be provided)


I hope that helps!



Stéfan





On Thu, Apr 6, 2017, at 15:50, Stefan van der Walt wrote:

> On Thu, Apr 6, 2017, at 09:57, Stefan van der Walt wrote:

>> On Wed, Apr 5, 2017, at 19:02, Serge Shakhov wrote:

>>>  Just want to clarify. Am I right that your solution isn't
>>>  applicable to PiecewiseAffineTransform at all?
>>> Do you think it may be possible to find a workaround?

>> 

>> Sorry, I should have been clearer.  With "defined" I meant
>> implemented.
> 

> Upon closer inspection, it turns out that the PiecewiseAffineTransform
> *does* have an inverse implemented.  It was the "+" operator causing
> the problem.
> 

> Still, I have found cases now where I get invalid answers for valid-
> seeming sets of coordinates.  I'm investigating further, but I think I
> have a solution to your problem.
> 

> Stéfan

> 

> _________________________________________________

> scikit-image mailing list

> scikit-image@python.org

> https://mail.python.org/mailman/listinfo/scikit-image


import skimage.transform
import matplotlib.pyplot as plt

from skimage import data
import numpy as np

src_im = data.checkerboard()
src = np.array([[(0,0),(200,0),(210,0)],
                [(0,210),(200,210),(210,210)]])
dst = np.array([[(0,0),(250,-20),(230,-5)],
                [(30,150),(240,170),(170,170)]])

src = src.reshape((-1,2), order='F')
dst = dst.reshape((-1,2), order='F')

# Estimate transformation
affine = skimage.transform.PiecewiseAffineTransform()
affine.estimate(src, dst)

# Shape of middle image, our registration target
r, c = src_im.shape

# Note that transformations take coordinates in (x, y) format,
# not (row, column), in order to be consistent with most literature
corners = np.array([[0, 0],
                    [0, r],
                    [c, 0],
                    [c, r]])

# Warp the image corners to their new positions
warped_corners = affine(corners)

# The overall output shape will be max - min
corner_min = np.min(warped_corners, axis=0)
corner_max = np.max(warped_corners, axis=0)
output_shape = (corner_max - corner_min)

# Ensure integer shape with np.ceil and dtype conversion
output_shape = np.ceil(output_shape[::-1]).astype(int)

# Re-estimate transform with offset
shifted_transform = skimage.transform.PiecewiseAffineTransform()
shifted_transform.estimate(src, dst - corner_min)

dst_arr = skimage.transform.warp(src_im, shifted_transform.inverse,
                                 output_shape=output_shape,
                                 order=1, mode='constant', cval=0,
                                 clip=False, preserve_range=False)

f, (ax0, ax1) = plt.subplots(1, 2)
ax0.imshow(src_im, cmap='gray')
ax1.imshow(dst_arr, cmap='gray')
plt.show()
_______________________________________________
scikit-image mailing list
scikit-image@python.org
https://mail.python.org/mailman/listinfo/scikit-image

Reply via email to