如何提高numpy迭代的效率?

2 投票
1 回答
564 浏览
提问于 2025-04-17 03:22

我正在做一个作业,内容是将灰度图像转换成1位的二进制图像,使用了一种叫做抖动的技术。我尝试了一个简单的4x4矩阵,这样可以让图像的大小变成原来的16倍。

dithering_matrix = array([[ 0,  8,  2, 10],
                          [12,  4, 14,  6],
                          [ 3, 11,  1,  9],
                          [15,  7, 13,  5]], dtype=uint8)
split_num = dithering_matrix.size + 1

我读取了一张512x512的图像到im这个数组里,然后做了以下操作:

output = list()
for row in im:
    row_output = list()
    for pixel in row:
        pixel_matrix = ((pixel / (256 / split_num)) > dithering_matrix) * 255
        row_output.append(pixel_matrix)
    output.append( hstack( tuple(row_output) ) )
output_matrix = vstack( tuple(output) )

我发现输出需要8到10秒,我觉得上面im的循环花了很多时间。在一些软件里,做同样的操作通常是瞬间完成的。那么,有没有办法提高效率呢?


更新: @Ignacio Vazquez-Abrams 我对性能分析工具不太熟悉:( 我试了cProfile,结果让我感到奇怪。

         1852971 function calls (1852778 primitive calls) in 9.127 seconds

   Ordered by: internal time
   List reduced from 561 to 20 due to restriction <20>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    6.404    6.404    9.128    9.128 a1.1.py:10(<module>)
      513    0.778    0.002    0.778    0.002 {numpy.core.multiarray.concatenate
}
   262144    0.616    0.000    1.243    0.000 D:\Python27\lib\site-packages\nump
y\core\shape_base.py:6(atleast_1d)
   262696    0.260    0.000    0.261    0.000 {numpy.core.multiarray.array}
   262656    0.228    0.000    0.487    0.000 D:\Python27\lib\site-packages\nump
y\core\numeric.py:237(asanyarray)
      515    0.174    0.000    1.419    0.003 {map}
   527019    0.145    0.000    0.145    0.000 {method 'append' of 'list' objects
}

a1.1.py的第10行是第一行from numpy import *(之前的所有注释),这让我很困惑。

1 个回答

8

如果你使用克罗内克乘积把每个像素变成一个4x4的小矩阵,这样就可以不用Python里的循环了:

im2 = np.kron(im, np.ones((4,4)))
dm2 = np.tile(dithering_matrix,(512,512))
out2 = ((im2 / (256 / split_num)) > dm2) * 255

在我的电脑上,这样做大约比你的版本快20倍。

撰写回答