Blobs.perimeter_approx#

Blobs.perimeter_approx(**kwargs: Any) Any#

Approximate perimeter of blob

Parameters:

epsilon (int) – maximum distance between the original curve and its approximation, default is exact contour

Returns:

Perimeter, one point per column

Return type:

ndarray(2,N) or list of ndarray(2,N)

The result is a low-order polygonal approximation to the original perimeter. Increasing epsilon reduces the number of perimeter points.

Example:

>>> from machinevisiontoolbox import Image
>>> im = Image.Read('shark2.png')
>>> blobs = im.blobs()
>>> blobs[0].perimeter.shape
(2, 471)
>>> blobs[0].perimeter_approx(5).shape
(2, 15)
>>> np.set_printoptions(threshold=10)
>>> blobs[0].perimeter_approx(5)
array([[367, 316, 299, ..., 411, 369, 368],
       [300, 345, 377, ..., 337, 326, 300]], shape=(2, 15), dtype=int32)

which in this case has reduced the number of perimeter points from 471 to 15.

To compute parameters of the area enclosed by the approximated perimeter we can first convert it to a Polygon2 object:

>>> from spatialmath import Polygon2
>>> poly = Polygon2(blobs[0].perimeter_approx(5), close=True)
>>> poly.area()
np.float64(7169.0)
>>> poly.moment(1, 0)  # first moment
np.float64(-2657962.3333333335)

(Source code, png, hires.png, pdf)

../../_images/machinevisiontoolbox-Blobs-perimeter_approx-1.png

Note

The perimeter is not closed, that is, the first and last point are not the same.

Seealso:

plot_perimeter perimeter perimeter_hull polar cv2.approxPolyDP