Skip to content

Instantly share code, notes, and snippets.

@horsto
Last active April 5, 2021 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horsto/4d91473e257c8dba0aa4b7a9eb65ff66 to your computer and use it in GitHub Desktop.
Save horsto/4d91473e257c8dba0aa4b7a9eb65ff66 to your computer and use it in GitHub Desktop.
Scikit image piecewise affine transform problem
# https://forum.image.sc/t/equivalent-for-matlabs-piecewiselineartransformation2d/51035
## Matlab:
# tform = fitgeotrans(moved_points,fixed_points, 'pwl');
## 'pwl' refers to "PiecewiseLinearTransformation2D"
############################################################
import numpy as np
from skimage.transform import PiecewiseAffineTransform, warp
# Load data
grid_picture = np.load('grid_picture.npy')
original_points = np.load('original_points.npy')
transformed_points = np.load('transformed_points.npy')
# Piecewise affine transform
tform = PiecewiseAffineTransform()
status = tform.estimate(transformed_points, original_points)
if not status:
raise ValueError('PiecewiseAffineTransform ran into an error during fitting')
# Unwarp image
grid_picture_unwarped = warp(grid_picture, tform, order=2,
preserve_range=False, output_shape=[256,256])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment