def testCrackConnectionImage(self): labels = vigra.analysis.labelImageWithBackground( vigra.readImage("crackConvert-test1.png")[0].astype(int), 8, 0)[0] cc = crackConnectionImage(labels) cc_ref = vigra.readImage("crackConvert-test1cc.png") self.assertEqual(numpy.abs(cc - cc_ref).max(), 0)
def pyCrackEdgeGraph(labelImage, eightConnectedRegions=True, progressHook=None): result = GeoMap(labelImage.size()) cc = crackConnectionImage(labelImage) if eightConnectedRegions: for y in range(1, cc.height() - 1): for x in range(1, cc.width() - 1): if cc[x, y] == CONN_ALL4: if labelImage[x, y] == labelImage[x - 1, y - 1]: cc[x, y] += CONN_DIAG_UPLEFT if labelImage[x - 1, y] == labelImage[x, y - 1]: cc[x, y] += CONN_DIAG_UPRIGHT # crossing regions? if cc[x, y] == CONN_ALL4 + CONN_DIAG_UPLEFT + CONN_DIAG_UPRIGHT: # preserve connectedness of higher label: if labelImage[x, y - 1] > labelImage[x - 1, y - 1]: cc[x, y] -= CONN_DIAG_UPLEFT else: cc[x, y] -= CONN_DIAG_UPRIGHT for y in range(cc.height()): for x in range(cc.width()): conn = int(cc[x, y]) if degree[conn] > 2: cc[x, y] = conn | CONN_NODE elif conn & CONN_ALL4 == (CONN_RIGHT | CONN_DOWN): cc[x, y] = conn | CONN_MAYBE_NODE if conn & CONN_DIAG: cc[x, y] = conn | CONN_MAYBE_NODE nodeImage = ScalarImage(cc.size()) # nodeImage encoding: each pixel's higher 28 bits encode the # (label + 1) of a node that has been inserted into the resulting # GeoMap at the corresponding position (+1 because zero is a valid # node label), while the lower 4 bits encode the four CONN_ # directions in which a GeoMap edge is already connected to this # node progressHook = progressHook and progressHook.rangeTicker(cc.height()) for startAt in (CONN_NODE, CONN_MAYBE_NODE): for y in range(cc.height()): if progressHook: progressHook() for x in range(cc.width()): nodeConn = int(cc[x, y]) if nodeConn & startAt: startNodeInfo = int(nodeImage[x, y]) if startNodeInfo: startNode = result.node((startNodeInfo >> 4) - 1) else: startNode = result.addNode((x - 0.5, y - 0.5)) nodeImage[x, y] = startNodeInfo = \ (startNode.label() + 1) << 4 for direction, startConn in enumerate(connections): if nodeConn & startConn and not startNodeInfo & startConn: edge, endPos, endConn = followEdge( cc, (x, y), direction) endNodeInfo = int(nodeImage[endPos]) if not endNodeInfo: endNode = result.addNode( (endPos[0] - 0.5, endPos[1] - 0.5)) endNodeInfo = (endNode.label() + 1) << 4 else: assert not endNodeInfo & endConn, "double connection?" endNode = result.node((endNodeInfo >> 4) - 1) edge = result.addEdge(startNode, endNode, edge) startNodeInfo |= startConn if edge.isLoop(): startNodeInfo |= endConn nodeImage[x, y] = startNodeInfo else: nodeImage[x, y] = startNodeInfo nodeImage[endPos] = endNodeInfo | endConn return result
def pyCrackEdgeGraph(labelImage, eightConnectedRegions = True, progressHook = None): result = GeoMap(labelImage.size()) cc = crackConnectionImage(labelImage) if eightConnectedRegions: for y in range(1, cc.height()-1): for x in range(1, cc.width()-1): if cc[x,y] == CONN_ALL4: if labelImage[x,y] == labelImage[x-1,y-1]: cc[x,y] += CONN_DIAG_UPLEFT if labelImage[x-1,y] == labelImage[x,y-1]: cc[x,y] += CONN_DIAG_UPRIGHT # crossing regions? if cc[x,y] == CONN_ALL4 + CONN_DIAG_UPLEFT + CONN_DIAG_UPRIGHT: # preserve connectedness of higher label: if labelImage[x,y-1] > labelImage[x-1,y-1]: cc[x,y] -= CONN_DIAG_UPLEFT else: cc[x,y] -= CONN_DIAG_UPRIGHT for y in range(cc.height()): for x in range(cc.width()): conn = int(cc[x,y]) if degree[conn] > 2: cc[x,y] = conn | CONN_NODE elif conn & CONN_ALL4 == (CONN_RIGHT | CONN_DOWN): cc[x,y] = conn | CONN_MAYBE_NODE if conn & CONN_DIAG: cc[x,y] = conn | CONN_MAYBE_NODE nodeImage = ScalarImage(cc.size()) # nodeImage encoding: each pixel's higher 28 bits encode the # (label + 1) of a node that has been inserted into the resulting # GeoMap at the corresponding position (+1 because zero is a valid # node label), while the lower 4 bits encode the four CONN_ # directions in which a GeoMap edge is already connected to this # node progressHook = progressHook and progressHook.rangeTicker(cc.height()) for startAt in (CONN_NODE, CONN_MAYBE_NODE): for y in range(cc.height()): if progressHook: progressHook() for x in range(cc.width()): nodeConn = int(cc[x, y]) if nodeConn & startAt: startNodeInfo = int(nodeImage[x, y]) if startNodeInfo: startNode = result.node((startNodeInfo >> 4) - 1) else: startNode = result.addNode((x - 0.5, y - 0.5)) nodeImage[x, y] = startNodeInfo = \ (startNode.label() + 1) << 4 for direction, startConn in enumerate(connections): if nodeConn & startConn and not startNodeInfo & startConn: edge, endPos, endConn = followEdge( cc, (x, y), direction) endNodeInfo = int(nodeImage[endPos]) if not endNodeInfo: endNode = result.addNode((endPos[0] - 0.5, endPos[1] - 0.5)) endNodeInfo = (endNode.label() + 1) << 4 else: assert not endNodeInfo & endConn, "double connection?" endNode = result.node((endNodeInfo >> 4) - 1) edge = result.addEdge(startNode, endNode, edge) startNodeInfo |= startConn if edge.isLoop(): startNodeInfo |= endConn nodeImage[x, y] = startNodeInfo else: nodeImage[x, y] = startNodeInfo nodeImage[endPos] = endNodeInfo | endConn return result