## Background

This article discusses an application of 2D Hartley transform of Image analysis in frequency domain. You can use this code for 2D Discreet Hartley transform on matrices or images.

## Using the Code

I am using ALGLIB (www.alglib.net) for evaluating 1D Hartley transform. This function is used to evaluate 2D FHT.

private double[,] FHT2DForward(double[,] c, int nx, int ny)
{
int i, j;
int m;
double[] real;
double[,] output;
output = c;
real = new double[nx];
for (j = 0; j < ny; j++)
{
for (i = 0; i < nx; i++)
{
real[i] = c[i, j];
}
m = (int)System.Math.Log((double)nx, 2);
fht.fhtr1d(ref real, nx);
for (i = 0; i < nx; i++)
{
output[i, j] = real[i];
}
}
real = new double[ny];
for (i = 0; i < nx; i++)
{
for (j = 0; j < ny; j++)
{
real[j] = output[i, j];
}
m = (int)System.Math.Log((double)ny, 2);
fht.fhtr1d(ref real, ny);
for (j = 0; j < ny; j++)
{
output[i, j] = real[j];
}
}
return (output);
}
private double[,] FHT2DInverse(double[,] c, int nx, int ny)
{
int i, j;
int m;
double[] real;
double[,] output;
output = c;
real = new double[nx];
for (j = 0; j < ny; j++)
{
for (i = 0; i < nx; i++)
{
real[i] = c[i, j];
}
m = (int)System.Math.Log((double)nx, 2);
fht.fhtr1dinv(ref real, nx);
for (i = 0; i < nx; i++)
{
output[i, j] = real[i];
}
}
real = new double[ny];
for (i = 0; i < nx; i++)
{
for (j = 0; j < ny; j++)
{
real[j] = output[i, j];
}
m = (int)System.Math.Log((double)ny, 2);
fht.fhtr1dinv(ref real, ny);
for (j = 0; j < ny; j++)
{
output[i, j] = real[j];
}
}
return (output);
}
private Bitmap Displaymap(int[,] output)
{
int i, j;
Bitmap image = new Bitmap(output.GetLength(0), output.GetLength(1));
BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, output.GetLength(0),
output.GetLength(1)),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* imagePointer1 = (byte*)bitmapData1.Scan0;
for (i = 0; i < bitmapData1.Height; i++)
{
for (j = 0; j < bitmapData1.Width; j++)
{
if (output[j, i] < 0)
{
imagePointer1[0] = 0;
imagePointer1[1] = 255;
imagePointer1[2] = 0;
}
else if ((output[j, i] >= 0) && (output[j, i] < 50))
{
imagePointer1[0] = (byte)((output[j, i]) * 4);
imagePointer1[1] = 0;
imagePointer1[2] = 0;
}
else if ((output[j, i] >= 50) && (output[j, i] < 100))
{
imagePointer1[0] = 0;
imagePointer1[1] = (byte)(output[j, i] * 2);
imagePointer1[2] = (byte)(output[j, i] * 2);
}
else if ((output[j, i] >= 100) && (output[j, i] < 150))
{
imagePointer1[0] = (byte)((output[j, i]) * 0.7);
imagePointer1[1] = 0;
imagePointer1[2] = (byte)((output[j, i]) * 0.7);
}
else if ((output[j, i] >= 150) && (output[j, i] < 255))
{
imagePointer1[0] = 0;
imagePointer1[1] = (byte)((output[j, i]) * 0.7);
imagePointer1[2] = 0;
}
else if ((output[j, i] > 255))
{
imagePointer1[0] = 0;
imagePointer1[1] = 0;
imagePointer1[2] = (byte)((output[j, i]) * 0.7);
}
imagePointer1[3] = 255;
imagePointer1 += 4;
}
imagePointer1 += (bitmapData1.Stride - (bitmapData1.Width * 4));
}
}
image.UnlockBits(bitmapData1);
return image;
}

## Points of Interest

For details of the FHT you can refer to the links below:

## Thanks

Thanks to the ALGLIB Project. I have just added one small level to that. All credit goes to them.

In mathematics, the **Hartley transform** is an integral transform closely related to the Fourier transform, but which transforms real-valued functions to real-valued functions. It was proposed as an alternative to the Fourier transform by R. V. L. Hartley in 1942, and is one of many known Fourier-related transforms. Compared to the Fourier transform, the Hartley transform has the advantages of transforming real functions to real functions (as opposed to requiring complex numbers) and of being its own inverse.

The discrete version of the transform, the Discrete Hartley transform, was introduced by R. N. Bracewell in 1983.

The two-dimensional Hartley transform can be computed by an analog optical process similar to an optical Fourier transform, with the proposed advantage that only its amplitude and sign need to be determined rather than its complex phase (Villasenor, 1994). However, optical Hartley transforms do not seem to have seen widespread use.

## History

- 22
^{nd} December, 2010: Initial post