public static void Notch(IOperand x, bool spawn = true, float radius1 = 0f, float radius2 = 0f) { if (x == null) { return; // No tolerance! } //Fourier.CImage[] cimg = ((frmStandard)x).cimg; Fourier.CImage[] cimg = new Fourier.CImage[3]; for (int i = 0; i < 3; i++) { cimg[i] = new Fourier.CImage(Normalize.ToFloat(((FormStandard)x).Image)[i], ((FormStandard)x).Image.Height, ((FormStandard)x).Image.Width); } if (!cimg[0].FrequencySpace) { for (int i = 0; i < 3; i++) { cimg[i] = _FFT(cimg[i]); } } int H = cimg[0].Height, W = cimg[0].Width; for (int c = 0; c < 3; c++) { for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { if ((i - W / 2) * (i - W / 2) + (j - H / 2) * (j - H / 2) < radius2 * radius2 && (i - W / 2) * (i - W / 2) + (j - H / 2) * (j - H / 2) > radius1 * radius1) { cimg[c].Data[j * W + i].Re = 0f; cimg[c].Data[j * W + i].Im = 0f; } } } } // Debug d1 = new Debug(cimg[0].ToBitmap()); // d1.Show(); for (int i = 0; i < 3; i++) { cimg[i] = _IFFT(cimg[i]); } float[][][] img = new float[3][][]; for (int i = 0; i < 3; i++) { img[i] = cimg[i].FromFloatModulus(); } if (spawn) { x.CreateSibling(img, "Notch filter of " + ((FormStandard)x).GetBitmap().ToString()); } else { ((FormStandard)x).Image = Normalize.FromFloat(img); } }
public static Fourier.CImage _FFT(Fourier.CImage cimg) { float scale = 1f / (float)Math.Sqrt(cimg.Width * cimg.Height); ComplexF[] data = cimg.Data; int offset = 0; for (int y = 0; y < cimg.Height; y++) { for (int x = 0; x < cimg.Width; x++) { if (((x + y) & 0x1) != 0) { data[offset] *= -1; } offset++; } } Exocortex.DSP.Fourier.FFT2(data, cimg.Width, cimg.Height, FourierDirection.Forward); cimg.FrequencySpace = true; for (int i = 0; i < data.Length; i++) { data[i] *= scale; } return(cimg); }
private void ResetImg() { img = Normalize.ToFloat(bmp); for (int i = 0; i < NCOLORS; i++) { rimg[i] = new Fourier.CImage(img[i], bmp.Height, bmp.Width); } }
private static Fourier.CImage _IFFT(Fourier.CImage cimg) { float scale = 1f / (float)Math.Sqrt(cimg.Width * cimg.Height); ComplexF[] data = cimg.Data; Exocortex.DSP.Fourier.FFT2(data, cimg.Width, cimg.Height, FourierDirection.Backward); cimg.FrequencySpace = false; for (int i = 0; i < data.Length; i++) { data[i] *= scale; } return(cimg); }
public static void Enhance(IOperand X = null, bool spawn = true) { if (X == null) { X = ExecutionStack.X; } if (X == null) { return; } Bitmap bmp = ((FormStandard)X).GetBitmap(); float[][][] img = Normalize.ToFloat(bmp); float[][][] hsi = ToHSI(img, bmp.Height, bmp.Width); for (int h = 0; h < bmp.Height; h++) { for (int w = 0; w < bmp.Width; w++) { hsi[1][h][w] = (hsi[1][h][w] * 1.10f > 1f ? 1f : hsi[1][h][w] * 1.10f); hsi[2][h][w] = (hsi[2][h][w] * 1.10f > 1f ? 1f : hsi[2][h][w] * 1.10f); } } img = FromHSI(hsi, bmp.Height, bmp.Width); if (spawn) { X.CreateSibling(img, "HSI Color Enhancement of " + bmp.ToString()); } else { ((FormStandard)X).Image = Normalize.FromFloat(img); Fourier.CImage[] cimg = new Fourier.CImage[3]; for (int i = 0; i < 3; i++) { cimg[i] = new Fourier.CImage(img[i], bmp.Height, bmp.Width); } } }
public static void FFT(IOperand X = null, bool spawn = true) { if (X == null) { X = ExecutionStack.X; } if (X == null) { return; } Bitmap bmp = X.GetBitmap(); float[][][] img = Normalize.ToFloat(bmp); int H = bmp.Height, W = bmp.Width; img = Pad(img, H, W); Bitmap temp = Normalize.FromFloat(img); Fourier.CImage[] cimg = new Fourier.CImage[3]; for (int i = 0; i < 3; i++) { cimg[i] = new Fourier.CImage(img[i], temp.Height, temp.Width); cimg[i] = _FFT(cimg[i]); img[i] = cimg[i].FromFloatModulus(); } if (spawn) { ((FormStandard)X).CreateSibling(img, "( Magnitude) FFT of " + bmp.ToString(), cimg); } else { ((FormStandard)X).Image = Normalize.FromFloat(img); ((FormStandard)X).CImg = cimg; } }
public static void ButterworthHigh(IOperand x, bool spawn = true, float radius = 0f) { if (x == null) { return; // No tolerance! } //Fourier.CImage[] cimg = ((frmStandard)x).cimg; Fourier.CImage[] cimg = new Fourier.CImage[3]; for (int i = 0; i < 3; i++) { cimg[i] = new Fourier.CImage(Normalize.ToFloat(((FormStandard)x).Image)[i], ((FormStandard)x).Image.Height, ((FormStandard)x).Image.Width); } if (!cimg[0].FrequencySpace) { for (int i = 0; i < 3; i++) { cimg[i] = _FFT(cimg[i]); } } int H = cimg[0].Height, W = cimg[0].Width; for (int c = 0; c < 3; c++) { for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { int D = (i - W / 2) * (i - W / 2) + (j - H / 2) * (j - H / 2); int D0 = (int)(radius * radius); if ((i - W / 2) * (i - W / 2) + (j - H / 2) * (j - H / 2) != 0) { cimg[c].Data[j * W + i].Re *= (1.0f - 1.0f / (float)Math.Pow(1.0f + (D / D0), 4)); cimg[c].Data[j * W + i].Im *= (1.0f - 1.0f / (float)Math.Pow(1.0f + (D / D0), 4)); } } } } // Debug d1 = new Debug(cimg[0].ToBitmap()); // d1.Show(); for (int i = 0; i < 3; i++) { cimg[i] = _IFFT(cimg[i]); } float[][][] img = new float[3][][]; for (int i = 0; i < 3; i++) { img[i] = cimg[i].FromFloatModulus(); } if (spawn) { x.CreateSibling(img, "Butterworth High Pass filter of " + ((FormStandard)x).GetBitmap().ToString()); } else { ((FormStandard)x).Image = Normalize.FromFloat(img); } }
public static void HighPass(IOperand x, bool spawn = true, float radius = 0f) { if (x == null) { return; // No tolerance! } //Fourier.CImage[] cimg = ((frmStandard)x).cimg; Fourier.CImage[] cimg = new Fourier.CImage[3]; for (int i = 0; i < 3; i++) { cimg[i] = new Fourier.CImage(Normalize.ToFloat(((FormStandard)x).Image)[i], ((FormStandard)x).Image.Height, ((FormStandard)x).Image.Width); } if (!cimg[0].FrequencySpace) { for (int i = 0; i < 3; i++) { cimg[i] = _FFT(cimg[i]); } } int H = cimg[0].Height, W = cimg[0].Width; for (int c = 0; c < 3; c++) { for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { if ((i - W / 2) * (i - W / 2) + (j - H / 2) * (j - H / 2) < radius * radius && !((i - W / 2) * (i - W / 2) + (j - H / 2) * (j - H / 2) == 0)) { cimg[c].Data[j * W + i].Re = 0f; cimg[c].Data[j * W + i].Im = 0f; } } } } // Debug d1 = new Debug(cimg[0].ToBitmap()); // d1.Show(); for (int i = 0; i < 3; i++) { cimg[i] = _IFFT(cimg[i]); } float[][][] img = new float[3][][]; for (int i = 0; i < 3; i++) { img[i] = cimg[i].FromFloatModulus(); } if (spawn) { x.CreateSibling(img, "High Pass filter of " + ((FormStandard)x).GetBitmap().ToString()); } else { ((FormStandard)x).Image = Normalize.FromFloat(img); } /* * * float[][][] real = new float[3][][]; * float[][][] imag = new float[3][][]; * for (int c = 0; c < 3; c++ ) * { * real[c] = cimg[c].FromFloatReal(); * imag[c] = cimg[c].FromFloatImag(); * } * /* * for (int c = 0; c < 3; c++ ) * for (int j = 0; j < cimg[0].Height; j++) * { * for (int i = 0; i < cimg[0].Width; i++) * { * if ((i - W / 2) * (i - W / 2) + (j - H / 2) * (j - H / 2) < radius * radius) * { * real[c][j][i] = 0f; * imag[c][j][i] = 0f; * * } * } * } */ /* * Fourier.CImage[] cr = new Fourier.CImage[3], ci = new Fourier.CImage[3]; * float[][][] img = new float[3][][]; * for (int i = 0; i < 3; i++ ) * { * cr[i] = new Fourier.CImage(real[i], H, W); * ci[i] = new Fourier.CImage(imag[i], H, W); * for (int j=0; j<cr[i].Data.Length; j++) * { * cr[i].Data[j].Im = ci[i].Data[j].Re; * } * cr[i] = _FFT(cr[i]); * img[i] = cr[i].FromFloatModulus(); * } */ //Fourier.CImage ci2 = new Fourier.CImage(Normalize.FromFloat(real), Normalize.FromFloat(imag)); //ci2 = _IFFT(ci2); /* * Fourier.CImage ci2 = new Fourier.CImage(Normalize.FromFloat(real), Normalize.FromFloat(imag)); * * Debug w1 = new Debug(ci2.ToBitmap()); * Debug w2 = new Debug(ci2.Magnitude()); * Debug w3 = new Debug(Normalize.FromFloat(real)); * Debug w4 = new Debug(Normalize.FromFloat(real)); * Debug w5 = new Debug(Normalize.FromFloat(imag)); * * ci2 = _IFFT(ci2); * * for (int i = 0; i < 3; i++ ) * cimg = new Fourier.CImage(cimg.Real(), cimg.Imag()); * //cimg = _IFFT(cimg); * float[][][] img = Normalize.ToFloat(cimg.ToBitmap()); * * Debug w6 = new Debug(ci2.ToBitmap()); * Debug w7 = new Debug(ci2.Magnitude()); * Debug w8 = new Debug(ci2.Real()); * * w1.Show(); * w2.Show(); * w3.Show(); * w4.Show(); * w5.Show(); * w6.Show(); * w7.Show(); * w8.Show(); * * * cimg = _IFFT(cimg); * * Debug w9 = new Debug(cimg.ToBitmap()); * Debug w10 = new Debug(cimg.Magnitude()); * Debug w11 = new Debug(cimg.Real()); * * w9.Show(); * w10.Show(); * w11.Show(); */ //float[][][] img = new float[3][][]; //for (int i = 0; i < 3; i++) //{ // img[i] = real[i]; //} }