Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Polar View of an Image

0.00/5 (No votes)
24 Feb 2009 1  
An article to describe the creation of a polar mapped view of an image (C#).

LenaPolar.PNG

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.

PolarMap1.png

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);
    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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here