Contents: .. toctree:
:maxdepth: 2
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.
Addition for dilation
a = add4dilate(f, c)
f: Gray-scale (uint8 or uint16) or binary image. Image c: Gray-scale (uint8 or uint16) or binary image. Constant
a: Image f + c
Addition of two images, with saturation.
y = addm(f1, f2)
binary image.
binary image. Or constant.
binary image.
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.
# # example 1 # f = to_uint8([255, 255, 0, 10, 0, 255, 250]) g = to_uint8([ 0, 40, 80, 140, 250, 10, 30]) y1 = addm(f,g) print y1 y2 = addm(g, 100) print y2 # # example 2 # a = readgray(‘keyb.tif’) b = addm(a,128) show(a) show(b)
Area closing
y = areaclose(f, a, Bc=None)
f: Gray-scale (uint8 or uint16) or binary image. a: Double non negative integer. Bc: Structuring Element Default: None (3x3 elementary cross). (
connectivity).
y: Same type of f
areaclose removes any pore (i.e., background 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.
# # example 1 # a=readgray(‘form-1.tif’) b=areaclose(a,400) show(a) show(b) # # example 2 # a=readgray(‘n2538.tif’) b=areaclose(a,400) show(a) show(b)
Area opening
y = areaopen(f, a, Bc=None)
f: Gray-scale (uint8 or uint16) or binary image. a: Double non negative integer. Bc: Structuring Element Default: None (3x3 elementary cross). (
connectivity).
y: Same type of f
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.
# # example 1 # 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 # # example 3 # a=readgray(‘form-1.tif’); b=areaopen(a,500); show(a); show(b); # # example 4 # a=readgray(‘bloodcells.tif’); b=areaopen(a,500); show(a); show(b);
Alternating Sequential Filtering
y = asf(f, SEQ=”OC”, b=None, n=1)
f: Gray-scale (uint8 or uint16) or binary image. SEQ: String Default: “OC”. ‘OC’, ‘CO’, ‘OCO’, ‘COC’. b: Structuring Element Default: None (3x3 elementary cross). n: Non-negative integer. Default: 1. (number of iterations).
y: Image
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.
# # example 1 # f=readgray(‘gear.tif’) g=asf(f,’oc’,secross(),2) show(f) show(g) # # example 2 # f=readgray(‘fabric.tif’) g=asf(f,’oc’,secross(),3) show(f) show(g)
Reconstructive Alternating Sequential Filtering
y = asfrec(f, SEQ=”OC”, b=None, bc=None, n=1)
f: Gray-scale (uint8 or uint16) or binary image. SEQ: String Default: “OC”. Values: “OC” or “CO”. b: Structuring Element Default: None (3x3 elementary cross). bc: Structuring Element Default: None (3x3 elementary cross). n: Non-negative integer. Default: 1. (number of iterations).
y: Same type of f
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.
# f=readgray(‘fabric.tif’) g=asfrec(f,’oc’,secross(),secross(),3) show(f) show(g)
benchmarking main functions of the toolbox.
bench(count=10)
function.
bench measures the speed of many of SDC Morphology Toolbox functions in seconds. An illustrative example of the output of bench is, for a MS-Windows 2000 Pentium 4, 2.4GHz, 533MHz system bus, machine: SDC Morphology Toolbox V1.2 27Sep02 Benchmark Made on Wed Jul 16 15:33:17 2003 computer= win32 image filename= csample.jpg width= 640 , height= 480 Function time (sec.) 1. Union bin 0.00939999818802 2. Union gray-scale 0.00319999456406 3. Dilation bin, secross 0.0110000014305 4. Dilation gray, secross 0.00780000686646 5. Dilation gray, non-flat 3x3 SE 0.0125 6. Open bin, secross 0.0125 7. Open gray-scale, secross 0.0141000032425 8. Open gray, non-flat 3x3 SE 0.0235000014305 9. Distance secross 0.021899998188 10. Distance Euclidean 0.0264999985695 11. Geodesic distance secross 0.028100001812 12. Geodesic distance Euclidean 0.303100001812 13. Area open bin 0.0639999985695 14. Area open gray-scale 0.148500001431 15. Label secross 0.071899998188 16. Regional maximum, secross 0.043700003624 17. Open by rec, gray, secross 0.0515000104904 18. ASF by rec, oc, secross, 1 0.090600001812 19. Gradient, gray-scale, secross 0.0171999931335 20. Thinning 0.0984999895096 21. Watershed 0.268799996376 Average 0.0632523809161
Convert a gray-scale image into a binary image
y = binary(f, k1=1)
binary image.
k1: Double Default: 1. Threshold value.
y: 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 k1 .
# # example 1 # a = array([0, 1, 2, 3, 4]) b=binary(a) print b # # example 2 # a=readgray(‘3.tif’) b=binary(a,82) show(a) show(b)
Blob measurements from a labeled image.
y = blob(fr, measurement, option=”image”)
fr: Gray-scale (uint8 or uint16) image. Labeled image. measurement: String Default: “”. Choice from ‘AREA’, ‘CENTROID’,
or ‘BOUNDINGBOX’.
results as a binary image; ‘data’: results a column vector of measurements (double).
y: Gray-scale (uint8 or uint16) or binary image.
Take measurements from the labeled image fr. The measurements are:
area, centroid, bounding rectangle.
‘IMAGE’: the result is an image; ‘DATA’: the result is a double column vector with the measurement for each blob.
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.
# # example 1 # fr=to_uint8([
[1,1,1,0,0,0], [1,1,1,0,0,2], [1,1,1,0,2,2]])
f_area=blob(fr,’area’) print f_area f_cent=blob(fr,’centroid’) print f_cent f_bb=blob(fr,’boundingbox’) print f_bb d_area=blob(fr,’area’,’data’) print d_area d_cent=blob(fr,’centroid’,’data’) print d_cent d_bb=blob(fr,’boundingbox’,’data’) print d_bb # # example 2 # f=readgray(‘blob3.tif’) fr=label(f) g=blob(fr,’area’) show(f) show(g) # # example 3 # f=readgray(‘blob3.tif’) fr=label(f) centr=blob(fr,’centroid’) show(f,dilate(centr)) # # example 4 # f=readgray(‘blob3.tif’) fr=label(f) box=blob(fr,’boundingbox’) show(f,box)
Generate a graphical representation of overlaid binary images.
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. |
|---|
N-Conditional bisector.
y = cbisector(f, B, n)
f: Binary image. B: Structuring Element n: positive integer ( filtering rate)
y: Binary image.
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.
# a=readgray(‘blob2.tif’) b=cbisector(a,sebox(),1) c=cbisector(a,sebox(),3) d=cbisector(a,sebox(),10) show(a,b) show(a,c) show(a,d)
Dilate an image conditionally.
y = cdilate(f, g, b=None, n=1)
f: Gray-scale (uint8 or uint16) or binary image. g: Gray-scale (uint8 or uint16) or binary image. Conditioning
image.
b: Structuring Element Default: None (3x3 elementary cross). n: Non-negative integer. Default: 1. (number of iterations).
y: Image
cdil creates the image y by dilating the image f by the structuring element b conditionally to the image g . This operator may be applied recursively n times.
# # example 1 # f = binary(to_uint8([[1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0,]])) g = binary(to_uint8([[1, 1, 1, 0, 0, 1, 1], [1, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0]])); y1=cdilate(f,g,secross()) y2=cdilate(f,g,secross(),3) # # example 2 # f = to_uint8([ [ 0, 0, 0, 80, 0, 0], [ 0, 0, 0, 0, 0, 0], [ 10, 10, 0, 255, 0, 0]]) g = to_uint8([ [ 0, 1, 2, 50, 4, 5], [ 2, 3, 4, 0, 0, 0], [ 12, 255, 14, 15, 16, 17]]) y1=cdilate(f,g,secross()) y2=cdilate(f,g,secross(),3) # # example 3 # g=readgray(‘pcb1bin.tif’) f=frame(g,5,5) y5=cdilate(f,g,secross(),5) y25=cdilate(f,g,secross(),25) show(g) show(g,f) show(g,y5) show(g,y25) # # example 4 # g=neg(readgray(‘n2538.tif’)) f=intersec(g,0) f=draw(f,’LINE:40,30,60,30:END’) y1=cdilate(f,g,sebox()) y30=cdilate(f,g,sebox(),30) show(g) show(f) show(y1) show(y30)
Center filter.
center() computes the morphological center of f w.r.t. to the structuring element b.
Erode an image conditionally.
y = cerode(f, g, b=None, n=1)
f: Gray-scale (uint8 or uint16) or binary image. g: Gray-scale (uint8 or uint16) or binary image. Conditioning
image.
b: Structuring Element Default: None (3x3 elementary cross). n: Non-negative integer. Default: 1. (number of iterations).
y: Image
cero creates the image y by eroding the image f by the structuring element b conditionally to g . This operator may be applied recursively n times.
# f = neg(text(‘hello’)) show(f) g = dilate(f,seline(7,90)) show(g) a1=cerode(g,f,sebox()) show(a1) a13=cerode(a1,f,sebox(),13) show(a13)
Close holes of binary and gray-scale images.
y = close_holes(f, Bc=None)
f: Gray-scale (uint8 or uint16) or binary image. Bc: Structuring Element Default: None (3x3 elementary cross). (
connectivity).
y: (same datatype of f ).
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.
# # example 1 # a = readgray(‘pcb1bin.tif’) b = close_holes(a) show(a) show(b) # # example 2 # a = readgray(‘boxdrill-B.tif’) b = close_holes(a) show(a) show(b)
Morphological closing.
y = close(f, b=None)
f: Gray-scale (uint8 or uint16) or binary image. b: Structuring Element Default: None (3x3 elementary cross).
y: Image
close creates the image y by the morphological closing of the image f by the structuring element b . In the binary case, the closing by a structuring element B 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 B . In the gray-scale case, there is a similar interpretation taking the functions umbra.
# # example 1 # f=readgray(‘blob.tif’) bimg=readgray(‘blob1.tif’) b=img2se(bimg) show(f) show(close(f,b)) show(close(f,b),gradm(f)) # # example 2 # f = readgray(‘form-1.tif’) show(f) y = close(f,sedisk(4)) show(y) # # example 3 # f = readgray(‘n2538.tif’) show(f) y = close(f,sedisk(3)) show(y)
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.
y = closerecth(f, bdil=None, bc=None)
f: Gray-scale (uint8 or uint16) or binary image. bdil: Structuring Element Default: None (3x3 elementary cross).
(dilation)
( connectivity)
y: Gray-scale (uint8 or uint16) or binary image.
closerecth creates the image y by subtracting the image f of its closing by reconstruction, defined by the structuring elements bc and bdil .
# a = readgray(‘danaus.tif’) show(a) b = closerecth(a,sebox(4)) show(b)
Closing Top Hat.
y = closeth(f, b=None)
f: Gray-scale (uint8 or uint16) or binary image. b: Structuring Element Default: None (3x3 elementary cross).
).
closeth creates the image y by subtracting the image f of its morphological closing by the structuring element b .
# a = readgray(‘danaus.tif’) show(a) b = closeth(a,sebox(5)) show(b)
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 : Dimension to concatenate (string):
img0, img1, ... : Images to concatenate |
|---|---|
| Returns: | img : resulting image (of the same type as inputs). |
Image transformation by conditional thickening.
y = cthick(f, g, Iab=None, n=-1, theta=45, DIRECTION=”CLOCKWISE”)
f: Binary image. g: Binary image. Iab: Interval Default: None (homothick). n: Non-negative integer. Default: -1. Number of
iterations.
‘ANTI-CLOCKWISE’.
y: Binary image.
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 .
# # example 1 # f=readgray(‘blob2.tif’) show(f) t=se2hmt(binary([[0,0,0],[0,0,1],[1,1,1]]),
binary([[0,0,0],[0,1,0],[0,0,0]]))
print intershow(t) f1=thick(f,t,40); # The thickening makes the image border grow show(f1) # # example 2 # f2=cthick(f,neg(frame(f)),t,40) # conditioning to inner pixels fn=cthick(f,neg(frame(f)),t) #pseudo convex hull show(f2) show(fn,f)
Image transformation by conditional thinning.
y = cthin(f, g, Iab=None, n=-1, theta=45, DIRECTION=”CLOCKWISE”)
f: Binary image. g: Binary image. Iab: Interval Default: None (homothin). n: Non-negative integer. Default: -1. Number of
iterations.
ANTI-CLOCKWISE’.
y: Binary image.
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 .
R = cwatershed(f, g, Bc=None, return_lines=False) R,L = cwatershed(f, g, Bc=None, return_lines=True)
Detection of watershed from markers.
f: Gray-scale (uint8 or uint16) image. markers: Gray-scale (uint8 or uint16) or binary image. marker
image: binary or labeled.
cross). (watershed connectivity)
return_lines: Whether to return lines as well as regions (default: False)
Y: Gray-scale (uint8 or uint16) or binary image.
cwatershed creates the image y by detecting the domain of the catchment basins of f indicated by the marker image g , according to the connectivity defined by Bc . According to the flag LINEREG y 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)))
# # example 1 # a = to_uint8([ [10, 10, 10, 10, 10, 10, 10], [10, 9, 6, 18, 6, 5, 10], [10, 9, 6, 18, 6, 8, 10], [10, 9, 9, 15, 9, 9, 10], [10, 9, 9, 15, 12, 10, 10], [10, 10, 10, 10, 10, 10, 10]]) b = (a == 6) print cwatershed(a,b) print cwatershed(a,b,secross(),return_lines=True)[1] # # example 2 # f=readgray(‘astablet.tif’) grad=gradm(f) mark=regmin(hmin(grad,17)) w=cwatershed(grad,mark) show(grad) show(mark) show(w)
Return the image datatype string
type = datatype(f)
binary image. Any image
‘uint8’, ‘uint16’ or ‘int32’
datatype returns a string that identifies the pixel datatype of the image f .
Dilate an image by a structuring element.
y = dilate(f, b=None)
f: Gray-scale (uint8 or uint16) or binary image. b: Structuring Element Default: None (3x3 elementary cross).
y: Image
dil 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.
# # example 1 # f=binary([
[0, 0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0]])
b=binary([1, 1, 0]) dilate(f,b) f=to_uint8([
[ 0, 1, 2, 50, 4, 5], [ 2, 3, 4, 0, 0, 0], [12, 255, 14, 15, 16, 17]])
dilate(f,b) # # example 2 # f=binary(readgray(‘blob.tif’)) bimg=binary(readgray(‘blob1.tif’)) b=img2se(bimg) show(f) show(dilate(f,b)) show(dilate(f,b),gradm(f)) # # example 3 # f=readgray(‘pcb_gray.tif’) b=sedisk(5) show(f) show(dilate(f,b))
Distance transform.
y = dist(f, Bc=None, metric=None)
f: Binary image. Bc: Structuring Element Default: None (3x3 elementary
cross). (connectivity)
Euclidean.
option.
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 .
# # example 1 # a = frame(binary(ones((5,9))),2,4) f4=dist(a) f8=dist(a,sebox()) fe=dist(a,sebox(),’EUCLIDEAN’) # # example 2 # f = readgray(‘gear.tif’) f = neg(gradm(f)) d4=dist(f) d8=dist(f,sebox()) de=dist(f,sebox(),’EUCLIDEAN’) show(f) show(d4%8) show(d8%8) show(de%8)
Superpose points, rectangles and lines on an image.
y = drawv(f, data, value, GEOM)
f: Gray-scale (uint8 or uint16) or binary image. data: Gray-scale (uint8 or uint16) or binary image. vector of
points. Each row gives information regarding a geometrical primitive. The interpretation of this data is dependent on the parameter GEOM. The line drawing algorithm is not invariant to image transposition.
gray-scale value associated to each point in parameter data. It can be a column vector of values or a single value.
‘point’,’line’, ‘rect’, or ‘frect’ for drawing points, lines, rectangles or filled rectangles respectively.
type of f .
drawv creates the image y by a superposition of points, rectangles and lines of gray-level k1 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 a matrix where each row gives the point’s row and column, in this order. 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 , each row in the data matrix gives the two points of the diagonal of the rectangle, where the points use the same row, column convention.
# # example 1 # f=to_uint8(zeros((3,5))) pcoords=to_uint16([[0,2,4],
[0,0,2]])
pvalue=to_uint16([1,2,3]) print drawv(f,pcoords,pvalue,’point’) print drawv(f,pcoords,pvalue,’line’) rectcoords=to_uint16([[0],
[0], [3], [2]])
print drawv(f,rectcoords, to_uint16(5), ‘rect’) # # example 2 # f=readgray(‘blob3.tif’) pc=blob(label(f),’centroid’,’data’) lines=drawv(intersec(f,0),transpose(pc),to_uint8(1),’line’) show(f,lines)
Eliminate the objects that hit the image frame.
y = edgeoff(f, Bc=None)
f: Binary image. Bc: Structuring Element Default: None (3x3 elementary cross). (
connectivity)
y: Binary image.
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 .
# a=readgray(‘form-1.tif’) b=edgeoff(a) show(a) show(b)
Interval to detect end-points.
iab = endpoints(option=”loop”)
option: string default: “loop”. ‘loop’ or ‘homotopic’
Iab: Interval
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.
# # example 1 # print intershow(endpoints()) # # example 2 # print intershow(endpoints(‘homotopic’)) # # example 3 # f = readgray(‘pcbholes.tif’) show(f) f1 = thin(f) show(f1) f2 = thin(f1,endpoints(),20) show(f2) # # example 4 # fn = thin(f1,endpoints(‘homotopic’)) show(dilate(fn))
Erode an image by a structuring element.
y = erode(f, b=None)
f: Gray-scale (uint8 or uint16) or binary image. b: Structuring Element Default: None (3x3 elementary cross).
y: Image
ero 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 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.
# # example 1 # f=binary([
[1, 1, 1, 0, 0, 1, 1], [1, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0]])
b=binary([1, 1, 0]) erode(f,b) f=to_uint8([
[ 0, 1, 2, 50, 4, 5], [ 2, 3, 4, 0, 0, 0], [12, 255, 14, 15, 16, 17]])
erode(f,b) # # example 2 # f=binary(readgray(‘blob.tif’)) bimg=binary(readgray(‘blob1.tif’)) b=img2se(bimg) g=erode(f,b) show(f) show(g) show(g,gradm(f)) # # example 3 # f=readgray(‘pcb_gray.tif’) b=sedisk(3) show(f) show(erode(f,b))
Flooding filter- h,v,a-basin and dynamics (depth, area, volume)
y = flood(fin, T, option, Bc=None)
fin: Gray-scale (uint8 or uint16) image. T: Criterion value. If T==-1, then the dynamics is
determined, not the flooding at this criterion. This was selected just to use the same algoritm to compute two completely distinct functions.
option: String Default: “”. criterion: ‘AREA’, ‘VOLUME’, ‘H’. Bc: Structuring Element Default: None (3x3 elementary
cross). Connectivity.
y: Gray-scale (uint8 or uint16) image.
This is a flooding algorithm. It is the basis to implement many topological functions. It is a connected filter that floods an image following some topological criteria: area, volume, depth. These filters are equivalent to area-close, volume-basin or h-basin, respectively. This code may be difficult to understand because of its many options. Basically, when t is negative, the generalized dynamics: area, volume, h is computed. When the flooding is computed, every time a new level in the flooding happens, a test is made to verify if the criterion has reached. This is used to set the value to that height. This value image will be used later for sup-reconstruction (flooding) at that particular level. This test happens in the raising of the water and in the merging of basins.
Create a frame image.
y = frame(f, WT=1, HT=1, DT=0, k1=None, k2=None)
binary image.
WT: Double Default: 1. Positive integer ( width thickness). HT: Double Default: 1. Positive integer ( height thickness). DT: Double Default: 0. Positive integer ( depth thickness). k1: Non-negative integer. Default: None (Maximum pixel value
allowed in f). Frame gray-level.
allowed in f). Background gray level.
y: image of same type as f .
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 k1 and the value of the other pixels is k2 . The thickness of the image frame is DT.
Control automatic data type conversion.
Y = freedom(L=5)
parameter is omitted, the current level is returned.
Y: Double current FREEDOM level
freedom controls the automatic data type conversion. There are 3 possible levels, called FREEDOM levels, for automatic conversion: 0 - image type conversion is not allowed; 1- image type conversion is allowed, but a warning is sent for each conversion; 2- image type conversion is allowed without warning. The FREEDOM levels are set or inquired by freedom . If an image is not in the required datatype, than it should be converted to the maximum and nearest pymorph Morphology Toolbox datatype. For example, if an image is in int32 and a morphological gray-scale processing that accepts only binary, uint8 or uint16 images, is required, it will be converted to uint16. Another example, if a binary image should be added to a uint8 image, the binary image will be converted to uint8. In cases of operators that have as parameters an image and a constant, the type of the image should be kept as reference, while the type of the constant should be converted, if necessary.
# # example 1 # a=subm([4., 2., 1.],to_uint8([3, 2, 0])) print a print datatype(a) # # example 2 # a=subm([4., 2., 1], binary([3, 2, 0])) print a print datatype(a) # # example 3 # a=subm(to_uint8([4, 3, 2, 1]), 1) print a print datatype(a)
Geodesic Distance Transform.
y = gdist(f, g, Bc=None, METRIC=None)
f: Binary image. g: Binary image. Marker image Bc: Structuring Element Default: None (3x3 elementary
cross). (metric for distance).
METRIC: String Default: None. ‘EUCLIDEAN’ if specified.
y: uint16 (distance image).
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 .
# # example 1 # f=binary([
[1,1,1,1,1,1], [1,1,1,0,0,1], [1,0,1,0,0,1], [1,0,1,1,0,0], [0,0,1,1,1,1], [0,0,0,1,1,1]])
[0,0,0,0,0,0], [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,0,0,0,1]])
y=gdist(f,g,secross()) print y # # example 2 # f=readgray(‘maze_bw.tif’) g=intersec(f,0) g=drawv(g,to_uint16([[2],[2],[6],[6]]),to_uint16(1),’frect’) y=gdist(f,g,sebox(),’EUCLIDEAN’) show(f,g)
Morphological gradient.
y = gradm(f, Bdil=None, Bero=None)
f: Gray-scale (uint8 or uint16) or binary image. Bdil: Structuring Element Default: None (3x3 elementary cross).
for the dilation.
for the erosion.
).
gradm creates the image y by the subtraction of the erosion of the image f by Bero of the dilation of f by Bdil .
# # example 1 # a = readgray(‘small_bw.tif’) b = gradm(a) show(a) show(b) # # example 2 # c=gradm(a,secross(0),secross()) d=gradm(a,secross(),secross(0)) show(a,c) show(a,d) # # example 3 # a = readgray(‘bloodcells.tif’) b = gradm(a) show(a) show(b)
Gray-scale statistics for each labeled region.
y = grain(fr, f, measurement, option=”image”)
to define the regions. Label 0 is the background region.
measuremens.
‘max’, ‘min’, ‘median’, ‘mean’, ‘sum’, ‘std’, ‘std1’.
results as a gray-scale mosaic image (uint16); ‘data’: results a column vector of measurements (double).
(double) with gray-scale statistics per region.
Computes gray-scale statistics of each grain in the image. The grains regions are specified by the labeled image fr 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: (‘image’) if the output is an uint16 image where each label value is changed to the measurement value, or (‘data’) a double column vector. In this case, the first element (index 1) is the measurement of region 1. The region with label zero is not measure as it is normally the background.
# # example 1 # f=to_uint8([range(6),range(6),range(6)]) fr=labelflat(f) grain(fr,f,’sum’,’data’) grain(fr,f,’sum’) # # example 2 # f=readgray(‘astablet.tif’) g=gradm(f) marker=regmin(close(g)) ws=cwatershed(g,marker,sebox(),’regions’) g=grain(ws,f,’mean’) show(f) show(g)
Convert a binary image into a gray-scale image.
y = gray(f, TYPE=”uint8”, k1=None)
f: Binary image. TYPE: String Default: “uint8”. ‘uint8’, ‘uint16’, or ‘int32’. k1: Non-negative integer. Default: None (Maximum pixel level
in pixel type).
binary image.
gray converts a binary image into a gray-scale image of a specified data type. The value k1 is assigned to the 1 pixels of f , while the 0 pixels are assigned to the minimum value associated to the specified data type.
# b=binary([0, 1, 0, 1]) print b c=gray(b) print c d=gray(b,’uint8’,100) print d e=gray(b,’uint16’) print e f=gray(b,’int32’,0) print f
Find the histogram of the image f.
Finds the histogram of the image f and returns the result in the vector h . 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). : |
|---|
Remove peaks with contrast less than h.
y = hmax(f, h=1, Bc=None)
f: Gray-scale (uint8 or uint16) image. h: Default: 1. Contrast parameter. Bc: Structuring Element Default: None (3x3 elementary cross).
Structuring element ( connectivity).
y: Gray-scale (uint8 or uint16) or binary image.
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 .
# # example 1 # a = to_uint8([
[4, 3, 6, 1, 3, 5, 2], [2, 9, 6, 1, 6, 7, 3], [8, 9, 3, 2, 4, 9, 4], [3, 1, 2, 1, 2, 4, 2]])
print hmax(a,2,sebox()) # # example 2 # f = readgray(‘r4x2_256.tif’) show(f) fb = hmax(f,50) show(fb) show(regmax(fb))
Remove basins with contrast less than h.
y = hmin(f, h=1, Bc=None)
f: Gray-scale (uint8 or uint16) image. h: Default: 1. Contrast parameter. Bc: Structuring Element Default: None (3x3 elementary cross).
Structuring element (connectivity).
y: Gray-scale (uint8 or uint16) or binary image.
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 userful for simplifying the basins of the image.
# # example 1 # a = to_uint8([
[10, 3, 6, 18, 16, 15, 10], [10, 9, 6, 18, 6, 5, 10], [10, 9, 9, 15, 4, 9, 10], [10, 10, 10, 10, 10, 10, 10]])
print hmin(a,1,sebox()) # # example 2 # f = readgray(‘r4x2_256.tif’) show(f) fb = hmin(f,70) show(fb) show(regmin(fb))
Interval for homotopic thickening.
Iab = homothick()
Iab: Interval
homothick creates an interval that is useful for the homotopic (i.e., that conserves the relation between objects and holes) thickening of binary images.
# print intershow(homothick())
Interval for homotopic thinning.
Iab = homothin()
Iab: Interval
homothin creates an interval that is useful for the homotopic (i.e., that conserves the relation between objects and holes) thinning of binary images.
Create a structuring element from a pair of images.
B = img2se(fd, FLAT=”FLAT”, f=None)
origin (0,0) is at the matrix center.
FLAT: String Default: “FLAT”. ‘FLAT’ or ‘NON-FLAT’. f: Unsigned gray-scale (uint8 or uint16), signed (int32) or
binary image. Default: None.
B: Structuring Element
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 .
# # example 1 # a = img2se(binary([
[0,1,0], [1,1,1], [0,1,0]]))
print seshow(a) # # example 2 # b = binary([
[0,1,1,1], [1,1,1,0]])
b1 = img2se(b) print seshow(b1) # # example 3 # c = binary([
[0,1,0], [1,1,1], [0,1,0]])
[0,0,0], [0,1,0], [0,0,0]])
e = img2se(c,’NON-FLAT’,d) print seshow(e)
Intersection of inf-generating operators.
y = infcanon(f, Iab, theta=45, DIRECTION=”CLOCKWISE”)
f: Binary image. Iab: Interval theta: Double Default: 45. Degrees of rotation: 45, 90, or
ANTI-CLOCKWISE’
y: Binary image.
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 .
Inf-generating.
y = infgen(f, Iab)
f: Binary image. Iab: Interval
y: Binary image.
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 .
Inf-reconstruction.
y = infrec(f, g, Bc=None)
f: Gray-scale (uint8 or uint16) or binary image. Marker image. g: Gray-scale (uint8 or uint16) or binary image. Conditioning
image.
Structuring element ( connectivity).
y: Image
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 .
# # example 1 # g=readgray(‘text_128.tif’) f=erode(g,seline(9,90)) y=infrec(f,g,sebox()) show(g) show(f) show(y) # # example 2 # g=neg(readgray(‘n2538.tif’)) f=intersec(g,0) f=draw(f,’LINE:40,30,60,30:END’) y30=cdilate(f,g,sebox(),30) y=infrec(f,g,sebox()) show(g) show(f) show(y30) show(y)
Minima imposition.
y = inpos(f, g, Bc=None)
f: Binary image. Marker image. g: Gray-scale (uint8 or uint16) image. input image. Bc: Structuring Element Default: None (3x3 elementary cross).
(connectivity).
y: Gray-scale (uint8 or uint16) image.
Minima imposition on g based on the marker f . inpos creates an image y by filing the valleys of g that does not cover the connect components of f . A remarkable property of y is that its regional minima are exactly the connect components of g .
Rotate an interval
Irot = interot(Iab, theta=45, DIRECTION=”CLOCKWISE”)
Iab: Interval theta: Double Default: 45. Degrees of rotation. Available
values are multiple of 45 degrees.
ANTI-CLOCKWISE’.
Irot: Interval
interot rotates the interval Iab by an angle theta .
# b1 = endpoints() b2 = interot(b1) print intershow(b1) print intershow(b2)
Intersection of images.
y = intersec(f1, f2, f3=None, f4=None, f5=None)
f1: Gray-scale (uint8 or uint16) or binary image. f2: Gray-scale (uint8 or uint16) or binary image. Or constant. f3: Gray-scale (uint8 or uint16) or binary image. Default: None.
Or constant.
Or constant.
Or constant.
y: Image
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.
# # example 1 # f=to_uint8([255, 255, 0, 10, 0, 255, 250]) g=to_uint8([ 0, 40, 80, 140, 250, 10, 30]) print intersec(f, g) print intersec(f, 0) # # example 2 # a = readgray(‘form-ok.tif’) b = readgray(‘form-1.tif’) c = intersec(a,b) show(a) show(b) show(c) # # example 3 # d = readgray(‘tplayer1.tif’) e = readgray(‘tplayer2.tif’) f = readgray(‘tplayer3.tif’) g = intersec(d,e,f) show(d) show(e) show(f) show(g)
Visualize an interval.
s = intershow(Iab)
Iab: Interval
s: String ( representation of the interval).
intershow creates a representation for an interval using 0, 1 and . ( don’t care).
# print intershow(homothin())
Check for binary image
bool = isbinary(f)
f:
bool: Boolean
isbinary returns True if the datatype of the input image is binary. A binary image has just the values 0 and 1.
# a=to_uint8([0, 1, 0, 1]) print isbinary(a) b=(a) print isbinary(b)
Apply an iso-contour color table to a gray-scale image.
| Parameters: | * X: Gray-scale (uint8 or uint16) image. Distance transform image. :
|
|---|
Label a binary image.
y = label(f, Bc=None)
f: Binary image. Bc: Structuring Element Default: None (3x3 elementary cross). (
connectivity).
is uint16, otherwise it is int32.
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.
# # example 1 # f=binary([
[0,1,0,1,1], [1,0,0,1,0]])
g=label(f) print g # # example 2 # f = readgray(‘blob3.tif’) g=label(f) nblobs=g.max() print nblobs show(f) lblshow(g)
Label the flat zones of gray-scale images.
y = labelflat(f, Bc=None, _lambda=0)
f: Gray-scale (uint8 or uint16) or binary image. Bc: Structuring Element Default: None (3x3 elementary
cross). ( connectivity).
_lambda: Default: 0. Connectivity given by |f(q)-f(p)|<=_lambda.
is uint16, otherwise it is int32.
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.
# # example 1 # f=to_uint8([
[5,5,8,3,0], [5,8,8,0,2]])
g=labelflat(f) print g g1=labelflat(f,secross(),2) print g1 # # example 2 # f=readgray(‘blob.tif’) d=dist(f,sebox(),’euclidean’) g= d /8 show(g) fz=labelflat(g,sebox()); lblshow(fz) print fz.max() # # example 3 # f=readgray(‘pcb_gray.tif’) g=labelflat(f,sebox(),3) show(f) lblshow(g)
Last erosion.
y = lastero(f, B=None)
f: Binary image. B: Structuring Element Default: None (3x3 elementary cross).
y: Binary image.
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 .
Get the possible minimum and maximum of an image.
y = limits(f)
binary image.
supremum.
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.
# print limits(binary([0, 1, 0])) print limits(to_uint8([0, 1, 2]))
Converts image representation from matrix to set
center of the matrix.
C: array of pixel coordinates
V: array of pixel values corresponding to the coordinates of C
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.
# # example 1 # f=to_uint8([[1,2,3],[4,5,6],[7,8,9]]) i,v=mat2set(f) print i print v # # example 2 # f=to_uint8([[1,2,3,4],[5,6,7,8]]) i,v=mat2set(f) print i print v
Returns the maximum value associated to an image datatype
max = maxleveltype(TYPE=’uint8’)
‘uint16’ or ‘int32’, specifying the image type
max: the maximum level value of type TYPE
Negate an image.
y = neg(f)
binary image.
binary 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 .
# # example 1 # f=to_uint8([255, 255, 0, 10, 20, 10, 0, 255, 255]) print neg(f) print neg(to_uint8([0, 1])) print neg(to_int32([0, 1])) # # example 2 # a = readgray(‘gear.tif’) b = neg(a) show(a) show(b) # # example 3 # c = readgray(‘astablet.tif’) d = neg(c) show(c) show(d)
Morphological opening.
y = open(f, b=None)
f: Gray-scale (uint8 or uint16) or binary image. b: Structuring Element Default: None (3x3 elementary cross).
y: Image
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.
# # example 1 # f=binary(readgray(‘blob.tif’)) bimg=binary(readgray(‘blob1.tif’)) b=img2se(bimg) show(f) show(open(f,b)) show(open(f,b),gradm(f)) # # example 2 # a=binary(readgray(‘pcb1bin.tif’)) b=open(a,sebox(2)) c=open(a,sebox(4)) show(a) show(b) show(c) # # example 3 # a=readgray(‘astablet.tif’) b=open(a,sedisk(18)) show(a) show(b)
Opening by reconstruction.
y = openrec(f, bero=None, bc=None)
f: Gray-scale (uint8 or uint16) or binary image. bero: Structuring Element Default: None (3x3 elementary cross).
(erosion).
(connectivity).
y: Image (same type of f ).
openrec creates the image y by an inf-reconstruction of the image f from its erosion by bero , using the connectivity defined by Bc .
Open-by-Reconstruction Top-Hat.
y = openrecth(f, bero=None, bc=None)
f: Gray-scale (uint8 or uint16) or binary image. bero: Structuring Element Default: None (3x3 elementary cross).
(erosion)
( connectivity)
).
openrecth creates the image y by subtracting the open by reconstruction of f , defined by the structuring elements bero e bc , of f itself.
Opening Top Hat.
y = openth(f, b=None)
f: Gray-scale (uint8 or uint16) or binary image. b: Structuring Element Default: None (3x3 elementary cross).
structuring element
).
openth creates the image y by subtracting the morphological opening of f by the structuring element b of f itself.
# a = readgray(‘keyb.tif’) show(a) b = openth(a,sebox(3)) show(b)
Open transform.
y = opentransf(f, type=’OCTAGON’, n=65535, Bc=None, Buser=None)
f: Binary image. type: String Default: ‘OCTAGON’. Disk family: ‘OCTAGON’,
‘CHESSBOARD’, ‘CITY-BLOCK’, ‘LINEAR-V’, ‘LINEAR-H’, ‘LINEAR-45R’, ‘LINEAR-45L’, ‘USER’.
n: Default: 65535. Maximum disk radii. Bc: Structuring Element Default: None (3x3 elementary cross).
Connectivity for the reconstructive opening. Used if ‘-REC’ suffix is appended in the ‘type’ string.
User disk, used if ‘type’ is ‘USER’.
y: Gray-scale (uint8 or uint16) image.
Compute the open transform of a binary image. The value of the pixels in the open transform gives the largest radii of the disk plus 1, where the open by it is not empty at that pixel. The disk sequence must satisfy the following: if r > s, rB is sB-open, i.e. rB open by sB is equal 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.
# # example 1 # f = binary([
[0,0,0,0,0,0,0,0], [0,0,1,1,1,1,0,0], [0,0,1,1,1,1,1,0], [0,1,0,1,1,1,0,0], [1,1,0,0,0,0,0,0]])
print opentransf( f, ‘city-block’) print opentransf( f, ‘linear-h’) print opentransf( f, ‘linear-45r’) print opentransf( f, ‘user’,10,secross(),binary([0,1,1])) print opentransf( f, ‘city-block-rec’) # # example 2 # f=readgray(‘numbers.tif’) show(f) g=opentransf(f,’OCTAGON’) show(g) # # example 3 # b=sedisk(3,‘2D’,’OCTAGON’) g1=open(f,b) show(g1) g2=(g > 3) print g1 == g2
pads f with value so that Bc can be applied scaled by scale.
y = pad4n(f, Bc, value, scale=1)
f: Image Bc: Structuring Element (connectivity). value: scale: Default: 1.
The converted image
Pattern spectrum (also known as granulometric size density).
h = patspec(f, type=’OCTAGON’, n=65535, Bc=None, Buser=None)
f: Binary image. type: String Default: ‘OCTAGON’. Disk family: ‘OCTAGON’,
‘CHESSBOARD’, ‘CITY-BLOCK’, ‘LINEAR-V’, ‘LINEAR-H’, ‘LINEAR-45R’, ‘LINEAR-45L’, ‘USER’.
n: Default: 65535. Maximum disk radii. Bc: Structuring Element Default: None (3x3 elementary cross).
Connectivity for the reconstructive granulometry. Used if ‘-REC’ suffix is appended in the ‘type’ string.
User disk, used if ‘type’ is ‘USER’.
vector.
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.
Apply a random color table to a gray-scale image.
| Parameters: | * X: Gray-scale (uint8 or uint16) image. Labeled image. : |
|---|
Regional Maximum.
y = regmax(f, Bc=None)
f: Gray-scale (uint8 or uint16) image. Bc: Structuring Element Default: None (3x3 elementary cross).
(connectivity).
y: Binary image.
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.
Regional Minimum (with generalized dynamics).
y = regmin(f, Bc=None, option=”binary”)
f: Gray-scale (uint8 or uint16) image. Bc: Structuring Element Default: None (3x3 elementary
cross). (connectivity).
‘binary’: output a binary image ‘value’: output a grayscale image with
points at the regional minimum with the pixel values of the input image
points at the regional minimum with its dynamics;
‘area-dyn’: int32 image with the area-dynamics; ‘volume-dyn’: int32 image with the volume-dynamics.
y: Gray-scale (uint8 or uint16) or binary image.
regmin creates a binary image f 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. There are three output options: binary image; valued image; and generalized dynamics. The dynamics of a regional minima is the minimum height a pixel has to climb in a walk to reach another regional minima with a higher dynamics. The area-dyn is the minimum area a catchment basin has to raise to reach another regional minima with higher area-dynamics. The volume-dyn is the minimum volume a catchment basin has to raise to reach another regional minima with a higher volume dynamics. The dynamics concept was first introduced in Grimaud:92 and it is the basic notion for the hierarchical or multiscale watershed transform.
# # example 1 # a = to_uint8([
[10, 10, 10, 10, 10, 10, 10], [10, 9, 6, 18, 6, 5, 10], [10, 9, 6, 18, 6, 5, 10], [10, 9, 9, 15, 4, 9, 10], [10, 9, 9, 15, 12, 10, 10], [10, 10, 10, 10, 10, 10, 10]])
print regmin(a) print regmin(a,secross(),’value’) print regmin(a,secross(),’dynamics’) # # example 2 # f1=readgray(‘bloodcells.tif’) m1=regmin(f1,sebox()) show(f1,m1) f2=hmin(f1,70) show(f2) m2=regmin(f2,sebox()) show(f2,m2) # # example 3 # f=readgray(‘cameraman.tif’) g=gradm(f) mh=regmin(g,secross(),’dynamics’) ws1=cwatershed(g, binary(mh, 20)) ws2=cwatershed(g, binary(mh, 40)) show(ws1) show(ws2)
Create a Hit-or-Miss Template (or interval) from a pair of structuring elements.
Iab = se2hmt(A, Bc)
A: Structuring Element Left extremity. Bc: Structuring Element Complement of the right extremity.
Iab: Interval
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.
Create an interval from a pair of structuring elements.
Iab = se2interval(a, b)
a: Structuring Element Left extremity. b: Structuring Element Right extremity.
Iab: Interval
se2interval creates the interval [a,b] from the structuring elements a and b such that a is less or equal b .
Create a box structuring element.
B = sebox(r=1)
r: Non-negative integer. Default: 1. Radius.
B: 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.
# b1 = sebox() seshow(b1) b2 = sebox(2) seshow(b2)
Diamond structuring element and elementary 3x3 cross.
B = secross(r=1)
r: Double Default: 1. (radius).
B: Structuring Element
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 , B is the elementary cross itself.
# b1 = secross() print seshow(b1) b2 = secross(2) print seshow(b2)
Dilate one structuring element by another
Bo = sedilate(B1, B2)
B1: Structuring Element B2: Structuring Element
Bo: Structuring Element
sedil dilates an structuring element by another. The main difference between this dilation and dil 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.
# b1 = seline(5) seshow(b1) b2 = sedisk(2) seshow(b2) b3 = sedilate(b1,b2) seshow(b3)
Create a disk or a semi-sphere structuring element.
B = sedisk(r=3, DIM=”2D”, METRIC=”EUCLIDEAN”, FLAT=”FLAT”, h=0)
r: Non-negative integer. Default: 3. Disk radius. DIM: String Default: “2D”. ‘1D’, ‘2D, or ‘3D’. METRIC: String Default: “EUCLIDEAN”. ‘EUCLIDEAN’, ‘ CITY-BLOCK’,
‘OCTAGON’, or ‘ CHESSBOARD’.
FLAT: String Default: “FLAT”. ‘FLAT’ or ‘NON-FLAT’. h: Double Default: 0. Elevation of the center of the
semi-sphere.
B: 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.
# # example 1 # a=seshow(sedisk(10,‘2D’,’CITY-BLOCK’)) b=seshow(sedisk(10,‘2D’,’EUCLIDEAN’)) c=seshow(sedisk(10,‘2D’,’OCTAGON’)) show(a) show(b) show(c) # # example 2 # d=seshow(sedisk(10,‘2D’,’CITY-BLOCK’,’NON-FLAT’)) e=seshow(sedisk(10,‘2D’,’EUCLIDEAN’,’NON-FLAT’)) f=seshow(sedisk(10,‘2D’,’OCTAGON’,’NON-FLAT’)) show(d) show(e) show(f) # # example 3 # g=sedisk(3,‘2D’,’EUCLIDEAN’,’NON-FLAT’) seshow(g) h=sedisk(3,‘2D’,’EUCLIDEAN’,’NON-FLAT’,5) seshow(h)
Create a line structuring element.
B = seline(l=3, theta=0)
l: Non-negative integer. Default: 3. theta: Double Default: 0. (degrees, clockwise)
B: Structuring Element
seline creates a structuring element B that is a line segment that has an extremity at the origin, length l and angle theta (0 degrees is east direction, clockwise). If l=0 , it generates the origin.
# seshow(seline()) b1 = seline(4,45) seshow(b1) b2 = seline(4,-180) seshow(b2) a=text(‘Line’) b=dilate(a,b1) show(a) show(b)
Reflect a structuring element
Bo = sereflect(Bi)
Bi: Structuring Element
Bo: Structuring Element
sereflect reflects a structuring element by rotating it 180 degrees.
# b1 = seline(5,30) print seshow(b1) b2 = sereflect(b1) print seshow(b2)
Rotate a structuring element.
BROT = serot(B, theta=45, DIRECTION=”CLOCKWISE”)
B: Structuring Element theta: Double Default: 45. Degrees of rotation. Available
values are multiple of 45 degrees.
ANTI-CLOCKWISE’.
BROT: Structuring Element
serot rotates a structuring element B of an angle theta .
# b = img2se(binary([[0, 0, 0], [0, 1, 1], [0, 0, 0]])); seshow(b) seshow(serot(b)) seshow(serot(b,45,’ANTI-CLOCKWISE’))
Display a structuring element as an image.
y = seshow(B, option=”NORMAL”)
B: Structuring Element option: String Default: “NORMAL”. ‘NORMAL’, ‘ EXPAND’ or ‘
NON-FLAT’
y: Gray-scale (uint8 or uint16) or binary 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.
# # example 1 # b=secross(3); print seshow(b) a = seshow(b,’EXPAND’) show(a) print seshow(b,’NON-FLAT’) # # example 2 # b=sedisk(2,‘2D’,’EUCLIDEAN’,’NON-FLAT’) print seshow(b)
N-1 iterative Minkowski additions
NB = sesum(B=None, N=1)
B: Structuring Element Default: None (3x3 elementary cross). N: Non-negative integer. Default: 1.
NB: Structuring Element
sesum creates the structuring element NB from N - 1 iterative Minkowski additions with the structuring element B .
# # example 1 # b = img2se(binary([[1, 1, 1], [1, 1, 1], [0, 1, 0]])) seshow(b) b3 = sesum(b,3) seshow(b3) # # example 2 # b = sedisk(1,‘2D’,’CITY-BLOCK’,’NON-FLAT’); seshow(b) seshow(sesum(b,2))
Converts image representation from set to matrix
M = set2mat(A)
corresponding pixel values
M: Image in matrix format, origin (0,0) at the matrix center
Return an image in the matrix format built from a tuple of an array of pixel coordinates and a corresponding array of pixel values
# coord=to_int32([
[ 0,0], [-1,0], [ 1,1]])
A=set2mat((coord,)) print A print datatype(A) vu = to_uint8([1,2,3]) f=set2mat((coord,vu)) print f print datatype(f) vi = to_int32([1,2,3]) g=set2mat((coord,vi)) print g print datatype(g)
Translate a structuring element
Bo = setrans(Bi, t)
Bi: Structuring Element t:
Bo: Structuring Element
setrans translates a structuring element by a specific value.
# b1 = seline(5) seshow(b1) b2 = setrans(b1,[2,-2]) seshow(b2)
Union of structuring elements
B = seunion(B1, B2)
B1: Structuring Element B2: Structuring Element
B: Structuring Element
seunion creates a structuring element from the union of two structuring elements.
# b1 = seline(5) seshow(b1) b2 = sedisk(3) seshow(b2) b3 = seunion(b1,b2) seshow(b3)
Morphological skeleton (Medial Axis Transform).
y = skelm(f, B=None, option=”binary”)
f: Binary image. B: Structuring Element Default: None (3x3 elementary
cross).
a binary image (medial axis); value: output a grayscale image with values of the radius of the disk to reconstruct the original image (medial axis transform).
y: Gray-scale (uint8 or uint16) or binary image.
skelm creates the image y by computing the morphological skeleton by B of the image f , when option is BINARY. 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 option is VALUE, 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.
# # example 1 # from numpy import ones a=neg(frame(binary(ones((7,9))))) print a print skelm(a) print skelm(a,sebox()) # # example 2 # a=readgray(‘pcbholes.tif’) b=skelm(a) show(a) show(b) # # example 3 # c=skelm(a,secross(),’value’) show(c)
Morphological skeleton reconstruction (Inverse Medial Axis Transform).
y = skelmrec(f, B=None)
f: Gray-scale (uint8 or uint16) or binary image. B: Structuring Element Default: None (3x3 elementary cross).
y: Binary image.
skelmrec reconstructs the valued morphological skeleton to recover the original image.
# from numpy import ones a=neg(frame(binary(ones((7,9))))) print a b=skelm(a,secross(),’value’) print b c=skelmrec(b,secross()) print c
Skeleton of Influence Zone - also know as Generalized Voronoi Diagram
y = skiz(f, Bc=None, return_lines=False, METRIC=None) y,lines = skiz(f, Bc=None, return_lines=True, METRIC=None)
f: Binary image. Bc: Structuring Element Default: None (3x3 elementary
cross). Connectivity for the distance measurement.
in the image. Default=False
METRIC: String Default: None. ‘EUCLIDEAN’ if specified.
y: Gray-scale (uint8 or uint16) or binary image.
skiz creates the image y by detecting the lines which are equidistant to two or more connected components of f , according to the connectivity defined by Bc . Depending on with the flag LINEREG, y will be a binary image with the skiz lines or a labeled image representing the zone of influence regions. When the connected objects of f are single points, the skiz is the Voronoi diagram.
# # example 1 # f=readgray(‘blob2.tif’) y=skiz(f,sebox(),’LINES’,’EUCLIDEAN’) show(f,y) # # example 2 # from numpy import zeros f=binary(zeros((100,100))) f[30,25],f[20,75],f[50,50],f[70,30],f[80,70] = 1,1,1,1,1 y = skiz(f,sebox(),’LINES’,’EUCLIDEAN’) show(f,y)
Subtraction of two images, with saturation.
y = subm(f1, f2)
binary image.
binary image. Or constant.
binary image.
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 .
# # example 1 # 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) # # example 2 # 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.
y = supcanon(f, Iab, theta=45, DIRECTION=”CLOCKWISE”)
f: Binary image. Iab: Interval theta: Double Default: 45. Degrees of rotation: 45, 90, or
ANTI-CLOCKWISE’
y: Binary image.
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 .
Sup-generating (hit-miss).
y = supgen(f, INTER)
f: Binary image. INTER: Interval
y: Binary image.
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 yet that the sup-generating operator is equivalent to the classical hit-miss operator.
# # example 1 # f=binary([
[0,0,1,0,0,1,1], [0,1,0,0,1,0,0], [0,0,0,1,1,0,0]])
i=endpoints() print intershow(i) g=supgen(f,i) print g # # example 2 # a=readgray(‘gear.tif’) b=supgen(a,endpoints()) show(a) show(dilate(b))
Sup-reconstruction.
y = suprec(f, g, Bc=None)
f: Gray-scale (uint8 or uint16) or binary image. Marker image. g: Gray-scale (uint8 or uint16) or binary image. Conditioning
image.
connectivity).
y: Image
suprec creates the image y by an infinite number of recursive iterations (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 .
Detection of similarity-based watershed from markers.
y = swatershed(f, g, B=None, LINEREG=”LINES”)
f: Gray-scale (uint8 or uint16) image. g: Gray-scale (uint8 or uint16) or binary image. Marker
image. If binary, each connected component is an object marker. If gray, it is assumed it is a labeled image.
cross). (watershed connectivity)
LINEREG: String Default: “LINES”. ‘LINES’ or ‘ REGIONS’.
y: Gray-scale (uint8 or uint16) or binary image.
swatershed creates the image y by detecting the domain of the catchment basins of f indicated by g , according with the connectivity defined by B . This watershed is a modified version where each basin is defined by a similarity criterion between pixels. The original watershed is normally applied to the gradient of the image. In this case, the gradient is taken internally. According to the flag LINEREG y will be a labeled image of the catchment basins domain or just a binary image that presents the watershed lines. The implementation of this function is based on LotuFalc:00 .
# f = to_uint8([
[0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 0, 0, 0, 0, 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, 2, 0, 0, 0]])
print swatershed(f,m,secross(),’REGIONS’)
Syetric difference between two images
y = symdif(f1, f2)
f1: Gray-scale (uint8 or uint16) or binary image. f2: Gray-scale (uint8 or uint16) or binary image.
y: Image i
symdif 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 .
# # example 1 # a = to_uint8([1, 2, 3, 4, 5]) b = to_uint8([5, 4, 3, 2, 1]) print symdif(a,b) # # example 2 # c = readgray(‘tplayer1.tif’) d = readgray(‘tplayer2.tif’) e = symdif(c,d) show(c) show(d) show(e)
Create a binary image of a text.
y = text(txt)
txt: String Default: “”. Text to be written.
y: Binary image.
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.
Image transformation by thickening.
y = thick(f, Iab=None, n=-1, theta=45, DIRECTION=”CLOCKWISE”)
f: Binary image. Iab: Interval Default: None (homothick). n: Non-negative integer. Default: -1. Number of
iterations.
ANTI-CLOCKWISE’
y: Binary image.
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 .
Image transformation by thinning.
y = thin(f, Iab=None, n=-1, theta=45, DIRECTION=”CLOCKWISE”)
f: Binary image. Iab: Interval Default: None (homothin). n: Non-negative integer. Default: -1. Number of
iterations.
ANTI-CLOCKWISE’
y: Binary image.
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.
# f=readgray(‘scissors.tif’) f1=thin(f) show(f,f1) # skeleton f2=thin(f1,endpoints(),15) # prunning 15 pixels show(f,f2) # prunned skeleton
Threshold (adaptive)
y = threshad(f, f1, f2=None)
f: Gray-scale (uint8 or uint16) image. f1: Gray-scale (uint8 or uint16) image. lower value f2: Gray-scale (uint8 or uint16) image. Default: None. upper
value
y: Binary image.
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 .
# a = readgray(‘keyb.tif’) show(a) b = threshad(a,to_uint8(10), to_uint8(50)) show(b) c = threshad(a,238) show(c)
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 option is ‘iray’, it performs an enhancement and, if the option is ‘binary’, 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.
Examples
Union of images.
y = union(f1, f2, f3=None, f4=None, f5=None)
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.
y: Image
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.
# # example 1 # f=to_uint8([255, 255, 0, 10, 0, 255, 250]) print ‘f=’,f g=to_uint8([ 0, 40, 80, 140, 250, 10, 30]) print ‘g=’,g print union(f, g) print union(f, 255) # # example 2 # a = readgray(‘form-ok.tif’) b = readgray(‘form-1.tif’) c = union(a,b) show(a) show(b) show(c) # # example 3 # d = readgray(‘danaus.tif’) e = (d < 80) f = union(d,gray(e)) show(d) show(e) show(f) # # example 4 # g = readgray(‘tplayer1.tif’) h = readgray(‘tplayer2.tif’) i = readgray(‘tplayer3.tif’) j = union(g,h,i) show(g) show(h) show(i) show(j)
Remove domes with volume less than v.
y = vmax(f, v=1, Bc=None)
f: Gray-scale (uint8 or uint16) image. v: Default: 1. Volume parameter. Bc: Structuring Element Default: None (3x3 elementary cross).
Structuring element (connectivity).
y: Gray-scale (uint8 or uint16) or binary image.
vmax This operator removes connected domes with volume less than v . This function is very similar to hmax , but instead of using a gray scale criterion (contrast) for the dome, it uses a volume criterion.
# # example 1 # a = to_uint8([
[4, 3, 6, 1, 3, 5, 2], [2, 9, 6, 1, 6, 7, 3], [8, 9, 3, 2, 4, 9, 4], [3, 1, 2, 1, 2, 4, 2]])
print vmax(a,10,sebox()) # # example 2 # f = readgray(‘astablet.tif’) show(f) fb = vmax(f,80000) show(fb) show(regmax(fb))
Watershed detection.
y = watershed(f, Bc=None, return_lines=False)
f: Gray-scale (uint8 or uint16) or binary image. Bc: Structuring Element Default: None (3x3 elementary
cross). ( connectivity)
return_lines: Whether to return the boundaries (default: returns segmentation)
y: Gray-scale (uint8 or uint16) or binary image.
watershed creates the image y by detecting the domain of the catchment basins of f , according to the connectivity defined by Bc . According to the flag LINEREG y will be a labeled image of the catchment basins domain or just a binary image that presents the watershed lines. The implementation of this function is based on VincSoil:91 .
# f=readgray(‘astablet.tif’) grad=gradm(f) w1=watershed(grad,sebox()) w2=watershed(grad,sebox(),’REGIONS’) show(grad) show(w1) lblshow(w2)