迭代图像大小不使用循环?

问题描述 投票:0回答:1
row,col = 4000, 4000  
a =np.zeros((row*col,6))
k=0
for i in range(row):
    for j in range(col):
        if k<len(a):
           a[k, 1:3]= [i,j] #Put pixsel coordinates in even rows -- 1. and 2. columns.
           k = k + 2

有一张尺寸为4000,4000的图像,我想迭代每个点的像素坐标(迭代尺寸).我就像上面那样做了.但是图像尺寸很大,这个过程需要很多时间.有没有更有效的方法可以做到这一点?

python image numpy loops image-processing
1个回答
0
投票

这可以在不循环的情况下完成,但是我不清楚为什么你只返回行的一半坐标,即在我的例子中,最后的非零行是坐标(4,9),因此在pix()中行被设置为5而不是10。

原函数。

def pixels():
    row,col = 10, 10 
    a =np.zeros((row*col,6))
    k=0
    for i in range(row):
        for j in range(col):
            if k<len(a):
                a[k, 1:3]= [i,j] #Put pixsel coordinates in even rows -- 1. and 2. columns.
                k = k + 2
    return a

带循环的新函数

def pix():
    row,col = 5, 10
    N = 2
    a = np.meshgrid(np.arange(row), np.arange(col), indexing='ij')
    b = np.transpose(a,np.roll(np.arange(N + 1), -1)).reshape(-1, N)
    z=np.zeros_like(b)
    s=np.hstack([b,z]).reshape(-1,2)
    l = np.zeros((s.shape[0],1), dtype=np.int)
    r = np.zeros((s.shape[0],3), dtype=np.int)

    return np.hstack([l, s, r])

比较:

>>> a=pix()
>>> b=pixels()
>>> np.all(b==a)
True

性能:

>>> timeit.timeit(pix, number=1000)
0.08045329099695664
>>> timeit.timeit(pixels, number=1000)
0.1498899580037687

pix()的输出。

>>> a
array([[0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 2, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 3, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 4, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 5, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 6, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 7, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 8, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 9, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 1, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 1, 2, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 1, 3, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 1, 4, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 1, 5, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 1, 6, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 1, 7, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 1, 8, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 1, 9, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 2, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 2, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 2, 2, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 2, 3, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 2, 4, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 2, 5, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 2, 6, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 2, 7, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 2, 8, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 2, 9, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 3, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 3, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 3, 2, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 3, 3, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 3, 4, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 3, 5, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 3, 6, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 3, 7, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 3, 8, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 3, 9, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 4, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 4, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 4, 2, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 4, 3, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 4, 4, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 4, 5, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 4, 6, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 4, 7, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 4, 8, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 4, 9, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])
© www.soinside.com 2019 - 2024. All rights reserved.