/// <summary> /// Converts a Bitmap to an RGB structure with or without alpha-channel. /// </summary> /// <param name="Data">Bitmap</param> /// <param name="alpha">Alpha-channel</param> /// <returns>RGBA structure array</returns> public static float[][,] ToRGB(this Bitmap Data, bool alpha = false) { BitmapData bmData = BitmapFormat.Lock32bpp(Data); float[][,] rgb = BitmapMatrix.ToRGB(bmData, alpha); BitmapFormat.Unlock(Data, bmData); return(rgb); }
/// <summary> /// Appy filter. /// </summary> /// <param name="bmData">Bitmap data</param> private unsafe void ApplyRGB(BitmapData bmData) { float[][,] rgb = BitmapMatrix.ToRGB(bmData, true); this.filter.Apply(rgb[0]); this.filter.Apply(rgb[1]); this.filter.Apply(rgb[2]); BitmapMatrix.FromRGB(rgb, bmData); return; }
/// <summary> /// Apply filter. /// </summary> /// <param name="images">Bitmap array</param> /// <returns>Bitmap</returns> public Bitmap Apply(params Bitmap[] images) { // data int N = images.GetLength(0); float[][][,] data = new float[N][][, ]; int height = images[0].Height; int width = images[1].Width; float[][,] weights = new float[N][, ]; // to rgb array for (int i = 0; i < N; i++) { data[i] = BitmapMatrix.ToRGB(images[i]); } // initialize weights for (int i = 0; i < N; i++) { weights[i] = Matrice.One(height, width); } // applying params weights = Mul(weights, Exp(data, this.sigma)); // normalizing float[,] z = new float[height, width]; for (int i = 0; i < N; i++) { z = z.Add(weights[i]); } for (int i = 0; i < N; i++) { weights[i] = weights[i].Div(z); } // pyramids float[][,] pyrW; float[][,] pyrIr; float[][,] pyrIg; float[][,] pyrIb; // outputs float[][,] zero = gap.Forward(new float[height, width]); float[][,] r = (float[][, ])zero.Clone(); float[][,] g = (float[][, ])zero.Clone(); float[][,] b = (float[][, ])zero.Clone(); int levels = r.GetLength(0); // do job for (int i = 0; i < N; i++) { pyrW = gap.Forward(weights[i]); zero = data[i]; pyrIr = lap.Forward(zero[0]); pyrIg = lap.Forward(zero[1]); pyrIb = lap.Forward(zero[2]); for (int l = 0; l < levels; l++) { z = pyrW[l]; r[l] = r[l].Add(z.Mul(pyrIr[l])); g[l] = g[l].Add(z.Mul(pyrIg[l])); b[l] = b[l].Add(z.Mul(pyrIb[l])); } } // reconstruction Bitmap bitmap = BitmapMatrix.FromRGB(new float[][, ] { lap.Backward(r), lap.Backward(g), lap.Backward(b) }); return(bitmap); }