Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created September 9, 2017 18:24
Show Gist options
  • Save TimSC/ec59f463ba58ea9ddcb269cad0ea713e to your computer and use it in GitHub Desktop.
Save TimSC/ec59f463ba58ea9ddcb269cad0ea713e to your computer and use it in GitHub Desktop.
Affine warp to straighen moose
from skimage.transform import PiecewiseAffineTransform, warp
from skimage.io import imread, imshow, imsave
import numpy as np
if __name__=="__main__":
img = imread("IMG_3785.JPG")
pw = PiecewiseAffineTransform()
if 0: #IMG_3784.JPG
src = np.array([(150, 234), (114, 4955), (3185, 4932), (3208, 276)])
x1 = 0.5*(src[0,0]+src[1,0])
x2 = 0.5*(src[2,0]+src[3,0])
y1 = 0.5*(src[3,1]+src[0,1])
y2 = 0.5*(src[1,1]+src[2,1])
dst = np.array([(x1, y1), (x1, y2), (x2, y2), (x2, y1)])
if 1: #IMG_3784.JPG
src = np.array([(154, 248), (193, 4961), (3256, 4961), (3284, 248)])
x1 = 0.5*(src[0,0]+src[1,0])
x2 = 0.5*(src[2,0]+src[3,0])
y1 = 0.5*(src[3,1]+src[0,1])
y2 = 0.5*(src[1,1]+src[2,1])
dst = np.array([(x1, y1), (x1, y2), (x2, y2), (x2, y1)])
pw.estimate(dst, src)
out = warp(img, pw, output_shape=img.shape)
imsave("out.jpg", out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment