Exemplo n.º 1
0
    def makeGraph(self, img, dia, xScale, yScale):
        print 'Building Graph Data Structure'
        start = time.time()
        G = Graph(directed=False)
        vprop = G.new_vertex_property('object')
        eprop = G.new_edge_property('object')
        epropW = G.new_edge_property("int32_t")
        avgScale = (xScale + yScale) / 2

        test = np.where(img == True)
        ss = np.shape(test)
        cccc = 0
        percentOld = 0.0
        print str(np.round(percentOld, 1)) + '%'
        for (i, j) in zip(test[1], test[0]):
            cccc += 1
            percent = (float(cccc) / float(ss[1])) * 100
            if percentOld + 10 < percent:
                print str(np.round(percent, 1)) + '%'
                percentOld = percent
            nodeNumber1 = (float(i) * yScale, float(j) * xScale)
            if gu.find_vertex(
                    G, vprop, {
                        'imgIdx': (j, i),
                        'coord': nodeNumber1,
                        'nrOfPaths': 0,
                        'diameter': float(dia[j][i]) * avgScale
                    }):
                v1 = gu.find_vertex(
                    G, vprop, {
                        'imgIdx': (j, i),
                        'coord': nodeNumber1,
                        'nrOfPaths': 0,
                        'diameter': float(dia[j][i]) * avgScale
                    })[0]
            else:
                v1 = G.add_vertex()
                vprop[G.vertex(v1)] = {
                    'imgIdx': (j, i),
                    'coord': nodeNumber1,
                    'nrOfPaths': 0,
                    'diameter': float(dia[j][i]) * avgScale
                }
            try:

                if img[j, i + 1] == True:
                    nodeNumber2 = (float(i + 1) * yScale, float(j) * xScale)
                    if gu.find_vertex(
                            G, vprop, {
                                'imgIdx': (j, i + 1),
                                'coord': nodeNumber2,
                                'nrOfPaths': 0,
                                'diameter': float(dia[j][i + 1]) * avgScale
                            }):
                        v2 = gu.find_vertex(
                            G, vprop, {
                                'imgIdx': (j, i + 1),
                                'coord': nodeNumber2,
                                'nrOfPaths': 0,
                                'diameter': float(dia[j][i + 1]) * avgScale
                            })[0]
                        if gu.find_edge(
                                G, eprop, {
                                    'coord1':
                                    vprop[v2]['coord'],
                                    'coord2':
                                    vprop[v1]['coord'],
                                    'weight': ((vprop[v1]['diameter'] +
                                                vprop[v2]['diameter']) / 2)**4,
                                    'RTP':
                                    False
                                }):
                            pass
                        else:
                            e = G.add_edge(v1, v2)
                            epropW[e] = (((vprop[v1]['diameter'] +
                                           vprop[v2]['diameter']) / 2) /
                                         avgScale)**4
                            eprop[e] = {
                                'coord1':
                                vprop[v1]['coord'],
                                'coord2':
                                vprop[v2]['coord'],
                                'weight': ((vprop[v1]['diameter'] +
                                            vprop[v2]['diameter']) / 2)**4,
                                'RTP':
                                False
                            }
                    else:
                        v2 = G.add_vertex()
                        vprop[G.vertex(v2)] = {
                            'imgIdx': (j, i + 1),
                            'coord': nodeNumber2,
                            'nrOfPaths': 0,
                            'diameter': float(dia[j][i + 1]) * avgScale
                        }
                        e = G.add_edge(v1, v2)
                        epropW[e] = (
                            ((vprop[v1]['diameter'] + vprop[v2]['diameter']) /
                             2) / avgScale)**4
                        eprop[e] = {
                            'coord1':
                            vprop[v1]['coord'],
                            'coord2':
                            vprop[v2]['coord'],
                            'weight':
                            ((vprop[v1]['diameter'] + vprop[v2]['diameter']) /
                             2)**4,
                            'RTP':
                            False
                        }
            except:
                pass
            try:
                if img[j, i - 1] == True:
                    nodeNumber2 = (float(i - 1) * yScale, float(j) * xScale)
                    if gu.find_vertex(
                            G, vprop, {
                                'imgIdx': (j, i - 1),
                                'coord': nodeNumber2,
                                'nrOfPaths': 0,
                                'diameter': float(dia[j][i - 1]) * avgScale
                            }):
                        v2 = gu.find_vertex(
                            G, vprop, {
                                'imgIdx': (j, i - 1),
                                'coord': nodeNumber2,
                                'nrOfPaths': 0,
                                'diameter': float(dia[j][i - 1]) * avgScale
                            })[0]
                        if gu.find_edge(
                                G, eprop, {
                                    'coord1':
                                    vprop[v2]['coord'],
                                    'coord2':
                                    vprop[v1]['coord'],
                                    'weight': ((vprop[v1]['diameter'] +
                                                vprop[v2]['diameter']) / 2)**4,
                                    'RTP':
                                    False
                                }):
                            pass
                        else:
                            e = G.add_edge(v1, v2)
                            epropW[e] = (((vprop[v1]['diameter'] +
                                           vprop[v2]['diameter']) / 2) /
                                         avgScale)**4
                            eprop[e] = {
                                'coord1':
                                vprop[v1]['coord'],
                                'coord2':
                                vprop[v2]['coord'],
                                'weight': ((vprop[v1]['diameter'] +
                                            vprop[v2]['diameter']) / 2)**4,
                                'RTP':
                                False
                            }
                    else:
                        v2 = G.add_vertex()
                        vprop[G.vertex(v2)] = {
                            'imgIdx': (j, i - 1),
                            'coord': nodeNumber2,
                            'nrOfPaths': 0,
                            'diameter': float(dia[j][i - 1]) * avgScale
                        }
                        e = G.add_edge(v1, v2)
                        epropW[e] = (
                            ((vprop[v1]['diameter'] + vprop[v2]['diameter']) /
                             2) / avgScale)**4
                        eprop[e] = {
                            'coord1':
                            vprop[v1]['coord'],
                            'coord2':
                            vprop[v2]['coord'],
                            'weight':
                            ((vprop[v1]['diameter'] + vprop[v2]['diameter']) /
                             2)**4,
                            'RTP':
                            False
                        }
            except:
                pass
            try:
                if img[j + 1, i] == True:
                    nodeNumber2 = (float(i) * yScale, float(j + 1) * xScale)
                    if gu.find_vertex(
                            G, vprop, {
                                'imgIdx': (j + 1, i),
                                'coord': nodeNumber2,
                                'nrOfPaths': 0,
                                'diameter': float(dia[j + 1][i]) * avgScale
                            }):
                        v2 = gu.find_vertex(
                            G, vprop, {
                                'imgIdx': (j + 1, i),
                                'coord': nodeNumber2,
                                'nrOfPaths': 0,
                                'diameter': float(dia[j + 1][i]) * avgScale
                            })[0]
                        if gu.find_edge(
                                G, eprop, {
                                    'coord1':
                                    vprop[v2]['coord'],
                                    'coord2':
                                    vprop[v1]['coord'],
                                    'weight': ((vprop[v1]['diameter'] +
                                                vprop[v2]['diameter']) / 2)**4,
                                    'RTP':
                                    False
                                }):
                            pass
                        else:
                            e = G.add_edge(v1, v2)
                            epropW[e] = (((vprop[v1]['diameter'] +
                                           vprop[v2]['diameter']) / 2) /
                                         avgScale)**4
                            eprop[e] = {
                                'coord1':
                                vprop[v1]['coord'],
                                'coord2':
                                vprop[v2]['coord'],
                                'weight': ((vprop[v1]['diameter'] +
                                            vprop[v2]['diameter']) / 2)**4,
                                'RTP':
                                False
                            }
                    else:
                        v2 = G.add_vertex()
                        vprop[G.vertex(v2)] = {
                            'imgIdx': (j + 1, i),
                            'coord': nodeNumber2,
                            'nrOfPaths': 0,
                            'diameter': float(dia[j + 1][i]) * avgScale
                        }
                        e = G.add_edge(v1, v2)
                        epropW[e] = (
                            ((vprop[v1]['diameter'] + vprop[v2]['diameter']) /
                             2) / avgScale)**4
                        eprop[e] = {
                            'coord1':
                            vprop[v1]['coord'],
                            'coord2':
                            vprop[v2]['coord'],
                            'weight':
                            ((vprop[v1]['diameter'] + vprop[v2]['diameter']) /
                             2)**4,
                            'RTP':
                            False
                        }
            except:
                pass
            try:
                if img[j - 1, i] == True:
                    nodeNumber2 = (float(i) * yScale, float(j - 1) * xScale)
                    if gu.find_vertex(
                            G, vprop, {
                                'imgIdx': (j - 1, i),
                                'coord': nodeNumber2,
                                'nrOfPaths': 0,
                                'diameter': float(dia[j - 1][i]) * avgScale
                            }):
                        v2 = gu.find_vertex(
                            G, vprop, {
                                'imgIdx': (j - 1, i),
                                'coord': nodeNumber2,
                                'nrOfPaths': 0,
                                'diameter': float(dia[j - 1][i]) * avgScale
                            })[0]
                        if gu.find_edge(
                                G, eprop, {
                                    'coord1':
                                    vprop[v2]['coord'],
                                    'coord2':
                                    vprop[v1]['coord'],
                                    'weight': ((vprop[v1]['diameter'] +
                                                vprop[v2]['diameter']) / 2)**4,
                                    'RTP':
                                    False
                                }):
                            pass
                        else:
                            e = G.add_edge(v1, v2)
                            epropW[e] = (((vprop[v1]['diameter'] +
                                           vprop[v2]['diameter']) / 2) /
                                         avgScale)**4
                            eprop[e] = {
                                'coord1':
                                vprop[v1]['coord'],
                                'coord2':
                                vprop[v2]['coord'],
                                'weight': ((vprop[v1]['diameter'] +
                                            vprop[v2]['diameter']) / 2)**4,
                                'RTP':
                                False
                            }
                    else:
                        v2 = G.add_vertex()
                        vprop[G.vertex(v2)] = {
                            'imgIdx': (j - 1, i),
                            'coord': nodeNumber2,
                            'nrOfPaths': 0,
                            'diameter': float(dia[j - 1][i]) * avgScale
                        }
                        e = G.add_edge(v1, v2)
                        epropW[e] = (
                            ((vprop[v1]['diameter'] + vprop[v2]['diameter']) /
                             2) / avgScale)**4
                        eprop[e] = {
                            'coord1':
                            vprop[v1]['coord'],
                            'coord2':
                            vprop[v2]['coord'],
                            'weight':
                            ((vprop[v1]['diameter'] + vprop[v2]['diameter']) /
                             2)**4,
                            'RTP':
                            False
                        }
            except:
                pass
#
        print '100.0%'
        print 'selecting largest connected component'
        G.edge_properties["ep"] = eprop
        G.edge_properties["w"] = epropW
        G.vertex_properties["vp"] = vprop
        l = gt.label_largest_component(G)
        print(l.a)
        u = gt.GraphView(G, vfilt=l)
        print '# vertices'
        print(u.num_vertices())
        print(G.num_vertices())
        print '# edges'
        print(u.num_edges())
        print 'building graph finished in: ' + str(time.time() - start) + 's'
        return u
Exemplo n.º 2
0
    def makeGraphFast(self, img, dia, xScale, yScale):
        print('Building Graph Data Structure'),
        start = time.time()
        G = Graph(directed=False)
        sumAddVertices = 0

        vprop = G.new_vertex_property('object')
        eprop = G.new_edge_property('object')
        epropW = G.new_edge_property("float")
        h, w = np.shape(img)
        avgScale = (xScale + yScale) / 2

        addedVerticesLine2 = []
        vListLine2 = []
        percentOld = 0
        counter = 0
        '''
        Sweep over each line in the image except the last line
        '''
        for idx, i in enumerate(img[:len(img) - 2]):
            '''
            Get foreground indices in the current line of the image and make vertices
            '''
            counter += 1
            percent = (float(counter) / float(h)) * 100
            if percentOld + 10 < percent:
                print(str(np.round(percent, 1)) + '% '),
                percentOld = percent

            line1 = np.where(i == True)
            if len(line1[0]) > 0:
                line1 = set(line1[0]).difference(set(addedVerticesLine2))
                vL = G.add_vertex(len(list(line1)))

                if len(line1) > 1:
                    vList = vListLine2 + list(vL)
                else:
                    vList = vListLine2 + [vL]
                line1 = addedVerticesLine2 + list(line1)
                for jdx, j in enumerate(line1):
                    vprop[vList[jdx]] = {
                        'imgIdx': (j, idx),
                        'coord': (float(j) * xScale, float(idx) * yScale),
                        'nrOfPaths': 0,
                        'diameter': float(dia[idx][j]) * avgScale
                    }
                '''
                keep order of the inserted vertices
                '''
                sumAddVertices += len(line1)

                addedVerticesLine2 = []
                vListLine2 = []
                '''
                Connect foreground indices to neighbours in the next line
                '''
                for v1 in line1:
                    va = vList[line1.index(v1)]
                    diagonalLeft = diagonalRight = True
                    try:
                        if img[idx][v1 - 1] == True:
                            diagonalLeft = False
                            vb = vList[line1.index(v1 - 1)]
                            e = G.add_edge(va, vb)
                            eprop[e] = {
                                'coord1':
                                vprop[va]['coord'],
                                'coord2':
                                vprop[vb]['coord'],
                                'weight': ((vprop[va]['diameter'] +
                                            vprop[vb]['diameter']) / 2),
                                'RTP':
                                False
                            }
                            epropW[e] = 2. / (eprop[e]['weight']**2)
                    except:
                        print 'Boundary vertex at: ' + str(
                            [v1, idx - 1]) + ' image size: ' + str([w, h])
                        pass

                    try:
                        if img[idx][v1 + 1] == True:
                            diagonalRight = False
                            vb = vList[line1.index(v1 + 1)]
                            e = G.add_edge(va, vb)
                            eprop[e] = {
                                'coord1':
                                vprop[va]['coord'],
                                'coord2':
                                vprop[vb]['coord'],
                                'weight': ((vprop[va]['diameter'] +
                                            vprop[vb]['diameter']) / 2),
                                'RTP':
                                False
                            }
                            epropW[e] = 2. / (eprop[e]['weight']**2)
                    except:
                        print 'Boundary vertex at: ' + str(
                            [v1 + 1, idx]) + ' image size: ' + str([w, h])
                        pass  # just if we are out of bounds

                    try:
                        if img[idx + 1][v1] == True:
                            diagonalRight = False
                            diagonalLeft = False
                            vNew = G.add_vertex()
                            vprop[vNew] = {
                                'imgIdx': (v1, idx + 1),
                                'coord':
                                (float(v1) * xScale, float(idx + 1) * yScale),
                                'nrOfPaths':
                                0,
                                'diameter':
                                float(dia[idx + 1][v1]) * avgScale
                            }
                            vListLine2.append(vNew)
                            e = G.add_edge(vList[line1.index(v1)], vNew)
                            eprop[e] = {
                                'coord1':
                                vprop[va]['coord'],
                                'coord2':
                                vprop[vNew]['coord'],
                                'weight': ((vprop[va]['diameter'] +
                                            vprop[vNew]['diameter']) / 2),
                                'RTP':
                                False
                            }
                            epropW[e] = 1. / (eprop[e]['weight']**2)
                            if v1 not in addedVerticesLine2:
                                addedVerticesLine2.append(v1)
                    except:
                        print 'Boundary vertex at: ' + str(
                            [v1, idx + 1]) + ' image size: ' + str([w, h])
                        pass

                    try:
                        if diagonalRight == True and img[idx + 1][v1 +
                                                                  1] == True:
                            vNew = G.add_vertex()
                            vprop[vNew] = {
                                'imgIdx': (v1 + 1, idx + 1),
                                'coord': (float(v1 + 1) * xScale,
                                          float(idx + 1) * yScale),
                                'nrOfPaths':
                                0,
                                'diameter':
                                float(dia[idx + 1][v1 + 1]) * avgScale
                            }
                            vListLine2.append(vNew)
                            e = G.add_edge(vList[line1.index(v1)], vNew)
                            eprop[e] = {
                                'coord1':
                                vprop[va]['coord'],
                                'coord2':
                                vprop[vNew]['coord'],
                                'weight': ((vprop[va]['diameter'] +
                                            vprop[vNew]['diameter']) / 2),
                                'RTP':
                                False
                            }
                            epropW[e] = 1.41 / (eprop[e]['weight']**2)
                            if v1 + 1 not in addedVerticesLine2:
                                addedVerticesLine2.append(v1 + 1)
                    except:
                        print 'Boundary vertex at: ' + str(
                            [v1 + 1, idx + 1]) + ' image size: ' + str([w, h])
                        pass

                    try:
                        if diagonalLeft == True and img[idx + 1][v1 -
                                                                 1] == True:
                            vNew = G.add_vertex()
                            vprop[vNew] = {
                                'imgIdx': (v1 - 1, idx + 1),
                                'coord': (float(v1 - 1) * xScale,
                                          float(idx + 1) * yScale),
                                'nrOfPaths':
                                0,
                                'diameter':
                                float(dia[idx + 1][v1 - 1]) * avgScale
                            }
                            vListLine2.append(vNew)
                            e = G.add_edge(vList[line1.index(v1)], vNew)
                            eprop[e] = {
                                'coord1':
                                vprop[va]['coord'],
                                'coord2':
                                vprop[vNew]['coord'],
                                'weight': ((vprop[va]['diameter'] +
                                            vprop[vNew]['diameter']) / 2),
                                'RTP':
                                False
                            }
                            epropW[e] = 1.41 / (eprop[e]['weight']**2)
                            if v1 - 1 not in addedVerticesLine2:
                                addedVerticesLine2.append(v1 - 1)
                    except:
                        print 'Boundary vertex at: ' + str(
                            [v1 - 1, idx + 1]) + ' image size: ' + str([w, h])
                        pass
                    try:
                        if img[idx][v1 + 1] == False and img[idx][
                                v1 - 1] == False and img[idx + 1][
                                    v1] == False and diagonalLeft == False and diagonalRight == False:
                            print 'tip detected'
                            if img[idx - 1][v1 - 1] == False and img[idx - 1][
                                    v1 + 1] == False and img[idx -
                                                             1][v1] == False:
                                print 'floating pixel'
                    except:
                        pass

        print 'done!'
        G.edge_properties["ep"] = eprop
        G.edge_properties["w"] = epropW
        G.vertex_properties["vp"] = vprop
        print 'graph build in ' + str(time.time() - start)
        l = gt.label_largest_component(G)
        u = gt.GraphView(G, vfilt=l)
        print '# vertices'
        print(u.num_vertices())
        print(G.num_vertices())
        if u.num_vertices() != G.num_vertices():
            self.__fail = float((G.num_vertices() - u.num_vertices())) / float(
                G.num_vertices())
        return u, u.num_vertices()