12,555,894 members (46,447 online)
alternative version

34K views
22 bookmarked
Posted

# Polar View of an Image

, 24 Feb 2009 CPOL
 Rate this:
An article to describe the creation of a polar mapped view of an image (C#).

## 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:

1. Open the input image for reading.
2. Create the mapping parameters: in this step, we create a lookup table for sine and cosine values.
3. Create a bitmap using polar mapping. The following code excerpt populates the bitmap.
```static Bitmap CreatePolarImage()
{
// In the Polar bitmap,
// Width = number of sectors
// Height = number of rings
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);
// Map the centre point
for (i = 0; i < noSectors; ++i)
{
Color c = Color.FromArgb(red, green, blue);
bmpOut.SetPixel(i, 0, c);
}
// Map the rest of the image

Rectangle rectOut = new Rectangle(0, 0, bmpOut.Width, bmpOut.Height);
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.

## Share

 Software Developer India
No Biography provided

 Architect India
Programming computers since about 1987, my first computer language was Fortran 77. Later I learnt C, C++ and C#. Also programmed a little in VB .Net. Worked with Enterprise Java for a short while. Currently, interested in image processing. I love watching Kannada movies, and listening to Kannada songs. Currently studying and understanding the Bhagavad Geetha.

Open to part-time (two days a week) software freelancing / consulting, in Bangalore area.
Contact at amarnaths (dot) codeproject (at) gmail (dot) com

 Software Developer India
Email ID: mahe_swe@rediffmail.com

## You may also be interested in...

 Pro Pro

 First Prev Next
 omnidirectional Tri Luhur19-Jan-14 17:50 Tri Luhur 19-Jan-14 17:50
 The image! Bob10002-Mar-09 12:05 Bob1000 2-Mar-09 12:05
 Re: The image! Member 47497913-Mar-09 3:46 Member 4749791 3-Mar-09 3:46
 Last Visit: 31-Dec-99 18:00     Last Update: 25-Oct-16 22:12 Refresh 1