pygame的快速像素读取 [英] Pygame quick pixel reading

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

问题描述

好了,希望有人能帮助我,因为我在这里的损失。

Alright, hopefully someone can help me with this because I'm at a loss here.

我工作的一个游戏在Python使用相机作为主控制器。为了玩游戏,如果你移动一个简单的光,并且控制了比赛。

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.

我第一次尝试使用surface.get_at(X,Y​​)的功能(这是不实际的code,这是一个例子):

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)

这是窘况慢,所以我开始做我的研究。快速检查到 pygame的文档表明,越来越像素1的时间使用这种方法将是缓慢的。于是我开始用surfarray尝试:

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)

然而,这也太慢。快速双重检查到surfarray文档显示,pygame.surfarray.pixels3d(面)会更快(因为没有实际的值被复制),但不幸的是我的摄像机图像不是24或32位格式。 所以,这里是我的问题:

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:

在pygame的,有没有什么快速的方式来阅读所有的像素?

In pygame, is there any quick way to read ALL pixels?

我必须能够读取每一个像素,做一个LUM的计算就可以了,(maxcolor + mincolor)/ 2,然后看它是否LUM是最大LUM。感谢您的时间。

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.

推荐答案

看来, surfarray 目标是通过numpy的阵列支持,所以你得到它要更快一些的机会靠着自己的本地方法。

It seems that the surfarray object is backed by numpy arrays, so your chances of getting it faster are relying on their native methods.

看看这个: numpy.argmax

您可能需要首先改变你的3D阵列到二维发光矩阵(但不与嵌套的循环做吧!XD)。也许你会需要一些整形拿到飞机,然后用简单的矩阵运算平均他们......或者,而不是RGB工作,你可以切换到更容易与像HSV或者只是灰度工作等图像格式。

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的快速像素读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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