Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created September 3, 2012 12:29
Show Gist options
  • Save TimSC/3608985 to your computer and use it in GitHub Desktop.
Save TimSC/3608985 to your computer and use it in GitHub Desktop.
Test code for scikits-image piecewise affine warp
import skimage.transform
from PIL import Image
from skimage import data
import numpy as np
if __name__ == "__main__":
#Load source image
srcIm = data.lena()
#Define control points for warp
#srcCloud = np.array([(100,100),(400,100),(400,400),(100,400)])
srcCloud = np.array([[240.0,251.5],
[250.0,246.0],
[264.0,244.0],
[277.0,246.5],
[291.5,254.0],
[320.5,256.0],
[335.5,251.5],
[347.0,250.0],
[357.0,253.0],
[360.0,260.0],
[249.5,266.5],
[256.5,262.0],
[268.0,258.5],
[282.5,265.5],
[286.0,273.5],
[275.0,277.0],
[267.0,276.0],
[257.0,272.0],
[316.5,271.5],
[323.5,263.5],
[332.5,260.5],
[344.0,263.0],
[348.0,269.0],
[343.0,274.0],
[335.0,277.5],
[324.5,276.0],
[296.5,272.0],
[292.0,291.0],
[290.0,303.5],
[287.0,322.5],
[297.5,327.0],
[305.5,328.5],
[312.5,327.0],
[319.0,323.0],
[318.5,308.0],
[317.0,295.5],
[313.0,274.5],
[264.0,348.0],
[282.5,347.0],
[303.0,344.5],
[313.0,345.5],
[319.0,348.5],
[308.5,362.0],
[295.5,365.0],
[281.0,359.5],
[201.0,315.0],
[208.5,341.0],
[222.5,362.0],
[240.5,380.5],
[260.0,385.5],
[273.0,388.0],
[289.0,389.5],
[302.0,386.5],
[314.0,374.0],
[321.5,358.5],
[331.0,343.0],
[340.0,324.0],
[349.0,303.5]])
#dstCloud = np.array([(100,100),(400,100),(400,400),(100,400)])
#dstCloud = np.array([(150,120),(374,105),(410,267),(105,390)])
dstCloud = np.array([[ 0.12233402, 0.07203401],
[ 0.18524639, 0.04183183],
[ 0.25405764, 0.03212213],
[ 0.32528491, 0.03801101],
[ 0.39708154, 0.05854361],
[ 0.60291846, 0.05854361],
[ 0.6747151, 0.03801101],
[ 0.74594236, 0.03212213],
[ 0.81475362, 0.04183184],
[ 0.87766598, 0.07203402],
[ 0.18998523, 0.1607586 ],
[ 0.22881356, 0.13370661],
[ 0.27634053, 0.12372751],
[ 0.32438883, 0.1319016 ],
[ 0.36830083, 0.16015729],
[ 0.32299877, 0.17647028],
[ 0.27642371, 0.18098838],
[ 0.23110902, 0.17650974],
[ 0.63169917, 0.16015729],
[ 0.67561118, 0.1319016 ],
[ 0.72365947, 0.12372752],
[ 0.77118645, 0.13370661],
[ 0.81001477, 0.16075861],
[ 0.76889098, 0.17650974],
[ 0.7235763, 0.18098838],
[ 0.67700123, 0.17647028],
[ 0.43629057, 0.15882798],
[ 0.42619022, 0.24916896],
[ 0.40403801, 0.33010681],
[ 0.36881519, 0.42423088],
[ 0.43039184, 0.46108425],
[ 0.5, 0.47741757],
[ 0.56960816, 0.46108425],
[ 0.63118481, 0.42423088],
[ 0.59596199, 0.33010681],
[ 0.57380978, 0.24916896],
[ 0.56370943, 0.15882798],
[ 0.28420508, 0.60334392],
[ 0.38391217, 0.56816616],
[ 0.5, 0.56787442],
[ 0.61608783, 0.56816617],
[ 0.71579492, 0.60334392],
[ 0.62462666, 0.70302648],
[ 0.5, 0.73352537],
[ 0.37537334, 0.70302648],
[ 0.01056112, 0.48743443],
[ 0.04040482, 0.6123741 ],
[ 0.08693353, 0.71819994],
[ 0.15447972, 0.81257495],
[ 0.24154724, 0.89760469],
[ 0.35237897, 0.96497571],
[ 0.5, 0.99403887],
[ 0.64762102, 0.96497571],
[ 0.75845276, 0.89760469],
[ 0.84552027, 0.81257496],
[ 0.91306646, 0.71819995],
[ 0.95959518, 0.6123741 ],
[ 0.98943888, 0.48743443]]) * 500.
#Perform transform
piecewiseAffine = skimage.transform.PiecewiseAffineTransform()
piecewiseAffine.estimate(dstCloud, srcCloud)
dstArr = skimage.transform.warp(srcIm, piecewiseAffine)
#Visualise result
dstArr = np.array(dstArr * 255., dtype=np.uint8)
print dstArr.min(), dstArr.max()
if dstArr.shape[2] == 1:
dstArr = dstArr.reshape((dstArr.shape[0],dstArr.shape[1]))
dstIm = Image.fromarray(dstArr)
dstIm.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment