Pygame 快速像素读取
好吧,希望有人能帮助我,因为我在这里不知所措。
我正在用 python 开发一款游戏,它使用相机作为主控制器。要玩游戏,您只需移动一个简单的灯光即可控制游戏。
到目前为止,我已经成功地将相机图像获取到表面,并将其显示在屏幕上。但当涉及到寻找光时,程序就慢得像爬行一样。
我的第一次尝试使用了 surface.get_at(x,y) 函数(这不是实际的代码,这是一个示例):
maxL = 0
point = (0,0)
mysurface = get_cameraImg()
for i in range(mysurface.get_width()):
for j in range(mysurface.get_height()):
color = mysurface.get_at(i,j)
lum = get_lum(color.r,color.g,color.b)
if(lum>maxL):
maxL = lum
point = (i,j)
这非常慢,所以我开始做我的研究。快速检查 pygame 文档 显示一次获取像素 1使用这种方法会很慢。于是我开始尝试使用 surfarray:
maxL = 0
point = (0,0)
mysurface = get_cameraImg()
ar = pygame.surfarray.array3d(mysurface)
for i in range(len(ar)):
for j in range(len(ar[i]):
color = ar[i][j]
lum = get_lum(color[0],color[1],color[2])
if(lum>maxL):
maxL = lum
point = (i,j)
但是,这也太慢了。对 surfarray 文档的快速双重检查表明 pygame.surfarray.pixels3d(surface) 会更快(因为没有复制实际值),但不幸的是我的相机图像不是 24 或 32 位格式。 所以,这是我的问题:
在 pygame 中,有没有快速读取所有像素的方法?
我必须能够读取每个像素,对其进行亮度计算,(maxcolor+mincolor)/2,然后查看它的亮度是否是最大亮度。谢谢您的宝贵时间。
Alright, hopefully someone can help me with this because I'm at a loss here.
I'm working on a game in python that uses the camera as the main controller. To play the game, you move a simple light around, and that controls the game.
So far, I've managed to get the camera image to a surface, and display it on the screen. But when it comes to finding the light, the program is slowed to a crawl.
My first attempt used the surface.get_at(x,y) function (this is not the actual code, this is an example):
maxL = 0
point = (0,0)
mysurface = get_cameraImg()
for i in range(mysurface.get_width()):
for j in range(mysurface.get_height()):
color = mysurface.get_at(i,j)
lum = get_lum(color.r,color.g,color.b)
if(lum>maxL):
maxL = lum
point = (i,j)
This was horrendously slow, so I started doing my research. A quick check to the pygame docs showed that getting pixels 1 at a time using this method would be slow. So then I started trying using a surfarray:
maxL = 0
point = (0,0)
mysurface = get_cameraImg()
ar = pygame.surfarray.array3d(mysurface)
for i in range(len(ar)):
for j in range(len(ar[i]):
color = ar[i][j]
lum = get_lum(color[0],color[1],color[2])
if(lum>maxL):
maxL = lum
point = (i,j)
However, this was also too slow. A quick double check to the surfarray docs showed that pygame.surfarray.pixels3d(surface) would be faster (as no actual value is copied), but unfortunately my camera image is not 24 or 32 bit format.
So, here's my question:
In pygame, is there any quick way to read ALL pixels?
I have to be able to read every pixel, do a lum calculation on it, (maxcolor+mincolor)/2, then see if it's lum is the max lum. Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
surfarray 对象似乎是由 numpy 数组支持的,因此您更快地获得它的机会取决于它们的本机方法。
看看这个: numpy.argmax
您可能需要将 3d 数组转换为首先是二维光度矩阵(但不要使用嵌套循环! XD)。也许您需要进行一些重塑才能获得平面,然后通过简单的矩阵运算对它们进行平均...或者您可以切换到更容易使用的其他图像格式(例如 HSV 或灰度),而不是使用 RGB。
It seems that the
surfarray
object is backed by numpy arrays, so your chances of getting it faster are relying on their native methods.Take a look at this: numpy.argmax
You might need to transform your 3d array to a 2d luminosity matrix first (but don't do it with nested loops! XD). Maybe you'll need some reshaping to get the planes, and then average them with simple matrix operations... Or instead of working in RGB, you could switch to other image format that is easier to work with like HSV or just grayscale.
不知道是否有特定于 Pygame 的方法,但是有一个简单的方法可以在许多 python 程序中加快很多速度:使用 Psycho,请参见此处
http://psyco.sourceforge.net/introduction.html
我想到的另一件事是:为什么你必须通过阅读像素来“找到光明”按像素?灯光的位置不是您已经预先知道的吗,因此您只需检查相机相对于灯光位置的位置即可?
Don't know if there's a way specific for Pygame, but there is a simple way to speed a lot of things up in many python programs: use Psycho, see here
http://psyco.sourceforge.net/introduction.html
Another thing that comes into my mind is: why do you have to "find the light" by reading pixel-by-pixel? Isn't the position of the light something you already know beforehand, so you could just check the camera position relative to the light position?