Module: measure

skimage.measure.approximate_polygon(coords, ...) Approximate a polygonal chain with the specified tolerance.
skimage.measure.find_contours(array, level) Find iso-valued contours in a 2D array for a given level value.
skimage.measure.perimeter(image[, neighbourhood]) Calculate total perimeter of all objects in binary image.
skimage.measure.regionprops(label_image[, ...]) Measure properties of labelled image regions.
skimage.measure.structural_similarity(X, Y) Compute the mean structural similarity index between two images.
skimage.measure.subdivide_polygon(coords[, ...]) Subdivision of polygonal curves using B-Splines.

approximate_polygon

skimage.measure.approximate_polygon(coords, tolerance)

Approximate a polygonal chain with the specified tolerance.

It is based on the Douglas-Peucker algorithm.

Note that the approximated polygon is always within the convex hull of the original polygon.

Parameters :

coords : (N, 2) array

Coordinate array.

tolerance : float

Maximum distance from original points of polygon to approximated polygonal chain. If tolerance is 0, the original coordinate array is returned.

Returns :

coords : (M, 2) array

Approximated polygonal chain where M <= N.

References

[R86]http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm

find_contours

skimage.measure.find_contours(array, level, fully_connected='low', positive_orientation='low')

Find iso-valued contours in a 2D array for a given level value.

Uses the “marching squares” method to compute a the iso-valued contours of the input 2D array for a particular level value. Array values are linearly interpolated to provide better precision for the output contours.

Parameters :

array : 2D ndarray of double

Input data in which to find contours.

level : float

Value along which to find contours in the array.

fully_connected : str, {‘low’, ‘high’}

Indicates whether array elements below the given level value are to be considered fully-connected (and hence elements above the value will only be face connected), or vice-versa. (See notes below for details.)

positive_orientation : either ‘low’ or ‘high’

Indicates whether the output contours will produce positively-oriented polygons around islands of low- or high-valued elements. If ‘low’ then contours will wind counter- clockwise around elements below the iso-value. Alternately, this means that low-valued elements are always on the left of the contour. (See below for details.)

Returns :

contours : list of (n,2)-ndarrays

Each contour is an ndarray of shape (n, 2), consisting of n (x, y) coordinates along the contour.

Notes

The marching squares algorithm is a special case of the marching cubes algorithm [R87]. A simple explanation is available here:

http://www.essi.fr/~lingrand/MarchingCubes/algo.html

There is a single ambiguous case in the marching squares algorithm: when a given 2 x 2-element square has two high-valued and two low-valued elements, each pair diagonally adjacent. (Where high- and low-valued is with respect to the contour value sought.) In this case, either the high-valued elements can be ‘connected together’ via a thin isthmus that separates the low-valued elements, or vice-versa. When elements are connected together across a diagonal, they are considered ‘fully connected’ (also known as ‘face+vertex-connected’ or ‘8-connected’). Only high-valued or low-valued elements can be fully-connected, the other set will be considered as ‘face-connected’ or ‘4-connected’. By default, low-valued elements are considered fully-connected; this can be altered with the ‘fully_connected’ parameter.

Output contours are not guaranteed to be closed: contours which intersect the array edge will be left open. All other contours will be closed. (The closed-ness of a contours can be tested by checking whether the beginning point is the same as the end point.)

Contours are oriented. By default, array values lower than the contour value are to the left of the contour and values greater than the contour value are to the right. This means that contours will wind counter-clockwise (i.e. in ‘positive orientation’) around islands of low-valued pixels. This behavior can be altered with the ‘positive_orientation’ parameter.

The order of the contours in the output list is determined by the position of the smallest x,y (in lexicographical order) coordinate in the contour. This is a side-effect of how the input array is traversed, but can be relied upon.

Warning

Array coordinates/values are assumed to refer to the center of the array element. Take a simple example input: [0, 1]. The interpolated position of 0.5 in this array is midway between the 0-element (at x=0) and the 1-element (at x=1), and thus would fall at x=0.5.

