def upperGeodesicDilate(imIn, imMask, imOut, n=1, se=mamba.DEFAULT_SE): """ Performs an upper geodesic dilation of image 'imIn' above 'imMask'. The result is put inside 'imOut', 'n' controls the size of the dilation. 'se' specifies the type of structuring element used to perform the computation (DEFAULT_SE by default). Warning! 'imMask' and 'imOut' must be different. """ mamba.logic(imIn, imMask, imOut, "sup") if imIn.getDepth() == 1: for i in range(n): mamba.diff(imOut, imMask, imOut) mamba.dilate(imOut, imOut, se=se) mamba.logic(imMask, imOut, imOut, "sup") else: imWrk1 = mamba.imageMb(imIn) imWrk2 = mamba.imageMb(imIn, 1) for i in range(n): mamba.generateSupMask(imOut, imMask, imWrk2, True) mamba.convertByMask(imWrk2, imWrk1, 0, mamba.computeMaxRange(imWrk1)[1]) mamba.logic(imOut, imWrk1, imOut, "inf") mamba.dilate(imOut, imOut, se=se) mamba.logic(imOut, imMask, imOut, "sup")
def hierarchicalLevel(imIn, imOut, grid=mamba.DEFAULT_GRID): """ Computes the next hierarchical level of image 'imIn' in the waterfalls transformation and puts the result in 'imOut'. This operation makes sure that the next hierarchical level is embedded in the previous one. 'imIn' must be a valued watershed image. """ imWrk0 = mamba.imageMb(imIn) imWrk1 = mamba.imageMb(imIn, 1) imWrk2 = mamba.imageMb(imIn, 1) imWrk3 = mamba.imageMb(imIn, 1) imWrk4 = mamba.imageMb(imIn, 32) mamba.threshold(imIn, imWrk1, 0, 0) mamba.negate(imWrk1, imWrk2) hierarchy(imIn, imWrk2, imWrk0, grid=grid) mamba.minima(imWrk0, imWrk2, grid=grid) mamba.label(imWrk2, imWrk4, grid=grid) mamba.watershedSegment(imWrk0, imWrk4, grid=grid) mamba.copyBytePlane(imWrk4, 3, imWrk0) mamba.threshold(imWrk0, imWrk2, 0, 0) mamba.diff(imWrk1, imWrk2, imWrk3) mamba.build(imWrk1, imWrk3) se = mamba.structuringElement(mamba.getDirections(grid), grid) mamba.dilate(imWrk3, imWrk1, 1, se) mamba.diff(imWrk2, imWrk1, imWrk1) mamba.logic(imWrk1, imWrk3, imWrk1, "sup") mamba.convertByMask(imWrk1, imWrk0, 255, 0) mamba.logic(imIn, imWrk0, imOut, "inf")
def hierarchicalLevel(imIn, imOut, grid=mamba.DEFAULT_GRID): """ Computes the next hierarchical level of image 'imIn' in the waterfalls transformation and puts the result in 'imOut'. This operation makes sure that the next hierarchical level is embedded in the previous one. 'imIn' must be a valued watershed image. """ imWrk0 = mamba.imageMb(imIn) imWrk1 = mamba.imageMb(imIn, 1) imWrk2 = mamba.imageMb(imIn, 1) imWrk3 = mamba.imageMb(imIn, 1) imWrk4 = mamba.imageMb(imIn, 32) mamba.threshold(imIn,imWrk1, 0, 0) mamba.negate(imWrk1, imWrk2) hierarchy(imIn, imWrk2, imWrk0, grid=grid) mamba.minima(imWrk0, imWrk2, grid=grid) mamba.label(imWrk2, imWrk4, grid=grid) mamba.watershedSegment(imWrk0, imWrk4, grid=grid) mamba.copyBytePlane(imWrk4, 3, imWrk0) mamba.threshold(imWrk0, imWrk2, 0, 0) mamba.diff(imWrk1, imWrk2, imWrk3) mamba.build(imWrk1, imWrk3) se = mamba.structuringElement(mamba.getDirections(grid), grid) mamba.dilate(imWrk3, imWrk1, 1, se) mamba.diff(imWrk2, imWrk1, imWrk1) mamba.logic(imWrk1, imWrk3, imWrk1, "sup") mamba.convertByMask(imWrk1, imWrk0, 255, 0) mamba.logic(imIn, imWrk0, imOut, "inf")
def drawEdge(imOut, thick=1): """ Draws a frame around the edge of 'imOut' whose value equals the maximum range value and whose thickness is given by 'thick' (default 1). """ imOut.reset() se=mamba.structuringElement([0,1,2,3,4,5,6,7,8], mamba.SQUARE) mamba.dilate(imOut, imOut, thick, se=se, edge=mamba.FILLED)
def buildClose(imIn, imOut, n=1, se=mamba.DEFAULT_SE): """ Performs a closing by dual reconstruction operation on image 'imIn' and puts the result in 'imOut'. 'n' controls the size of the closing. """ imWrk = mamba.imageMb(imIn) mamba.copy(imIn, imWrk) mamba.dilate(imIn, imOut, n, se=se) mamba.dualBuild(imWrk, imOut, grid=se.getGrid())
def opening(imIn, imOut, n=1, se=mamba.DEFAULT_SE, edge=mamba.FILLED): """ Performs an opening operation on image 'imIn' and puts the result in 'imOut'. 'n' controls the size of the opening and 'se' the structuring element used. The default edge is set to 'FILLED'. Note that the edge setting operates in the erosion only. """ mamba.erode(imIn, imOut, n, se=se, edge=edge) mamba.dilate(imOut, imOut, n, se=se.transpose())
def gradient(imIn, imOut, n=1, se=mamba.DEFAULT_SE): """ Computes the morphological gradient of image 'imIn' and puts the result in 'imOut'. The thickness can be controlled using parameter 'n' (1 by default). The structuring element used by the erosion and dilation is defined by 'se' (DEFAULT_SE by default). """ imWrk = mamba.imageMb(imIn) mamba.erode(imIn, imWrk, n, se=se) mamba.dilate(imIn, imOut, n, se=se) mamba.sub(imOut, imWrk, imOut)
def cellsOpen(imIn, imOut, n=1, se=mamba.DEFAULT_SE, edge=mamba.FILLED): """ Simultaneous opening of size 'n' (default 1) of all the cells of the partition image 'imIn' with 'se' structuring element. The resulting partition is put in 'imOut'. 'edge' is set to FILLED by default. This operation works on 8-bit and 32-bit partitions. """ imWrk = mamba.imageMb(imIn) cellsErode(imIn, imWrk, n, se=se, edge=edge) mamba.dilate(imWrk, imOut, n, se=se.transpose())
def lowerGeodesicDilate(imIn, imMask, imOut, n=1, se=mamba.DEFAULT_SE): """ Performs a lower geodesic dilation of image 'imIn' below 'imMask'. The result is put inside 'imOut', 'n' controls the size of the dilation. 'se' specifies the type of structuring element used to perform the computation (DEFAULT_SE by default). Warning! 'imMask' and 'imOut' must be different. """ mamba.logic(imIn, imMask, imOut, "inf") for i in range(n): mamba.dilate(imOut, imOut, se=se) mamba.logic(imMask, imOut, imOut, "inf")
def mosaicGradient(imIn, imOut, grid=mamba.DEFAULT_GRID): """ Builds the mosaic-gradient image of 'imIn' and puts the result in 'imOut'. The mosaic-gradient image is built by computing the differences of two mosaic images generated from 'imIn', the first one having its watershed lines valued by the suprema of the adjacent catchment basins values, the second one been valued by the infima. """ imWrk1 = mamba.imageMb(imIn) imWrk2 = mamba.imageMb(imIn) imWrk3 = mamba.imageMb(imIn) imWrk4 = mamba.imageMb(imIn) imWrk5 = mamba.imageMb(imIn) imWrk6 = mamba.imageMb(imIn, 1) mosaic(imIn, imWrk2, imWrk3, grid=grid) mamba.sub(imWrk2, imWrk3, imWrk1) mamba.logic(imWrk2, imWrk3, imWrk2, "sup") mamba.negate(imWrk2, imWrk2) mamba.threshold(imWrk3, imWrk6, 1, 255) mamba.multiplePoints(imWrk6, imWrk6, grid=grid) mamba.convertByMask(imWrk6, imWrk3, 0, 255) se = mamba.structuringElement(mamba.getDirections(grid), grid) mamba.dilate(imWrk1, imWrk4, se=se) mamba.dilate(imWrk2, imWrk5, se=se) while mamba.computeVolume(imWrk3) != 0: mamba.dilate(imWrk1, imWrk1, 2, se=se) mamba.dilate(imWrk2, imWrk2, 2, se=se) mamba.logic(imWrk1, imWrk3, imWrk1, "inf") mamba.logic(imWrk2, imWrk3, imWrk2, "inf") mamba.logic(imWrk1, imWrk4, imWrk4, "sup") mamba.logic(imWrk2, imWrk5, imWrk5, "sup") mamba.erode(imWrk3, imWrk3, 2, se=se) mamba.negate(imWrk5, imWrk5) mamba.sub(imWrk4, imWrk5, imOut)
def removeEdgeParticles(imIn, imOut, grid=mamba.DEFAULT_GRID): """ Removes particles (connected components) touching the edge in image 'imIn'. The resulting image is put in image 'imOut'. Although this operator may be used with greytone images, it should be considered with caution. """ imWrk = mamba.imageMb(imIn) se = mamba.structuringElement(mamba.getDirections(grid), grid) mamba.dilate(imWrk, imWrk, se=se, edge=mamba.FILLED) mamba.logic(imIn, imWrk, imWrk, "inf") build(imIn, imWrk, grid=grid) mamba.diff(imIn, imWrk, imOut)
def partitionDilate(imIn, imOut, n=1, grid=mamba.DEFAULT_GRID): """ Graph dilation of the corresponding partition image 'imIn'. The size is given by 'n'. The corresponding partition image of the resulting dilated graph is put in 'imOut'. 'grid' can be set to HEXAGONAL or SQUARE. """ imWrk = mamba.imageMb(imIn) mamba.copy(imIn, imOut) mamba.copy(imIn, imWrk) se = mamba.structuringElement(mamba.getDirections(grid), grid) for i in range(n): mamba.dilate(imOut, imOut, se=se) cellsBuild(imWrk, imOut, grid=grid)
def closing(imIn, imOut, n=1, se=mamba.DEFAULT_SE, edge=mamba.FILLED): """ Performs a closing operation on image 'imIn' and puts the result in 'imOut'. 'n' controls the size of the closing and 'se' the structuring element used. The default edge is set to 'FILLED'. If 'edge' is set to 'EMPTY', the operation is slightly modified to avoid errors (non extensivity). """ imWrk = mamba.imageMb(imIn) if edge==mamba.EMPTY: mamba.copy(imIn, imWrk) mamba.dilate(imIn, imOut, n, se=se) mamba.erode(imOut, imOut, n, se=se.transpose(), edge=edge) if edge==mamba.EMPTY: mamba.logic(imOut, imWrk, imOut, "sup")
def cellsErode(imIn, imOut, n=1, se=mamba.DEFAULT_SE, edge=mamba.FILLED): """ Simultaneous erosion of size 'n' (default 1) of all the cells of the partition image 'imIn' with 'se' structuring element. The resulting partition is put in 'imOut'. 'edge' is set to FILLED by default. This operation works on 8-bit and 32-bit partitions. """ imWrk1 = mamba.imageMb(imIn) imWrk2 = mamba.imageMb(imIn, 1) mamba.dilate(imIn, imWrk1, n=n, se=se) mamba.erode(imIn, imOut, n=n, se=se, edge=edge) mamba.generateSupMask(imOut, imWrk1, imWrk2, False) mamba.convertByMask(imWrk2, imWrk1, 0, mamba.computeMaxRange(imIn)[1]) mamba.logic(imOut, imWrk1, imOut, "inf")
def closing(imIn, imOut, n=1, se=mamba.DEFAULT_SE, edge=mamba.FILLED): """ Performs a closing operation on image 'imIn' and puts the result in 'imOut'. 'n' controls the size of the closing and 'se' the structuring element used. The default edge is set to 'FILLED'. If 'edge' is set to 'EMPTY', the operation is slightly modified to avoid errors (non extensivity). """ imWrk = mamba.imageMb(imIn) if edge == mamba.EMPTY: mamba.copy(imIn, imWrk) mamba.dilate(imIn, imOut, n, se=se) mamba.erode(imOut, imOut, n, se=se.transpose(), edge=edge) if edge == mamba.EMPTY: mamba.logic(imOut, imWrk, imOut, "sup")
def dilateByCylinder3D(imInOut, height, section): """ Dilates 3D image 'imInOut' using a cylinder with an hexagonal section of size 2x'section' and a height of 2x'height'. The image is modified by this function. The edge is always set to EMPTY. """ l = len(imInOut) for im in imInOut: mamba.dilate(im, im, section, se=mamba.HEXAGON) provIm3D = m3D.image3DMb(imInOut) for i in range(l): mamba.copy(imInOut[i], provIm3D[i]) for j in range(max(0,i-height), min(l,i+height+1)): mamba.logic(provIm3D[i], imInOut[j], provIm3D[i], "sup") m3D.copy3D(provIm3D, imInOut)
def halfGradient(imIn, imOut, type="intern", n=1, se=mamba.DEFAULT_SE): """ Computes the half morphological gradient of image 'imIn' ond puts the result in 'imOut'. 'type' indicates if the half gradient should be internal or external. Possible values are : "extern" : dilation(imIn) - imIn "intern" : imIn - erosion(imIn) The thickness can be controlled using parameter 'n' (1 by default). The structuring element used by the erosion or the dilation is defined by 'se'. """ imWrk = mamba.imageMb(imIn) if type=="extern": mamba.dilate(imIn, imWrk, n, se=se) mamba.sub(imWrk, imIn, imOut) else: mamba.erode(imIn, imWrk, n, se=se) mamba.sub(imIn, imWrk, imOut)
def strongLevelling(imIn, imOut, n, eroFirst, grid=mamba.DEFAULT_GRID): """ Strong levelling of 'imIn', result in 'imOut'. 'n' defines the size of the erosion and dilation of 'imIn' in the operation. If 'eroFirst' is true, the operation starts with an erosion, it starts with a dilation otherwise. This filter is stronger (more efficient) that simpleLevelling. However, the order of the initial operations (erosion and dilation) matters. """ imWrk = mamba.imageMb(imIn) se = mamba.structuringElement(mamba.getDirections(grid), grid) if eroFirst: mamba.erode(imIn, imWrk, n, se=se) mamba.build(imIn, imWrk, grid=grid) mamba.dilate(imIn, imOut, n, se=se) mamba.dualBuild(imWrk, imOut, grid=grid) else: mamba.dilate(imIn, imWrk, n, se=se) mamba.dualBuild(imIn, imWrk, grid=grid) mamba.erode(imIn, imOut, n, se=se) mamba.build(imWrk, imOut, grid=grid)
# Importing mamba import mamba import mambaDisplay im = mamba.imageMb("wheel.png", 1) im1 = mamba.imageMb(im, 1) im2 = mamba.imageMb(im, 1) # Opening of image mamba.opening(im, im1, 3) # Selection of the outside region mamba.negate(im1, im2) mamba.removeEdgeParticles(im2, im1) mamba.diff(im2, im1, im2) # Extracting the wheel teeth mamba.logic(im, im2, im2, "inf") # Cleaning the image mamba.opening(im2, im2) # Counting and marking each tooth mamba.thinD(im2, im1) nb_teeth = mamba.computeVolume(im1) print("Number of teeth: %d" % (nb_teeth)) mamba.dilate(im1, im1, 3, mamba.SQUARE3X3) im1.convert(8) im8 = mamba.imageMb(im, 8) mamba.convert(im, im8) mamba.subConst(im8, 1, im8) mamba.logic(im8, im1, im8, "sup") name = mambaDisplay.tagOneColorPalette(255, (0, 0, 255)) im8.save('wheel_teeth.png', palette=mambaDisplay.getPalette(name))
# Importing mamba import mamba import mambaDisplay im = mamba.imageMb("wheel.png", 1) im1 = mamba.imageMb(im, 1) im2 = mamba.imageMb(im, 1) # Opening of image mamba.opening(im, im1, 3) # Selection of the outside region mamba.negate(im1, im2) mamba.removeEdgeParticles(im2, im1) mamba.diff(im2, im1, im2) # Extracting the wheel teeth mamba.logic(im, im2, im2, "inf") # Cleaning the image mamba.opening(im2, im2) # Counting and marking each tooth mamba.thinD(im2, im1) nb_teeth = mamba.computeVolume(im1) print("Number of teeth: %d" % (nb_teeth)) mamba.dilate(im1, im1, 3, mamba.SQUARE3X3) im1.convert(8) im8 = mamba.imageMb(im, 8) mamba.convert(im, im8) mamba.subConst(im8, 1, im8) mamba.logic(im8, im1, im8, "sup") name = mambaDisplay.tagOneColorPalette(255, (0,0,255)) im8.save('wheel_teeth.png', palette=mambaDisplay.getPalette(name))