//image smoothing by entered size for average filter process private static Bitmap SmoothHelper(Bitmap img, int m, int n, SmoothInColorSpace cSpace) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); List <ArraysListInt> Result = new List <ArraysListInt>(); double[,] filter; if (!Checks.BinaryInput(img)) { List <ArraysListInt> ColorList = Helpers.GetPixels(img); if (m >= 1 && n >= 1) { //create average filter by entered size filter = ImageFilter.FspecialSize(m, n, "average"); //smooth in choosen color space switch (cSpace) { case SmoothInColorSpace.RGB: if (Depth == 8) { var bw = ImageFilter.Filter_double(ColorList[0].Color, filter).ArrayToUint8(); Result.Add(new ArraysListInt() { Color = bw }); Result.Add(new ArraysListInt() { Color = bw }); Result.Add(new ArraysListInt() { Color = bw }); } else { Result.Add(new ArraysListInt() { Color = ImageFilter.Filter_double(ColorList[0].Color, filter).ArrayToUint8() }); Result.Add(new ArraysListInt() { Color = ImageFilter.Filter_double(ColorList[1].Color, filter).ArrayToUint8() }); Result.Add(new ArraysListInt() { Color = ImageFilter.Filter_double(ColorList[2].Color, filter).ArrayToUint8() }); } break; case SmoothInColorSpace.HSV: var hsv = RGBandHSV.RGB2HSV(img); var hsv_temp = ImageFilter.Filter_double(hsv[2].Color, filter, PadType.replicate); //Filter by V - Value (Brightness/яркость) //artificially if V > 1; make him 1 Result = RGBandHSV.HSV2RGB(hsv[0].Color, hsv[1].Color, hsv_temp.ToBorderGreaterZero(1)); break; case SmoothInColorSpace.Lab: var lab = RGBandLab.RGB2Lab(img); var lab_temp = ImageFilter.Filter_double(lab[0].Color, filter, PadType.replicate); //Filter by L - lightness Result = RGBandLab.Lab2RGB(lab_temp.ToBorderGreaterZero(255), lab[1].Color, lab[2].Color); break; case SmoothInColorSpace.fakeCIE1976L: var fakeCIE1976L = RGBandLab.RGB2Lab1976(img); var fakeCIE1976L_temp = ImageFilter.Filter_double(fakeCIE1976L[0].Color, filter, PadType.replicate); //Filter by L - lightness Result = RGBandLab.Lab1976toRGB(fakeCIE1976L_temp, fakeCIE1976L[1].Color, fakeCIE1976L[2].Color); break; } image = Helpers.SetPixels(image, Result[0].Color, Result[1].Color, Result[2].Color); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } } else { Console.WriteLine("m and n parameters must be positive and greater or equal 1. Recommended 2 & 2 and higher. Method >Smooth<. Return black square."); } } else { Console.WriteLine("What did you expected to smooth binaty image? Return black square."); } return(image); }
////Equalize histogram for image brocess private static Bitmap EqualizeHelper(Bitmap img, HisteqColorSpace cSpace) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); List <ArraysListInt> Result = new List <ArraysListInt>(); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); if (!Checks.BinaryInput(img)) { List <ArraysListInt> ColorList = Helpers.GetPixels(img); //obtain histogram in choosen color space switch (cSpace) { case HisteqColorSpace.RGB: if (Depth == 8) { var bw = HisteqHelper(ColorList[0].Color); Result.Add(new ArraysListInt() { Color = bw }); Result.Add(new ArraysListInt() { Color = bw }); Result.Add(new ArraysListInt() { Color = bw }); } else { Result.Add(new ArraysListInt() { Color = HisteqHelper(ColorList[0].Color) }); Result.Add(new ArraysListInt() { Color = HisteqHelper(ColorList[1].Color) }); Result.Add(new ArraysListInt() { Color = HisteqHelper(ColorList[2].Color) }); } break; case HisteqColorSpace.HSV: var hsv = RGBandHSV.RGB2HSV(img); var hsv_temp = HisteqHelper((hsv[2].Color).ImageDoubleToUint8()); //Filter by V - Value (Brightness/яркость) //artificially if V > 1; make him 1 Result = RGBandHSV.HSV2RGB(hsv[0].Color, hsv[1].Color, hsv_temp.ImageUint8ToDouble().ToBorderGreaterZero(1)); break; case HisteqColorSpace.Lab: var lab = RGBandLab.RGB2Lab(img); var lab_temp = HisteqHelper((lab[0].Color).ArrayToUint8()); //Filter by L - lightness Result = RGBandLab.Lab2RGB(lab_temp.ArrayToDouble().ToBorderGreaterZero(255), lab[1].Color, lab[2].Color); break; case HisteqColorSpace.fakeCIE1976L: var fakeCIE1976L = RGBandLab.RGB2Lab1976(img); var fakeCIE1976L_temp = HisteqHelper(fakeCIE1976L[0].Color.ArrayToUint8()); //Filter by L - lightness Result = RGBandLab.Lab1976toRGB(fakeCIE1976L_temp.ArrayToDouble(), fakeCIE1976L[1].Color, fakeCIE1976L[2].Color); break; } image = Helpers.SetPixels(image, Result[0].Color, Result[1].Color, Result[2].Color); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } } else { Console.WriteLine("What did you expected to HistogramEqualization binaty image? Return black square."); } return(image); }
//Sharpen process in selected color space private static Bitmap SharpHelper(Bitmap img, UnSharpInColorSpace cSpace, SharpFilterType filterType) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); List <ArraysListInt> Result = new List <ArraysListInt>(); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); double[,] Resultemp; if (!Checks.BinaryInput(img)) { List <ArraysListInt> ColorList = Helpers.GetPixels(img); //sharp in choosen color space switch (cSpace) { case UnSharpInColorSpace.RGB: if (Depth == 8) { var bw = UnSharpHelperInt(ColorList[0].Color, filterType.ToString()); Result.Add(new ArraysListInt() { Color = bw }); Result.Add(new ArraysListInt() { Color = bw }); Result.Add(new ArraysListInt() { Color = bw }); } else { Result.Add(new ArraysListInt() { Color = UnSharpHelperInt(ColorList[0].Color, filterType.ToString()) }); //R Result.Add(new ArraysListInt() { Color = UnSharpHelperInt(ColorList[1].Color, filterType.ToString()) }); //G Result.Add(new ArraysListInt() { Color = UnSharpHelperInt(ColorList[2].Color, filterType.ToString()) }); //B } break; case UnSharpInColorSpace.HSVi: var hsvi = RGBandHSV.RGB2HSV(img); var hsvi_temp = UnSharpHelperInt(((hsvi[2].Color).ArrayMultByConst(100).ArrayToUint8()), filterType.ToString()); //Filter by V - Value (Brightness/яркость) Resultemp = hsvi_temp.ArrayToDouble().ArrayDivByConst(100).ToBorderGreaterZero(1); Result = RGBandHSV.HSV2RGB(hsvi[0].Color, hsvi[1].Color, Resultemp); break; case UnSharpInColorSpace.HSVd: var hsvd = RGBandHSV.RGB2HSV(img); var hsvd_temp = UnSharpHelperDouble((hsvd[2].Color).ArrayMultByConst(100), filterType.ToString()); //Filter by V - Value (Brightness/яркость) //artificially if V > 1, make him 1 Resultemp = hsvd_temp.ArrayDivByConst(100).ToBorderGreaterZero(1); Result = RGBandHSV.HSV2RGB(hsvd[0].Color, hsvd[1].Color, Resultemp); break; case UnSharpInColorSpace.Labi: var labi = RGBandLab.RGB2Lab(img); var labi_temp = UnSharpHelperInt((labi[0].Color).ArrayToUint8(), filterType.ToString()); //Filter by L - lightness Result = RGBandLab.Lab2RGB(labi_temp.ArrayToDouble(), labi[1].Color, labi[2].Color); break; case UnSharpInColorSpace.Labd: var labd = RGBandLab.RGB2Lab(img); var labd_temp = UnSharpHelperDouble(labd[0].Color, filterType.ToString()); //Filter by L - lightness Result = RGBandLab.Lab2RGB(labd_temp.ToBorderGreaterZero(255), labd[1].Color, labd[2].Color); break; case UnSharpInColorSpace.fakeCIE1976Labi: var fakeCIE1976abLi = RGBandLab.RGB2Lab1976(img); var fakeCIE1976Labi_temp = UnSharpHelperInt((fakeCIE1976abLi[0].Color).ArrayToUint8(), filterType.ToString()); //Filter by L - lightness Result = RGBandLab.Lab1976toRGB(fakeCIE1976Labi_temp.ArrayToDouble(), fakeCIE1976abLi[1].Color, fakeCIE1976abLi[2].Color); break; case UnSharpInColorSpace.fakeCIE1976Labd: var fakeCIE1976Labd = RGBandLab.RGB2Lab1976(img); var fakeCIE1976Labd_temp = UnSharpHelperDouble((fakeCIE1976Labd[0].Color), filterType.ToString()); //Filter by L - lightness Result = RGBandLab.Lab1976toRGB(fakeCIE1976Labd_temp.ToBorderGreaterZero(255), fakeCIE1976Labd[1].Color, fakeCIE1976Labd[2].Color); break; } image = Helpers.SetPixels(image, Result[0].Color, Result[1].Color, Result[2].Color); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } } else { Console.WriteLine("What did you expected to sharp binary image? Return black rectangle."); } return(image); }