Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import sys, os, numpy
  3.  
  4. sys.path.append(r"D:\tools\OSGeo4W64\apps\Python27")
  5.  
  6. os.environ['PATH'] = r"D:\tools\OSGeo4W64\bin"
  7.  
  8. import networkx as nx
  9.  
  10. from PIL import Image
  11.  
  12. import matplotlib.pyplot as plt
  13.  
  14. # lire une matrice (Adjacency matrix)
  15. # à partir d'une image
  16. # https://en.wikipedia.org/wiki/Adjacency_matrix
  17. im = Image.open(r"raster/matrix.png")
  18.  
  19. # matrix.png
  20. #     0 1 2 3 4 5 <- noeuds
  21. #
  22. # 0   0 1 0 0 1 0
  23. # 1   1 0 1 0 1 0
  24. # 2   0 1 0 1 0 0
  25. # 3   0 0 1 0 1 1
  26. # 4   1 1 0 1 0 0
  27. # 5   0 0 0 1 0 0
  28.  
  29. imarray = numpy.array(im)
  30.  
  31. extract = imarray[:,:,0]
  32.  
  33. A=numpy.matrix(extract)
  34.  
  35. # on va créer le graphe à partir de la matrice
  36. # on divise par 255 (le max) pour avoir des poinds allant
  37. # de 0 à 1
  38. G=nx.from_numpy_matrix(A/255.0)
  39.  
  40. nx.draw(G, with_labels=True)
  41.  
  42. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement