[英]Creating large adjacency matrix from image in python
I would like to create a large, weighted adjacency matrix from an image (so lots of vertices... in the order of > 10^5 vertices) in python.我想从 python 中的图像(这么多顶点......以> 10 ^ 5个顶点的顺序)创建一个大的加权邻接矩阵。 Weights between adjacent pixels are color gradients (I take care of this).
相邻像素之间的权重是颜色渐变(我负责)。 Doing it by iterating through pixels is very slow... it takes over 4 minutes.
通过迭代像素来完成它非常慢......它需要超过 4 分钟。 :-( Are there any libraries that can do this nicely in reasonable time?
:-( 是否有任何图书馆可以在合理的时间内很好地做到这一点?
The following is my code which runs very slowly:以下是我运行非常缓慢的代码:
def indToCoord(ind, w, h):
x = ind % w
y = (ind - x)/h
return (x,y)
def isAdj(p1, p2, im):
adj = []
w, h = im.size
x1, y1 = p1
x2, y2 = p2
if (x1, y1) == (x2, y2):
return 0
elif abs(x1 - x2) > 1:
return 0
elif abs(y1 - y2) > 1:
return 0
elif abs(x1 - x2) + abs(y1 - y2) >= 2:
return 0
return util.colorGradient(im, p1, p2)
def adjForPixel(pixels, p1, im):
return [isAdj(p1,p2,im) for p2 in pixels]
# The following is the function I use to create an Adjacency Matrix from an image
def getAdjMatrix(im):
width, height = im.size
pixels = [(x,y) for x in xrange(width) for y in xrange(height)]
pixelAdjMatr = [adjForPixel(pixels, p, im) for p in pixels]
return pixelAdjMatr
adj_matrix = getAdjMatrix(im)
Thank you!谢谢!
Python module/library NetworkX has an adjacency matrix implementation. Python模块/库NetworkX具有邻接矩阵实现。 It returns a scipy matrix
它返回一个scipy矩阵
https://networkx.github.io/documentation/latest/reference/generated/networkx.linalg.graphmatrix.adjacency_matrix.html https://networkx.github.io/documentation/latest/reference/generated/networkx.linalg.graphmatrix.adjacency_matrix.html
import networkx as nx
import scipy as sp
g = nx.Graph([(1,1)])
a = nx.adjacency_matrix(g)
print a, type(a)
returns 回报
(0, 0) 1 <class 'scipy.sparse.csr.csr_matrix'>
img_to_graph will do the trick. img_to_graph可以解决问题。 This creates a so-called "connectivity matrix".
这创建了一个所谓的“连接矩阵”。 The adjacency matrix is such a ubiquitous and important term that
sklearn
's departure here is not awesome.邻接矩阵是一个无处不在的重要术语,
sklearn
在这里的离开并不令人敬畏。
But this function will do the trick.但是这个函数可以解决问题。 I found that
networkx
's function, cited above, was only useful for graphs.我发现
networkx
函数只对图形有用。 In order to convert an image to a networkx
graph from a numpy
array, you are pretty much reduced to interpreted for loop writing.为了将图像从
numpy
数组转换为networkx
图,您几乎可以减少为循环写入进行解释。 This is because the networkx
library does provide an optimized numpy->graph
function, but that function assumes the numpy
array is already an adjacency matrix .这是因为
networkx
库确实提供了优化的numpy->graph
函数,但该函数假定numpy
数组已经是一个邻接矩阵。
This is a compiled -O3 --simd --omp
only loop scenario for any reasonably sized images, and may even benefit from some cache optimization strategies (ruling out python3's numba
Just-In-Time compiler).对于任何合理大小的图像,这是一个已编译的
-O3 --simd --omp
仅循环方案,甚至可能受益于某些缓存优化策略(排除 python3 的numba
即时编译器)。
That makes the networkx
answer another problem.这使得
networkx
回答了另一个问题。 I'd go ahead and use the sklearn
function there:我会继续在那里使用
sklearn
函数:
sklearn.feature_extraction.image.img_to_graph(your_img)
# returns scipy.sparse.coo.coo_matrix
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.