12,444,517 members (53,045 online)
alternative version

16.5K views
14 bookmarked
Posted

# Generation of Banded Images

, 19 Feb 2009 CPOL
 Rate this:
An article to describe the generation of Images containing Mach bands at arbitrary angles (C#)

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

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:

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:

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:
```//Step 2 Computation of Distance AE
double thetaRadians = 3.1415927 * theta / 180;
double diagLength;
if (theta < 0)
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);```
``` // 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 yTopRight = -width * Math.Sin(thetaRadians);
double yTopLeft = 0.0;

double dVal;
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

## Share

 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

## You may also be interested in...

 Pro Pro

 First Prev Next
 Reinventing the wheel VCSKicks20-Feb-09 9:02 VCSKicks 20-Feb-09 9:02
 I'm just wondering... Guillaume Leparmentier19-Feb-09 4:41 Guillaume Leparmentier 19-Feb-09 4:41
 Re: I'm just wondering... Amarnath S19-Feb-09 17:53 Amarnath S 19-Feb-09 17:53
 Last Visit: 31-Dec-99 18:00     Last Update: 23-Aug-16 15:34 Refresh 1