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

2024-09-28 17:01:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

有一张40004000大小的图像,我想迭代每个点的像素坐标(迭代大小)。我像上面一样做了。但是图像大小很大,这个过程需要很多时间。有没有更有效的方法


Tags: in图像forlenifputnpzeros
1条回答
网友
1楼 · 发布于 2024-09-28 17:01:00

这可以在不循环的情况下完成,但是我不清楚为什么只返回一半行上的坐标,即在我的示例中,最后一个非零行是坐标(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]])

相关问题 更多 >