最快的方式来遍历每个像素 [英] Fastest Way To Loop Through Every Pixel

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

问题描述

当我第一次尝试遍历图像的每个像素时,我在范围内使用了


(0,thisHeight):
$ x $ b for thisX在范围内(0,thisWidth):

#Pixel thisX的行为,这个是


但它需要450-1000毫秒

我希望速度低于10毫秒


我尝试过使用SWIG和pypy但是他们都没有成功地编译我的文件。
编译我的文件。

As my first attempt to loop through every pixel of an image, I used

for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY

But it takes 450-1000 milliseconds

I want speeds less than 10 milliseconds

I have tried using SWIG, and pypy but they all are unsuccessfull in
compiling my files.

推荐答案



Chaos写道:

Chaos wrote:

作为我第一次尝试遍历图像的每个像素,我在范围内使用了


(0,thisHeight):

for thisX in range(0,thisWidth):

#Pixel thisX的行为,这是
As my first attempt to loop through every pixel of an image, I used

for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY



OT:你不需要0 in范围呼叫。拿出来并不会让它变得更快。

OT: you don''t need the 0 in the range call. Taking it out doesn''t make
it run faster, though.


>

但是它需要450-1000毫秒


我希望速度低于10毫秒


我尝试过使用SWIG和pypy,但它们都没有成功

编译我的文件。
>
But it takes 450-1000 milliseconds

I want speeds less than 10 milliseconds

I have tried using SWIG, and pypy but they all are unsuccessfull in
compiling my files.



不成功因为......什么?

[我不知道swig是用来编译Python脚本]


坚持使用Python:

使用for thisX尝试

(1)使用xrange而不是范围。

(2)widthRange = range(thisWidth)

for thisY in range(thisHeight) :
$ x $ b for widthX:

一般来说,在循环外提升循环不变量。


你考虑过Pyrex ?


这完全取决于Pixel thisX,thisY中的#Actions正在做。

也许有一个图书馆(PIL,pygame,...)可以做你想做的事情。
试图这样做。

也许如果你告诉我们你在做什么,我们可以给你更好的

建议。


HTH,

John

Unsuccessful because .... what?
[I wasn''t aware that swig was intended to compile Python scripts]

Sticking with Python:
With the "for thisX" try
(1) using xrange instead of range.
(2) widthRange = range(thisWidth)
for thisY in range(thisHeight):
for thisX in widthrange:
and in general, hoist loop-invariants outside the loop.

Have you considered Pyrex?

It all depends on what "#Actions here for Pixel thisX, thisY" is doing.
Perhaps there is a library (PIL, pygame, ...) that does what you are
trying to do.
Perhaps if you show us what you are doing, we can give you better
advice.

HTH,
John




John Machin写道:

John Machin wrote:

Chaos写道:
Chaos wrote:

当我第一次尝试遍历图像的每个像素时,我在范围内使用了


(0,thisHeight) :
$ x $ b for thisX in range(0,thisWidth):

#Pctions this for Pixel thisX,thisY
As my first attempt to loop through every pixel of an image, I used

for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY



OT:你不需要在范围通话中使用0。拿出来并不会让它变得更快。


OT: you don''t need the 0 in the range call. Taking it out doesn''t make
it run faster, though.



但需要450-1000毫秒


我希望速度低于10毫秒


我尝试过使用SWIG和pypy但是它们都没有成功使用

编译我的文件。

But it takes 450-1000 milliseconds

I want speeds less than 10 milliseconds

I have tried using SWIG, and pypy but they all are unsuccessfull in
compiling my files.



不成功,因为......什么?

[我不知道swig是为了编译Python脚本]


坚持使用Python:

使用for thisX尝试

(1)使用xrange而不是范围。

(2)widthRange = range(thisWidth)

for thisY in range(thisHeight) :
$ x $ b for widthX:

一般来说,在循环外提升循环不变量。


你考虑过Pyrex ?


这完全取决于Pixel thisX,thisY中的#Actions正在做。

也许有一个图书馆(PIL,pygame,...)可以做你想做的事情。
试图这样做。

也许如果你告诉我们你在做什么,我们可以给你更好的

建议。


HTH,

John


Unsuccessful because .... what?
[I wasn''t aware that swig was intended to compile Python scripts]

Sticking with Python:
With the "for thisX" try
(1) using xrange instead of range.
(2) widthRange = range(thisWidth)
for thisY in range(thisHeight):
for thisX in widthrange:
and in general, hoist loop-invariants outside the loop.

Have you considered Pyrex?

It all depends on what "#Actions here for Pixel thisX, thisY" is doing.
Perhaps there is a library (PIL, pygame, ...) that does what you are
trying to do.
Perhaps if you show us what you are doing, we can give you better
advice.

HTH,
John



Nope仍然是相同的速度。我也试过pyrex,但我不明白如何构建pyx文件

。我没有看到如何使用C设置文件。我

不确定你是否应该使用这个例子或建立你自己的。


与pypy我尝试打开py文件时出现了一个奇怪的错误。它说evey文件的第一个字符是未知的。


我可能会再次尝试SWIG,因为我不记得为什么我不再使用它了。 />

Nope still same speed. I also tried pyrex but I couldnt understand how
to build pyx files. I didnt see how to set up the files using C. I
wasnt sure if you were supposed use the example or build your own.

With pypy I got a strange error trying to open py files. It said the
first character of evey py file was unknown.

I may try SWIG again becuase I fail to rememeber why I stopped using it.


Hello Chaos,


无论你在#Actions here ...中做什么可能很好地表达为数字的
ufunction。然后,您可以将表达式

转换为数值表达式。查看numpy / scipy。


一般来说,如果thisHeight,thisWidth很大则使用xrange而不是范围。

range()先生成一个数字列表然后通过它们进行迭代。

首先尝试一下。


然后当然如果你做了很多次整个事情你可以只需

预生成索引,如下所示:

all_indices = []
$ x $ b for x in xrange(thisHeight):

for j in xrange( thisWidth):

all_indices.append((i,j))


然后每次你需要在这里运行''#Actions ......''你可以在all_indices中使用

代表(i,j):

#Actions here ... blah blah


In一般来说,如果有一种方法可以显着优化通用的循环,它们可能已经过优化......


Nick V.
Chaos写道:
Hello Chaos,

Whatever you do in "#Actions here ..." might be expressed nicely as a
ufunction for numeric. Then you might be able to convert the expression
to a numeric expression. Check out numpy/scipy.

In general, if thisHeight, thisWidth are large use xrange not range.
range() generates a list of numbers first then iterates through them.
So try that first.

Then of course if you do the whole thing many times you could just
pre-generate the indices as in:
all_indices=[]
for i in xrange(thisHeight):
for j in xrange(thisWidth):
all_indices.append( (i,j) )

Then each time you need to run ''#Actions here...'' you can just use
for (i,j) in all_indices:
#Actions here ... blah blah

In general, if there would be a way to significantly optimize generic
for loops, they would probably be already optimized...

Nick V.
Chaos wrote:

作为我第一次尝试遍历图像的每个像素,我使用了


for thisY in range(0,thisHeight):
$ X $ b for thisX in range(0,thisWidth):

#Actions here for Pixel thisX,thisY


但需要450-1000毫秒


我希望速度低于10毫秒


我尝试过使用SWIG和pypy但他们都没有成功地编译我的文件。

As my first attempt to loop through every pixel of an image, I used

for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY

But it takes 450-1000 milliseconds

I want speeds less than 10 milliseconds

I have tried using SWIG, and pypy but they all are unsuccessfull in
compiling my files.


这篇关于最快的方式来遍历每个像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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