# faster CircleModel estimation #2428

Open
opened this Issue Jan 3, 2017 · 1 comment

None yet

### 2 participants

Contributor
commented Jan 3, 2017 edited

## Description

looking to the code I would recommend to use non-iterative circle estimation which also does not need any initialization parameters, and given this example it runs by several orders faster...

CircleModel time [s]: 0.577063083649
LeastSqaure time [s]: 0.000691890716553

## Way to reproduce

[If reporting a bug, please include the following important information:]

• Code example
``````import time
import numpy as np
import matplotlib.pylab as plt
from skimage.measure import CircleModel

circle = CircleModel()
xy = circle.predict_xy(t=np.linspace(0, 2 * np.pi, 1e6), params=(2, 3, 4))
x, y = xy[:, 0], xy[:, 1]
plt.plot(x, y, 'g', label='true')

x += (np.random.rand(len(x)) - 0.5) * 3
y += (np.random.rand(len(y)) - 0.5) * 3
plt.plot(x, y, 'bx', label='noise')

t = time.time()
model = CircleModel()
model.estimate(np.array([x, y]).T)
print 'CircleModel time [s]:', time.time() - t
xy = model.predict_xy(t=np.linspace(0, 2 * np.pi, 100))
x, y = xy[:, 0], xy[:, 1]
plt.plot(x, y, 'r', label='CircleModel')

t = time.time()
space1 = np.array([[np.sum(x ** 2), np.sum(x * y), np.sum(x)],
[np.sum(x * y), np.sum(y ** 2), np.sum(y)],
[np.sum(x), np.sum(y), float(len(x))]])
space2 = np.array([[np.sum(x * (x ** 2 + y ** 2)),
np.sum(y * (x ** 2 + y ** 2)),
np.sum(x ** 2 + y ** 2)]]).T
A, B, C = np.linalg.pinv(space1).dot(space2)
x0 = A / 2
y0 = B / 2
r = np.sqrt(4 * C + A ** 2 + B ** 2) / 2
print 'LeastSqaure time [s]:', time.time() - t

xy = circle.predict_xy(t=np.linspace(0, 2 * np.pi, 50), params=(x0, y0, r))
x, y = xy[:, 0], xy[:, 1]
plt.plot(x, y, 'm', label='LeastSqaure')

plt.legend(loc=0)
plt.show()
``````
• Python version 2.7.12
added the enhancement label Jan 4, 2017
Member
commented Jan 4, 2017
 That sounds like a lice improvement. looking forward a PR :) BTW, I do not see any example about models in measure, while there are several for RANSAC. That would be nice I think to show also an example about that.
referenced this issue Jan 7, 2017
Open