• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Jun 26th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. x, y = np.where(skel>0)
2. y_coord = y.tolist()
3. x_coord = x.tolist()
4. df = pd.DataFrame({"y_coord": y_coord, "x_coord": x_coord})
5. df['geometry'] = list(zip(df.x_coord, df.y_coord))
6. a = (df.geometry).tolist()
7.
8. def CatmullRomSpline(P0, P1, P2, P3, nPoints=100):
9.   """
10.   P0, P1, P2, and P3 should be (x,y) point pairs that define the Catmull-Rom spline.
11.   nPoints is the number of points to include in this curve segment.
12.   """
13.   # Convert the points to numpy so that we can do array multiplication
14.   P0, P1, P2, P3 = map(np.array, [P0, P1, P2, P3])
15.
16.   # Calculate t0 to t4
17.   alpha = 0.5
18.   def tj(ti, Pi, Pj):
19.     xi, yi = Pi
20.     xj, yj = Pj
21.     return ( ( (xj-xi)**2 + (yj-yi)**2 )**0.5 )**alpha + ti
22.
23.   t0 = 0
24.   t1 = tj(t0, P0, P1)
25.   t2 = tj(t1, P1, P2)
26.   t3 = tj(t2, P2, P3)
27.
28.   # Only calculate points between P1 and P2
29.   t = np.linspace(t1,t2,nPoints)
30.
31.   # Reshape so that we can multiply by the points P0 to P3
32.   # and get a point for each value of t.
33.   t = t.reshape(len(t),1)
34.   print(t)
35.   A1 = (t1-t)/(t1-t0)*P0 + (t-t0)/(t1-t0)*P1
36.   A2 = (t2-t)/(t2-t1)*P1 + (t-t1)/(t2-t1)*P2
37.   A3 = (t3-t)/(t3-t2)*P2 + (t-t2)/(t3-t2)*P3
38.   print(A1)
39.   print(A2)
40.   print(A3)
41.   B1 = (t2-t)/(t2-t0)*A1 + (t-t0)/(t2-t0)*A2
42.   B2 = (t3-t)/(t3-t1)*A2 + (t-t1)/(t3-t1)*A3
43.
44.   C  = (t2-t)/(t2-t1)*B1 + (t-t1)/(t2-t1)*B2
45.   return C
46.
47.
48.
49. def CatmullRomChain(P):
50.   """
51.   Calculate Catmull Rom for a chain of points and return the combined curve.
52.   """
53.   sz = len(P)
54.
55.   # The curve C will contain an array of (x,y) points.
56.   C = []
57.   for i in range(sz-3):
58.     c = CatmullRomSpline(P[i], P[i+1], P[i+2], P[i+3])
59.     C.extend(c)
60.
61.   return C
62.
63. # Define a set of points for curve to go through
64. Points = a
65.
66. # Calculate the Catmull-Rom splines through the points
67. c = CatmullRomChain(Points)
68.
69. # Convert the Catmull-Rom curve points into x and y arrays and plot
70. x,y = zip(*c)
71. plt.plot(x,y)
72.
73. # Plot the control points
74. px, py = zip(*Points)
75. plt.plot(px,py,'or')
76.
77. plt.show()
78.
79. from scipy.interpolate import Akima1DInterpolator
80.
81. x1, y1 = np.where(skel>0)
82.
83. akima1 = Akima1DInterpolator(x1, y1)
84.
85. x1_new = np.linspace(0, 1024, 1024)
86.
87. plt.plot(x1, y1, 'bo')
88.
89. plt.plot(x1_new, akima1(x1_new), 'b', label='random points')
90.
91. plt.xlim(0, 1024)
92. plt.ylim(0, 1024)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top