I’m currently working on a simple 3D panorama viewer for a website. For mobile performance reasons I’m using the Three.js CSS 3 renderer. This requires a cube map, split up into six single images.
I’m recording the images on the iPhone with Google Photo Sphere, or similar apps that create 2:1 equirectangular panoramas. I then resize and convert these to a cubemap with this website: http://gonchar.me/panorama/ (Flash)
Preferably, I’d like to do the conversion myself, either on the fly in Three.js, if that’s possible, or in Photoshop. I found Andrew Hazelden’s Photoshop actions, and they seem kind of close, but no direct conversion is available. Is there a mathematical way to convert these, or some sort of script that does it? I’d like to avoid going through a 3D application like Blender, if possible.
Maybe this is a long shot, but I thought I’d ask. I have okay experience with JavaScript, but I’m pretty new to Three.js. I’m also hesitant to rely on the WebGL functionality, since it seems either slow or buggy on mobile devices. Support is also still spotty.
12
Answers
If you want to do it server side there are many options. ImageMagick has a bunch of command-line tools which could slice your image into pieces. You could put the command to do this into a script and just run that each time you have a new image.
It’s hard to tell quite what algorithm is used in the program. We can try and reverse engineer quite what is happening by feeding a square grid into the program. I’ve used a grid from Wikipedia:
Which gives:
This gives us a clue as to how the box is constructed.
Imaging a sphere with lines of latitude and longitude on it, and a cube surrounding it. Now projecting from the point at center of the sphere produces a distorted grid on the cube.
Mathematically, take polar coordinates r, θ, ø, for the sphere r=1, 0 < θ < π, -π/4 < ø < 7π/4
centrally project these to the cube. First we divide into four regions by the latitude -π/4 < ø < π/4, π/4 < ø < 3π/4, 3π/4 < ø < 5π/4, 5π/4 < ø < 7π/4. These will either project to one of the four sides the top or the bottom.
Assume we are in the first side -π/4 < ø < π/4. The central projection of
(sin θ cos ø, sin θ sin ø, cos θ) will be (a sin θ cos ø, a sin θ sin ø, a cos θ) which hits the x=1 plane when
so
and the projected point is
If | cot θ / cos ø | < 1, this will be on the front face. Otherwise, it will be projected on the top or bottom and you will need a different projection for that. A better test for the top uses the fact that the minimum value of cos ø will be cos π/4 = 1/√2, so the projected point is always on the top if cot θ / (1/√2) > 1 or tan θ < 1/√2. This works out as θ < 35º or 0.615 radians.
Put this together in Python:
The
projection
function takes thetheta
andphi
values and returns coordinates in a cube from -1 to 1 in each direction. The cubeToImg takes the (x,y,z) coordinates and translates them to the output image coordinates.The above algorithm seems to get the geometry right using an image of buckingham palace. We get:
This seems to get most of the lines in the paving right.
We are getting a few image artefacts. This is due to not having a one-to-one map of pixels. We need to do is use an inverse transformation. Rather than a loop through each pixel in the source and finding the corresponding pixel in the target, we loop through the target images and find the closest corresponding source pixel.
The results of this are:
If anyone want to go in the reverse, see this JS Fiddle page.
There are various representations of environment maps. Here is a nice overview.
Overview – Panoramic Images
If you use Photosphere (or any panorama app for that matter), you most likely already have the horizontal latitude / longitude representation.
You can then simply draw a textured three.js SphereGeometry. Here is a tutorial on how to render earth.
Tutorial – How to Make the Earth in WebGL?
Best of luck :).
I wrote a script to cut the generated cubemap into individual files (posx.png, negx.png, posy.png, negy.png, posz.png and negz.png). It will also pack the 6 files into a .zip file.
The source is here: https://github.com/dankex/compv/blob/master/3d-graphics/skybox/cubemap-cut.py
You can modify the array to set the image files:
The converted files are:
First: unless you really have to convert the images yourself (i.e., because of some specific software requirement), don’t.
The reason is that, even though there is a very simple mapping between equirectangular projection and cubic projection, the mapping between the areas is not simple: when you establish a correspondence between a specific point of your destination image and a point in the source with an elementary computation, as soon as you convert both points to pixels by rounding you are doing a very raw approximation that doesn’t consider the size of the pixels, and the quality of the image is bound to be low.
Second: even if you need to do the conversion at runtime, are you sure that you need to do the conversion at all? Unless there is some very stringent performance problem, if you just need a skybox, create a very big sphere, stitch the equirectangular texure on it, and off you go. Three.js provides the sphere already, as far as I remember 😉
Third: NASA provides a tool to convert between all conceivable projections (I just found out, tested it, and works like a charm). You can find it here:
G.Projector — Global Map Projector
And I find it reasonable to think that the guys know what they are doing 😉
It turns out that the "guys" know what they do up to some point: the generated cubemap has an hideous border which makes the conversion not that easy…
I found the definitive tool for equirectangular to cubemap conversion, and it’s called
erect2cubic
.It’s a small utility that generates a script to be fed to hugin, in this way:
(information siphoned from Vinay’s Hacks page)
And it will generate all six cubemap faces. I’m using it for my project and it works like a charm!
The only downside of this approach is that the script
erect2cubit
is not in the standard Ubuntu distribution (which is what I’m using) and I had to resort to the instructions at a blog describing how to install and use erect2cubic to find out how to install it.It is totally worth it!
Given the excellent accepted answer, I wanted to add my corresponding C++ implementation, based on OpenCV.
For those not familiar with OpenCV, think of
Mat
as an image. We first construct two maps that remap from the equirectangular image to our corresponding cubemap face. Then, we do the heavy lifting (i.e., remapping with interpolation) using OpenCV.The code can be made more compact, if readability is not of concern.
Given the following input:
The following faces are generated:
Image courtesy of Optonaut.
cmft Studio supports
conversion/filtering
of variousHDR/LDR
projections tocubemaps
.https://github.com/dariomanesku/cmftStudio
UPDATE 2: It looks like someone else had already built a far superior web application than my own. Their conversion runs client-side, so there aren’t any uploads and downloads to worry about.
I suppose if you hate JavaScript for some reason, or are trying to do this on your mobile, then my web application below is okay.
UPDATE: I’ve published a simple web application where you can upload a panorama and have it return the six skybox images in a ZIP file.
The source is a cleaned up reimplementation of what’s below, and is available on GitHub.
The application is presently running on a single free-tier Heroku dyno, but please don’t attempt to use it as an API. If you want automation, make your own deployment; single click Deploy to Heroku available.
Original: Here’s a (naively) modified version of Salix Alba’s absolutely fantastic answer that converts one face at a time, spits out six different images and preserves the original image’s file type.
Aside from the fact most use cases probably expect six separate images, the main advantage of converting one face at a time is that it makes working with huge images a lot less memory intensive.
A very simple C++ application which converts an equirectangular panorama to cube map based on the answer by Salix Alba:
Photo Panorama Converter
Here’s a JavaScript version of Benjamin Dobell’s code. The
convertFace
needs to be passed twoìmageData
objects and a face ID (0-6).The provided code can safely be used in a web worker, since it has no dependencies.
I created a solution for this problem using OpenGL and made a command-line tool around it. It works both on images and videos, and it is the fastest tool that I found out there.
Convert360 – Project on GitHub.
OpenGL Shader – The fragment shader used for the re-projection.
The usage is as simple as:
To get something like this:
Perhaps I am missing something here. But it seems that most if not all the presented transformation code may be somewhat incorrect. They take a spherical panorama (equirectangular — 360 deg horizontally and 180 deg vertically) and seem to convert to the cube faces using a Cartesian <-> cylindrical transformation. Should they not be using a Cartesian <-> spherical transformation?
See Spherical Coordinates.
I suppose that as long as they reverse the calculation to go from the cube faces to the panorama, then it should work out. But the images of the cube faces may be slightly different when using the spherical transformation.
If I start with this equirectangular (spherical panorama):
Then if I use a cylindrical transformation (which I am not 100% sure is correct at this time), I get this result:
But if I use a spherical transformation, I get this result:
They are not the same. But my spherical transformation result seems to match the result of Danke Xie, but his link does not show the kind of transformation he is using, as best I can read it.
So am I misunderstanding the code being used by many of the contributors to this topic?
kubi can convert from an equirectangular image to cube faces. I have written it to be fast and flexible. It provides options to choose the output layout (six separate images is the default) and decide on the resampling method.