skimage2.measure.CircleModel#

class skimage2.measure.CircleModel(center, radius)[source]#

Bases: _BaseModel

Total least squares estimator for 2D circles.

The functional model of the circle is:

r**2 = (x - xc)**2 + (y - yc)**2

This estimator minimizes the squared distances from all points to the circle:

min{ sum((r - sqrt((x_i - xc)**2 + (y_i - yc)**2))**2) }

A minimum number of 3 points is required to solve for the parameters.

Parameters:
centerarray-like, shape (2,)

Coordinates of circle center.

radiusfloat

Circle radius.

Raises:
ValueError

If center does not have length 2.

Notes

The estimation is carried out using a 2D version of the spherical estimation given in [1].

References

[1]

Jekel, Charles F. Obtaining non-linear orthotropic material models for pvc-coated polyester via inverse bubble inflation. Thesis (MEng), Stellenbosch University, 2016. Appendix A, pp. 83-87. https://hdl.handle.net/10019.1/98627

Examples

>>> t = np.linspace(0, 2 * np.pi, 25)
>>> xy = CircleModel((2, 3), 4).predict_xy(t)
>>> model = CircleModel.from_estimate(xy)
>>> model.center
array([2., 3.])
>>> model.radius
4.0
>>> res = model.residuals(xy)
>>> np.abs(np.round(res, 9))
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0.])

The estimation can fail when — for example — all the input or output points are the same. If this happens, you will get a transform that is not “truthy” - meaning that bool(tform) is False:

>>> # A successfully estimated model is truthy:
>>> if model:
...     print("Estimation succeeded.")
Estimation succeeded.
>>> # Not so for a degenerate model with identical points.
>>> bad_data = np.ones((4, 2))
>>> bad_model = CircleModel.from_estimate(bad_data)
>>> if not bad_model:
...     print("Estimation failed.")
Estimation failed.

Trying to use this failed estimation transform result will give a suitable error:

>>> bad_model.residuals(xy)
Traceback (most recent call last):
  ...
FailedEstimationAccessError: No attribute "residuals" for failed estimation ...
__init__(center, radius)[source]#
estimate(data)[source]#

Estimate circle model from data using total least squares.

Deprecated since version 0.26: estimate is deprecated since version 0.26 and will be removed in version 2.2. Please use CircleModel.from_estimate class constructor instead.

Parameters:
datandarray of shape (N, 2)

N points with (x, y) coordinates, respectively.

Returns:
successbool

True, if model estimation succeeds.

classmethod from_estimate(data)[source]#

Estimate circle model from data using total least squares.

Parameters:
datandarray of shape (N, 2)

N points with (x, y) coordinates, respectively.

Returns:
modelSelf or FailedEstimation

An instance of the circle model if the estimation succeeded. Otherwise, we return a special FailedEstimation object to signal a failed estimation. Testing the truth value of the failed estimation object will return False. E.g.

model = CircleModel.from_estimate(...)
if not model:
    raise RuntimeError(f"Failed estimation: {model}")
property params#

Return model attributes center, radius as 1D array.

Deprecated since version 0.26: params is deprecated since version 0.26 and will be removed in version 2.2. params attribute deprecated; use center, radius attributes instead.

predict_xy(t, params=<DEPRECATED>)[source]#

Predict x- and y-coordinates using the estimated model.

Parameters:
tarray-like

Angles in circle in radians. Angles start to count from positive x-axis to positive y-axis in a right-handed system.

Returns:
xy(…, 2) array

Predicted x- and y-coordinates.

Other Parameters:
paramsDEPRECATED, optional

Optional parameters xc, yc, radius.

Deprecated since version 0.26.

residuals(data)[source]#

Determine residuals of data to model.

For each point the shortest distance to the circle is returned.

Parameters:
datandarray of shape (N, 2)

N points with (x, y) coordinates, respectively.

Returns:
residualsndarray of shape (N,)

Residual for each data point.