def computeDiameter(imIn, dir, scale=(1.0, 1.0), grid=mamba.DEFAULT_GRID): """ Computes the diameter (diametral variation) of binary image 'imIn' in direction 'dir'. 'scale' is a tuple defining the horizontal and vertical scale factors (default is 1.0). Beware, if the input image 'imIn' is not a binary image, the function raises an error. """ if imIn.getDepth() != 1: mamba.raiseExceptionOnError(core.MB_ERR_BAD_DEPTH) if dir == 0: return 0.0 dir = ((dir - 1)%(mamba.gridNeighbors(grid)//2)) +1 imWrk = mamba.imageMb(imIn) mamba.copy(imIn, imWrk) mamba.diffNeighbor(imIn, imWrk, 1<<dir, grid=grid) if grid == mamba.HEXAGONAL: l = scale[1] if dir != 2: l = 2*l*scale[0]/math.sqrt(scale[0]*scale[0] + 4*scale[1]*scale[1]) else: if dir == 1: l = scale[0] elif dir == 3: l = scale[1] else: l = scale[0]*scale[1]/math.sqrt(scale[0]*scale[0] + scale[1]*scale[1]) l = l*mamba.computeVolume(imWrk) return l
def computeDiameter(imIn, dir, scale=(1.0, 1.0), grid=mamba.DEFAULT_GRID): """ Computes the diameter (diametral variation) of binary image 'imIn' in direction 'dir'. 'scale' is a tuple defining the horizontal and vertical scale factors (default is 1.0). Beware, if the input image 'imIn' is not a binary image, the function raises an error. """ if imIn.getDepth() != 1: mamba.raiseExceptionOnError(core.MB_ERR_BAD_DEPTH) if dir == 0: return 0.0 dir = ((dir - 1) % (mamba.gridNeighbors(grid) // 2)) + 1 imWrk = mamba.imageMb(imIn) mamba.copy(imIn, imWrk) mamba.diffNeighbor(imIn, imWrk, 1 << dir, grid=grid) if grid == mamba.HEXAGONAL: l = scale[1] if dir != 2: l = 2 * l * scale[0] / math.sqrt(scale[0] * scale[0] + 4 * scale[1] * scale[1]) else: if dir == 1: l = scale[0] elif dir == 3: l = scale[1] else: l = scale[0] * scale[1] / math.sqrt(scale[0] * scale[0] + scale[1] * scale[1]) l = l * mamba.computeVolume(imWrk) return l
def hitOrMiss3D(imIn, imOut, dse, edge=mamba.EMPTY): """ Performs a binary Hit-or-miss operation on 3D image 'imIn' using the doubleStructuringElement3D 'dse'. Result is put in 'imOut'. WARNING! 'imIn' and 'imOut' must be different images. """ (width, height, length) = imIn.getSize() depth = imIn.getDepth() if depth != 1: mamba.raiseExceptionOnError(core.MB_ERR_BAD_DEPTH) if length != len(imOut): mamba.raiseExceptionOnError(core.MB_ERR_BAD_SIZE) zext = dse.grid.getZExtension() imWrk = m3D.image3DMb(width, height, length + zext * 2, depth) # Border handling imWrk.reset() m3D.copy3D(imIn, imWrk, firstPlaneOut=1) if edge == mamba.FILLED: m3D.negate3D(imWrk, imWrk) for i in range(zext): imWrk[i].reset() imWrk[length + zext * 2 - 1 - i].reset() dse = dse.flip() # Central point if dse.se1.hasZero(): m3D.copy3D(imWrk, imOut, firstPlaneIn=1) else: if dse.se0.hasZero(): for i in range(length): mamba.negate(imWrk[i + 1], imOut[i]) else: imOut.fill(1) # Other directions dirs = m3D.getDirections3D(dse.getGrid(), True) dirs0 = dse.se0.getDirections() dirs1 = dse.se1.getDirections() grid2D = dse.getGrid().get2DGrid() for d in dirs: if d in dirs1: for i in range(length): (planeOffset, dc) = dse.getGrid().convertFromDir(d, i) mamba.infNeighbor(imWrk[i + 1 + planeOffset], imOut[i], 1 << dc, grid=grid2D, edge=edge) elif d in dirs0: for i in range(length): (planeOffset, dc) = dse.getGrid().convertFromDir(d, i) mamba.diffNeighbor(imWrk[i + 1 + planeOffset], imOut[i], 1 << dc, grid=grid2D, edge=edge)
def hitOrMiss3D(imIn, imOut, dse, edge=mamba.EMPTY): """ Performs a binary Hit-or-miss operation on 3D image 'imIn' using the doubleStructuringElement3D 'dse'. Result is put in 'imOut'. WARNING! 'imIn' and 'imOut' must be different images. """ (width,height,length) = imIn.getSize() depth = imIn.getDepth() if depth!=1: mamba.raiseExceptionOnError(core.MB_ERR_BAD_DEPTH) if length!=len(imOut): mamba.raiseExceptionOnError(core.MB_ERR_BAD_SIZE) zext = dse.grid.getZExtension() imWrk = m3D.image3DMb(width, height, length+zext*2, depth) # Border handling imWrk.reset() m3D.copy3D(imIn, imWrk, firstPlaneOut=1) if edge==mamba.FILLED: m3D.negate3D(imWrk, imWrk) for i in range(zext): imWrk[i].reset() imWrk[length+zext*2-1-i].reset() dse = dse.flip() # Central point if dse.se1.hasZero(): m3D.copy3D(imWrk, imOut, firstPlaneIn=1) else: if dse.se0.hasZero(): for i in range(length): mamba.negate(imWrk[i+1], imOut[i]) else: imOut.fill(1) # Other directions dirs = m3D.getDirections3D(dse.getGrid(), True) dirs0 = dse.se0.getDirections() dirs1 = dse.se1.getDirections() grid2D = dse.getGrid().get2DGrid() for d in dirs: if d in dirs1: for i in range(length): (planeOffset, dc) = dse.getGrid().convertFromDir(d,i) mamba.infNeighbor(imWrk[i+1+planeOffset], imOut[i], 1<<dc, grid=grid2D, edge=edge) elif d in dirs0: for i in range(length): (planeOffset, dc) = dse.getGrid().convertFromDir(d,i) mamba.diffNeighbor(imWrk[i+1+planeOffset], imOut[i], 1<<dc, grid=grid2D, edge=edge)
def diameterLabelling(imIn, imOut, dir, grid=mamba.DEFAULT_GRID): """ Labels each connected component of the binary image 'imIn' with its diameter in direction 'dir'. The labelled image is stored in the 32-bit image 'imOut'. If 'imIn' is a 8-bit or 32-bit image, this function works too. However, the 0-valued connected components are labelled with 0. This procedure works on hexagonal or square grid. 'dir' can be any strictly positive integer value. """ imWrk1 = mamba.imageMb(imIn, 1) imWrk2 = mamba.imageMb(imIn) ed = 1 << ((dir - 1)%(mamba.gridNeighbors(grid)//2)) +1 if imIn.getDepth() == 1: mamba.copy(imIn, imWrk1) mamba.diffNeighbor(imIn, imWrk1, ed, grid=grid) else: mamba.nonEqualNeighbors(imIn, imWrk2, ed, grid=grid, edge= mamba.EMPTY) mamba.threshold(imWrk2, imWrk1, 1, mamba.computeMaxRange(imWrk2)[1]) # They are used for the labelling. measureLabelling(imIn, imWrk1, imOut)
def diameterLabelling(imIn, imOut, dir, grid=mamba.DEFAULT_GRID): """ Labels each connected component of the binary image 'imIn' with its diameter in direction 'dir'. The labelled image is stored in the 32-bit image 'imOut'. If 'imIn' is a 8-bit or 32-bit image, this function works too. However, the 0-valued connected components are labelled with 0. This procedure works on hexagonal or square grid. 'dir' can be any strictly positive integer value. """ imWrk1 = mamba.imageMb(imIn, 1) imWrk2 = mamba.imageMb(imIn) ed = 1 << ((dir - 1) % (mamba.gridNeighbors(grid) // 2)) + 1 if imIn.getDepth() == 1: mamba.copy(imIn, imWrk1) mamba.diffNeighbor(imIn, imWrk1, ed, grid=grid) else: mamba.nonEqualNeighbors(imIn, imWrk2, ed, grid=grid, edge=mamba.EMPTY) mamba.threshold(imWrk2, imWrk1, 1, mamba.computeMaxRange(imWrk2)[1]) # They are used for the labelling. measureLabelling(imIn, imWrk1, imOut)