# Re-projection: rectilinear zenith and nadir to/from equirectangular

(Morgan Hardwood) #1

Hello @David_Tschumperle

Does G’MIC ship with any tools for reprojection? Specifically to re-project the zenith and nadir from a 360x180 equirectangular image into rectilinear projections, and then the reverse of that to re-project the two rectilinear images back into the equirectangular image. If not, could I tickle your or someone’s interest who knows G’MIC and is capable of coding this?

MathMap was a very interesting project, a plugin for GIMP which let you code such things in a simple way, but sadly it’s been dead for a long time and I see G’MIC as having taken its place. If you are interested in implementing this, or if you can point someone you know to this request, someone who might be capable and interested in implementing it, then maybe this code I used in MathMap can help.

This part takes an equirectangular image at a 2:1 aspect ratio and spits out two rectilinear adjacent 1:1 images on a 2:1 layer.
Reproject rectilinear zenith and nadir from equirectangular:

``````filter ToNadirZenith (image in)
# Filter created by Seb Przd

if x<0 then
sinphi1=1;
xx=x+X/2;
else
sinphi1=-1;
xx=x-X/2;
end;

yy=y;

rr=sqrt(xx^2+yy^2);

c=atan(rr/Y);

phi =
if rr == 0 then
0
else
asin(cos(c)*sinphi1)
end;

xxx=atan(xx,-yy*sinphi1)*X/pi;
yyy=phi*Y/(pi/2);

in(xy:[xxx,yyy])
end
``````

This part takes the 2:1 layer and reprojects them onto the 2:1 canvas, so you end up with the zenith and nadir at the top and bottom and transparency in the middle.
Reproject rectilinear zenith and nadir into equirectangular:

``````filter FromNadirZenith (image in)
# Filter created by Seb Przd

output=1;
if y>Y/4 then
sinphi1=1;
xc=-X/2;
else if y<-Y/4 then
sinphi1=-1;
xc=X/2;
else
output=0;
end;
end;

cosc=sinphi1*sin(y/Y*pi/2);
xx=cos(y/Y*pi/2)*sin(x/X*pi)/cosc;
yy=-sinphi1*cos(y/Y*pi/2)*cos(x/X*pi)/cosc;

if abs(xx)>1 then output=0; end;

if output then
in(xy:[xx*X/2+xc,yy*Y])
else
rgbaColor(0,0,0,0)
end

end
``````

Create equirectangle Panorama from gear 360 images
Panography - Patching the zenith and nadir
(David Tschumperlé) #2

Hello @Morgan_Hardwood,

Yes this seems to be something possible to do with G’MIC. I’ll have a look at it, after the holidays (I’m not at home right now, so I cannot try coding some G’MIC scripts ).
I’ll let you informed.

Merry XMas and Happy new year 2016.

(Morgan Hardwood) #3

Awesome! Thank you @David_Tschumperle

(David Tschumperlé) #4

I’m a bit lost with the used coordinate system.
From the doc, I understand that x and y goes from -1 to 1, even for a non-square image, right ?
So what are X and Y supposed to be ? Any idea ?

(David Tschumperlé) #5

Also it could be nice to get one of the input image as well as its transform, so I can check my routines does exactly the same than the Mathmap ones.

(Morgan Hardwood) #6

@David_Tschumperle sorry, I can’t help you with the script yet, but I will dig a little maybe I will find something!

Here is the original image + the zenith and nadir in a rectilinear projection:
http://filebin.net/g0eux7n4fz
The images are JPEGs but usually I’d do this while they are still in 16- or 32-bit precision.

It is not important that your version looks identical to this one, because the zenith and nadir can use any field of view, not necessarily the one used by the mathmap script. An angle of about 90°-120° would be good. If the user can adjust the angle, even better. What’s important is that this can then be re-projected back into the equirectangular form.

(Morgan Hardwood) #7

I tested and if I replaced X by constant 1 and Y with 0.5 the results were identical.

It looks like this is what they are:
http://www.complang.tuwien.ac.at/schani/mathmap/reference.html

X (nil:1)
The biggest possible value for x for the image.
Y (nil:1)
The biggest possible value for y for the image.

I replaced X with the key above it on the QWERTY keyboard, s, and Y with the key below it, h.

Change of X:
0.9

1.0

1.1

Change of Y:
0.4

0.5

0.6

Such a pity MathMap is dead!

Some additional info I found. It’s generally greek to me but maybe it helps?

1. Remapping in panotools, specifically the rect_erect and sphere_tp_erect functions: https://sourceforge.net/p/panotools/discussion/333550/thread/67115168/
rect_erect()
math.c#l733
SpaceTransform.cpp#l298
sphere_tp_erect()
math.c#l1807
SpaceTransform.cpp#l366

2. Third response in this thread, the link should open it directly:

(David Tschumperlé) #8

OK, so I’ve converted the two Mathmap scripts you give in the original post into equivalent G’MIC commands -equirectangular2nadirzenith and -nadirzenith2equirectangular, and I’ve added a new filter in the G’MIC plug-in for GIMP as well (located in Deformations / Equirectangular to nadir/zenith).
All these should be available after a filter update.
This is how it looks in the plug-in :

To be perfectly honest, I didn’t take the time to understand the maths behind the formulas, I’ve only re-implemented them in G’MIC. We probably can do a better job with those filters (but this takes time I don’t have at the moment).
If someone is interested, the code of the commands are now included in the gmic_stdlib.gmic file from the G’MIC sources.

(Morgan Hardwood) #9

It works perfectly @David_Tschumperle, thank you so much!