Blobs#

class Blobs(image: Any = None, kulpa: bool = True, binaryImage: bool = False, **kwargs: Any)[source]#

Find blobs and compute their attributes

Parameters:
  • image (Image, optional) – image to use, defaults to None

  • kulpa (bool, optional) – apply Kulpa’s correction factor to circularity, defaults to True

  • binaryImage (bool, optional) – if True, the input image is treated as a binary image for the purpose of moment calculation, otherwise greyscale moments are computed, defaults to False

Uses OpenCV functions findContours to find a hierarchy of regions represented by their contours, and boundingRect, moments to compute moments, perimeters, centroids etc.

A region is defined as a connected group of non-zero pixels, the particular values do not matter.

This class behaves like a list and each element of the list is a blob represented by a Blob instance

>>> from machinevisiontoolbox import Image
>>> img = Image.Read('sharks.png')
>>> blobs = img.blobs()
>>> len(blobs)
4
>>> blobs[0]
Blobs(nblobs=1)
>>> blobs.area
array([18465.5,  7509.5,  7523. , 14356. ])

The list can be indexed, sliced or used as an iterator in a for loop or comprehension, for example:

for blob in blobs:
    # do a thing

areas = [blob.area for blob in blobs]

However the last line can also be written as:

areas = blobs.area

since all methods return a scalar if applied to a single blob:

blobs[1].area

or a list if applied to multiple blobs:

blobs.area

A blob has many attributes:

Geometric attributes

Property

Description

area

The area of the blob.

bbox

The bounding box of the blob.

umin, umax

The horizontal extent of the bounding box.

vmin, vmax

The vertical extent of the bounding box.

touch

True if the blob touches the border of the image.

fillfactor

Ratio of blob area to bounding box area.

bboxarea

Area of the bounding box.

Moment attributes

Property

Description

u, v

The centroid (center of mass) of the blob.

orientation

Orientation of the equivalent ellipse.

a, b

The equivalent ellipse radii.

aligned_box

A rotated bounding box with sides parallel to the axes of the equivalent ellipse.

moments

The moments of the blob including central and normalised values up to 3rd order.

humoments

Seven Hu moment invariants (invariant to position, orientation and scale).

Boundary and shape attributes

Property

Description

contour_point

A point on the contour of the blob.

perimeter

A 2xN array of points on the perimeter of the blob.

perimeter_length

The perimeter length of the blob.

circularity

The circularity of the blob.

perimeter_approx

A polygonal approximation to the perimeter.

perimeter_hull

The convex hull of the perimeter.

MEC

The minimum enclosing circle.

MER

The minimum enclosing rectangle.

Hierarchy and region attributes

Property

Description

color

The value of pixels within the blob.

children

A list of references to child Blob instances.

parent

A reference to the parent Blob instance, or None if no parent.

level

The depth of the blob in the region tree.

dotfile

Write a GraphViz dot file representing the blob hierarchy.

Note

findContours can give surprising results for small images:

  • The perimeter length is computed between the mid points of the pixels, and the OpenCV function arcLength seems to underestimate the perimeter even more. The perimeter length is not the same as the number of pixels in the contour.

  • The area will be less than the number of pixels in the blob, because the area is computed from the moments of the blob, which are computed from the contour, see above.

References:
Seealso:

filter sort opencv.moments, opencv.boundingRect, opencv.findContours

Methods

aligned_box(**kwargs)

Compute rectangle aligned with ellipse axes for blobs

append(item)

S.append(value) -- append value to the end of the sequence

blob_frame()

Transformation from blob coordinate frame to image frame

clear()

copy()

count(value)

dotfile([filename, direction, show])

Create a GraphViz dot file

extend(other)

S.extend(iterable) -- extend sequence by appending elements from the iterable

filter([area, circularity, color, touch, aspect])

Filter blobs

humoments(**kwargs)

Hu image moment invariants of blobs

index(value, [start, [stop]])

Raises ValueError if the value is not present.

insert(i, item)

S.insert(index, value) -- insert value before index

label_image([image])

Create label image from blobs

perimeter_approx(**kwargs)

Approximate perimeter of blob

perimeter_hull(**kwargs)

Convex hull of blob's perimeter

plot_aligned_box(**kwargs)

Plot aligned rectangles of blobs using Matplotlib

plot_axes(**kwargs)

Plot equivalent ellipse axes of blobs using Matplotlib

plot_box(**kwargs)

Plot a bounding box for the blob using Matplotlib

plot_centroid([label])

Plot the centroid of blobs using Matplotlib

plot_ellipse(**kwargs)

Plot the equivalent ellipses of blobs using Matplotlib

plot_labelbox([label])

Plot a labelled bounding box of blobs using Matplotlib

plot_MEC(**kwargs)

Plot minimum enclosing circles of blobs using Matplotlib

plot_MER(**kwargs)

Plot minimum enclosing rectangles of blobs using Matplotlib

plot_perimeter([show, epsilon, clockwise])

Plot the perimeter of blobs using Matplotlib

polar(**kwargs)

Boundary in polar coordinate form

polarmatch(target)

Compare polar profiles

pop([index])

Raise IndexError if list is empty or index is out of range.

remove(item)

S.remove(value) -- remove first occurrence of value.

reverse()

S.reverse() -- reverse IN PLACE

sort([by, reverse])

Sort blobs

Properties

a

Radius of equivalent ellipse

area

Area of the blob

aspect

Blob aspect ratio

b

Radius of equivalent ellipse

bbox

Bounding box

bboxarea

Area of the bounding box

centroid

Centroid of blob

children

Child blobs

circularity

Blob circularity

color

Blob color

fillfactor

Fill factor, ratio of area to bounding box area

id

Blob id number

level

Blob level in hierarchy

MEC

Minimum enclosing circle of blob

MER

Minimum enclosing rectangle of blob

moments

Moments of blobs

orientation

Blob orientation

p

Centroid point of blob

parent

Parent blob

perimeter

Perimeter of the blob

perimeter_length

Perimeter length of the blob

touch

Blob edge touch status

u

u-coordinate of the blob centroid

umax

Maximum u-axis extent

umin

Minimum u-axis extent

v

v-coordinate of the blob centroid

vmax

Minimum v-axis extent

vmin

Maximum v-axis extent