Introduction
Image warping gives interesting results. Many special effects can be achieved through image warping. Warping is a process where an image is transformed in some way, to arrive at another image. This is as though the image is printed on a rubber sheet and that sheet is stretched in a non-uniform way. Polar mapping is an example of image warping. This is similar to the standard Log Polar mapping available in image processing. The receptor distribution in the retina of our eye resembles a log polar array. The difference between a log polar map and a polar map is that the concentric circles are non-uniformly spaced in a log polar map, whereas they are uniformly spaced in a polar map. In this article, we illustrate code to do polar mapping of an image.
Polar Mapping
The basic geometry of polar mapping is shown in the figure below. Equally spaced concentric circles are drawn centered at the image centre, and a number of equally spaced sectors are drawn. Pixels at the points of intersection of these circles and radial lines are plotted on a rectangular grid, and the resulting image is a polar view. In a log polar mapping, the radii of the concentric circles vary on a logarithmic scale.
Using the Code
The code is developed as follows:
- Open the input image for reading.
- Create the mapping parameters: in this step, we create a lookup table for sine and cosine values.
- Create a bitmap using polar mapping. The following code excerpt populates the bitmap.
static Bitmap CreatePolarImage()
{
Bitmap bmpOut = new Bitmap(noSectors, noRings,
PixelFormat.Format24bppRgb);
int i, j, j1, pixelsize = 3;
int x, y;
byte red = 0, green = 0, blue = 0;
double f1 = 0.0, f2 = 0.0;
float r = dr;
x = Convert.ToInt32(Math.Round(cx));
y = Convert.ToInt32(Math.Round(cy));
GetRGBVals(x, y, ref red, ref green, ref blue);
for (i = 0; i < noSectors; ++i)
{
Color c = Color.FromArgb(red, green, blue);
bmpOut.SetPixel(i, 0, c);
}
Rectangle rectOut = new Rectangle(0, 0, bmpOut.Width, bmpOut.Height);
BitmapData bmdOut = bmpOut.LockBits(rectOut, ImageLockMode.ReadWrite,
bmpOut.PixelFormat);
unsafe
{
for (j = 0; j < noRings; ++j, r += dr)
{
byte* row = (byte*)bmdOut.Scan0 + (j * bmdOut.Stride);
for (i = 0; i < noSectors; ++i)
{
f1 = (double)(cosValues[i]);
f2 = (double)(sinValues[i]);
x = Convert.ToInt32(Math.Round(cx + r * f1));
y = Convert.ToInt32(Math.Round(cy + r * f2));
if (srcRect.Contains(x, y))
{
GetRGBVals(x, y, ref red, ref green, ref blue);
}
else
{
red = green = blue = 0;
}
j1 = i * pixelsize;
row[j1] = blue;
row[j1 + 1] = green;
row[j1 + 2] = red;
}
}
}
bmpOut.UnlockBits(bmdOut);
return bmpOut;
}
static void GetRGBVals(int x, int y, ref byte red,
ref byte green, ref byte blue)
{
Color c = bmpIn.GetPixel(x, y);
red = c.R;
green = c.G;
blue = c.B;
}
This code is developed in C# on Visual Studio 2008, as a command line application. The usage of this code is:
PolarView.exe <infile> <outfile.png> <noSectors> <noRings>
It generates a PNG file as output.
Closure
A simple program to generate an interesting image warp in the form of a polar view was explained above. A point to note is that rotation of an image is just a cyclic shift along the horizontal axis in the polar view. The top of this page shows an image and its polar map. This program was inspired by the book "Digital Image Processing" by Nick Efford. This code can be further enhanced to use a better interpolation function for determining the source pixel.