This means that to find reasonable contours, it is best to find contours midway between the expected “light” and “dark” values. In particular, given a binarized array, do not choose to find contours at the low or high value of the array. This will often yield degenerate contours, especially around structures that are a single array element wide. Instead choose a middle value, as above.

References

[R87](1, 2) Lorensen, William and Harvey E. Cline. Marching Cubes: A High Resolution 3D Surface Construction Algorithm. Computer Graphics (SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170).

perimeter

skimage.measure.perimeter(image, neighbourhood=4)

Calculate total perimeter of all objects in binary image.

Parameters :

image : array

binary image

neighbourhood : 4 or 8, optional

neighbourhood connectivity for border pixel determination, default 4

Returns :

perimeter : float

total perimeter of all objects in binary image

References

[R88]K. Benkrid, D. Crookes. Design and FPGA Implementation of a Perimeter Estimator. The Queen’s University of Belfast. http://www.cs.qub.ac.uk/~d.crookes/webpubs/papers/perimeter.doc

regionprops

skimage.measure.regionprops(label_image, properties=['Area', 'Centroid'], intensity_image=None)

Measure properties of labelled image regions.

Parameters :

label_image : (N, M) ndarray

Labelled input image.

properties : {‘all’, list}

Shape measurements to be determined for each labelled image region. Default is [‘Area’, ‘Centroid’]. The following properties can be determined:

  • Area : int

    Number of pixels of region.

  • BoundingBox : tuple

    Bounding box (min_row, min_col, max_row, max_col)

  • CentralMoments : (3, 3) ndarray

    Central moments (translation invariant) up to 3rd order.

    mu_ji = sum{ array(x, y) * (x - x_c)^j * (y - y_c)^i }

    where the sum is over the x, y coordinates of the region, and x_c and y_c are the coordinates of the region’s centroid.

  • Centroid : array

    Centroid coordinate tuple (row, col).

  • ConvexArea : int

    Number of pixels of convex hull image.

  • ConvexImage : (H, J) ndarray

    Binary convex hull image which has the same size as bounding box.

  • Coordinates : (N, 2) ndarray

    Coordinate list (row, col) of the region.

  • Eccentricity : float

    Eccentricity of the ellipse that has the same second-moments as the region. The eccentricity is the ratio of the distance between its minor and major axis length. The value is between 0 and 1.

  • EquivDiameter : float

    The diameter of a circle with the same area as the region.

  • EulerNumber : int

    Euler number of region. Computed as number of objects (= 1) subtracted by number of holes (8-connectivity).

  • Extent : float

    Ratio of pixels in the region to pixels in the total bounding box. Computed as Area / (rows*cols)

  • FilledArea : int

    Number of pixels of filled region.

  • FilledImage : (H, J) ndarray

    Binary region image with filled holes which has the same size as bounding box.

  • HuMoments : tuple

    Hu moments (translation, scale and rotation invariant).

  • Image : (H, J) ndarray

    Sliced binary region image which has the same size as bounding box.

  • MajorAxisLength : float

    The length of the major axis of the ellipse that has the same normalized second central moments as the region.

  • MaxIntensity: float

    Value with the greatest intensity in the region.

  • MeanIntensity: float

    Value with the mean intensity in the region.

  • MinIntensity: float

    Value with the least intensity in the region.

  • MinorAxisLength : float

    The length of the minor axis of the ellipse that has the same normalized second central moments as the region.

  • Moments : (3, 3) ndarray

    Spatial moments up to 3rd order.

    m_ji = sum{ array(x, y) * x^j * y^i }

    where the sum is over the x, y coordinates of the region.

  • NormalizedMoments : (3, 3) ndarray

    Normalized moments (translation and scale invariant) up to 3rd order.

    nu_ji = mu_ji / m_00^[(i+j)/2 + 1]

    where m_00 is the zeroth spatial moment.

  • Orientation : float

    Angle between the X-axis and the major axis of the ellipse that has the same second-moments as the region. Ranging from -pi/2 to pi/2 in counter-clockwise direction.

  • Perimeter : float

    Perimeter of object which approximates the contour as a line through the centers of border pixels using a 4-connectivity.

  • Solidity : float

    Ratio of pixels in the region to pixels of the convex hull image.

  • WeightedCentralMoments : (3, 3) ndarray

    Central moments (translation invariant) of intensity image up to 3rd order.

    wmu_ji = sum{ array(x, y) * (x - x_c)^j * (y - y_c)^i }

    where the sum is over the x, y coordinates of the region, and x_c and y_c are the coordinates of the region’s centroid.

  • WeightedCentroid : array

    Centroid coordinate tuple (row, col) weighted with intensity image.

  • WeightedHuMoments : tuple

    Hu moments (translation, scale and rotation invariant) of intensity image.

  • WeightedMoments : (3, 3) ndarray

    Spatial moments of intensity image up to 3rd order.

    wm_ji = sum{ array(x, y) * x^j * y^i }

    where the sum is over the x, y coordinates of the region.

  • WeightedNormalizedMoments : (3, 3) ndarray

    Normalized moments (translation and scale invariant) of intensity image up to 3rd order.

    wnu_ji = wmu_ji / wm_00^[(i+j)/2 + 1]

    where wm_00 is the zeroth spatial moment (intensity-weighted area).

