import matplotlib.pyplot as plt
import torch
from kornia.core import concatenate, stack
from kornia.geometry.line import ParametrizedLine, fit_line
Fit line tutorial
Basic
Line
kornia.geometry
This tutorial use shows how to generate a line based on points. Using the
ParametrizedLine
and fit_line
from kornia.gemetry.line
= 1.2 # standard deviation for the points
std = 50 # total number of points num_points
# create a baseline
= torch.tensor([0.0, 0.0])
p0 = torch.tensor([1.0, 1.0])
p1
= ParametrizedLine.through(p0, p1)
l1 print(l1)
Origin: Parameter containing:
tensor([0., 0.], requires_grad=True)
Direction: Parameter containing:
tensor([0.7071, 0.7071], requires_grad=True)
# sample some points and weights
= [], []
pts, w for t in torch.linspace(-10, 10, num_points):
= l1.point_at(t)
p2 = torch.rand_like(p2) * std
p2_noise += p2_noise
p2
pts.append(p2)1 - p2_noise.mean())
w.append(= stack(pts)
pts = stack(w) w
# fit the the line
= fit_line(pts[None, ...], w[None, ...])
l2 print(l2)
# project some points along the estimated line
= l2.point_at(-10)
p3 = l2.point_at(10) p4
Origin: Parameter containing:
tensor([[0.5933, 0.5888]], requires_grad=True)
Direction: Parameter containing:
tensor([[-0.7146, -0.6995]], requires_grad=True)
= concatenate((p3, p4), dim=0).detach().numpy()
X = pts.detach().numpy()
X_pts
0], X_pts[:, 1], "ro")
plt.plot(X_pts[..., :, 0], X[:, 1])
plt.plot(X[:, plt.show()