public static Bitmap ifft2d(Bitmap input) { int width = (int)Math.Pow(2, Math.Ceiling(Math.Log(input.Width, 2))); int height = (int)Math.Pow(2, Math.Ceiling(Math.Log(input.Width, 2))); byte[] data = ImageUtils.greyScale(input); if (width != input.Width || height != input.Height) { // Pad data byte[] newdata = new byte[width * height]; int xoff = width - input.Width; int yoff = height - input.Height; for (int i = 0; i < input.Height; i++) { for (int k = 0; k < input.Width; k++) { newdata[(i + yoff) * width + k + xoff] = data[i * input.Width + k]; } } data = newdata; } Complex[,] cmplx = new Complex[height, width]; double scale = 1.0 / (double)Math.Sqrt(width * height); scale = 1; for (int i = 0; i < data.Length; i++) { cmplx[i / width, i % width] = new Complex(data[i] / 256.0); } cmplx = ifft2d(cmplx); for (int i = 0; i < height; i++) { for (int k = 0; k < width; k++) { if (((k + i) & 0x1) != 0) { cmplx[i, k] *= -1; } } } for (int i = 0; i < data.Length; i++) { data[i] = (byte)Math.Min(255, (int)(cmplx[i / width, i % width].GetModulus() * 256.0 * scale)); } Bitmap output = ImageUtils.getBitmap(data, PixelFormat.Format8bppIndexed, width, height); return(output); }
public static Bitmap fft2d(Bitmap input, bool reorder) { int width = input.Width; int height = input.Height; byte[] data = ImageUtils.greyScale(input); Complex[,] cmplx = new Complex[height, width]; double scale = 1.0 / (double)Math.Sqrt(width * height); for (int i = 0; i < data.Length; i++) { cmplx[i / width, i % width] = new Complex(data[i] / 256.0); } cmplx = fft2d(cmplx); for (int i = 0; i < data.Length; i++) { data[i] = (byte)Math.Min(255, (int)(cmplx[i / width, i % width].GetModulus() * 256.0) * scale); } // Swapping data -> lowest frequencies at center if (reorder) { for (int i = 0; i < height / 2; i++) { for (int k = 0; k < width / 2; k++) { byte val1 = data[i * width + k]; byte val2 = data[i * width + k + width / 2]; data[i * width + k] = data[(i + width / 2) * width + k + width / 2]; data[(i + width / 2) * width + k + width / 2] = val1; data[i * width + k + width / 2] = data[(i + width / 2) * width + k]; data[(i + width / 2) * width + k] = val2; } } } Bitmap output = ImageUtils.getBitmap(data, PixelFormat.Format8bppIndexed, width, height); return(output); }