12,510,818 members (49,422 online)
alternative version

110.2K views
87 bookmarked
Posted

# Mapping Images on Spherical Surfaces Using C#

, 15 Jul 2016 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

```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

## Share

 Engineer Brazil
Senior Analyst

## You may also be interested in...

 Pro Pro

 First Prev Next
 How do you avoid clashes with remapped pixels? JWhattam11-Nov-15 12:47 JWhattam 11-Nov-15 12:47
 Can not find the complete ource code mans.098724-Jan-13 5:02 mans.0987 24-Jan-13 5:02
 My vote of 5 manoj kumar choubey26-Feb-12 21:44 manoj kumar choubey 26-Feb-12 21:44
 Any ideas to make it faster? Tiramisung1-Apr-10 16:16 Tiramisung 1-Apr-10 16:16
 Re: Any ideas to make it faster? andalmeida2-Apr-10 3:55 andalmeida 2-Apr-10 3:55
 Re: Any ideas to make it faster? Tiramisung4-Apr-10 22:07 Tiramisung 4-Apr-10 22:07
 Re: Any ideas to make it faster? simonxy17-Aug-15 23:55 simonxy 17-Aug-15 23:55
 I really upset! Mr. Cencious12-Nov-07 23:28 Mr. Cencious 12-Nov-07 23:28
 Re: I really upset! andalmeida13-Nov-07 0:56 andalmeida 13-Nov-07 0:56
 Re: I really upset! Mr. Cencious15-Nov-07 16:15 Mr. Cencious 15-Nov-07 16:15
 Re: I really upset! Mr. Cencious29-Nov-07 20:41 Mr. Cencious 29-Nov-07 20:41
 Rotation seeblunt11-Aug-07 17:36 seeblunt 11-Aug-07 17:36
 Re: Rotation seeblunt11-Aug-07 18:02 seeblunt 11-Aug-07 18:02
 Already worked it out Modified code /// /// /// /// /// /// /// private double MapCoordinate(double i1, double i2, double w1, double w2, double p) { return ((p - i1) / (i2 - i1)) * (w2 - w1) + w1; } public Bitmap Create(Bitmap imgBitmap) { return Create(imgBitmap, 0, 0); } public Bitmap Create(Bitmap imgBitmap, double rotateX, Double rotateY) { return Create(imgBitmap, rotateX, rotateY, 0); } public Bitmap Create(Bitmap imgBitmap, double rotateX,Double rotateY, double rotateLongitude) { double radius = imgBitmap.Height / 2; int width = imgBitmap.Width; int height = imgBitmap.Height; int xcentre = width / 2; int ycentre = height / 2; Bitmap newBitmap = new Bitmap(imgBitmap.Width, imgBitmap.Height , PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(newBitmap); for (int i = 0; i < imgBitmap.Width; i++) { for (int j = 0; j < imgBitmap.Height; j++) { // map the angles from image coordinates longitude double theta = MapCoordinate(0.0, width - 1, theta1, theta0, i); if (rotateLongitude != 0) { theta+= rotateLongitude; } // latitude double phi = MapCoordinate(0.0, 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 if (rotateX != 0) { RotateAxis(rotateX, ref x, ref z); } if (rotateY != 0) { RotateAxis(rotateY, ref y, ref z); } // plot only positive points if (z >= 0) { Color color = imgBitmap.GetPixel(i, j); Brush brs = new SolidBrush(color); //newBitmap.SetPixel((int)x + xcentre, (int)y + ycentre, color); int ix = (int)x + xcentre; int iy = (int)y + ycentre; g.FillRectangle(brs, ix, iy, 3, 3); brs.Dispose(); } } } return newBitmap; } private void RotateAxis(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; }
 Re: Rotation andalmeida12-Aug-07 14:04 andalmeida 12-Aug-07 14:04
 Mapping an image Cylindrical Surface zhongyisun10-Aug-07 6:27 zhongyisun 10-Aug-07 6:27
 Re: Mapping an image Cylindrical Surface andalmeida10-Aug-07 7:24 andalmeida 10-Aug-07 7:24
 Re: Mapping an image Cylindrical Surface zhongyisun10-Aug-07 7:45 zhongyisun 10-Aug-07 7:45
 Curve tracing algorithm GD602-Aug-07 12:09 GD60 2-Aug-07 12:09
 Re: Curve tracing algorithm andalmeida3-Aug-07 2:50 andalmeida 3-Aug-07 2:50
 Re: Curve tracing algorithm GD605-Aug-07 11:03 GD60 5-Aug-07 11:03
 Why? Paul Selormey23-Jul-07 20:41 Paul Selormey 23-Jul-07 20:41
 Re: Why? andalmeida24-Jul-07 2:39 andalmeida 24-Jul-07 2:39
 Re: Why? andalmeida25-Jul-07 5:57 andalmeida 25-Jul-07 5:57
 Re: Why? Paul Selormey25-Jul-07 6:24 Paul Selormey 25-Jul-07 6:24
 Re: Why? Johnno7430-Jul-07 13:39 Johnno74 30-Jul-07 13:39
 Re: Why? Patrick Sears31-Jul-07 9:55 Patrick Sears 31-Jul-07 9:55
 Re: Why? andalmeida31-Jul-07 9:58 andalmeida 31-Jul-07 9:58
 Re: Why? Patrick Sears31-Jul-07 10:17 Patrick Sears 31-Jul-07 10:17
 Re: Why? Werdna13-Aug-07 4:57 Werdna 13-Aug-07 4:57
 sweet Ben Daniel23-Jul-07 18:10 Ben Daniel 23-Jul-07 18:10
 Re: sweet andalmeida24-Jul-07 3:53 andalmeida 24-Jul-07 3:53
 Re: sweet N.L. Neilson2-Aug-07 11:01 N.L. Neilson 2-Aug-07 11:01
 Re: sweet andalmeida2-Aug-07 11:03 andalmeida 2-Aug-07 11:03
 Re: sweet andalmeida2-Aug-07 11:17 andalmeida 2-Aug-07 11:17
 Re: sweet N.L. Neilson2-Aug-07 11:51 N.L. Neilson 2-Aug-07 11:51
 Re: sweet andalmeida2-Aug-07 11:51 andalmeida 2-Aug-07 11:51
 Re: sweet N.L. Neilson2-Aug-07 13:48 N.L. Neilson 2-Aug-07 13:48
 Last Visit: 31-Dec-99 18:00     Last Update: 29-Sep-16 21:23 Refresh 1