def agglomerative_clustering_3d(vol, seg_vol, use_gradient = False): labels = vigra.analysis.labelVolume(seg_vol) volBig = vigra.resize(vol, [ vol.shape[0]*2-1, vol.shape[1]*2-1, vol.shape[2]*2-1 ] ) if use_gradient: sigmaGradMag = 2.0 gradMag = vigra.filters.gaussianGradientMagnitude(volBig, sigmaGradMag) # get 3D grid graph and edgeMap for grid graph # from gradMag of interpolated vloume or plain volume (depending on use_gradient) gridGraph = vigra.graphs.gridGraph(vol.shape[0:2]) gridGraphEdgeIndicator = [] if use_gradient: gridGraphEdgeIndicator = vigra.graphs.edgeFeaturesFromInterpolatedImage(gridGraph, gradMag) else: gridGraphEdgeIndicator = vigra.graphs.edgeFeaturesFromInterpolatedImage(gridGraph, volBig) # get region adjacency graph from super-pixel labels rag = vigra.graphs.regionAdjacencyGraph(gridGraph, labels) # accumalate edge weights from gradient magintude edgeWeights = rag.accumulateEdgeFeatures(gridGraphEdgeIndicator) # agglomerative clustering beta = 0.5 nodeNumStop = 20 clustering = vigra.graphs.agglomerativeClustering(graph = rag, edgeWeights = edgeWeights, beta = beta, nodeNumStop = nodeNumStop) print clustering
def agglomerative_clustering_2d(img, labels, use_gradient = False): labels = vigra.analysis.labelImage(labels) imgBig = vigra.resize(img, [img.shape[0]*2-1, img.shape[1]*2-1]) if use_gradient: # compute the gradient on interpolated image sigmaGradMag = 2.0 gradMag = vigra.filters.gaussianGradientMagnitude(imgBig, sigmaGradMag) # get 2D grid graph and edgeMap for grid graph # from gradMag of interpolated image or from plain image (depending on use_gradient) gridGraph = vigra.graphs.gridGraph(img.shape[0:2]) gridGraphEdgeIndicator = [] if use_gradient: gridGraphEdgeIndicator = vigra.graphs.edgeFeaturesFromInterpolatedImage(gridGraph, gradMag) else: gridGraphEdgeIndicator = vigra.graphs.edgeFeaturesFromInterpolatedImage(gridGraph, imgBig) # get region adjacency graph from super-pixel labels rag = vigra.graphs.regionAdjacencyGraph(gridGraph, labels) # accumalate edge weights from gradient magintude edgeWeights = rag.accumulateEdgeFeatures(gridGraphEdgeIndicator) # agglomerative clustering beta = 0.5 nodeNumStop = 20 clustering = vigra.graphs.agglomerativeClustering(graph = rag, edgeWeights = edgeWeights, beta = beta, nodeNumStop = nodeNumStop) res = np.zeros( labels.shape ) for c in range(len(clustering)): res[ np.where( labels== (c-1) ) ] = clustering[c] res = vigra.Image(res) res = vigra.analysis.labelImage(res) return res
takeNth = 3 if loadN is None: loadN = 0 imgs = [] gt = [] for i in range(1, loadN + 1): hName = "horse%03d.jpg" % (i, ) rgbImg = vigra.impex.readImage(imgPath + hName) gtImg = vigra.impex.readImage(gtPath + hName).astype('uint32')[::takeNth, ::takeNth] gtImg[gtImg < 125] = 0 gtImg[gtImg >= 125] = 1 rgbImg = vigra.resize(rgbImg, [gtImg.shape[0], gtImg.shape[1]]) imgs.append(rgbImg) gt.append(gtImg) fUnary = [ vigra.colors.transform_RGB2Lab, vigra.colors.transform_RGB2Luv, partial(vigra.filters.gaussianGradientMagnitude, sigma=1.0), partial(vigra.filters.gaussianGradientMagnitude, sigma=2.0), ] fBinary = [ vigra.colors.transform_RGB2Lab, vigra.colors.transform_RGB2Luv, partial(vigra.filters.gaussianGradientMagnitude, sigma=1.0), partial(vigra.filters.gaussianGradientMagnitude, sigma=2.0),
def eccentricity( img, distFunc = "exponential", showPathImage = False, percentageOfPaths = 100, imgSaveName = "" ): img = img.astype(numpy.uint8) dim = len(img.shape) ## Enlarge image by one pixel on each side bigShape = [] for s in img.shape: bigShape.append(s + 2) bigImg = numpy.ones(bigShape) slices = [] for i in range(dim): slices.append(slice(1, bigImg.shape[i]-1)) bigImg[ slices ] = img inside = numpy.where(bigImg==0) outside = numpy.where(bigImg==1) ## Apply distanceTransform and modify (outside: high values, inside: low values) distImage = vigra.filters.distanceTransform2D(bigImg.astype(numpy.float32)) imgp = distImage.copy() # if showPathImage: # imgp = distImage.copy() if distFunc == "exponential": distImage = numpy.exp(distImage*-gamma) elif distFunc == "linear": maxDist = distImage.max() distImage = maxDist - distImage + 10 elif distFunc == "inverse": w = numpy.where(distImage!=0) distImage[w] = 1/distImage[w] else: print "wrong parameters for distFunc in eccentricity" ## Distance in the inside between two pixels is 1.0 #distImage = bigImg.copy().astype(numpy.float32) #distImage[inside]=1.0 # if len(imgSaveName)>1: # plt.imshow(numpy.swapaxes(distImage, 1, 0)) # plt.savefig(imgSaveName+"_dist.png") # #scipy.misc.imsave(imgSaveName+"_dist.png", numpy.swapaxes(distImage, 1, 0)) ## Set the outside to a very high value distImage[outside]=1000.0 ## Image copy to draw the paths in #imgp = distImage.copy() imgp[outside] = 100 ## Get image graph and its path finder gridGraph = vigraph.gridGraph(bigImg.shape[0:dim],False) graphShape = [] for s in distImage.shape: graphShape.append(s*2-1) edgeWeights = vigra.resize(distImage, graphShape, order=0) #edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImage(gridGraph, edgeWeights) edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImageCorrected(gridGraph,edgeWeights) pathFinder = vigraph.ShortestPathPathDijkstra(gridGraph) ## Find borders in img if dim == 2: bigLblImg = vigra.analysis.labelImage(bigImg.astype(numpy.uint8)) else: bigLblImg = vigra.analysis.labelVolume(bigImg.astype(numpy.uint8)) rag = vigraph.GridRegionAdjacencyGraph(gridGraph, bigLblImg) node = vigraph.GridRegionAdjacencyGraph.nodeFromId(rag, long( bigLblImg[inside][0] )) edges = vigraph._ragFindEdges(rag, gridGraph, rag.affiliatedEdges, bigLblImg, node) borderImg = numpy.zeros(bigImg.shape) for edge in edges: slices = [] for d in range(dim): slices.append( slice(edge[d], edge[d]+1) ) borderImg[slices] = 1 ## End points for paths (all points on the border) targets = numpy.where(borderImg==1) nTargets = len(targets[0]) ## Find the diameter (longest path) eccLength = numpy.empty(nTargets) eccLength.fill(-1) eccTargetPath = {} vpIndex = 0 vpGraphIndex = [] for d in range(dim): vpGraphIndex.append(int(targets[d][vpIndex])) vp = gridGraph.coordinateToNode(vpGraphIndex) visited = numpy.zeros(nTargets) while True: visited[vpIndex] = 1 pathFinder.run(edgeWeights, vp) eccChanged = False for j in range(nTargets): targetIndex = [] for d in range(dim): targetIndex.append(int(targets[d][j])) target = gridGraph.coordinateToNode(targetIndex) pathLength = pathFinder.distance(target) m = max(eccLength[j], pathLength) if m > eccLength[j]: eccChanged = True eccLength[j] = m eccTargetPath[j] = pathFinder.path(pathType='coordinates', target=target) vpIndex = numpy.argmax(eccLength) vpGraphIndex = [] for d in range(dim): vpGraphIndex.append(int(targets[d][vpIndex])) vp = gridGraph.coordinateToNode(vpGraphIndex) if visited[vpIndex] or not eccChanged: break ## Find the length of the diameter (non-weighted) path = eccTargetPath[vpIndex] dMax = 0 for k in range(1, len(path)): diffCount = 0 for d in range(dim): if path[k][d] != path[k-1][d]: diffCount += 1 dMax += numpy.sqrt(diffCount) ## Find the midpoint of the diameter dMax = dMax/2 if len(path) == 0: path = numpy.empty( (1, 2), numpy.uint8 ) path[0][0] = targets[0][0] path[0][1] = targets[1][0] p1 = path[0] d1 = 0 for k in range(1, len(path)): p2 = path[k] d2 = d1 + numpy.linalg.norm(p2-p1) if d2 > dMax: if (abs(d2-dMax) < abs(d1-dMax)): p1 = p2 break p1 = p2 d1 = d2 ## Compute eccentricity from center (p1) to all points on border sourceIndex = [] for d in range(dim): sourceIndex.append(int(p1[d])) source = gridGraph.coordinateToNode(sourceIndex) pathFinder.run(edgeWeights, source) maxPathLength = 0 for j in range(nTargets): targetIndex = [] for d in range(dim): targetIndex.append(int(targets[d][j])) target = gridGraph.coordinateToNode(targetIndex) pathLength = pathFinder.distance(target) maxPathLength = max(maxPathLength, pathLength) imgp[targetIndex[0], targetIndex[1]] = 40*pathLength imgp[ path[:,0], path[:,1] ] = 12*maxPathLength imgp[sourceIndex[0], sourceIndex[1]] = 40*maxPathLength plt.figure(distFunc) plt.imshow(numpy.swapaxes(imgp, 1, 0), interpolation='none') if len(imgSaveName)>1: scipy.misc.imsave(imgSaveName+".png", numpy.swapaxes(imgp, 1, 0))
slicWeight = 10.0 # SLIC color - spatial weight beta = 0.5 # node vs edge weight nodeNumStop = 50 # desired num. nodes in result # load image and convert to LAB img = vigra.impex.readImage(filepath) # get super-pixels with slic on LAB image imgLab = vigra.colors.transform_RGB2Lab(img) labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight, superpixelDiameter) labels = vigra.analysis.labelImage(labels) # compute gradient on interpolated image imgLabBig = vigra.resize(imgLab, [imgLab.shape[0]*2-1, imgLab.shape[1]*2-1]) gradMag = vigra.filters.gaussianGradientMagnitude(imgLabBig, sigmaGradMag) # get 2D grid graph and edgeMap for grid graph # from gradMag of interpolated image gridGraph = graphs.gridGraph(img.shape[0:2]) gridGraphEdgeIndicator = graphs.edgeFeaturesFromInterpolatedImage(gridGraph, gradMag) # get region adjacency graph from super-pixel labels rag = graphs.regionAdjacencyGraph(gridGraph, labels) # accumulate edge weights from gradient magnitude edgeWeights = rag.accumulateEdgeFeatures(gridGraphEdgeIndicator) # accumulate node features from grid graph node map # which is just a plain image (with channels)
import skneuro import vigra from volumina.api import Viewer from PyQt4.QtGui import QApplication import skneuro.blockwise_filters as bf import skneuro.denoising as dn import pylab import h5py if True: #data = vigra.impex.readHDF5('/mnt/CLAWS1/tbeier/data/knott1000/knott-block-full2/d-gt.h5','sbfsem')[0:100,0:100,0:100].astype('float32') #dc = data.copy() data = vigra.impex.readImage( '/home/tbeier/Desktop/10683114_980333491982687_1214393854_o.jpg' ).astype(numpy.float32) data = vigra.resize(data, [data.shape[0] / 2, data.shape[1] / 2]) #data = (data[:,:,0]+data[:,:,1]+data[:,:,2])/(3.0) dc = data.copy().view(numpy.ndarray) else: if False: fp = h5py.File( "/mnt/CLAWS1/tbeier/data/knott1000_results/pixel_classification/boundary_prob_r1.h5", "r") pmap = fp['exported_data'][0:300, 0:300, 0:300, 1] fd = h5py.File( "/mnt/CLAWS1/tbeier/data/knott1000/knott-block-full2/d-gt.h5", "r") data = fd['sbfsem'][0:300, 0:300, 0:300]
def eccentricity(img, distFunc="exponential", showPathImage=False, percentageOfPaths=100, imgSaveName=""): img = img.astype(numpy.uint8) dim = len(img.shape) ## Enlarge image by one pixel on each side bigShape = [] for s in img.shape: bigShape.append(s + 2) bigImg = numpy.ones(bigShape) slices = [] for i in range(dim): slices.append(slice(1, bigImg.shape[i] - 1)) bigImg[slices] = img inside = numpy.where(bigImg == 0) outside = numpy.where(bigImg == 1) ## Apply distanceTransform and modify (outside: high values, inside: low values) distImage = vigra.filters.distanceTransform2D(bigImg.astype(numpy.float32)) imgp = distImage.copy() # if showPathImage: # imgp = distImage.copy() if distFunc == "exponential": distImage = numpy.exp(distImage * -gamma) elif distFunc == "linear": maxDist = distImage.max() distImage = maxDist - distImage + 10 elif distFunc == "inverse": w = numpy.where(distImage != 0) distImage[w] = 1 / distImage[w] else: print "wrong parameters for distFunc in eccentricity" ## Distance in the inside between two pixels is 1.0 #distImage = bigImg.copy().astype(numpy.float32) #distImage[inside]=1.0 # if len(imgSaveName)>1: # plt.imshow(numpy.swapaxes(distImage, 1, 0)) # plt.savefig(imgSaveName+"_dist.png") # #scipy.misc.imsave(imgSaveName+"_dist.png", numpy.swapaxes(distImage, 1, 0)) ## Set the outside to a very high value distImage[outside] = 1000.0 ## Image copy to draw the paths in #imgp = distImage.copy() imgp[outside] = 100 ## Get image graph and its path finder gridGraph = vigraph.gridGraph(bigImg.shape[0:dim], False) graphShape = [] for s in distImage.shape: graphShape.append(s * 2 - 1) edgeWeights = vigra.resize(distImage, graphShape, order=0) #edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImage(gridGraph, edgeWeights) edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImageCorrected( gridGraph, edgeWeights) pathFinder = vigraph.ShortestPathPathDijkstra(gridGraph) ## Find borders in img if dim == 2: bigLblImg = vigra.analysis.labelImage(bigImg.astype(numpy.uint8)) else: bigLblImg = vigra.analysis.labelVolume(bigImg.astype(numpy.uint8)) rag = vigraph.GridRegionAdjacencyGraph(gridGraph, bigLblImg) node = vigraph.GridRegionAdjacencyGraph.nodeFromId( rag, long(bigLblImg[inside][0])) edges = vigraph._ragFindEdges(rag, gridGraph, rag.affiliatedEdges, bigLblImg, node) borderImg = numpy.zeros(bigImg.shape) for edge in edges: slices = [] for d in range(dim): slices.append(slice(edge[d], edge[d] + 1)) borderImg[slices] = 1 ## End points for paths (all points on the border) targets = numpy.where(borderImg == 1) nTargets = len(targets[0]) ## Find the diameter (longest path) eccLength = numpy.empty(nTargets) eccLength.fill(-1) eccTargetPath = {} vpIndex = 0 vpGraphIndex = [] for d in range(dim): vpGraphIndex.append(int(targets[d][vpIndex])) vp = gridGraph.coordinateToNode(vpGraphIndex) visited = numpy.zeros(nTargets) while True: visited[vpIndex] = 1 pathFinder.run(edgeWeights, vp) eccChanged = False for j in range(nTargets): targetIndex = [] for d in range(dim): targetIndex.append(int(targets[d][j])) target = gridGraph.coordinateToNode(targetIndex) pathLength = pathFinder.distance(target) m = max(eccLength[j], pathLength) if m > eccLength[j]: eccChanged = True eccLength[j] = m eccTargetPath[j] = pathFinder.path(pathType='coordinates', target=target) vpIndex = numpy.argmax(eccLength) vpGraphIndex = [] for d in range(dim): vpGraphIndex.append(int(targets[d][vpIndex])) vp = gridGraph.coordinateToNode(vpGraphIndex) if visited[vpIndex] or not eccChanged: break ## Find the length of the diameter (non-weighted) path = eccTargetPath[vpIndex] dMax = 0 for k in range(1, len(path)): diffCount = 0 for d in range(dim): if path[k][d] != path[k - 1][d]: diffCount += 1 dMax += numpy.sqrt(diffCount) ## Find the midpoint of the diameter dMax = dMax / 2 if len(path) == 0: path = numpy.empty((1, 2), numpy.uint8) path[0][0] = targets[0][0] path[0][1] = targets[1][0] p1 = path[0] d1 = 0 for k in range(1, len(path)): p2 = path[k] d2 = d1 + numpy.linalg.norm(p2 - p1) if d2 > dMax: if (abs(d2 - dMax) < abs(d1 - dMax)): p1 = p2 break p1 = p2 d1 = d2 ## Compute eccentricity from center (p1) to all points on border sourceIndex = [] for d in range(dim): sourceIndex.append(int(p1[d])) source = gridGraph.coordinateToNode(sourceIndex) pathFinder.run(edgeWeights, source) maxPathLength = 0 for j in range(nTargets): targetIndex = [] for d in range(dim): targetIndex.append(int(targets[d][j])) target = gridGraph.coordinateToNode(targetIndex) pathLength = pathFinder.distance(target) maxPathLength = max(maxPathLength, pathLength) imgp[targetIndex[0], targetIndex[1]] = 40 * pathLength imgp[path[:, 0], path[:, 1]] = 12 * maxPathLength imgp[sourceIndex[0], sourceIndex[1]] = 40 * maxPathLength plt.figure(distFunc) plt.imshow(numpy.swapaxes(imgp, 1, 0), interpolation='none') if len(imgSaveName) > 1: scipy.misc.imsave(imgSaveName + ".png", numpy.swapaxes(imgp, 1, 0))
import opengm import vigra import numpy import time import sys fname = "135069.jpg" img = vigra.readImage(fname) img = numpy.sum(img, axis=2) img = vigra.resize(img, [s / 1 for s in img.shape]) noise = numpy.random.random(img.size).reshape(img.shape) * 255 print noise.shape img += noise img -= img.min() img /= img.max() print "shape", img.shape vigra.imshow(img) #vigra.show() threshold = 0.24 labelsNaive = img > threshold vigra.imshow(labelsNaive) #vigra.show() nVar = img.size nLabelsPerVar = 2 variableSpace = numpy.ones(nVar) * nLabelsPerVar gm = opengm.gm(variableSpace) t0 = time.time() # add unaries
gamma = 0.15 # exp(-gamma * edgeIndicator) edgeThreshold = 2.5 # values higher are considered as edges scale = 1.0 # how much smoothing iterations = 10 # how man smoothing iterations # load image and convert to LAB img = vigra.impex.readImage(filepath) # get super-pixels with slic on LAB image imgLab = vigra.colors.transform_RGB2Lab(img) labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight, superpixelDiameter) labels = vigra.analysis.labelImage(labels) # compute gradient on interpolated image imgLabBig = vigra.resize(imgLab, [imgLab.shape[0] * 2 - 1, imgLab.shape[1] * 2 - 1]) gradMag = vigra.filters.gaussianGradientMagnitude(imgLabBig, sigmaGradMag) # get 2D grid graph and edgeMap for grid graph # from gradMag of interpolated image gridGraph = graphs.gridGraph(img.shape[0:2]) gridGraphEdgeIndicator = graphs.edgeFeaturesFromInterpolatedImage( gridGraph, gradMag) # get region adjacency graph from super-pixel labels rag = graphs.regionAdjacencyGraph(gridGraph, labels) # accumulate edge weights from gradient magnitude edgeIndicator = rag.accumulateEdgeFeatures(gridGraphEdgeIndicator) # accumulate node features from grid graph node map
def eccentricity( img, distFunc = "exponential", showPathImage = False, percentageOfPaths = 100, imgSaveName = "" ): ## Enlarge image by one pixel on each side img = img.astype(numpy.uint8) bigImg = numpy.ones( (img.shape[0]+2, img.shape[1]+2) ) bigImg[1:bigImg.shape[0]-1, 1:bigImg.shape[1]-1] = img ## Find borders in img (replace with graph functions) borderImg = numpy.zeros(bigImg.shape) for y in range(bigImg.shape[1]-1): for x in range(bigImg.shape[0]-1): if bigImg[x,y] == 0: if bigImg[x+1,y] == 1 or bigImg[x,y+1] == 1: borderImg[x, y] = 1 else: if bigImg[x+1,y] == 0: borderImg[x+1, y] = 1 if bigImg[x,y+1] == 0: borderImg[x, y+1] = 1 ## regionImageToCrackEdgeImage ( labelImage ) # ## Apply distanceTransform and modify (outside: high values, inside: low values) # distImage = vigra.filters.distanceTransform2D(bigImg.astype(numpy.float32)) # if showPathImage: # imgp = distImage.copy() # if distFunc == "exponential": # distImage = numpy.exp(distImage*-gamma) # elif distFunc == "linear": # maxDist = distImage.max() # distImage = maxDist - distImage # elif distFunc == "inverse": # w = numpy.where(distImage!=0) # distImage[w] = 1/distImage[w] # else: # print "wrong parameters for distFunc in eccentricity" ## Distance in the inside between two pixels is 1.0 distImage = bigImg.copy().astype(numpy.float32) distImage[numpy.where(bigImg==0)]=1.0 ## Set the outside to a very high value distImage[numpy.where(bigImg==1)]=10000.0 imgp = distImage.copy() ## Get image graph and its path finder gridGraph = vigraph.gridGraph(bigImg.shape[0:2],False) edgeWeights = vigra.resize(distImage,[distImage.shape[0]*2-1,distImage.shape[1]*2-1],order=0) edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImageCorrected(gridGraph,edgeWeights) pathFinder = vigraph.ShortestPathPathDijkstra(gridGraph) ## End points for paths (all points on the border) targets = numpy.where(borderImg==1) tx,ty = targets nTargets = len(tx) ## Indices of start points for paths (random) nPoints = int(numpy.ceil(percentageOfPaths * nTargets / 100.0)) numpy.random.seed(42) starts = numpy.random.permutation(range(nTargets))[:nPoints] ## Compute paths maxPaths = [] maxPathLengths = [] for i in range(nPoints): source = gridGraph.coordinateToNode((int(tx[starts[i]]), int (ty[starts[i]]))) pathFinder.run(edgeWeights, source) maxPathLength = 0 for j in range(nTargets): target = gridGraph.coordinateToNode((int(tx[j]), int(ty[j]))) path = pathFinder.path(pathType='coordinates', target=target) pathLength = pathFinder.distance(target) if pathLength > maxPathLength or maxPathLength == 0: maxPathLength = pathLength maxPath = path maxPaths.append(maxPath) maxPathLengths.append(maxPathLength) if showPathImage or len(imgSaveName)>1: val = (imgp.max()+imgp.min())/2 for p in maxPaths: imgp[p[:,0], p[:,1]] = val if showPathImage: plt.figure(distFunc) plt.imshow(imgp, interpolation='none') if len(imgSaveName)>1: scipy.misc.imsave(imgSaveName, imgp) return maxPathLengths
sps = [] gts = [] pbar = getPbar(len(imgFiles), 'Load Image') pbar.start() for i,path in enumerate(imgFiles): if i>20 : break gtPath = gtBasePath + os.path.basename(path) rgbImg = vigra.impex.readImage(path) gtImg = vigra.impex.readImage(gtPath).astype('uint32')[::takeNth,::takeNth] gtImg[gtImg<125] = 0 gtImg[gtImg>=125] = 1 rgbImg = vigra.resize(rgbImg, [gtImg.shape[0],gtImg.shape[1]]) #vigra.imshow(gtImg.astype('float32')) #vigra.show() labImg = vigra.colors.transform_RGB2Lab(rgbImg.astype('float32')) sp,nSeg = vigra.analysis.slicSuperpixels(labImg, intensityScaling=20.0, seedDistance=5) sp = vigra.analysis.labelImage(sp)-1 #vigra.segShow(rgbImg, sp) #vigra.show() gg = vigra.graphs.gridGraph(rgbImg.shape[0:2]) rag = vigra.graphs.regionAdjacencyGraph(gg,sp) gt,qtq = rag.projectBaseGraphGt(gtImg)
def eccentricity(img, label, distFunc="exponential", showPathImage=False, percentageOfPaths=100, imgSaveName=""): img = img.astype(numpy.uint8) dim = len(img.shape) ## Enlarge image by one pixel on each side bigShape = [] for s in img.shape: bigShape.append(s + 2) bigImg = numpy.ones(bigShape) slices = [] for i in range(dim): slices.append(slice(1, bigImg.shape[i] - 1)) bigImg[slices] = img inside = numpy.where(bigImg == 0) outside = numpy.where(bigImg == 1) # ## Apply distanceTransform and modify (outside: high values, inside: low values) # distImage = vigra.filters.distanceTransform2D(bigImg.astype(numpy.float32)) # if showPathImage: # imgp = distImage.copy() # if distFunc == "exponential": # distImage = numpy.exp(distImage*-gamma) # elif distFunc == "linear": # maxDist = distImage.max() # distImage = maxDist - distImage # elif distFunc == "inverse": # w = numpy.where(distImage!=0) # distImage[w] = 1/distImage[w] # else: # print "wrong parameters for distFunc in eccentricity" ## Distance in the inside between two pixels is 1.0 distImage = bigImg.copy().astype(numpy.float32) distImage[inside] = 1.0 ## Set the outside to a very high value distImage[outside] = 10000.0 ## Image copy to draw the paths in imgp = distImage.copy() imgp[outside] = 100 ## Get image graph and its path finder gridGraph = vigraph.gridGraph(bigImg.shape[0:dim], False) graphShape = [] for s in distImage.shape: graphShape.append(s * 2 - 1) edgeWeights = vigra.resize(distImage, graphShape, order=0) edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImageCorrected( gridGraph, edgeWeights) pathFinder = vigraph.ShortestPathPathDijkstra(gridGraph) ## Find borders in img if dim == 2: bigLblImg = vigra.analysis.labelImage(bigImg.astype(numpy.uint8)) else: bigLblImg = vigra.analysis.labelVolume(bigImg.astype(numpy.uint8)) rag = vigraph.GridRegionAdjacencyGraph(gridGraph, bigLblImg) node = vigraph.GridRegionAdjacencyGraph.nodeFromId( rag, long(bigLblImg[inside][0])) edges = vigraph._ragFindEdges(rag, gridGraph, rag.affiliatedEdges, bigLblImg, node) # ## Remove duplicates # edges_a = numpy.ascontiguousarray(edges) # unique_edges = numpy.unique(edges_a.view([('', edges_a.dtype)]*edges_a.shape[1])) # edges = unique_edges.view(edges_a.dtype).reshape((unique_edges.shape[0], edges_a.shape[1])) borderImg = numpy.zeros(bigImg.shape) for edge in edges: slices = [] for d in range(dim): slices.append(slice(edge[d], edge[d] + 1)) borderImg[slices] = 1 # ## Find borders in img # borderImg = numpy.zeros(bigImg.shape) # if dim == 2: # for y in range(bigImg.shape[1]-1): # for x in range(bigImg.shape[0]-1): # if bigImg[x,y] == 0: # if bigImg[x+1,y] == 1 or bigImg[x,y+1] == 1: # borderImg[x, y] = 1 # else: # if bigImg[x+1,y] == 0: # borderImg[x+1, y] = 1 # if bigImg[x,y+1] == 0: # borderImg[x, y+1] = 1 # else: # for z in range(bigImg.shape[2]-1): # for y in range(bigImg.shape[1]-1): # for x in range(bigImg.shape[0]-1): # if bigImg[x,y,z] == 0: # if bigImg[x+1,y,z] == 1 or bigImg[x,y+1,z] == 1 or bigImg[x,y,z+1] == 1: # borderImg[x, y, z] = 1 # else: # if bigImg[x+1,y,z] == 0: # borderImg[x+1,y,z] = 1 # if bigImg[x,y+1,z] == 0: # borderImg[x,y+1,z] = 1 # if bigImg[x,y,z+1] == 0: # borderImg[x,y,z+1] = 1 ## End points for paths (all points on the border) targets = numpy.where(borderImg == 1) nTargets = len(targets[0]) ## Indices of start points for paths (random) nPoints = int(numpy.ceil(percentageOfPaths * nTargets / 100.0)) numpy.random.seed(42) starts = numpy.random.permutation(range(nTargets))[:nPoints] ## Compute paths maxPaths = [] maxPathLengths = [] for i in range(nPoints): sourceIndex = [] for d in range(dim): sourceIndex.append(int(targets[d][starts[i]])) source = gridGraph.coordinateToNode(sourceIndex) pathFinder.run(edgeWeights, source) maxPathLength = 0 for j in range(nTargets): targetIndex = [] for d in range(dim): targetIndex.append(int(targets[d][j])) target = gridGraph.coordinateToNode(targetIndex) path = pathFinder.path(pathType='coordinates', target=target) pathLength = pathFinder.distance(target) if pathLength > maxPathLength or maxPathLength == 0: maxPathLength = pathLength maxPath = path maxPaths.append(maxPath) maxPathLengths.append(maxPathLength) imgp[sourceIndex[0], sourceIndex[1]] = 3 * maxPathLength * maxPathLength if showPathImage: val = (imgp.max() + imgp.min()) / 2 for p in maxPaths: imgp[p[:, 0], p[:, 1]] = val if showPathImage: plt.figure(distFunc) plt.imshow(numpy.swapaxes(imgp, 1, 0), interpolation='none') if len(imgSaveName) > 1: scipy.misc.imsave(imgSaveName, numpy.swapaxes(imgp, 1, 0)) return maxPathLengths
def eccentricity( img, label, distFunc = "exponential", showPathImage = False, percentageOfPaths = 100, imgSaveName = "" ): img = img.astype(numpy.uint8) dim = len(img.shape) ## Enlarge image by one pixel on each side bigShape = [] for s in img.shape: bigShape.append(s + 2) bigImg = numpy.ones(bigShape) slices = [] for i in range(dim): slices.append(slice(1, bigImg.shape[i]-1)) bigImg[ slices ] = img inside = numpy.where(bigImg==0) outside = numpy.where(bigImg==1) # ## Apply distanceTransform and modify (outside: high values, inside: low values) # distImage = vigra.filters.distanceTransform2D(bigImg.astype(numpy.float32)) # if showPathImage: # imgp = distImage.copy() # if distFunc == "exponential": # distImage = numpy.exp(distImage*-gamma) # elif distFunc == "linear": # maxDist = distImage.max() # distImage = maxDist - distImage # elif distFunc == "inverse": # w = numpy.where(distImage!=0) # distImage[w] = 1/distImage[w] # else: # print "wrong parameters for distFunc in eccentricity" ## Distance in the inside between two pixels is 1.0 distImage = bigImg.copy().astype(numpy.float32) distImage[inside]=1.0 ## Set the outside to a very high value distImage[outside]=10000.0 ## Image copy to draw the paths in imgp = distImage.copy() imgp[outside] = 100 ## Get image graph and its path finder gridGraph = vigraph.gridGraph(bigImg.shape[0:dim],False) graphShape = [] for s in distImage.shape: graphShape.append(s*2-1) edgeWeights = vigra.resize(distImage, graphShape, order=0) edgeWeights = vigra.graphs.edgeFeaturesFromInterpolatedImageCorrected(gridGraph, edgeWeights) pathFinder = vigraph.ShortestPathPathDijkstra(gridGraph) ## Find borders in img if dim == 2: bigLblImg = vigra.analysis.labelImage(bigImg.astype(numpy.uint8)) else: bigLblImg = vigra.analysis.labelVolume(bigImg.astype(numpy.uint8)) rag = vigraph.GridRegionAdjacencyGraph(gridGraph, bigLblImg) node = vigraph.GridRegionAdjacencyGraph.nodeFromId(rag, long( bigLblImg[inside][0] )) edges = vigraph._ragFindEdges(rag, gridGraph, rag.affiliatedEdges, bigLblImg, node) # ## Remove duplicates # edges_a = numpy.ascontiguousarray(edges) # unique_edges = numpy.unique(edges_a.view([('', edges_a.dtype)]*edges_a.shape[1])) # edges = unique_edges.view(edges_a.dtype).reshape((unique_edges.shape[0], edges_a.shape[1])) borderImg = numpy.zeros(bigImg.shape) for edge in edges: slices = [] for d in range(dim): slices.append( slice(edge[d], edge[d]+1) ) borderImg[slices] = 1 # ## Find borders in img # borderImg = numpy.zeros(bigImg.shape) # if dim == 2: # for y in range(bigImg.shape[1]-1): # for x in range(bigImg.shape[0]-1): # if bigImg[x,y] == 0: # if bigImg[x+1,y] == 1 or bigImg[x,y+1] == 1: # borderImg[x, y] = 1 # else: # if bigImg[x+1,y] == 0: # borderImg[x+1, y] = 1 # if bigImg[x,y+1] == 0: # borderImg[x, y+1] = 1 # else: # for z in range(bigImg.shape[2]-1): # for y in range(bigImg.shape[1]-1): # for x in range(bigImg.shape[0]-1): # if bigImg[x,y,z] == 0: # if bigImg[x+1,y,z] == 1 or bigImg[x,y+1,z] == 1 or bigImg[x,y,z+1] == 1: # borderImg[x, y, z] = 1 # else: # if bigImg[x+1,y,z] == 0: # borderImg[x+1,y,z] = 1 # if bigImg[x,y+1,z] == 0: # borderImg[x,y+1,z] = 1 # if bigImg[x,y,z+1] == 0: # borderImg[x,y,z+1] = 1 ## End points for paths (all points on the border) targets = numpy.where(borderImg==1) nTargets = len(targets[0]) ## Indices of start points for paths (random) nPoints = int(numpy.ceil(percentageOfPaths * nTargets / 100.0)) numpy.random.seed(42) starts = numpy.random.permutation(range(nTargets))[:nPoints] ## Compute paths maxPaths = [] maxPathLengths = [] for i in range(nPoints): sourceIndex = [] for d in range(dim): sourceIndex.append(int(targets[d][starts[i]])) source = gridGraph.coordinateToNode(sourceIndex) pathFinder.run(edgeWeights, source) maxPathLength = 0 for j in range(nTargets): targetIndex = [] for d in range(dim): targetIndex.append(int(targets[d][j])) target = gridGraph.coordinateToNode(targetIndex) path = pathFinder.path(pathType='coordinates', target=target) pathLength = pathFinder.distance(target) if pathLength > maxPathLength or maxPathLength == 0: maxPathLength = pathLength maxPath = path maxPaths.append(maxPath) maxPathLengths.append(maxPathLength) imgp[sourceIndex[0], sourceIndex[1]] = 3*maxPathLength*maxPathLength if showPathImage: val = (imgp.max()+imgp.min())/2 for p in maxPaths: imgp[p[:,0], p[:,1]] = val if showPathImage: plt.figure(distFunc) plt.imshow(numpy.swapaxes(imgp, 1, 0), interpolation='none') if len(imgSaveName)>1: scipy.misc.imsave(imgSaveName, numpy.swapaxes(imgp, 1, 0)) return maxPathLengths
import numpy import skneuro import vigra from volumina.api import Viewer from PyQt4.QtGui import QApplication import skneuro.blockwise_filters as bf import skneuro.denoising as dn import pylab import h5py if True: #data = vigra.impex.readHDF5('/mnt/CLAWS1/tbeier/data/knott1000/knott-block-full2/d-gt.h5','sbfsem')[0:100,0:100,0:100].astype('float32') #dc = data.copy() data = vigra.impex.readImage('/home/tbeier/Desktop/10683114_980333491982687_1214393854_o.jpg').astype(numpy.float32) data = vigra.resize(data, [data.shape[0]/2, data.shape[1]/2]) #data = (data[:,:,0]+data[:,:,1]+data[:,:,2])/(3.0) dc = data.copy().view(numpy.ndarray) else : if False: fp = h5py.File("/mnt/CLAWS1/tbeier/data/knott1000_results/pixel_classification/boundary_prob_r1.h5","r") pmap = fp['exported_data'][0:300,0:300,0:300,1] fd = h5py.File("/mnt/CLAWS1/tbeier/data/knott1000/knott-block-full2/d-gt.h5", "r") data = fd['sbfsem'][0:300,0:300,0:300] vigra.impex.writeHDF5(data[0:300, 0:300, 0:300], "sub.h5", "data") vigra.impex.writeHDF5(pmap[0:300, 0:300, 0:300], "sub.h5", "pmap")
import opengm import vigra import numpy import time import sys fname = "135069.jpg" img = vigra.readImage(fname) img = numpy.sum(img,axis=2) img = vigra.resize(img,[s/1 for s in img.shape]) noise = numpy.random.random(img.size).reshape(img.shape)*255 print noise.shape img += noise img -= img.min() img /= img.max() print "shape", img.shape vigra.imshow(img) #vigra.show() threshold = 0.24 labelsNaive = img > threshold vigra.imshow(labelsNaive) #vigra.show() nVar = img.size nLabelsPerVar = 2 variableSpace = numpy.ones(nVar)*nLabelsPerVar gm = opengm.gm(variableSpace) t0 = time.time()
def getFeatures(rag, img, imgId): featureNames = ['1-Feature'] ############################## Filter ################################################### filters = [] ### Gradient Magnitude ### imgLab = vigra.colors.transform_RGB2Lab(img) imgLabBig = vigra.resize(imgLab, [imgLab.shape[0]*2-1, imgLab.shape[1]*2-1]) ##### was ist der Vorteil hiervon? ##### filters.append(vigra.filters.gaussianGradientMagnitude(imgLabBig, 1.)) featureNames.append('GradMag1') filters.append(vigra.filters.gaussianGradientMagnitude(imgLabBig, 2.)) featureNames.append('GradMag2') filters.append(vigra.filters.gaussianGradientMagnitude(imgLabBig, 5.)) featureNames.append('GradMag5') ### Hessian of Gaussian Eigenvalues ### sigmahoG = 2.0 hoG = vigra.filters.hessianOfGaussianEigenvalues(rgb2gray(imgLab), sigmahoG) filters.append(hoG[:,:,0]) featureNames.append('HessGauss1') filters.append(hoG[:,:,1]) featureNames.append('HessGauss2') ### Laplacian of Gaussian ### loG = vigra.filters.laplacianOfGaussian(imgLab) loG = loG[:,:,0] # es gilt hier: loG[:,:,i] = loG[:,:,j], i,j = 1,2,3 filters.append(loG) featureNames.append('LoG') ### Canny Filter ### scaleCanny = 2.0 thresholdCanny = 2.0 markerCanny = 1 canny = vigra.VigraArray(vigra.analysis.cannyEdgeImage(rgb2gray(img), scaleCanny, thresholdCanny, markerCanny), dtype=np.float32) filters.append(canny) featureNames.append('Canny') ### Structure Tensor Eigenvalues ### strucTens = vigra.filters.structureTensorEigenvalues(imgLab, 1.5, 3.0) filters.append(strucTens[:,:,0]) featureNames.append('StrucTensor1') filters.append(strucTens[:,:,1]) featureNames.append('StrucTensor2') n4 = vigra.impex.readImage('images/edgeDetectors/n4/' + imgId + '.png') filters.append(n4) featureNames.append('N4') dollar = vigra.impex.readImage('images/edgeDetectors/dollar/' + imgId + '.png') filters.append(dollar) featureNames.append('Dollar') ########################################################################################## ############# Edge Weights Calculation ############# edgeWeightsList = [] featureSpace = np.ones((rag.edgeNum, 1)) for i in range(len(filters)): gridGraphEdgeIndicator = graphs.edgeFeaturesFromImage(rag.baseGraph, filters[i]) edgeWeights = rag.accumulateEdgeFeatures(gridGraphEdgeIndicator) #edgeWeights /= edgeWeights.max() edgeWeights = edgeWeights.reshape(edgeWeights.shape[0], 1) featureSpace = np.concatenate((featureSpace, edgeWeights), axis=1) pos = np.where(np.array(featureNames)=='N4')[0][0] edgeWeights = featureSpace[:,pos] * rag.edgeLengths() edgeWeights /= edgeWeights.max() edgeWeights = edgeWeights.reshape(edgeWeights.shape[0], 1) featureSpace = np.concatenate((featureSpace, edgeWeights), axis=1) featureNames.append('N4_EdgeLengthWeighted') pos = np.where(np.array(featureNames)=='Dollar')[0][0] edgeWeights = featureSpace[:,pos] * rag.edgeLengths() edgeWeights /= edgeWeights.max() edgeWeights = edgeWeights.reshape(edgeWeights.shape[0], 1) featureSpace = np.concatenate((featureSpace, edgeWeights), axis=1) featureNames.append('Dollar_EdgeLengthWeighted') rgbDummy = np.array(n4) rgbDummy = rgbDummy.reshape(rgbDummy.shape[0], rgbDummy.shape[1], 1) edgeWeights = getEdgeWeightsFromNodesAround3(rag, rgbDummy, 1, variance=True, mean=True, meanRatio=True, medianRatio=True, skewness=True, kurtosis=True) featureSpace = np.concatenate((featureSpace, edgeWeights), axis=1) featureNames.extend(('N4_Variance_1', 'N4_Mean_1', 'N4_MeanRatio_1', 'N4_MedianRatio_1', 'N4_Skewness_1', 'N4_Kurtosis_1')) edgeWeights = getEdgeWeightsFromNodesAround3(rag, rgbDummy, 3, variance=True, mean=True, meanRatio=True, medianRatio=True, skewness=True, kurtosis=True) featureSpace = np.concatenate((featureSpace, edgeWeights), axis=1) featureNames.extend(('N4_Variance_3', 'N4_Mean_3', 'N4_MeanRatio_3', 'N4_MedianRatio_3', 'N4_Skewness_3', 'N4_Kurtosis_3')) rgbDummy = np.array(dollar) rgbDummy = rgbDummy.reshape(rgbDummy.shape[0], rgbDummy.shape[1], 1) edgeWeights = getEdgeWeightsFromNodesAround3(rag, rgbDummy, 1, variance=True, mean=True, meanRatio=True, medianRatio=True, skewness=True, kurtosis=True) featureSpace = np.concatenate((featureSpace, edgeWeights), axis=1) featureNames.extend(('Dollar_Variance_1', 'Dollar_Mean_1', 'Dollar_MeanRatio_1', 'Dollar_MedianRatio_1', 'Dollar_Skewness_1', 'Dollar_Kurtosis_1')) edgeWeights = getEdgeWeightsFromNodesAround3(rag, rgbDummy, 3, variance=True, mean=True, meanRatio=True, medianRatio=True, skewness=True, kurtosis=True) featureSpace = np.concatenate((featureSpace, edgeWeights), axis=1) featureNames.extend(('Dollar_Variance_3', 'Dollar_Mean_3', 'Dollar_MeanRatio_3', 'Dollar_MedianRatio_3', 'Dollar_Skewness_3', 'Dollar_Kurtosis_3')) edgeWeights = getEdgeWeightsFromNodesAround3(rag, imgLab, 1, variance=True, mean=True, meanRatio=True, medianRatio=True, skewness=True, kurtosis=True) featureSpace = np.concatenate((featureSpace, edgeWeights), axis=1) featureNames.extend(('Variance_1_R', 'Variance_1_G', 'Variance_1_B', 'Mean_1_R', 'Mean_1_G', 'Mean_1_B', 'MeanRatio_1_R', 'MeanRatio_1_G', 'MeanRatio_1_B', 'MedianRatio_1_R', 'MedianRatio_1_G', 'MedianRatio_1_B', 'Skewness_1_R', 'Skewness_1_G', 'Skewness_1_B', 'Kurtosis_1_R', 'Kurtosis_1_G', 'Kurtosis_1_B')) edgeWeights = getEdgeWeightsFromNodesAround3(rag, imgLab, 3, variance=True, mean=True, meanRatio=True, medianRatio=True, skewness=True, kurtosis=True) featureSpace = np.concatenate((featureSpace, edgeWeights), axis=1) featureNames.extend(('Variance_3_R', 'Variance_3_G', 'Variance_3_B', 'Mean_3_R', 'Mean_3_G', 'Mean_3_B', 'MeanRatio_3_R', 'MeanRatio_3_G', 'MeanRatio_3_B', 'MedianRatio_3_R', 'MedianRatio_3_G', 'MedianRatio_3_B', 'Skewness_3_R', 'Skewness_3_G', 'Skewness_3_B', 'Kurtosis_3_R', 'Kurtosis_3_G', 'Kurtosis_3_B')) featureSpace = featureSpace.astype(np.float64) ### Normalize to [-1, 1] '''for edgeWeights in featureSpace.transpose(): if (edgeWeights.min() < 0): edgeWeights -= edgeWeights.min() maximum = edgeWeights.max() edgeWeights *= 2 edgeWeights /= maximum edgeWeights -= 1 ''' ### Normalize to [0, 1] for edgeWeights in featureSpace.transpose(): if (edgeWeights.min() < 0): edgeWeights -= edgeWeights.min() edgeWeights /= edgeWeights.max() return featureSpace, featureNames