Transform irregular quadrilateral to rectangle in python matplotlib -


i have data video.

  • vertices of rectangle
  • points of animal tracking inside rectangle.

due image deformation, "rectangle" not regular. want transform data in order plot them in matplotlib rectangle.

is there easy method?

this maze , trancking. decompose 5 quadrilaterals

you can use skimage.transform.projectivetransform scikit-image transform coordinates inside quadrilateral local square space [0, 1] × [0, 1].

for more info on how apply linear algebra solve problem, see projectivetransform.estimate or "projective mappings image warping" paul heckbert, 1999.

suppose have corners of quadrilateral in clockwise order:

bottom_left = [58.6539, 31.512] top_left = [27.8129, 127.462] top_right = [158.03, 248.769] bottom_right = [216.971, 84.2843] 

we instantiate projectivetransform , ask find projective transformation mapping points inside quadrilateral unit square:

from skimage.transform import projectivetransform t = projectivetransform() src = np.asarray(     [bottom_left, top_left, top_right, bottom_right]) dst = np.asarray([[0, 0], [0, 1], [1, 1], [1, 0]]) if not t.estimate(src, dst): raise exception("estimate failed") 

now, transformation t ready transform points unit square. of course, changing dst above, can scale different rectangle unit square (or entirely different quadrilateral).

data = np.asarray([     [69.1216, 51.7061], [72.7985, 73.2601], [75.9628, 91.8095],     [79.7145, 113.802], [83.239, 134.463], [86.6833, 154.654],     [88.1241, 163.1], [97.4201, 139.948], [107.048, 115.969],     [115.441, 95.0656], [124.448, 72.6333], [129.132, 98.6293],     [133.294, 121.731], [139.306, 155.095], [143.784, 179.948],     [147.458, 200.341], [149.872, 213.737], [151.862, 224.782], ]) data_local = t(data) 

we plot input data , transformed data see transformation working:

import matplotlib.pyplot plt plt.figure() plt.plot(src[[0,1,2,3,0], 0], src[[0,1,2,3,0], 1], '-') plt.plot(data.t[0], data.t[1], 'o') plt.figure() plt.plot(dst.t[0], dst.t[1], '-') plt.plot(data_local.t[0], data_local.t[1], 'o') plt.show() 

input data result


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -