Morphological operations
These methods perform morphological filtering operations such as dilation and erosion on binary and greyscale images.
- class machinevisiontoolbox.ImageMorph.ImageMorphMixin[source]
Image processing morphological operations on the Image class
- erode(se, n=1, border='replicate', bordervalue=0, **kwargs)[source]
Morphological erosion
- Parameters
se (ndarray(N,M)) – structuring element
n (int, optional) – number of times to apply the erosion, defaults to 1
border (str, optional) – option for boundary handling, see
convolve
, defaults to ‘replicate’bordervalue (scalar, optional) – padding value, defaults to 0
kwargs – addition options passed to
opencv.erode
- Returns
eroded image
- Return type
Image
Returns the image after morphological erosion with the structuring element
se
appliedn
times.Example:
>>> from machinevisiontoolbox import Image >>> import numpy as np >>> img = Image.Squares(1,7) >>> img.print() 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >>> img.erode(np.ones((3,3))).print() 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Note
It is cheaper to apply a smaller structuring element multiple times than one large one, the effective structuing element is the Minkowski sum of the structuring element with itself N times.
The structuring element typically has odd side lengths.
For a greyscale image this is the maximum value over the structuring element.
- References
Robotics, Vision & Control for Python, Section 11.6, P. Corke, Springer 2023.
- Seealso
- dilate(se, n=1, border='replicate', bordervalue=0, **kwargs)[source]
Morphological dilation
- Parameters
se (ndarray(N,M)) – structuring element
n (int, optional) – number of times to apply the dilation, defaults to 1
border (str, optional) – option for boundary handling, see
convolve
, defaults to ‘replicate’bordervalue (scalar, optional) – padding value, defaults to 0
kwargs – addition options passed to
opencv.dilate
- Returns
dilated image
- Return type
Image
Returns the image after morphological dilation with the structuring element
se
appliedn
times.Example:
>>> from machinevisiontoolbox import Image >>> import numpy as np >>> pixels = np.zeros((7,7)); pixels[3,3] = 1 >>> img = Image(pixels) >>> img.print() 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 >>> img.dilate(np.ones((3,3))).print() 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 0 255 255 255 0 0 0 0 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Note
It is cheaper to apply a smaller structuring element multiple times than one large one, the effective structuing element is the Minkowski sum of the structuring element with itself N times.
The structuring element typically has odd side lengths.
For a greyscale image this is the minimum value over the structuring element.
- References
Robotics, Vision & Control for Python, Section 11.6, P. Corke, Springer 2023.
- Seealso
- morph(se, op, n=1, border='replicate', bordervalue=0, **kwargs)[source]
Morphological neighbourhood processing
- Parameters
se (ndarray(N,M)) – structuring element
op (str) – morphological operation, one of: ‘min’, ‘max’, ‘diff’
n (int, optional) – number of times to apply the operation, defaults to 1
border (str, optional) – option for boundary handling, see
ve
, defaults to ‘replicate’bordervalue (scalar, optional) – padding value, defaults to 0
kwargs – addition options passed to
opencv.morphologyEx
- Returns
morphologically transformed image
- Return type
Image
Apply the morphological operation
oper
with structuring elementse
to the imagen
times.'oper'
description
'min'
minimum value over the structuring element
'max'
maximum value over the structuring element
'diff'
maximum - minimum value over the structuring element
Note
It is cheaper to apply a smaller structuring element multiple times than one large one, the effective structuing element is the Minkowski sum of the structuring element with itself N times.
Performs greyscale morphology
The structuring element should have an odd side length.
For a binary image, min = erosion, max = dilation.
- References
Robotics, Vision & Control for Python, Section 11.6, P. Corke, Springer 2023.
- Seealso
- open(se, n=1, border='replicate', bordervalue=0, **kwargs)[source]
Morphological opening
- Parameters
se (ndarray(N,M)) – structuring element
n (int, optional) – number of times to apply the erosion then dilation, defauts to 1
border (str, optional) – option for boundary handling, see
convolve
, defaults to ‘replicate’bordervalue (scalar, optional) – padding value, defaults to 0
kwargs – addition options passed to
opencv.morphologyEx
- Returns
dilated image
- Return type
Image
Returns the image after morphological opening with the structuring element
se
applied asn
erosions followed byn
dilations.Example:
>>> from machinevisiontoolbox import Image >>> import numpy as np >>> img = Image.Read("eg-morph1.png") >>> img.print('{:1d}') 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000010000000000000000000000001100000000 00000000000000010000000000000000000000001100000000 00000000001111111111000000000000000000001100000000 00000000001111111111000000000000000000001100000000 00000000001111111111000001111100000000001100000000 00000000001111111111000001111100000000001100000000 00000000001111111111111111111100000000001100000000 00000000111111111111000001111100000000001100000000 00000000001111111111000001111100000000001100000000 00000000001111111111000000000000000000001100000000 00000000001111111111000000000000000000001100000000 00000000001111111111000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000111111111111111111110000000000000000000000000 00000111111111111111111110000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 >>> img.open(np.ones((5,5))).print('{:1d}') 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000001111111111000000000000000000000000000000 00000000001111111111000000000000000000000000000000 00000000001111111111000001111100000000000000000000 00000000001111111111000001111100000000000000000000 00000000001111111111000001111100000000000000000000 00000000001111111111000001111100000000000000000000 00000000001111111111000001111100000000000000000000 00000000001111111111000000000000000000000000000000 00000000001111111111000000000000000000000000000000 00000000001111111111000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000
Note
For binary image an opening operation can be used to eliminate small white noise regions.
It is cheaper to apply a smaller structuring element multiple times than one large one, the effective structuing element is the Minkowski sum of the structuring element with itself N times.
The structuring element typically has odd side lengths.
- References
Robotics, Vision & Control for Python, Section 11.6, P. Corke, Springer 2023.
- Seealso
close :meth:`morph
opencv.morphologyEx
- close(se, n=1, border='replicate', bordervalue=0, **kwargs)[source]
Morphological closing
- Parameters
se (ndarray(N,M)) – structuring element
n (int, optional) – number of times to apply the dilation then erosion, defauts to 1
border (str, optional) – option for boundary handling, see
convolve
, defaults to ‘replicate’bordervalue (scalar, optional) – padding value, defaults to 0
kwargs – addition options passed to
opencv.morphologyEx
- Returns
dilated image
- Return type
Image
Returns the image after morphological opening with the structuring element
se
applied asn
dilations followed byn
erosions.Example:
>>> from machinevisiontoolbox import Image >>> import numpy as np >>> img = Image.Read("eg-morph2.png") >>> img.print('{:1d}') 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000010000000000000000000000001100000000 00000000000000010000000000000000000000001100000000 00000000001111111111000000000000000000001100000000 00000000001111111111000000000000000000001100000000 00000000001111111111000001111100000000001100000000 00000000001111111111000001111100000000001100000000 00000000001111001111111111111100000000001100000000 00000000111111001111000001111100000000001100000000 00000000001111111111000001111100000000001100000000 00000000001111111111000000000000000000001100000000 00000000001111111111000000000000000000001100000000 00000000001111111111000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000111111111111111111110000000000000000000000000 00000111111111111111111110000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 >>> img.close(np.ones((5,5))).print('{:1d}') 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000010000000000000000000000001100000000 00000000000000010000000000000000000000001100000000 00000000001111111111000000000000000000001100000000 00000000001111111111000000000000000000001100000000 00000000001111111111000001111100000000001100000000 00000000001111111111000001111100000000001100000000 00000000001111111111111111111100000000001100000000 00000000111111111111000001111100000000001100000000 00000000001111111111000001111100000000001100000000 00000000001111111111000000000000000000001100000000 00000000001111111111000000000000000000001100000000 00000000001111111111000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000001100000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000111111111111111111110000000000000000000000000 00000111111111111111111110000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000
Note
For binary image a closing operation can be used to eliminate joins between regions.
It is cheaper to apply a smaller structuring element multiple times than one large one, the effective structuing element is the Minkowski sum of the structuring element with itself N times.
The structuring element typically has odd side lengths.
- References
Robotics, Vision & Control for Python, Section 11.6, P. Corke, Springer 2023.
- Seealso
- hitormiss(s1, s2=None, border='replicate', bordervalue=0, **kwargs)[source]
Hit or miss transform
- Parameters
s1 (ndarray(N,M)) – structuring element 1
s2 (ndarray(N,M)) – structuring element 2
kwargs – arguments passed to
opencv.morphologyEx
- Returns
transformed image
- Return type
Image
Return the hit-or-miss transform of the binary image which is defined by two structuring elements structuring elements
\[Y = (X \ominus S_1) \cap (X \ominus S_2)\]which is the logical-and of the binary image and its complement, eroded by two different structuring elements. This preserves pixels where ones in the window are consistent with \(S_1\) and zeros in the window are consistent with \(S_2\).
- If only
s1
is provided it has three possible values: 1, must match a non-zero value
-1, must match a zero value
0, don’t care, matches any value.
Example:
Note
For the single argument case
s1
\(=S_1 - S_2\).- References
Robotics, Vision & Control for Python, Section 11.6.3, P. Corke, Springer 2023.
- Seealso
- thin(**kwargs)[source]
Morphological skeletonization
- Parameters
kwargs – options passed to
hitormiss
- Returns
Image
- Return type
Image
instance
Return the thinned version (skeleton) of the binary image as another binary image. Any non-zero region is replaced by a network of single-pixel wide lines.
Example:
>>> from machinevisiontoolbox import Image >>> img = Image.String('000000|011110|011110|000000') >>> img.print() 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0 >>> img.thin().print() 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 >>> img = Image.Read("shark2.png") >>> skeleton = img.thin()
- References
Robotics, Vision & Control for Python, Section 11.6.3, P. Corke, Springer 2023.
- Seealso
- thin_animate(delay=0.5, **kwargs)[source]
Morphological skeletonization with animation
- Parameters
delay (float, optional) – time in seconds between each iteration of display, default to 0.5
kwargs – options passed to
hitormiss
- Returns
Image
- Return type
Image
instance
Return the thinned version (skeleton) of the binary image as another binary image. Any non-zero region is replaced by a network of single-pixel wide lines.
The algorithm is iterative, and the result of of each iteration is displayed using Matplotlib.
Example:
>>> from machinevisiontoolbox import Image >>> img = Image.Read("shark2.png") >>> img.thin_animate() Image: 500 x 500 (uint8)
- References
Robotics, Vision & Control for Python, Section 11.6.3, P. Corke, Springer 2023.
- Seealso
- endpoint(**kwargs)[source]
Find end points on a binary skeleton image
- Parameters
kwargs – options passed to
hitormiss
- Returns
Image
- Return type
Image
instance
Return a binary image where pixels are True if the corresponding pixel in the binary image is the end point of a single-pixel wide line such as found in an image skeleton.
Example:
>>> from machinevisiontoolbox import Image >>> img = Image.String('000000|011110|000000') >>> img.print() 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 >>> img.endpoint().print() 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0
Note
Computed using the hit-or-miss morphological operator.
- References
Robotics, Vision & Control for Python, Section 11.6.3, P. Corke, Springer 2023.
- Seealso
- triplepoint(**kwargs)[source]
Find triple points
- Parameters
kwargs – options passed to
hitormiss
- Returns
Image
- Return type
Image
instance
Return a binary image where pixels are True if the corresponding pixel in the binary image is a triple point, that is where three single-pixel wide line intersect. These are the Voronoi points in an image skeleton.
Example:
>>> from machinevisiontoolbox import Image >>> img = Image.String('000000|011110|001000|001000|000000') >>> img.print() 0 0 0 0 0 0 0 1 1 1 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 >>> img.triplepoint().print() 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Note
Computed using the hit-or-miss morphological operator.