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 pass 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
The area of the blob.
The bounding box of the blob.
The horizontal extent of the bounding box.
The vertical extent of the bounding box.
True if the blob touches the border of the image.
Ratio of blob area to bounding box area.
Area of the bounding box.
Moment attributes
Property
Description
u,vThe centroid (center of mass) of the blob.
Orientation of the equivalent ellipse.
a, bThe equivalent ellipse radii.
A rotated bounding box with sides parallel to the axes of the equivalent ellipse.
The moments of the blob including central and normalised values up to 3rd order.
Seven Hu moment invariants (invariant to position, orientation and scale).
Boundary and shape attributes
Property
Description
contour_pointA point on the contour of the blob.
A 2xN array of points on the perimeter of the blob.
The perimeter length of the blob.
The circularity of the blob.
A polygonal approximation to the perimeter.
The convex hull of the perimeter.
The minimum enclosing circle.
The minimum enclosing rectangle.
Hierarchy and region attributes
Property
Description
The value of pixels within the blob.
A list of references to child
Blobinstances.A reference to the parent
Blobinstance, or None if no parent.The depth of the blob in the region tree.
Write 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
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(*args, **kwargs)Plot aligned rectangles of blobs using Matplotlib
plot_axes(*args, **kwargs)Plot equivalent ellipse axes of blobs using Matplotlib
plot_box(*args, **kwargs)Plot a bounding box for the blob using Matplotlib
plot_centroid(*args[, label])Plot the centroid of blobs using Matplotlib
plot_ellipse(*args, **kwargs)Plot the equivalent ellipses of blobs using Matplotlib
plot_labelbox([fmt, label])Plot a labelled bounding box of blobs using Matplotlib
plot_MEC(*args, **kwargs)Plot minimum enclosing circles of blobs using Matplotlib
plot_MER(*args, **kwargs)Plot minimum enclosing rectangles of blobs using Matplotlib
plot_perimeter(*args[, 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
Radius of equivalent ellipse
Area of the blob
Blob aspect ratio
Radius of equivalent ellipse
Bounding box
Area of the bounding box
Centroid of blob
Child blobs
Blob circularity
Blob color
Fill factor, ratio of area to bounding box area
Blob id number
Blob level in hierarchy
Minimum enclosing circle of blob
Minimum enclosing rectangle of blob
Moments of blobs
Blob orientation
Centroid point of blob
Parent blob
Perimeter of the blob
Perimeter length of the blob
Blob edge touch status
u-coordinate of the blob centroid
Maximum u-axis extent
Minimum u-axis extent
v-coordinate of the blob centroid
Minimum v-axis extent
Maximum v-axis extent