intensity_image : (N, M) ndarray, optional

Intensity image with same size as labelled image. Default is None.

Returns :

properties : list of dicts

List containing a property dict for each region. The property dicts contain all the specified properties plus a ‘Label’ field.

References

[R89]Wilhelm Burger, Mark Burge. Principles of Digital Image Processing: Core Algorithms. Springer-Verlag, London, 2009.
[R90]B. Jähne. Digital Image Processing. Springer-Verlag, Berlin-Heidelberg, 6. edition, 2005.
[R91]T. H. Reiss. Recognizing Planar Objects Using Invariant Image Features, from Lecture notes in computer science, p. 676. Springer, Berlin, 1993.
[R92]http://en.wikipedia.org/wiki/Image_moment

Examples

>>> from skimage.data import coins
>>> from skimage.morphology import label
>>> img = coins() > 110
>>> label_img = label(img)
>>> props = regionprops(label_img)
>>> props[0]['Centroid'] # centroid of first labelled object

structural_similarity

skimage.measure.structural_similarity(X, Y, win_size=7, gradient=False, dynamic_range=None)

Compute the mean structural similarity index between two images.

Parameters :

X, Y : (N,N) ndarray

Images.

win_size : int

The side-length of the sliding window used in comparison. Must be an odd value.

gradient : bool

If True, also return the gradient.

dynamic_range : int

Dynamic range of the input image (distance between minimum and maximum possible values). By default, this is estimated from the image data-type.

Returns :

s : float

Strucutural similarity.

grad : (N * N,) ndarray

Gradient of the structural similarity index between X and Y. This is only returned if gradient is set to True.

References

[R93]Wang, Z., Bovik, A. C., Sheikh, H. R., & Simoncelli, E. P. (2004). Image quality assessment: From error visibility to structural similarity. IEEE Transactions on Image Processing, 13, 600-612.

subdivide_polygon

skimage.measure.subdivide_polygon(coords, degree=2, preserve_ends=False)

Subdivision of polygonal curves using B-Splines.

Note that the resulting curve is always within the convex hull of the original polygon. Circular polygons stay closed after subdivision.

Parameters :

coords : (N, 2) array

Coordinate array.

degree : {1, 2, 3, 4, 5, 6, 7}, optional

Degree of B-Spline. Default is 2.

preserve_ends : bool, optional

Preserve first and last coordinate of non-circular polygon. Default is False.

Returns :

coords : (M, 2) array

Subdivided coordinate array.

References

[R94]http://mrl.nyu.edu/publications/subdiv-course2000/coursenotes00.pdf