Click here to Skip to main content
15,886,067 members
Articles / Desktop Programming / Win32

Generation of Banded Images

Rate me:
Please Sign up or sign in to vote.
4.50/5 (10 votes)
19 Feb 2009CPOL3 min read 28.3K   234   15   3
An article to describe the generation of Images containing Mach bands at arbitrary angles (C#)
img1.png img2.png

img3.png

Introduction

Images come from various sources. Most digital images come from digital cameras which typically capture scenes encountered in daily life. However, for learning purposes, we need to generate synthetic images. Synthetic images constitute ideal patterns not often found naturally. Image containing bands are an example of one such pattern. These bands show a gradation in grayscale values, and are called as Mach bands. Mach bands are typically shown horizontally or vertically, and display an interesting optical illusion. In this article, we present a program which generates Mach bands (grayscale) at an arbitrary angle, as specified by the user. The input parameters are the width and height of the image to be generated, along with the number of steps, and angle at which the bands are to be oriented.

Necessary Mathematics

The figure below shows the geometry of the image:

Rotation_Matrix.jpg - Click to enlarge image

It is desired to generate equally spaced bands at an angle of theta to the horizontal, as shown above. For this, we need to calculate the maximum distance to be divided equally; a look at the figure above shows that this distance is AE. This is the line to which all strips are perpendicular. Using simple trigonometry, it is possible to show that this distance is:

RotationFormulaFinal.png

When the angle becomes 90, this equation cannot be used, but the equivalent distance can easily be calculated.

The distance AE is divided into equally spaced intervals and each such interval is assigned a grayscale value, with these grayscale values increasing from 0 to 255 in equal intervals, depending on the number of steps specified by the user.

Now, considering a general pixel (x, y) in the image, a grayscale value needs to be assigned to this pixel. It becomes easy to assign this grayscale value if we transform (x, y) to a new coordinate system (x’, y’) oriented at an angle theta, as shown in the above figure. The transformation equations between the two coordinate systems are: 

RotationMatrixFinal.png

With this, it becomes a simple task to determine the grayscale value.

Using the Code

The inputs to the program are width, height, numSteps and theta. The steps in generating the image are:

  1. Creation of an array containing the grayscale values (this array has dimension of numSteps).
  2. Computation of the distance AE, and computing the step size – which is the width of a band. Of course, the special cases where the angle becomes 90 degrees, or negative need to be handled. This part of the code is shown below.
  3. Formation of the bitmap – this involves computing the grayscale value for each pixel in the bitmap. A function getGrayValue() computes the grayscale value. The code listing for this function is given below:
C#
//Step 2 Computation of Distance AE
double thetaRadians = 3.1415927 * theta / 180;
double thetaRad = thetaRadians;
double diagLength;
if (theta < 0)
    thetaRad = -thetaRadians;
if ((theta == 90) || (theta == -90))
{
    diagLength = width;
}
else
{
    double diagLength1 = height / Math.Cos(thetaRad);
    double val = (width - height * Math.Tan(thetaRad));
    double diagLength2 = val * Math.Sin(thetaRad);
    diagLength = Math.Abs(diagLength1 + diagLength2);
}

double stepSize = diagLength / (1.0 * numSteps);
C#
 // Function to return the gray value corresponding to a x, y pixel position in the image
static int getGrayValue(int x, int y, int width, int height, int numSteps, 
            double thetaRadians, double diagLength, double stepSize)
{
    // Convert the coordinates to the new coordinate system
    double yPrime = -x * Math.Sin(thetaRadians) + y * Math.Cos(thetaRadians);
    double yTopRight = -width * Math.Sin(thetaRadians);
    double yTopLeft = 0.0;

    double dVal;
    if (thetaRadians >= 0)
        dVal = (yPrime - yTopRight) / stepSize;
    else
        dVal = (yPrime - yTopLeft) / stepSize;

    int iVal = Convert.ToInt32(Math.Floor(dVal));
    if (iVal > numSteps - 1) iVal = numSteps - 1;
    if (iVal < 0) iVal = 0;

    // Find out the gray value corresponding to this pixel
    int iGray = Convert.ToInt32(grayValues[iVal]);
    return iGray;
}  

This is a command line application developed on Visual Studio 2008 and C#. The usage of this application is:

MakeSteps <filename.png> <width> <height> <numSteps> <thetaDegrees>

The program generates a PNG file as output.

Conclusions

A C# program for generating arbitrarily oriented grayscale bands in an image has been explained above. Sample images obtained from this application are shown at the top of this page. It is seen that when the number of steps is made large, we get an image with a smooth gradient. This program was inspired by the book “Digital Image Processing” by Nick Efford. This code can be further enhanced to include anti-aliasing.

Acknowledgements

The author would like to thank S Mahesh Reddy and Harsha T for all their efforts in the preparation of this article and associated coding.

History

  • 19th February, 2009: Initial post

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.



Comments and Discussions

 
GeneralReinventing the wheel Pin
VCSKicks20-Feb-09 9:02
VCSKicks20-Feb-09 9:02 
But many people don't know how the wheel was made Poke tongue | ;-P

very informative, awesome

Visual C# Kicks - Free C#.NET articles, resources, and downloads at
http://www.vcskicks.com

QuestionI'm just wondering... Pin
Guillaume Leparmentier19-Feb-09 4:41
Guillaume Leparmentier19-Feb-09 4:41 
AnswerRe: I'm just wondering... Pin
Amarnath S19-Feb-09 17:53
professionalAmarnath S19-Feb-09 17:53 

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.