小编pik*_*ika的帖子

使用 numpy 快速迭代像素

我有两个分辨率为 4095x4095 的图像,每个像素都有不同的颜色(一个图像中没有重复的像素)。我正在尝试构建一个“地图”,描述两个图像之间每个像素的移动。

我现在拥有的是一种有效但非常幼稚的算法,它只是循环遍历所有像素,直到找到匹配项,然后移动到下一个像素。这种方法需要数年时间才能遍历图像中的所有像素。我想知道是否可以使用 numpy 来加快速度。到目前为止,我无法让它发挥作用。

工作,但缓慢的算法:

import PIL
import time
from PIL import Image

raw = Image.open('image1.png')
rawLoad = raw.load()
rendered = Image.open('image2.png')
renderedLoad = rendered.load()
counter = 1
timer = time.time()

for rendered_x in range(rendered.width):
        for rendered_y in range(rendered.height):
            for raw_x in range(raw.width):
                    for raw_y in range(raw.height):
                        if rawLoad[raw_x, raw_y] == renderedLoad[rendered_x, rendered_y]:
                            print('Found pixel no. '+str(counter)+' pos '+str(rendered_x)+' '+str(rendered_y)+' in position '+str(raw_x)+' '+str(raw_y)+'. Took '+str(round(time.time() - timer, 2))+' s.')
                            break
                    else:
                        continue
                    break
            counter += 1
            timer …
Run Code Online (Sandbox Code Playgroud)

python numpy python-imaging-library

5
推荐指数
1
解决办法
244
查看次数

标签 统计

numpy ×1

python ×1

python-imaging-library ×1