Skip to content

Instantly share code, notes, and snippets.

@Po-Hsuan-Huang
Created March 22, 2017 07:26
Show Gist options
  • Save Po-Hsuan-Huang/7e2ad4c1d6b6ae8d64aedc2776183128 to your computer and use it in GitHub Desktop.
Save Po-Hsuan-Huang/7e2ad4c1d6b6ae8d64aedc2776183128 to your computer and use it in GitHub Desktop.
The code reference on my stackoverflow question 'Tracking positions of points after piecewiseAffineTransformaiton.'
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 20 09:49:48 2017
@author: po-hsuan
"""
import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import PiecewiseAffineTransform, warp
from skimage import data
def find_map_index(x,y, rows, cols):
# in PIL warping map coordinate
return rows*y + x
def find_linspace_index(x,y,lin_rows,lin_cols):
print 'linrows', lin_rows
print 'atual, ', x, y
# find the nearest elemet to x, y in the linspaces
# convert to warpping cooridnates
index_x = np.where(lin_rows > x)[0][0]
index_y = np.where(lin_cols > y)[0][0]
print 'indeces, ', index_x, index_y
print 'inline ', lin_rows[index_x], lin_cols[index_y]
return index_x, index_y
if __name__ == '__main__':
#---my label positions
ori_labelx = [ 100, 100, 300, 300]
ori_labely = [ 100, 300, 100, 300]
#---
image = data.astronaut()
rows, cols = image.shape[0], image.shape[1]
lin_cols = np.linspace(0, cols, 50)
lin_rows = np.linspace(0, rows, 50)
src_rows, src_cols = np.meshgrid(lin_rows, lin_cols)
src = np.dstack([src_cols.flat, src_rows.flat])[0]
# find the indices of the coordinates in linspace
index_x = range(4)
index_y = range(4)
for i, (x, y) in enumerate (zip(ori_labelx, ori_labely)):
index_x[i], index_y[i] = find_linspace_index(x, y, lin_rows, lin_cols)
# add sinusoidal oscillation to row coordinates
dst_rows = src[:, 1] - np.sin(np.linspace(0, 3 * np.pi, src.shape[0])) * 50
dst_cols = src[:, 0]
dst_rows *= 1.
# dst_rows += 1. * 50
dst = np.vstack([dst_cols, dst_rows]).T
# find the indices in the warping map
idx = range(4)
for i, (x, y) in enumerate (zip (index_x, index_y)):
idx[i] = find_map_index(x,y, 50, 50 )
#---put label positions after transform
u0 = dst[idx[0], :]
u1 = dst[idx[1], :]
u2 = dst[idx[2], :]
u3 = dst[idx[3], :]
#---
dst_label = np.vstack((u0, u1, u2, u3))
print dst_label
#left bound
left = min(u0[0], u3[0])
#right bound
right = max(u1[0], u2[0])
#top bound
top = min(u0[1], u1[1])
#bottom bound
bottom = max(u2[1], u3[1])
tform = PiecewiseAffineTransform()
tform.estimate(src, dst)
#%%
out_rows = image.shape[0] + 50#- 1.5 * 50
out_cols = cols
out = warp(image, tform.inverse, output_shape=(rows + 100, cols))
fig, ax = plt.subplots()
ax.imshow(image)
ax.plot(dst_label[ :, 1], dst_label[ :, 0],'g*')
ax.plot(tform(src)[:, 0], tform(src)[:, 1], '.b')
# ax.plot([left, right], [top, bottom], 'r.')
ax.plot(ori_labely, ori_labelx,'bo')
ax.axis((0, out_cols, out_rows, 0))
plt.show()
#%%
fig, ax = plt.subplots()
ax.imshow(out)
ax.plot(dst_label[ :, 0], dst_label[ :, 1],'g*')
ax.plot(ori_labely, ori_labelx,'bo')
# ax.plot(tform.inverse(src)[:, 0], tform.inverse(src)[:, 1], '.b')
print left,right,top,bottom
ax.axis((0, out_cols, out_rows+50, -50))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment