Ejemplo n.º 1
0
 def __init__(self, data=None, **attr):
     # the init could be
     # graph = Graph(g) where g is a Graph
     # graph = Graph((v,e)) where v is Graph.v like and e is Graph.e like
     # graph = Graph(({},e)) for an edgelist with no specified nodes
     # others with classmethods like
     # Graph.from_adjacency_matrix(m)
     # Graph.from_adjacency_list(l)
     #
     # should abstract the data here
     self._nodedata = {}  # empty node attribute dict
     self._adjacency = {}  # empty adjacency dict
     # the interface is n,e,a,data
     self.n = Nodes(self._nodedata, self._adjacency) # rename to self.nodes
     self.e = Edges(self._nodedata, self._adjacency) # rename to self.edges
     self.a = Adjacency(self._adjacency) # rename to self.adjacency
     self.data = {}   # dictionary for graph attributes
     # load with data
     if hasattr(data,'n') and not hasattr(data,'name'): # it is a new graph
         self.n.update(data.n)
         self.e.update(data.e)
         self.data.update(data.data)
         data = None
     try:
         nodes,edges = data # containers of edges and nodes
         self.n.update(nodes)
         self.e.update(edges)
     except: # old style
         if data is not None:
             convert.to_networkx_graph(data, create_using=self)
     # load __init__ graph attribute keywords
     self.data.update(attr)
Ejemplo n.º 2
0
 def setUp(self):
     self.Graph = nxGraph
     # build dict-of-dict-of-dict K3
     ed1, ed2, ed3 = ({}, {}, {})
     self.k3adj = {
         0: {
             1: ed1,
             2: ed2
         },
         1: {
             0: ed1,
             2: ed3
         },
         2: {
             0: ed2,
             1: ed3
         }
     }
     self.k3edges = [(0, 1), (0, 2), (1, 2)]
     self.k3nodes = [0, 1, 2]
     self.K3 = self.Graph()
     self.K3.adj = self.K3._adjacency = self.K3.edge = self.k3adj
     self.K3.node = self.K3._nodedata = {}
     self.K3.node[0] = {}
     self.K3.node[1] = {}
     self.K3.node[2] = {}
     self.K3.n = Nodes(self.K3._nodedata, self.K3._adjacency)
     self.K3.e = Edges(self.K3._nodedata, self.K3._adjacency)
     self.K3.a = Adjacency(self.K3._adjacency)
Ejemplo n.º 3
0
    def addEdges(self, v1, v2, weight=None, orientation=None):
        """
            Add a new edges in the Graph

            Required parameters :
            v1 : the first edge
            v2 : the second edge

            Optional parameters :
            weight : if weighted graph, must add a weight (default : None)
            orientation : if oriented graph, must add an orientation (default : None)
        """
        existing_vertices = self.getVertices()
        if v1 in existing_vertices and v2 in existing_vertices:
            if not self.existingEdges(v1, v2):
                if not self.weighted:
                    weight = None
                else:
                    if weight == None:
                        print("Error : Weighted graph, must add a weight.")
                        return -1
                if not self.oriented:
                    orientation = None
                else:
                    if orientation == None:
                        print(
                            "Error : Oriented Graph, must add an orientation.")
                        return -1
                new_edges = Edges(v1, v2, weight, orientation)
                self.edges.append(new_edges)
            else:
                print("Error : [" + v1 + "," + v2 + "]" + " already exists.")
        else:
            print("Error : " + v1 + " or " + v2 + " doesn't exist.")
Ejemplo n.º 4
0
 def __init__(self, graph, subnodes):
     # TODO Can we replace nbunch_iter with set(subnodes) & set(graph)?
     #      We lose the Error messages...
     self._subnodes = set(self._nbunch_iter(graph, subnodes))
     self._nodedata = SubNbrDict(self._subnodes, graph._nodedata)
     self._adjacency = SubAdjacency(self._subnodes, graph._adjacency)
     self.data = graph.data
     self.n = Nodes(self._nodedata, self._adjacency)
     self.e = Edges(self._nodedata, self._adjacency)
     self.a = self._adjacency
Ejemplo n.º 5
0
class Sketch:
    def __init__(self, image):
        self.img = cv.imread(image)

    def sketch(self):
        grey = cv.cvtColor(self.img, cv.COLOR_BGR2GRAY)
        inv = 255 - grey
        blur = cv.GaussianBlur(inv, (13,13), 0)
        return cv.divide(grey, 255-blur, scale=256)

img = Image.open(filename)

sketch = Sketch(filename).sketch()
cv.imwrite("sketch.png", sketch)
bg = Background(img.size, octaves=6).background()
edges = Edges(filename).edges()
#sketchTrans = cv.cvtColor(sketch, cv.COLOR_GRAY2RGBA)

mask = edges[3]
sketch = cv.bitwise_and(sketch, edges, edges)
(thresh, sketch) = cv.threshold(sketch, 240, 255, cv.THRESH_BINARY)
#sketch = cv.multiply(sketch, np.array(bg), scale=(1./128))


h, w = sketch.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
#mask[1:h+1, 1:w+1] = sketch
sketchColor = cv.cvtColor(sketch, cv.COLOR_GRAY2RGBA)
white = np.all(sketchColor == [255,255,255,255], axis=-1)
sketchColor[white, -1] = 0
cv.imwrite("final.png", sketchColor)