Contents:
pymorph a powerful collection of state-of-the-art gray-scale morphological tools that can be applied to image segmentation, non-linear filtering, pattern recognition and image analysis.
Concatenate two or more images along width, height or depth.
Concatenate two or more images in any of the dimensions: width, height or depth. If the images do not match the dimension, a larger image is create with zero pixels to accommodate them. The images must have the same datatype.
Parameters : | dim : {‘width’, ‘heigh’, ‘depth’, ‘w’, ‘h’, ‘d’ }
img0, img1, ... : Images to concatenate |
---|---|
Returns : | img : resulting image (of the same type as inputs). |
Get the possible minimum and maximum of an image. The possible minimum and the possible maximum of an image depend on its data type. These values are important to compute many morphological operators (for instance, negate of an image). The output is a vector, where the first element is the possible minimum and the second, the possible maximum.
Parameters : | f : Unsigned gray-scale (uint8 or uint16), signed (int32) or binary image. |
---|---|
Returns : | y : Two element vector
|
Center filter.
center() computes the morphological center of f w.r.t. to the structuring element b.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. b : Structuring element (default: 3x3 cross). |
---|---|
Returns : | y : Image |
Close holes of binary and gray-scale images.
close_holes creates the image y by closing the holes of the image f, according with the connectivity defined by the structuring element Bc.The images can be either binary or gray-scale.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. Bc : Connectivity (default: 3x3 cross). |
---|---|
Returns : | y : image (ndarray) of same type as f. |
Distance transform.
dist creates the distance image y of the binary image f. The value of y at the pixel x is the distance of x to the complement of f, that is, the distance of x to nearest point in the complement of f. The distances available are based on the Euclidean metrics and on metrics generated by a a regular graph, that is characterized by a connectivity rule defined by the structuring element Bc. The implementation of the Euclidean algorithm is based on LotuZamp:01 .
Parameters : | f : ndarray of type bool
Bc : ndarray, optional
metric : {‘euclidean’ [default], ‘euclidean2’}, optional
|
---|---|
Returns : | y : ndarray
|
See also
Conditional (geodesic) distance transform.
cdist creates the conditional distance image y of the binary image f.
y[y,x] = distance of (y,x) to foreground in a path through complement(c) y[y,x] = 0, if f[y,x] == 0 or c[y,x] == 1
The distances available are based on the Euclidean metrics and on metrics generated by a a regular graph, that is characterized by a connectivity rule defined by the structuring element Bc. The implementation of the Euclidean algorithm is based on LotuZamp:01 .
Parameters : | f : Binary image. g : Binary image. Bc : Structuring Element Default: None (3x3 elementary
metric : Metric to use, one of (‘euclidean’, ‘euclidean2’), ‘euclidean’ by default. |
---|---|
Returns : | y : ndarray
|
Eliminate the objects that hit the image frame.
edgeoff creates the binary image y by eliminating the objects (connected components) of the binary image f that hit the image frame, according to the connectivity defined by the structuring element Bc.
Parameters : | f : Binary image. Bc : Structuring element (default: 3x3 cross) |
---|---|
Returns : | y : Binary image. |
Create a frame image.
frame creates an image `y, with the same dimensions (W,H,D) and same pixel type of the image f, such that the value of the pixels in the image frame is framevalue and the value of the other pixels is k2 . The thickness of the image frame is given by the HT, WT, and DT parameters.
Parameters : | f : Input image Ht : Height thickness (default 1) Wt : Width thickness (default 1) Dt : Depth thickness (default 0) framevalue : Frame grey-level (default: max of f.dtype) bgvalue : Background grey-level (default: min of f.dtype) |
---|---|
Returns : | y : image of same type as f . |
Apply a random color table to a gray-scale image.
Zero is guaranteed to be mapped to black (i.e., (0,0,0) triplet)
Parameters : | X : 2d-array of uint8 of shape (h,w)
|
---|---|
Returns : | y : 3d-array of uint8 of shape (h, w, 3)
|
Apply an iso-contour color table to a gray-scale image.
Parameters : | X : 2d-array
N : integer, optional
|
---|---|
Returns : | Y : Gray-scale (uint8 or uint16) or binary image. |
Apply binary overlays as colour layers on a binary or gray-scale image
Parameters : | X : Gray-scale (uint8 or uint16) or binary image. red : Red overlay, binary image(default: None) green : Green overlay, binary image (default: None) blue : blue overlay, binary image (default: None) magenta : magenta overlay, binary image (default: None) yellow : yellow overlay, binary image (default: None) cyan : cyan overlay, binary image (default: None) |
---|---|
Returns : | Y : Colour image (in HxWx3 format) |
Find the histogram of the image f.
Finds the histogram of the image f.
For binary image the vector size is 2. For gray-scale uint8 and uint16 images, the vector size is the maximum pixel value plus one. h[0] gives the number of pixels with value 0.
Parameters : | f : Input image (of any integer type). |
---|---|
Returns : | h : Histogram in a uint16 or an int32 vector. |
Label a binary image.
label creates the image y by labeling the connect components of a binary image f, according to the connectivity defined by the structuring element Bc. The background pixels (with value 0) are not labeled. The maximum label value in the output image gives the number of its connected components.
Parameters : | f : Binary image. Bc : Connectivity (default: 3x3 cross) |
---|---|
Returns : | y : Image of same size as f.
|
Negate an image.
neg returns an image y that is the negation (i.e., inverse or involution) of the image f. In the binary case, y is the complement of f.
Parameters : | f : Unsigned gray-scale (uint8 or uint16), signed (int32) or binary image. |
---|---|
Returns : | y : ndarray of same shape and dtype as f |
Threshold (adaptive)
threshad creates the image y as the threshold of the image f by the images f1 and f2. A pixel in y has the value 1 when the value of the corresponding pixel in f is between the values of the corresponding pixels in f1 and f2.
Parameters : | f : Gray-scale (uint8 or uint16) image. f1 : Lower value. f2 : Upper value. |
---|---|
Returns : | y : Binary image. |
Toggle operator
Image contrast enhancement or classification by the toggle operator.
toggle creates the image y that is an enhancement or classification of the image f by the toggle operator, with parameters f1 and f2. If gray, it performs an enhancement; otherwise, it performs a binary classification.
In the enhancement, a pixel takes the value of the corresponding pixel in f1 or f2, according to a minimum distance criterion from f to f1 or f to f2. In the classification, the pixels in f nearest to f1 receive the value 0, while the ones nearest to f2 receive the value 1.
Parameters : | f : Gray-scale (uint8 or uint16) image. f1 : Gray-scale (uint8 or uint16) image. f2 : Gray-scale (uint8 or uint16) image. gray_mode : boolean, optional
|
---|---|
Returns : | y : ndaimge of same shape and dtype as f |
Addition of two images, with saturation.
addm creates the image y by pixelwise addition of images f1 and f2 . When the addition of the values of two pixels saturates the image data type considered, the greatest value of this type is taken as the result of the addition.
Parameters : | f1 : Unsigned gray-scale (uint8 or uint16), signed (int32) or
f2 : Unsigned gray-scale (uint8 or uint16), signed (int32) or
|
---|---|
Returns : | y: Unsigned gray-scale (uint8 or uint16), signed (int32) or :
|
Examples
f = to_uint8([255, 255, 0, 10, 0, 255, 250])
g = to_uint8([ 0, 40, 80, 140, 250, 10, 30])
print addm(f,g)
print addm(g, 100)
prints out
[255 255 80 150 250 255 255]
[100 140 180 240 255 110 130]
Area closing
areaclose removes any pore (i.e., background connected component) with area less than a in binary image f. The connectivity is given by the structuring element Bc. This operator is generalized to gray-scale images by applying the binary operator successively on slices of f taken from higher threshold levels to lower threshold levels.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. a : Non negative integer. Bc : Structuring element (default: 3x3 cross). |
---|---|
Returns : | y : Same type of f |
Area opening
areaopen removes any grain (i.e., connected component) with area less than a of a binary image f. The connectivity is given by the structuring element Bc. This operator is generalized to gray-scale images by applying the binary operator successively on slices of f taken from higher threshold levels to lower threshold levels.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. a : Double non negative integer. Bc : Structuring element (default: 3x3 cross). |
---|---|
Returns : | y : Same type of f |
Examples
f=binary(to_uint8([
[1, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 1],
[0, 0, 0, 0, 1, 0, 0]]))
y=areaopen(f,4,secross())
print y
example 2
f=to_uint8([
[10, 11, 0, 0, 0, 0, 20],
[10, 0, 5, 8, 9, 0, 15],
[10, 0, 0, 0, 10, 0, 0]])
y=areaopen(f,4,secross())
print y
a=readgray('form-1.tif')
b=areaopen(a,500)
show(a)
show(b)
a=readgray('bloodcells.tif')
b=areaopen(a, 500)
show(a)
show(b)
Alternating Sequential Filtering
asf creates the image y by filtering the image f by n iterations of the close and open alternating sequential filter characterized by the structuring element B. The sequence of opening and closing is controlled by the parameter seq. ‘OC’ performs opening after closing, ‘CO’ performs closing after opening, ‘OCO’ performs opening after closing after opening, and ‘COC’ performs closing after opening after closing.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. seq : Order of operations, one of (‘OC’, ‘CO’, ‘OCO’, ‘COC’), default: ‘OC’. B : Structuring element (default: 3x3 cross). n : integer, optional
|
---|---|
Returns : | y : Image |
Reconstructive Alternating Sequential Filtering
asf creates the image y by filtering the image f by n iterations of the close by reconstruction and open by reconstruction alternating sequential filter characterized by the structuring element B. The structure element Bc is used in the reconstruction. The sequence of opening and closing is controlled by the parameter seq. ‘OC’ performs opening after closing, and ‘CO’ performs closing after opening.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. seq : Order of operations, one of (‘OC’, ‘CO’), default: ‘OC’. B : Structuring element (default: 3x3 cross). Bc : Structuring element (default: 3x3 cross). N : Number of iterations (default: 1). |
---|---|
Returns : | y : Same type of f |
Convert a gray-scale image into a binary image
binary converts a gray-scale image f into a binary image y by a threshold rule. A pixel in y has the value 1 if and only if the corresponding pixel in f has a value greater or equal to k.
Parameters : | f : Unsigned gray-scale (uint8 or uint16), signed (int32) or binary image. k : integer, optional
|
---|---|
Returns : | y : Binary image. |
Blob measurements from a labeled image.
Take measurements from the labeled image f.
The measurements are:
output controls the output format:
The region with label zero is not measured as it is normally the background. The measurement of region with label 1 appears at the first row of the output.
Parameters : | f : Gray-scale (uint8 or uint16) image. Labeled image. measurement : Measurement. One of (‘area’, ‘centroid’, ‘boundingbox’). output : {‘image’ [default], ‘data’}, optional
|
---|---|
Returns : | y : Gray-scale (uint8 or uint16) or binary image. |
N-Conditional bisector.
cbisector creates the binary image y by performing a filtering of the morphological skeleton of the binary image f, relative to the structuring element B. The strength of this filtering is controlled by the parameter n. Particularly, if n==0 , y is the morphological skeleton of f itself.
Parameters : | f : Binary image. B : Structuring element n : filtering rate (positive integer) |
---|---|
Returns : | y : Binary image. |
Conditional dilation
cdilate creates the image y by dilating the image f by the structuring element Bc conditionally to the image g. This operator may be applied recursively n times.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. g : Conditioning image. (Gray-scale or binary). Bc : Structuring element (default: 3x3 cross) n : Number of iterations (default: 1) |
---|---|
Returns : | y : Image |
Conditional erosion
cerode creates the image y by eroding the image f by the structuring element Bc conditionally to g. This operator may be applied recursively n times.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. g : Conditioning image. Bc : Structuring element (default: 3x3 cross) n : Number of iterations (default: 1) |
---|---|
Returns : | y : Image |
Morphological closing.
close creates the image y by the morphological closing of the image f by the structuring element Bc. In the binary case, the closing by a structuring element Bc may be interpreted as the intersection of all the binary images that contain the image f and have a hole equal to a translation of Bc. In the gray-scale case, there is a similar interpretation taking the functions umbra.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. Bc : Structuring Element Default: None (3x3 elementary cross). |
---|---|
Returns : | y : Image |
Closing by reconstruction.
closerec() creates the image y by a sup-reconstruction (with the connectivity defined by the structuring element Bc) of the image f from its dilation by Bdil.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image Bdil : Dilation structuring element (default 3x3 elementary cross) Bc : Connectivity structuring element (default: 3x3 elementary cross) |
---|---|
Returns : | y : Image (same type as f) |
Close-by-Reconstruction Top-Hat.
closerecth creates the image y by subtracting the image f of its closing by reconstruction, defined by the structuring elements Bc and Bdil.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. Bdil : Dilation structuring element (default: 3x3 cross) Bc : Connectivity structuring element (default: 3x3 cross) |
---|---|
Returns : | y : Gray-scale (uint8 or uint16) or binary image. |
Closing Top Hat.
closeth creates the image y by subtracting the image f of its morphological closing by the structuring element B.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. B : Structuring Element Default: None (3x3 elementary cross). |
---|---|
Returns : | y : ndarray of same shape and dtype as f |
Image transformation by conditional thickening.
cthick creates the binary image y by performing a thickening of the binary image f conditioned to the binary image g. The number of iterations of the conditional thickening is n and in each iteration the thickening is characterized by rotations of theta of the interval Iab.
Parameters : | f : Binary image. g : Binary image. Iab : Interval (default: homothick). n : Number of iterations, -1 for infinite (default: -1) theta : Degrees of rotation, 45 (default), 90, or 180. direction: ‘clockwise’ or ‘anti-clockwise’. : |
---|---|
Returns : | y : Binary image. |
Image transformation by conditional thinning.
cthin creates the binary image y by performing a thinning of the binary image f conditioned to the binary image g. The number of iterations of the conditional thinning is n and in each iteration the thinning is characterized by rotations of theta of the interval Iab.
Parameters : | f : Binary image. g : Binary image. Iab : Interval (default: homothin()). n : Number of iterations, -1 for infinite (default: -1) theta : Degrees of rotation, 45 (default), 90, or 180. direction: ‘clockwise’ or ‘anti-clockwise’. : |
---|---|
Returns : | y : Binary image. |
Detection of watershed from markers.
cwatershed creates the image R by detecting the domain of the catchment basins of f indicated by the marker image g, according to the connectivity defined by Bc.
If return_lines, L will be a labeled image of the catchment basins domain or just a binary image that presents the watershed lines. To know more about watershed and watershed from markers, see BeucMeye:93. The implementation of this function is based on LotuFalc:00.
WARNING: There is a common mistake related to the marker image g. If this image contains only zeros and ones, but it is not a binary image, the result will be an image with all ones. If the marker image is binary, you have to set this explicitly (e.g., cwatershed(f,g>0) or cwatershed(f,g.astype(bool)))
Parameters : | f : Gray-scale (uint8 or uint16) image. markers : Gray-scale (uint8 or uint16) or binary image. marker
Bc : Watershed connectivity (default: 3x3 cross) return_lines : Whether to return lines as well as regions (default: False) |
---|---|
Returns : | R : Gray-scale (uint8 or uint16) or binary image. L : Binary line image |
See also
Dilate an image by a structuring element.
dilate performs the dilation of image f by the structuring element B. Dilation is a neighbourhood operator that compares locally B with f, according to an intersection rule. Since Dilation is a fundamental operator to the construction of all other morphological operators, it is also called an elementary operator of Mathematical Morphology. When f is a gray-scale image, B may be a flat or non-flat structuring element.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. B : Structuring element (default: 3x3 cross). |
---|---|
Returns : | y: Same type as `f` : |
Superpose points, rectangles and lines on an image.
drawv creates the image y by a superposition of points, rectangles and lines of gray-level value on the image f. The parameters for each geometrical primitive are defined by each line in the data parameter. For points, they are represented by 2-D coordinates. For lines, they are drawn with the same convention used by points, with a straight line connecting them in the order given by the data matrix. For rectangles and filled rectangles, data should be an array of 2 x 2D points.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. data : Vector of coordinates. value : Gray value(s) for drawing.
geometry : { ‘point’, ‘line’, ‘rect’, ‘frect’ (filled rectangles)} |
---|---|
Returns : | y : Image of same type as f |
Interval to detect end-points.
endpoints creates an interval that is useful to detect end-points of curves (i.e., one pixel thick connected components) in binary images. It can be used to prune skeletons and to mark objects transforming them in a single pixel or closed loops if they have holes. There are two options available: ‘loop’, deletes all points but preserves loops if used in thin ; ‘homotopic’, deletes all points but preserves the last single point or loops.
Parameters : | option : {‘loop’ [default], ‘homotopic’}
|
---|---|
Returns : | Iab : Interval |
Erode an image by a structuring element.
erode performs the erosion of the image f by the structuring element b. Erosion is a neighbourhood operator that compairs locally b with f, according to an inclusion rule. Since erosion is a fundamental operator to the construction of all other morphological operators, it is called an elementary operator of Mathematical Morphology. When f is a gray-scale image, b may be a flat or non-flat structuring element.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. b : Structuring element (Default: 3x3 elementary cross). |
---|---|
Returns : | y : Image |
See also
Geodesic Distance Transform.
gdist creates the geodesic distance image y of the binary image f relative to the binary image g. The value of y at the pixel x is the length of the smallest path between x and f. The distances available are based on the Euclidean metrics and on metrics generated by a neighbourhood graph, that is characterized by a connectivity rule defined by the structuring element Bc. The connectivity for defining the paths is consistent with the metrics adopted to measure their length. In the case of the Euclidean distance, the space is considered continuos and, in the other cases, the connectivity is the one defined by Bc.
Parameters : | f : Binary image. g : Binary image. Marker image Bc : Structuring Element Default: None (3x3 elementary
metric: string default: none. ‘euclidean’ if specified. : |
---|---|
Returns : | y : uint16 (distance image). |
Morphological gradient.
gradm creates the image y by the subtraction of the erosion of the image f by Bero of the dilation of f by Bdil.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. Bdil : Structuring element for dilation (default: 3x3 cross). Bero : Structuring element for erosion (default: 3x3 cross). |
---|---|
Returns : | y : Image of same type as f. |
Gray-scale statistics for each labeled region.
Computes gray-scale statistics of each grain in the image. The grains regions are specified by the labeled image labels and the gray-scale information is specified by the image f. The statistics to compute is specified by the parameter measurement, which has the same options as in function stats. The parameter option defines the output format:
Parameters : | f : ndarray (gray-scale) labels : ndarray of integer dtype
measurement : {‘max’, ‘min’, ‘median’, ‘mean’, ‘sum’, ‘std’, ‘std1’ }
option : {‘image’ [default], ‘data’ }
|
---|---|
Returns : | y: Gray-scale (uint8 or uint16) image. :
|
Convert a binary image into a gray-scale image.
gray converts a binary image into a gray-scale image of a specified data type. The value k is assigned to the 1 pixels of f, while the 0 pixels are assigned to the minimum value associated to the specified data type.
Parameters : | f : Binary image. dtype : { ‘uint8’ [default], ‘uint16’, ‘int32’ } k : scalar, optional
|
---|---|
Returns : | y : Unsigned gray-scale (uint8 or uint16), signed (int32) or binary image. |
Remove basins with contrast less than h.
hmin sup-reconstructs the gray-scale image f from the marker created by the addition of the positive integer value h to f, using the connectivity Bc. This operator removes connected basins with contrast less than h. This function is very useful for simplifying the basins of the image.
Parameters : | f : Gray-scale (uint8 or uint16) image. h : Contrast (default: 1). Bc : Connectivity structuring element (default: 3x3 cross). |
---|---|
Returns : | y : Gray-scale (uint8 or uint16) or binary image. |
Remove peaks with contrast less than h.
y = hmax(f, h=1, Bc={3x3 cross})
hmax inf-reconstructs the gray-scale image f from the marker created by the subtraction of the positive integer value h from f, using connectivity Bc. This operator removes connected peaks with contrast less than h.
Parameters : | f : Gray-scale (uint8 or uint16) image. h : Contrast (default: 1). Bc : Connectivity structuring element (default: 3x3 cross). |
---|---|
Returns : | y : Gray-scale (uint8 or uint16) or binary image. |
Interval for homotopic thickening.
homothick creates an interval that is useful for the homotopic (i.e., that conserves the relation between objects and holes) thickening of binary images.
Returns : | Iab: Interval : |
---|
Interval for homotopic thinning.
homothin creates an interval that is useful for the homotopic (i.e., that conserves the relation between objects and holes) thinning of binary images.
Returns : | Iab : Interval |
---|
Create a structuring element from a pair of images.
img2se creates a flat structuring element B from the binary image fd or creates a non-flat structuring element B from the binary image fd and the gray-scale image f. fd represents the domain of B and f represents the image of the points in fd.
Parameters : | fd : Binary image.
flat : boolean, optional
f : ndarray of type (uint8 or uint16, int32, or bool), optional |
---|---|
Returns : | B : Structuring Element |
Intersection of inf-generating operators.
infcanon creates the image y by computing intersections of transformations of the image f by inf-generating (i.e., dual of the hit-or-miss) operators. These inf-generating operators are characterized by rotations (in the clockwise or anti-clockwise direction) of theta degrees of the interval Iab.
Parameters : | f : Binary image. Iab : Interval theta : Degrees of rotation (default: 45) direction : { ‘clockwise’ [default], ‘anti-clockwise’ } |
---|---|
Returns : | y : Binary image. |
Inf-generating.
infgen creates the image y by computing the transformation of the image f by the inf-generating operator (or dual of the hit-or-miss) characterized by the interval Iab.
Parameters : | f : Binary image. Iab : Interval |
---|---|
Returns : | y: Binary image. : |
Inf-reconstruction.
infrec creates the image y by an infinite number of recursive iterations (iterations until stability) of the dilation of f by Bc conditioned to g. We say the y is the inf-reconstruction of g from the marker f. For algorithms and applications, see Vinc:93b.
Parameters : | f : Marker image (gray or binary). g : Conditioning image (gray or binary). Bc : Connectivity Structuring element (default: 3x3 cross). |
---|---|
Returns : | y : Image |
Minima imposition.
Minima imposition on g based on the marker f. inpos creates an image y by filing the valleys of g that do not cover the connect components of f. A remarkable property of y is that its regional minima are exactly the connect components of g.
Parameters : | f : Binary image. Marker image. g : Gray-scale (uint8 or uint16) image. input image. Bc : connectivity structuring element (default: 3x3 cross). |
---|---|
Returns : | y : Gray-scale (uint8 or uint16) image. |
Rotate an interval
interot rotates the interval Iab by theta.
Parameters : | Iab : Interval theta : Degrees of rotation. Should be a multiple of 45 degrees. If not,
direction : { ‘clockwise’ [default], ‘anti-clockwise’ } |
---|---|
Returns : | Irot : Interval |
Examples
b1 = endpoints()
b2 = interot(b1)
print intershow(b1)
print intershow(b2)
Intersection of images.
intersec creates the image y by taking the pixelwise minimum between the images f1, f2, f3, f4, and f5 . When f1, f2, f3, f4, and f5 are binary images, y is the intersection of them.
Parameters : | f1 : Image (gray or binary) or constant. f2 : Image (gray or binary) or constant. f3 : Image (gray or binary) or constant, optional. f4 : Image (gray or binary) or constant, optional. f5 : Image (gray or binary) or constant, optional. |
---|---|
Returns : | y : Image |
Visualize an interval.
intershow creates a representation for an interval using 0, 1 and . (don’t care).
Parameters : | Iab : Interval |
---|---|
Returns : | s : string representation of the interval. |
Examples
print intershow(homothick())
prints out
0 0 0
. 0 .
0 0 0
Check for binary image
isbinary returns True if the datatype of the input image is binary. A binary image has just the values 0 and 1.
Parameters : | f : Image |
---|---|
Returns : | is_b : bool
|
Transforms f into a binary image
Parameters : | f : image of any type, consisting only of 0s and 1s. |
---|---|
Returns : | fbin : binary image |
Check if two images are equal
isequal compares the images f1 and f2 and returns True, if f1 and `f2 have the same size and f1(x) == f2(x), for all pixel x; otherwise, returns False.
Parameters : | f1 : Unsigned gray-scale (uint8 or uint16), signed (int32) or binary image. f2 : Unsigned gray-scale (uint8 or uint16), signed (int32) or binary image. |
---|---|
Returns : | r_eq : Boolean
|
Label the flat zones of gray-scale images.
labelflat creates the image y by labeling the flat zones of f, according to the connectivity defined by the structuring element Bc. A flat zone is a connected region of the image domain in which all the pixels have the same gray-level (lambda=0). When lambda is different than zero, a quasi-flat zone is detected where two neighboring pixels belong to the same region if their difference gray-levels is smaller or equal lambda. The minimum label of the output image is 1 and the maximum is the number of flat-zones in the image.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. Bc : Structuring element, optional
lambda_ : Default: 0. Connectivity given by |f(q)-f(p)|<=lambda_. |
---|---|
Returns : | y: ndarray of same shape as `f` :
|
Last erosion.
y = lastero(f, B=None)
lastero creates the image y by computing the last erosion by the structuring element B of the image f . The objects found in y are the objects of the erosion by nB that can not be reconstructed from the erosion by (n+1)B , where n is a generic non negative integer. The image y is a proper subset of the morphological skeleton by B of f .
Parameters : | f : Binary image. B : Structuring Element (default: 3x3 elementary cross). |
---|---|
Returns : | y : Binary image. |
Morphological opening.
open creates the image y by the morphological opening of the image f by the structuring element b. In the binary case, the opening by the structuring element b may be interpreted as the union of translations of b included in f. In the gray-scale case, there is a similar interpretation taking the functions umbra.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. b : Structuring element (default: 3x3 elementary cross). |
---|---|
Returns : | y : Image |
Opening by reconstruction.
openrec creates the image y by an inf-reconstruction of the image f from its erosion by bero, using the connectivity defined by Bc.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. Bero : Eroding structuring element (default: 3x3 cross). Bc : Connecting structuring element (default: 3x3 cross). |
---|---|
Returns : | y : Image (same type as f). |
Open-by-Reconstruction Top-Hat.
openrecth creates the image y by subtracting the open by reconstruction of f, defined by the structuring elements Bero and Bc, of f itself.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. Bero : Erosion structuring element (default: 3x3 cross). Bc : Connectivity structuring element (default: 3x3 cross). |
---|---|
Returns : | y : Image of same type as f. |
Opening Top Hat.
openth creates the image y by subtracting the morphological opening of f by the structuring element b of f itself.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. b : Structuring element (default: 3x3 cross). |
---|---|
Returns : | y : Image of same type as f. |
Open transform.
Compute the open transform of a binary image. The value of the pixels in the open transform is radius of the smallest disk, which if used to open, nullifies that pixel.
The disk sequence must satisfy the following: if r > s, then rB is sB-open, i.e. open(rB,sB) == rB. Note that the Euclidean disk does not satisfy this property in the discrete grid. This function also computes the reconstructive open transform by adding the suffix ‘-rec’ in the ‘type’ parameter.
Parameters : | f : binary image. type : { ‘octagon’ [default], ‘chessboard’, ‘citi-block’, ‘linear-h’, ‘linear-v’, ‘linear-45r’, ‘user’ }
n : integer, optional
Bc : Structuring element, optional
Buser : structure element, optional
|
---|---|
Returns : | y : Gray-scale (uint16) image. |
Pattern spectrum (also known as granulometric size density).
Compute the Pattern Spectrum of a binary image. See Mara:89b . The pattern spectrum is the histogram of the open transform, not taking the zero values.
Parameters : | f : Binary image. type : { ‘octagon’ [default], ‘chessboard’, ‘citi-block’, ‘linear-h’, ‘linear-v’, ‘linear-45r’, ‘user’ }
n : integer, optional
Bc : Structuring element, optional
Buser : structure element, optional
|
---|---|
Returns : | h : ndarray |
Regional Maximum.
regmax creates a binary image y by computing the regional maxima of f, according to the connectivity defined by the structuring element Bc. A regional maximum is a flat zone not surrounded by flat zones of higher gray values.
Parameters : | f : Gray-scale image. Bc : Connectivity structuring element (default: 3x3 cross). |
---|---|
Returns : | y : Binary image. |
Regional Minimum (with generalized dynamics).
regmin creates a binary image by computing the regional minima of f, according to the connectivity defined by the structuring element Bc.
A regional minimum is a flat zone not surrounded by flat zones of lower gray values. A flat zone is a maximal connected component of a gray-scale image with same pixel values.
Parameters : | f : Gray-scale (uint8 or uint16) image. Bc : Connectivity structuring element (default: 3x3 cross). option : ‘binary’
|
---|---|
Returns : | y : Gray-scale (uint8 or uint16) or binary image. |
Create an interval from a pair of structuring elements.
se2interval creates the interval [a,b] from the structuring elements a and b such that a is less or equal b.
Parameters : | a : Structuring element left extremity b : Structuring element right extremity |
---|---|
Returns : | Iab : Interval |
Create a Hit-or-Miss Template (or interval) from a pair of structuring elements.
se2hmt creates the Hit-or-Miss Template (HMT), also called interval [A,Bc] from the structuring elements A and Bc such that A is included in the complement of Bc . The only difference between this function and se2interval is that here the second structuring element is the complement of the one used in the other function. The advantage of this function over se2interval is that this one is more flexible in the use of the structuring elements as they are not required to have the same size.
Parameters : | A : Structuring Element Left extremity. Bc : Structuring Element Complement of the right extremity. |
---|---|
Returns : | Iab : Interval |
Transforms the Bc structuring element into an array of indices so that f.flat[Bi[i]] corresponds to the i-th element where Bc is true
Equivalent of:
g = zeros(f.shape)
h,w=Bc.shape
g[:h,:w]=Bc
Bi, = where(g.ravel())
This is useful for implementing many functions. See the implementation of label() for an example.
Parameters : | f : image Bc : structuring element |
---|---|
Returns : | Bi : array of indice offsets. |
Create a box structuring element.
sebox creates the structuring element B formed by r successive Minkowski additions of the elementary square (i.e., the 3x3 square centered at the origin) with itself. If R=0, B is the unitary set that contains the origin. If R=1, B is the elementary square itself.
Parameters : | r : integer, optional
|
---|---|
Returns : | B : Structuring Element |
Diamond structuring element and elementary 3x3 cross.
secross creates the structuring element B formed by r successive Minkowski additions of the elementary cross (i.e., the 3x3 cross centered at the origin) with itself. If r=0, B is the unitary set that contains the origin. If r=1 (the default), B is the elementary cross itself.
Parameters : | r : integer, optional
|
---|---|
Returns : | B : Structuring Element |
Create a disk or a semi-sphere structuring element.
sedisk creates a flat structuring element B that is disk under the metric metric , centered at the origin and with radius r or a non-flat structuring element that is a semi-sphere under the metric metric, centered at (0, h) and with radius r . this structuring element can be created on the 1D, 2D or 3D space.
Parameters : | r : Non-negative integer. Disk radius (default: 3) dim : {1, 2 [default], 3}
metric : {‘euclidean’ [default], ‘city-block’, ‘octagon’, ‘chessboard’ } flat : boolean, optional
h : double, optional
|
---|---|
Returns : | B : Structuring Element |
Create a line structuring element.
seline creates a structuring element B that is a line segment that has an extremity at the origin, length length and angle theta (0 degrees is east direction, clockwise). If length==0 , it generates the origin.
Note that length is simply the number of on-pixels. Therefore, in diagonal directions it is off by a factor of sqrt{2}.
Parameters : | length : Length of line (default: 3) theta : Degrees, clockwise (default: 0, i.e. east) |
---|---|
Returns : | B : Structuring Element |
Examples
>>> import pymorph
>>> pymorph.seline(2)
array([[0],
[1],
[1]], dtype=uint8)
>>> pymorph.seline(4)
array([[0],
[0],
[0],
[1],
[1],
[1],
[1]], dtype=uint8)
>>> pymorph.seline(3,45)
array([[0, 0, 0],
[0, 1, 1],
[0, 0, 1]], dtype=uint8)
Rotate a structuring element. serot rotates structuring element b by theta degrees.
Parameters : | B : Structuring Element theta : Degrees of rotation. Should be a multiple of 45 degrees. If not,
direction : { ‘clockwise’ [default], ‘anti-clockwise’ } |
---|---|
Returns : | brot : structuring element |
Display a structuring element as an image.
seshow used with the option ‘expand’ generates an image y that is a suitable graphical representation of the structuring element b. This function is useful to convert a structuring element to an image. The origin of the structuring element is at the center of the image. If b is flat, y is binary, otherwise, y is signed int32 image. When using the option ‘non-flat’, the output `y is always a signed int32 image.
Parameters : | b : Structuring element option : { ‘normal’ [default], ‘expand’, ‘non-flat’ }
|
---|---|
Returns : | y : Gray-scale (uint8 or uint16) or binary image. |
N-1 iterative Minkowski additions
sesum creates the structuring element Bn from N - 1 iterative Minkowski additions with the structuring element B.
Parameters : | B : Structuring element (Default: 3x3 elementary cross). N : Non-negative integer. Default: 1. |
---|---|
Returns : | Bn : Structuring Element |
Translate a structuring element
setrans translates a structuring element by a specific value.
Parameters : | Bi : Structuring Element t : translation amount |
---|---|
Returns : | Bo : Structuring element of same type as Bi |
Reflect a structuring element
sereflect reflects a structuring element about the origin
Parameters : | Bi : Structuring Element |
---|---|
Returns : | Bo : Structuring element of same type as Bi |
Dilate one structuring element by another
sedilate dilates an structuring element by another. The main difference between this dilation and dilate is that the dilation between structuring elements are not bounded, returning another structuring element usually larger than anyone of them. This gives the composition of the two structuring elements by Minkowski addition.
Parameters : | B1 : Structuring Element B2 : Structuring Element |
---|---|
Returns : | Bo: Structuring Element : |
Union of structuring elements
seunion creates a structuring element from the union of two structuring elements.
Parameters : | B1 : Structuring element B2 : Structuring element |
---|---|
Returns : | B : structure element
|
Morphological skeleton (Medial Axis Transform).
skelm creates the image y by computing the morphological skeleton by B of the image f. In this case, the pixels of value 1 in y are center of maximal balls (generated from B ) included in f. This is also called Medial Axis. If return_binary is False, the non zeros pixels in y are the radius plus 1 of the maximal balls. This is called Medial Axis Transform or valued morphological skeleton.
Parameters : | f : Binary image. B : Structuring Element Default: None (3x3 elementary
return_binary : boolean, optional
|
---|---|
Returns : | y : Gray-scale (uint8 or uint16) or binary image. |
Morphological skeleton reconstruction (inverse medial axis transform).
skelmrec reconstructs the valued morphological skeleton to recover the original image.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. B : Structuring element (default: 3x3 cross). |
---|---|
Returns : | y : Binary image. |
Skeleton of Influence Zone - also know as Generalized Voronoi Diagram
skiz creates the labeled image by detecting the lines which are equidistant to two or more connected components of f, according to the connectivity defined by Bc.
labeled will be a labeled image, while lines will be a binary image of the lines between the regions Depending on with the flag
When the connected objects of f are single points, the skiz is the Voronoi diagram.
Parameters : | f : Binary image. Bc : Structuring Element (default: 3x3 cross).
return_lines : boolean, optional
metric : str, optional
|
---|---|
Returns : | y : Gray-scale (uint8 or uint16) image. lines : binary image.
|
Subtraction of two images, with saturation.
subm creates the image y by pixelwise subtraction of the image f2 from the image f1. When the subtraction of the values of two pixels is negative, 0 is taken as the result of the subtraction. When f1 and f2 are binary images, y represents the set subtraction of f2 from f1.
Parameters : | f1 : Unsigned gray-scale (uint8 or uint16), signed (int32) or binary image. f2 : Unsigned gray-scale (uint8 or uint16), signed (int32) or binary image. Or constant. |
---|---|
Returns : | y : ndarray of same shape and dtype as f1 |
Examples
f = to_uint8([255, 255, 0, 10, 20, 10, 0, 255, 255])
g = to_uint8([10, 20, 30, 40, 50, 40, 30, 20, 10])
print subm(f, g)
print subm(f, 100)
print subm(100, f)
a = readgray('boxdrill-C.tif')
b = readgray('boxdrill-B.tif')
c = subm(a,b)
show(a)
show(b)
show(c)
Union of sup-generating or hit-miss operators.
supcanon creates the image y by computing the union of transformations of the image f by sup-generating operators. These hit-miss operators are characterized by rotations (in the clockwise or anti-clockwise direction) of theta degrees of the interval Iab.
Parameters : | f : Binary image. Iab : Interval theta : {45 [default], 90, 180 }
direction : {‘clockwise’ [default], ‘anti-clockwise’}
Returns : —— : y : Binary image. |
---|
Sup-generating (hit-miss).
supgen creates the binary image y by computing the transformation of the image f by the sup-generating operator characterized by the interval Iab. The sup-generating operator is just a relaxed template matching, where the criterion to keep a shape is that it be inside the interval Iab. Note that we have the classical template matching when a==b. Note also that the sup-generating operator is equivalent to the classical hit-miss operator.
Parameters : | f : Binary image. interval : Interval |
---|---|
Returns : | y : Binary image. |
Sup-reconstruction.
suprec creates the image y by an infinite number of iterations (until stability) of the erosion of f by Bc conditioned to g. We say that y is the sup-reconstruction of g from the marker f.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image.
g : Gray-scale (uint8 or uint16) or binary image.
Bc : Connectivity structuring element (default: 3x3 cross) |
---|---|
Returns : | y : Image |
Generate a graphical representation of overlaid binary images. Does not actually display the image anywhere!
Generate an expanded binary image as a graphical representation of up to three binary input images. The 1-pixels of the first image are represented by square contours, the pixels of the optional second image are represented by circles and for the third image they are represented by shaded squares. This function is useful to create graphical illustration of small images.
Parameters : | f1 : Binary image. f2 : Binary image. Default: None. f3 : Binary image. Default: None. factor : Double Default: 17. Expansion factor for the output
|
---|---|
Returns : | y : Binary image. |
Symmetric difference between two images
symdiff creates the image y by taken the union of the subtractions of f1 from f2 and f2 from f1. When f1 and f2 are binary images, y represents the set of points that are in f1 and not in f2 or that are in f2 and not in f1.
Parameters : | f1 : Gray-scale (uint8 or uint16) or binary image. f2 : Gray-scale (uint8 or uint16) or binary image. |
---|---|
Returns : | y : Image |
Image transformation by thickening.
thick creates the binary image y by performing a thickening of the binary image f. The number of iterations of the thickening is n and each iteration is performed by union of f with the points that are detected in f by the hit-miss operators characterized by rotations of theta degrees of the interval Iab.
Parameters : | f : Binary image. Iab : Interval Default: None (homothick). n : Number of iterations. Default: -1, i.e., infinite theta : Degrees of rotation: 45 (default), 90, or 180. direction : One of (‘clockwise’, ‘anti-clockwise’). |
---|---|
Returns : | y : Binary image. |
Image transformation by thinning.
thin creates the binary image y by performing a thinning of the binary image f. The number of iterations of the thinning is n and each iteration is performed by subtracting the points that are detect in f by hit-miss operators characterized by rotations of theta of the interval Iab. When n is infinite and the interval is homothin (default conditions), thin gives the skeleton by thinning.
Parameters : | f : Binary image. Iab : Interval Default:(homothin). n : Number of iterations. (default: -1, i.e. infinite) theta : Double Default: 45. Degrees of rotation: 45, 90, or direction : One of (‘clockwise’, ‘anti-clockwise’). default: clockwise. |
---|---|
Returns : | y : Binary image. |
Union of images.
union creates the image y by taking the pixelwise maximum between the images given. When the images are binary images, y represents the union of them.
Parameters : | f1 : Gray-scale (uint8 or uint16) or binary image. f2 : Gray-scale (uint8 or uint16) or binary image. Or constant args : Gray-scale (uint8 or uint16) or binary images. |
---|---|
Returns : | y : Image |
Watershed transform.
watershed creates the image y by detecting the domain of the catchment basins of f, according to the connectivity defined by Bc.
The implementation of this function is based on VincSoil:91.
Parameters : | f : Gray-scale (uint8 or uint16) or binary image. return_lines : boolean
|
---|---|
Returns : | W : labeled image Wl : separation lines |
Returns the maximum value supported by a datatype
Parameters : | type : { ‘uint8’ [default], ‘uint16’, ‘int32’} |
---|---|
Returns : | max : numeric value
|
Convert an image to an int32 image.
img = to_int32(f)
to_int32 clips the input image between the values -2147483648 and 2147483647 and converts it to the signed 32-bit datatype.
Parameters : | f : Any image |
---|---|
Returns : | img : The converted image |
Convert an image to an uint8 image.
to_uint8 clips the input image between the values 0 and 255 and converts it to the unsigned 8-bit datatype.
Parameters : | f : Any image |
---|---|
Returns : | img : Gray-scale uint8 image. The converted image |
Examples
print to_uint8([-3, 0, 8, 600])
prints out
[ 0 0 8 255]
Convert an image to a uint16 image.
to_uint16 clips the input image between the values 0 and 65535 and converts it to the unsigned 16-bit datatype.
Parameters : | f : Any image |
---|---|
Returns : | img : The converted image |
Transform (possibly colour) image to gray image.
If input file is a color RGB image, it is converted to gray-scale using the equation:
I = 0.2989 * R + 0.587 * G + 0.114 * B
Parameters : | f : ndarray of shape HxWx3
|
---|---|
Returns : | g : gray image of shape HxW same type as input |
Return the image datatype string
datatype returns a string that identifies the pixel datatype of the image f.
Consider using f.dtype.
Parameters : | f : Unsigned gray-scale (uint8 or uint16), signed (int32) or binary image
|
---|---|
Returns : | type : str
|
Addition for dilation. Assumes that the minimum value of dtype(f) is to represent -inf (where -inf + c == -inf, for all c)
Parameters : | f : Gray-scale or binary image. c : Gray-scale, binary image, or constant. |
---|---|
Returns : | a : f + c (but with correct implementation of -inf) |
Converts image representation from matrix to set
Return tuple with array of pixel coordinates and array of corresponding pixel values. The input image is in the matrix format, like the structuring element, where the origin (0,0) is at the center of the matrix.
Parameters : | A : ndarray
|
---|---|
Returns : | C : array of pixel coordinates V : array of pixel values corresponding to the coordinates of C |
See also
Converts image representation from set to matrix
Return an image in the matrix format built from a tuple of an array of pixel coordinates and a corresponding array of pixel values
Parameters : | A : tuple (ndarray, ndarray) or tuple (ndarray,)
|
---|---|
Returns : | M : Image in matrix format, origin (0,0) at the matrix center |
Examples
coords = to_int32([ [0,0] , [-1,0] , [1,1]])
print set2mat( (coords,) )
prints out
[0,1,0]
[0,1,0]
[0,0,1]
pads f with value so that Bc can be applied scaled by scale.
Parameters : | f : Image Bc : Structuring Element (connectivity). value : dtype
scale : (default=1). |
---|---|
Returns : | y : The converted image |
Create a binary image of a text.
text creates the binary image y of the text txt . The background of y is 0, while its foreground is 1. The text should be composed only by lower and upper case letters.
Parameters : | txt : Text as a string |
---|---|
Returns : | y : Binary image. |