Click here to Skip to main content
Click here to Skip to main content

Polar View of an Image

, , , 24 Feb 2009
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Authors

Harsha T
Software Developer
India India
No Biography provided

Amarnath S
Architect
India 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 while. Currently, interested in image processing. I love watching Kannada movies, and listening to Kannada songs.

S Mahesh Reddy
Software Developer
India India
Email ID: mahe_swe@rediffmail.com

Comments and Discussions

 
Questionomnidirectional PinmemberTri Luhur19-Jan-14 17:50 
GeneralThe image! PinmemberBob10002-Mar-09 12:05 
GeneralRe: The image! PinmemberMember 47497913-Mar-09 3:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 24 Feb 2009
Article Copyright 2009 by Harsha T, Amarnath S, S Mahesh Reddy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid