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 Nonekulpa (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
findContoursto find a hierarchy of regions represented by their contours, andboundingRect,momentsto 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
Blobinstance>>> 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
areaThe area of the blob.
bboxThe bounding box of the blob.
umin,umaxThe horizontal extent of the bounding box.
vmin,vmaxThe vertical extent of the bounding box.
touchTrue if the blob touches the border of the image.
fillfactorRatio of blob area to bounding box area.
bboxareaArea of the bounding box.
Moment attributes
Property
Description
u,vThe centroid (center of mass) of the blob.
orientationOrientation of the equivalent ellipse.
a, bThe equivalent ellipse radii.
aligned_boxA rotated bounding box with sides parallel to the axes of the equivalent ellipse.
momentsThe moments of the blob including central and normalised values up to 3rd order.
humomentsSeven Hu moment invariants (invariant to position, orientation and scale).
Boundary and shape attributes
Property
Description
contour_pointA point on the contour of the blob.
perimeterA 2xN array of points on the perimeter of the blob.
perimeter_lengthThe perimeter length of the blob.
circularityThe circularity of the blob.
perimeter_approxA polygonal approximation to the perimeter.
perimeter_hullThe convex hull of the perimeter.
MECThe minimum enclosing circle.
MERThe minimum enclosing rectangle.
Hierarchy and region attributes
Property
Description
colorThe value of pixels within the blob.
childrenA list of references to child
Blobinstances.parentA reference to the parent
Blobinstance, or None if no parent.levelThe depth of the blob in the region tree.
dotfileWrite a GraphViz dot file representing the blob hierarchy.
Note
findContourscan give surprising results for small images:The perimeter length is computed between the mid points of the pixels, and the OpenCV function
arcLengthseems 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:
P. Corke, Robotics, Vision & Control for Python, Springer, 2023, Section 12.1.2.1.
- Seealso:
filtersortopencv.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
aRadius of equivalent ellipse
areaArea of the blob
aspectBlob aspect ratio
bRadius of equivalent ellipse
bboxBounding box
bboxareaArea of the bounding box
centroidCentroid of blob
childrenChild blobs
circularityBlob circularity
colorBlob color
fillfactorFill factor, ratio of area to bounding box area
idBlob id number
levelBlob level in hierarchy
MECMinimum enclosing circle of blob
MERMinimum enclosing rectangle of blob
momentsMoments of blobs
orientationBlob orientation
pCentroid point of blob
parentParent blob
perimeterPerimeter of the blob
perimeter_lengthPerimeter length of the blob
touchBlob edge touch status
uu-coordinate of the blob centroid
umaxMaximum u-axis extent
uminMinimum u-axis extent
vv-coordinate of the blob centroid
vmaxMinimum v-axis extent
vminMaximum v-axis extent