0

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

Mohsen Haddadi
  • 1,296
  • 2
  • 16
  • 20
  • alpha, alpha0, beta and beta0 are the same for every pixel? If that is the case, you should calculate the trigonometric operations prior to the loop, and substitute the operations inside the loop by these results to reduce computational costs – Jalo Nov 08 '16 at 10:16
  • yes the angles are the same for every pixel. – Mohsen Haddadi Nov 08 '16 at 11:32
  • but the problem is although these trigonometric operations are complicated and long for every pixel, they don't take that much time. the major time is taken on the loops, for instance for(800,600) operation in a loop even if i type continue it takes 0.3 seconds to be done – Mohsen Haddadi Nov 08 '16 at 11:41
  • I think that I do not really get your main problem. In your post, you say that you are conscious that there are other ways that work faster (They do indeed), but that you still want to use a loop based method. Maybe you can clear a bit this confusion. – Jalo Nov 08 '16 at 12:05
  • I think you are right and my question was unnecessarily long. so I edited my question to a shorter one. My problem is although I know how that fast ways work for simple operation like resizing and shifting vertically and horizontally an image, I don't know how to implement more complicated equation like rotation equation on an image with those fast methods . So if someone can show me a sample script written for this rotation equation I can use that sample script for my equation. Thank you very much! – Mohsen Haddadi Nov 08 '16 at 13:18
  • Converting to a NumPy data strucure, performing the operations using NumPy vectorized acess and blitting the numpy array back to a pygame surface can be the ony way to get close to real time for this kind of stuff in Pygame – jsbueno Nov 11 '16 at 13:44

2 Answers2

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!

Douglas
  • 1,304
  • 10
  • 26
0

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.

CodeSurgeon
  • 2,435
  • 2
  • 15
  • 36
  • Just for documetning purposes, OpenGL is accessible from inside Pygame as well - but one would be better using "pyglet" which, besides much of the display/keyboard/IO contro pygame offers, has a more direct interface to opengl. – jsbueno Nov 11 '16 at 13:43
  • @MohsenHaddadi As an example of how this can be done using PyOpenGL, you can check out the github link in [this](http://stackoverflow.com/q/40561595/2588654) question I asked while figuring out how to render a skybox. – CodeSurgeon Nov 13 '16 at 02:15