how to manipulate an image very fast in accordance to an own math function in python

試著忘記壹切 提交于 2019-12-25 04:25:02

问题


I'm trying to create a 360 degree camera just like google street cameras (This is my whole code if you are interested)

I have a individual kind of perspective equation that map pixel [xold,yold] to [xnew,ynew] in accordance to Alpha and Beta angles as inputs.

To simplify that equation and my question, i assume i'm just trying to rotate an image. Now my question is how to rotate an image by using rotation equation on each pixel very fast on pygame or anyother intractive shell:

  1. xnew = xold * cos(alpha) - yold * sin(alpha)
  2. ynew = xold * sin(alpha) + yold * cos(alpha)

Assume pygame.transform.rotate() is not available


回答1:


Read the following words from pygame.org: http://www.pygame.org/docs/ref/surface.html

"There is support for pixel access for the Surfaces. Pixel access on hardware surfaces is slow and not recommended. Pixels can be accessed using the get_at() and set_at() functions. These methods are fine for simple access, but will be considerably slow when doing of pixel work with them. If you plan on doing a lot of pixel level work, it is recommended to use a pygame.PixelArray object for direct pixel access of surfaces, which gives an array like view of the surface. For involved mathematical manipulations try the pygame.surfarray module for accessing surface pixel data using array interfaces module (It’s quite quick, but requires NumPy.)"

pygame.Surface.set_at((x,y),Color) is definitely the easiest way to do it, but for performance (which is what you asked), you must use pygame.PixelArray or pygame.surfarray.

I can't do the coding for you because I'm short on time, but these websites will point you in the right direction:

http://www.pygame.org/docs/ref/pixelarray.html#pygame.PixelArray http://www.pygame.org/docs/ref/surfarray.html#module-pygame.surfarray

Good luck with your coding!




回答2:


Given that you are trying to simulate a 3D environment, it would be extremely hard to beat a solution with PyOpenGL performance-wise. From what I saw when I ran your code, it looks like you are implementing a "skybox", where the viewer would be in a virtual cube. OpenGL is meant for 3D computations like this, so you do not need to manually shift pixels on at a time but instead let the GPU do you that for you while you just pass in a series of vertices and textures! If you need really complicated equations that manipulate every single pixel on the screen, you would then be able to use GLSL shaders to do that work on the GPU in parallel. Let me know if you want me to elaborate on this if you are interested in this approach, as it is would be very different from your current code.



来源:https://stackoverflow.com/questions/40484076/how-to-manipulate-an-image-very-fast-in-accordance-to-an-own-math-function-in-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!