# Mapping Images on Spherical Surfaces Using C#

, 9 Apr 2013 GPL3
 Rate this:
Mapping images on spherical surfaces using C#

## Introduction

This article describes how to map a flat 2D image (JPG, BMP, or GIF) on a sphere by using basic algebra.

The process is very simple where the x axis of the image will be mapped on sphere longitudes and the y axis of the image will be mapped on sphere latitudes.

The process of mapping is similar to proportion equations x-x0/y-y0 = px-x0/py-y0

```public static double MapCoordinate(double i1, double i2, double w1,
double w2, double p)
{
return ((p - i1) / (i2 - i1)) * (w2 - w1) + w1;
}```

Original image

Resulting image

## Background

### A Sphere Can Be Represented by Spherical Coordinates in R3

• phi (latitude angle)
• theta (longitude angle)

#### Image 2

• Where radius is a constant, phi=[-PI/2,PI/2], and theta=[0,2*PI]

### To Find the Cartesian Coordinates from Spherical Coordinates

• x = radius * sin(phi) * cos(theta)
• y = radius * sin(phi) * sin(theta)
• z = radius * cos(theta)
```double phi0 = 0.0;
double phi1 = Math.PI;
double theta0 = 0.0;
double theta1 = 2.0*Math.PI;```

## The Code

At first we code the image loading

```System.Drawing.Image image1 = new Bitmap(Server.MapPath(
"./images/worldmap4.gif"));
Bitmap imgBitmap = new Bitmap(image1);```

Now we make a loop through the 2 dimensions of the image, map phi and theta angles from image coordinates, get the cartesian 3D coordinates from phi and theta, provide some rotation to the obtained 3D points and plot them with respective image color:

```for (int i = 0; i < imgBitmap.Width; i++)
{
for (int j = 0; j < imgBitmap.Height; j++)
{
// map the angles from image coordinates
double theta = Algebra.MapCoordinate(0.0, imgBitmap.Width - 1,
theta1, theta0, i);
double phi = Algebra.MapCoordinate( 0.0, imgBitmap.Height - 1,phi0,
phi1, j);
// find the cartesian coordinates
double x = radius * Math.Sin(phi) * Math.Cos(theta);
double y = radius * Math.Sin(phi) * Math.Sin(theta);
double z = radius * Math.Cos(phi);
// apply rotation around X and Y axis to reposition the sphere
RotX(1.5, ref y, ref z);
RotY(-2.5, ref x, ref z);
// plot only positive points
if (z > 0)
{
Color color = imgBitmap.GetPixel(i, j);
Brush brs = new SolidBrush(color);
int ix = (int)x + 100;
int iy = (int)y + 100;
graphics.FillRectangle(brs, ix, iy, 1, 1);
brs.Dispose();
}
}
}```

## The Rotation Functions [almost forgot]

Actually I made a 3D Math class, but here you will need only these functions

```public static void RotX(double angle, ref double y, ref double z)
{
double y1 = y * System.Math.Cos(angle) - z * System.Math.Sin(angle);
double z1 = y * System.Math.Sin(angle) + z * System.Math.Cos(angle);
y = y1;
z = z1;
}
public static void RotY(double angle, ref double x, ref double z)
{
double x1 = x * System.Math.Cos(angle) - z * System.Math.Sin(angle);
double z1 = x * System.Math.Sin(angle) + z * System.Math.Cos(angle);
x = x1;
z = z1;
}
public static void RotZ(double angle, ref double x, ref double y)
{
double x1 = x * System.Math.Cos(angle) - y * System.Math.Sin(angle);
double y1 = x * System.Math.Sin(angle) + y * System.Math.Cos(angle);
x = x1;
y = y1;
}```

See sample

## About the Author

Engineer IBM
Brazil
Senior Analyst

Founder of TIHunter Vagas de TI

## Comments and Discussions

 First PrevNext
 Can not find the complete ource code mans.0987 24-Jan-13 6:02
 My vote of 5 manoj kumar choubey 26-Feb-12 22:44
 Any ideas to make it faster? Tiramisung 1-Apr-10 17:16
 Re: Any ideas to make it faster? andalmeida 2-Apr-10 4:55
 Re: Any ideas to make it faster? Tiramisung 4-Apr-10 23:07
 I really upset! Mr. Cencious 13-Nov-07 0:28
 Re: I really upset! andalmeida 13-Nov-07 1:56
 Re: I really upset! Mr. Cencious 15-Nov-07 17:15
 Re: I really upset! Mr. Cencious 29-Nov-07 21:41
 Rotation seeblunt 11-Aug-07 18:36
 Re: Rotation seeblunt 11-Aug-07 19:02
 Re: Rotation andalmeida 12-Aug-07 15:04
 Mapping an image Cylindrical Surface zhongyisun 10-Aug-07 7:27
 Re: Mapping an image Cylindrical Surface andalmeida 10-Aug-07 8:24
 Re: Mapping an image Cylindrical Surface zhongyisun 10-Aug-07 8:45
 Curve tracing algorithm GD60 2-Aug-07 13:09
 Re: Curve tracing algorithm andalmeida 3-Aug-07 3:50
 Re: Curve tracing algorithm GD60 5-Aug-07 12:03
 Why? Paul Selormey 23-Jul-07 21:41
 The codes! ``` Color color = imgBitmap.GetPixel(i, j); Brush brs = new SolidBrush(color); Pen pen = new Pen(color); int ix = (int)x + 100; int iy = (int)y + 100; graphics.FillRectangle(brs, ix, iy, 1, 1); ```   you are creating a pen several times without even using it, and a brush several times without even disposing any. Please, you only need to create the brush once and do `brush.Color = imgBitmap.GetPixel(i, j);`, since it a solid-brush.   BTW, it is a cool idea, similar to image reprojection algorithm in GIS.   Best regards, Paul.   Jesus Christ is LOVE! Please tell somebody.
 Re: Why? andalmeida 24-Jul-07 3:39
 Re: Why? andalmeida 25-Jul-07 6:57
 Re: Why? Paul Selormey 25-Jul-07 7:24
 Re: Why? Johnno74 30-Jul-07 14:39
 Re: Why? Patrick Sears 31-Jul-07 10:55
 Re: Why? andalmeida 31-Jul-07 10:58
 Last Visit: 31-Dec-99 19:00     Last Update: 28-Dec-14 14:05 Refresh 12 Next »

General    News    Suggestion    Question    Bug    Answer    Joke    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.