def gm2Gradient(gm, sigma): """'reverse-engineer' gradient vector image from scalar image via Hessian from Gaussian derivative filters of given scale""" # create vectors in direction of negative curvature: er = vigra.tensorEigenRepresentation(vigra.gaussianHessian(gm, sigma)) return vigra.transformImage(er, gm, "\l e,i: i*Vector(-sin(e[2]), cos(e[2]))")
def gm2Gradient(gm, sigma): """'reverse-engineer' gradient vector image from scalar image via Hessian from Gaussian derivative filters of given scale""" # create vectors in direction of negative curvature: er = vigra.tensorEigenRepresentation( vigra.gaussianHessian(gm, sigma)) return vigra.transformImage( er, gm, "\l e,i: i*Vector(-sin(e[2]), cos(e[2]))")
def colorGradient(img, scale, sqrt = True): """Calculate Gaussian color gradient. Calculates sum of Gaussian gradient tensors in all single bands, and returns pair of (gm, grad) where gm is the square root of the trace of the color gradient tensor and grad is a 2-band image with the large eigenvectors of the appropriate sqrt(gm2) lengths. If `sqrt` is set to False, returns (gm2, grad) instead, i.e. does not apply sqrt to the first image (slight optimization if you don't need the gm image).""" colorTensor = gradientTensor(img, scale) # gm2 := sum of squared magnitudes of grad. in each channel gm2 = vigra.tensorTrace(colorTensor) # rebuild vector in dir. of large eigenvector with length sqrt(gm2): grad = vigra.transformImage( # FIXME: use tensor2Gradient? vigra.tensorEigenRepresentation(colorTensor), gm2, "\l e, mag2: sqrt(mag2)*Vector(cos(-e[2]), sin(-e[2]))", {}) if sqrt: gm = vigra.transformImage(gm2, "\l x: sqrt(x)") return gm, grad return gm2, grad
def colorGradient(img, scale, sqrt=True): """Calculate Gaussian color gradient. Calculates sum of Gaussian gradient tensors in all single bands, and returns pair of (gm, grad) where gm is the square root of the trace of the color gradient tensor and grad is a 2-band image with the large eigenvectors of the appropriate sqrt(gm2) lengths. If `sqrt` is set to False, returns (gm2, grad) instead, i.e. does not apply sqrt to the first image (slight optimization if you don't need the gm image).""" colorTensor = gradientTensor(img, scale) # gm2 := sum of squared magnitudes of grad. in each channel gm2 = vigra.tensorTrace(colorTensor) # rebuild vector in dir. of large eigenvector with length sqrt(gm2): grad = vigra.transformImage( # FIXME: use tensor2Gradient? vigra.tensorEigenRepresentation(colorTensor), gm2, "\l e, mag2: sqrt(mag2)*Vector(cos(-e[2]), sin(-e[2]))", {}) if sqrt: gm = vigra.transformImage(gm2, "\l x: sqrt(x)") return gm, grad return gm2, grad
def tensor2Gradient(tensor): """rebuild vectors in dir. of large eigenvectors with lengths sqrt(trace)""" return vigra.transformImage( vigra.tensorEigenRepresentation(tensor), "\l e: sqrt(e[0]+e[1])*Vector(cos(-e[2]), sin(-e[2]))", {})