在Python中循环遍历图像的每个像素的更快方法? [英] Faster way to loop through every pixel of an image in Python?

查看:2493
本文介绍了在Python中循环遍历图像的每个像素的更快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要遍历2560x2160 2D numpy数组(图像)的每个像素。我的问题的简化版本如下:

I need to loop through each pixel of a 2560x2160 2D numpy array (image). A simplified version of my problem is as follows:

import time
import numpy as np

t = time.clock()
limit = 9000
for (x,y), pixel in np.ndenumerate(image):
    if( pixel > limit )
        pass
tt = time.clock()
print tt-t

这个在我的电脑上完成了大约30秒的讨厌。 (酷睿i7,8GB内存)
是否有更快的方式使用内部'if'语句执行此循环?我只对超过一定限度的像素感兴趣,但我确实需要它们的(x,y)索引和值。

This is taking an obnoxious ~30 seconds to complete on my computer. ( Core i7, 8GB ram ) Is there a faster way to perform this loop with an interior 'if' statement? I am only interested in pixels above a certain limit, but I do need their (x,y) indices and value.

推荐答案

使用布尔矩阵:

x, y = (image > limit).nonzero()
vals = image[x, y]

这篇关于在Python中循环遍历图像的每个像素的更快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