//filtering by int private static int[,] UnSharpHelperInt(int[,] cPlane, string fType) { int[,] Result; if (fType == "unsharp") { Result = (ImageFilter.Filter_double(cPlane, ImageFilter.Dx3FWindow(fType))).ArrayToUint8(); } else { Result = cPlane.SubArrays(ImageFilter.Filter_int(cPlane, ImageFilter.Ix3FWindow(fType), PadType.replicate)).Uint8Range(); } return(Result); }
private static Bitmap ContourHelper(Bitmap img, CountourVariant variant) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); //arrays, where store color components result after operations int[,] resultR = new int[img.Height, img.Width]; int[,] resultG = new int[img.Height, img.Width]; int[,] resultB = new int[img.Height, img.Width]; bool type = true; //filtered values storage List <ArraysListDouble> filt = new List <ArraysListDouble>(); //obtain color components. form 8bpp works too, but not recommended to use 8-bit .jpeg\tif\jpg images List <ArraysListInt> ColorList = Helpers.GetPixels(img); var Red = ColorList[0].Color; var Green = ColorList[1].Color; var Blue = ColorList[2].Color; double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); if (Depth == 8 || Checks.BlackandWhite24bppCheck(ColorList)) { type = false; } //variants 1-4 black & white. Variants 5, 6 - colored if (variant == CountourVariant.Variant1_BW || variant == CountourVariant.Variant5_RGB || variant == CountourVariant.Variant2_BW) { //using filter and array operations count RGB values in 2d dimentions x and y for variants with double if (!type) { filt.Add(new ArraysListDouble() { Color = ImageFilter.Filter_double(Red, "Sobel") }); //b&w x filt.Add(new ArraysListDouble() { Color = ImageFilter.Filter_double(Red, "SobelT") }); //b&w y } else { filt.Add(new ArraysListDouble() { Color = ImageFilter.Filter_double(Red, "Sobel") }); //Rx filt.Add(new ArraysListDouble() { Color = ImageFilter.Filter_double(Red, "SobelT") }); //Ry filt.Add(new ArraysListDouble() { Color = ImageFilter.Filter_double(Green, "Sobel") }); //Gx filt.Add(new ArraysListDouble() { Color = ImageFilter.Filter_double(Green, "SobelT") }); //Gy filt.Add(new ArraysListDouble() { Color = ImageFilter.Filter_double(Blue, "Sobel") }); //Bx filt.Add(new ArraysListDouble() { Color = ImageFilter.Filter_double(Blue, "SobelT") }); //By } if (variant == CountourVariant.Variant1_BW) { //gradient for one color component B&W result if (!type) { resultR = Gradient.Grad(filt[0].Color, filt[1].Color).ImageDoubleToUint8(); } else { resultR = Gradient.Grad(filt[0].Color, filt[1].Color, filt[2].Color, filt[3].Color, filt[4].Color, filt[5].Color).ImageDoubleToUint8(); } resultG = resultR; resultB = resultR; //Black & White result } else if (variant == CountourVariant.Variant2_BW) { //gradient for one color component B&W result if (!type) { resultR = Gradient.Grad(filt[0].Color, filt[1].Color).ArrayMultByConst(2).ImageDoubleToUint8(); } else { resultR = Gradient.Grad(filt[0].Color, filt[1].Color, filt[2].Color, filt[3].Color, filt[4].Color, filt[5].Color).ArrayMultByConst(2).ImageDoubleToUint8(); } resultG = resultR; resultB = resultR; //Black & White result } else { if (type) { //RGB gradients var RG = (filt[0].Color).PowArrayElements(2).SumArrays((filt[1].Color).PowArrayElements(2)).SqrtArrayElements(); //R gradient var GG = (filt[2].Color).PowArrayElements(2).SumArrays((filt[3].Color).PowArrayElements(2)).SqrtArrayElements(); //G gradient var BG = (filt[4].Color).PowArrayElements(2).SumArrays((filt[5].Color).PowArrayElements(2)).SqrtArrayElements(); //B gradient resultR = RG.ArrayToUint8(); resultG = GG.ArrayToUint8(); resultB = BG.ArrayToUint8(); } else { Console.WriteLine("Need RGB image for Variant5_RGB as input. Contour Method."); return(image); } } } else if (variant == CountourVariant.Variant3_BW || variant == CountourVariant.Variant4_BW) { //convert image into gray scale var gray = MoreHelpers.BlackandWhiteProcessHelper(img); double[,] GG = new double[img.Height, img.Height]; //gray gradient if (variant == CountourVariant.Variant3_BW) { var Gx = ImageFilter.Filter_double(gray, "Sobel"); var Gy = ImageFilter.Filter_double(gray, "SobelT"); GG = Gx.PowArrayElements(2).SumArrays(Gy.PowArrayElements(2)).SqrtArrayElements(); } else { var Gx = ImageFilter.Filter_int(gray, "Sobel"); var Gy = ImageFilter.Filter_int(gray, "SobelT"); GG = Gx.ArrayToDouble().PowArrayElements(2).SumArrays(Gy.ArrayToDouble().PowArrayElements(2)).SqrtArrayElements(); } resultR = GG.ArrayToUint8(); resultG = resultR; resultB = resultR; } else if (variant == CountourVariant.Variant6_RGB) { //using filter and array operations count RGB values in 2d dimentions x and y for variants with int if (type) { var Rix = ImageFilter.Filter_int(Red, "Sobel"); var Riy = ImageFilter.Filter_int(Red, "SobelT"); var Gix = ImageFilter.Filter_int(Green, "Sobel"); var Giy = ImageFilter.Filter_int(Green, "SobelT"); var Bix = ImageFilter.Filter_int(Blue, "Sobel"); var Biy = ImageFilter.Filter_int(Blue, "SobelT"); var RG = Rix.ArrayToDouble().PowArrayElements(2).SumArrays(Riy.ArrayToDouble().PowArrayElements(2)).SqrtArrayElements(); //R gradient var GG = Gix.ArrayToDouble().PowArrayElements(2).SumArrays(Giy.ArrayToDouble().PowArrayElements(2)).SqrtArrayElements(); //G gradient var BG = Bix.ArrayToDouble().PowArrayElements(2).SumArrays(Biy.ArrayToDouble().PowArrayElements(2)).SqrtArrayElements(); //B gradient resultR = RG.ArrayToUint8(); resultG = GG.ArrayToUint8(); resultB = BG.ArrayToUint8(); } else { Console.WriteLine("Need RGB image for Variant6_RGB as input. Contour Method."); return(image); } } image = Helpers.SetPixels(image, resultR, resultG, resultB); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } return(image); }
private static Bitmap HighContrastProcess(Bitmap img, ContrastFilter filter, HighContastRGB cPlane, [CallerMemberName] string callName = "") { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); int[,] resultR = new int[img.Height, img.Width]; int[,] resultG = new int[img.Height, img.Width]; int[,] resultB = new int[img.Height, img.Width]; double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); int[,] filterWindow = new int[3, 3]; if (filter == ContrastFilter.filterOne) { filterWindow = ImageFilter.Ix3FWindow("HighContrast1"); } else { filterWindow = ImageFilter.Ix3FWindow("HighContrast2"); } if (callName == "HighContrastBlackWhite") { if (Depth == 8 || Checks.BlackandWhite24bppCheck(img)) { var GrayC = MoreHelpers.BlackandWhiteProcessHelper(img); resultR = ImageFilter.Filter_int(GrayC, filterWindow, PadType.replicate); resultG = resultR; resultB = resultR; } else { Console.WriteLine("There non 8bit or 24bit black and white image at input. Method:" + callName); } } else { if (Checks.RGBinput(img)) { List <ArraysListInt> ColorList = Helpers.GetPixels(img); switch (cPlane) { case HighContastRGB.R: resultR = ImageFilter.Filter_int(ColorList[0].Color, filterWindow, PadType.replicate); resultG = ColorList[1].Color; resultB = ColorList[2].Color; break; case HighContastRGB.G: resultG = ImageFilter.Filter_int(ColorList[1].Color, filterWindow, PadType.replicate); resultR = ColorList[0].Color; resultB = ColorList[2].Color; break; case HighContastRGB.B: resultB = ImageFilter.Filter_int(ColorList[2].Color, filterWindow, PadType.replicate); resultR = ColorList[0].Color; resultG = ColorList[1].Color; break; case HighContastRGB.RGB: resultR = ImageFilter.Filter_int(ColorList[0].Color, filterWindow, PadType.replicate); resultG = ImageFilter.Filter_int(ColorList[1].Color, filterWindow, PadType.replicate); resultB = ImageFilter.Filter_int(ColorList[2].Color, filterWindow, PadType.replicate); break; } } } image = Helpers.SetPixels(image, resultR, resultG, resultB); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } return(image); }