Transforms with scikit-image

Adrien Foucart

Documentation: https://scikit-image.org/docs/stable/api/skimage.transform.html

Estimate a transform

Source and destination points are given as NxD arrays, with the points given in (x, y) coordinates, not in (row, column).

So for instance, we could have:

src_points = np.array([[100, 100],
                       [300, 400],
                       [500, 300]])

dst_points = np.array([[100, 200],
                       [20, 470],
                       [250, 500]])

tx = AffineTransform()
tx.estimate(src_points, dst_points)
print(tx.rotation, tx.translation, tx.scale, tx.shear)

Which will give us a rotation of 0.53 radians, a translation of x=101.25, y=95, a scale factor of sx=0.88, sy=0.91, and a shear angle of 0.38.

Transforming an image

The warp function allows us to apply the transform to an image:

im_ = warp(im, tx.inverse)

Note that we have to use tx.inverse, not tx: the transform in warp is from the destination to the source.

Result of the affine transform computed above on the astronaut image

Transforming points

The transform object is callable, and can be used to compute transformed coordinates:

warped_points = tx(src_points)
print(warped_points)

Will give us the same coordinates as the dst_points in return.

Effect of shear

Shear is a bit weird. We can specify shear along x and y axis with a tuple, or just along x axis.

tx_shear_x = AffineTransform(shear=(math.pi/6, 0))
tx_shear_y = AffineTransform(shear=(0, math.pi/6))
tx_shear_xy = AffineTransform(shear=(math.pi/6, math.pi/6))
Effects of shear on astronaut image, with the help of some colored squares

For x shear, the angle is the rotation of the y-axis, with 0 as “down” and going clockwise, etc. It’s weird.