Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / Win32

Polar View of an Image

, ,
Rate me:
Please Sign up or sign in to vote.
3.50/5 (4 votes)
24 Feb 2009CPOL2 min read 58.7K   1.6K   22   3
An article to describe the creation of a polar mapped view of an image (C#).

Image 1

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.
C#
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)


Written By
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 short while. I love watching Kannada movies, and listening to Kannada songs. Currently studying and understanding the Bhagavad Geetha and teaching Sanskrit on YouTube.



Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
Software Developer
India India
Email ID: mahe_swe@rediffmail.com

Comments and Discussions

 
Questionomnidirectional Pin
daehyunn19-Jan-14 17:50
daehyunn19-Jan-14 17:50 
GeneralThe image! Pin
Bob10002-Mar-09 12:05
professionalBob10002-Mar-09 12:05 
GeneralRe: The image! Pin
Uilleam3-Mar-09 3:46
Uilleam3-Mar-09 3:46 

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

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