private static int[,] FindLineHelper(int[,] im, double[,] filter) { int[,] result = new int[im.GetLength(0), im.GetLength(1)]; var temp = (ImageFilter.Filter_double(im.ImageUint8ToDouble(), filter, PadType.replicate)).AbsArrayElements(); var max = temp.Cast <double>().ToArray().Max(); for (int i = 0; i < im.GetLength(0); i++) { for (int j = 0; j < im.GetLength(1); j++) { if (temp[i, j] >= max) { result[i, j] = 255; } else { result[i, j] = 0; } } } return(result); }
private static int[,] EdgeHelperv2(double scale, int[,] im, double threshold, double[,] filter, double[,] filterT, double fdiv, EdgeDirection direction) { int[,] result = new int[im.GetLength(0), im.GetLength(1)]; double[,] b = new double[im.GetLength(0), im.GetLength(1)]; double cutoff = 0; double tresh = 0; //Sobel approximation to derivative var bx = ImageFilter.Filter_double(im, filterT, fdiv); var by = ImageFilter.Filter_double(im, filter, fdiv); //compute the magnitude if (direction == EdgeDirection.horizontal) { b = by.PowArrayElements(2); } else if (direction == EdgeDirection.vertical) { b = bx.PowArrayElements(2); } else if (direction == EdgeDirection.both || direction == EdgeDirection.def) { b = bx.PowArrayElements(2).SumArrays(by.PowArrayElements(2)); } else { Console.WriteLine("Wrong direction"); } if (threshold == 0) { cutoff = scale * b.Cast <double>().ToArray().Average(); tresh = Math.Sqrt(cutoff); } else { cutoff = Math.Pow(threshold, 2); } for (int i = 0; i < im.GetLength(0); i++) { for (int j = 0; j < im.GetLength(1); j++) { if (b[i, j] > cutoff) { result[i, j] = 255; } else { result[i, j] = 0; } } } return(result); }
private static int[,] HmeanCount(int[,] colorPlane, double[,] filter, int m, int n) { //colorPlaneArray must be in range [0..1] (ImageUint8ToDouble function) //f = m * n / imfilter(1 / (colorPlaneArray + eps), filter, 'replicate'); var harmean = ImageFilter.Filter_double(colorPlane.ImageUint8ToDouble().ArraySumWithConst((2.2204 * Math.Pow(10, -16))) .ConstDivByArrayElements(1), filter, PadType.replicate).ConstDivByArrayElements(((double)m * (double)n)).ImageDoubleToUint8(); return(harmean); }
private static int[,] GmeanCount(int[,] colorPlane, double[,] filter, int m, int n) { //colorPlaneArray must be in range [0..1] (ImageUint8ToDouble function) //f = exp(imfilter(log(colorPlaneArray), filter, 'replicate')) ^ (1 / m / n); var gmean = (ImageFilter.Filter_double((colorPlane.ImageUint8ToDouble().LogArrayElements()), filter, PadType.replicate)).ExpArrayElements() .PowArrayElements(((double)1 / (double)m / (double)n)).ImageDoubleToUint8(); return(gmean); }
private static int[,] CharmeanCount(int[,] colorPlane, double[,] filter, double filterOrder) { //colorPlaneArray must be in range [0..1] (ImageUint8ToDouble function) //f = imfilter(colorPlaneArray ^ (filterOrder + 1), filter, 'replicate'); //f = f / (imfilter(colorPlaneArray ^ filterOrder, filter, 'replicate') + eps); var charmean = ImageFilter.Filter_double(colorPlane.ImageUint8ToDouble().PowArrayElements((filterOrder + 1)), filter, PadType.replicate) .ArraydivElements(ImageFilter.Filter_double(colorPlane.ImageUint8ToDouble().PowArrayElements(filterOrder), filter, PadType.replicate)).ImageDoubleToUint8(); return(charmean); }
//filtering by double private static double[,] UnSharpHelperDouble(double[,] cPlane, string fType) { double[,] Result; if (fType == "unsharp") { Result = ImageFilter.Filter_double(cPlane, ImageFilter.Dx3FWindow(fType), PadType.replicate); } else { Result = cPlane.SubArrays(ImageFilter.Filter_double(cPlane, ImageFilter.Dx3FWindow(fType), PadType.replicate)); } return(Result); }
//filtering by double private static double[,] FSpecialFilterHelper(double[,] cPlane, double[,] filter, FSpecialFilterType filterType) { double[,] Result; if (filterType == FSpecialFilterType.laplacian || filterType == FSpecialFilterType.laplacofGauss) { Result = cPlane.SubArrays(ImageFilter.Filter_double(cPlane, filter, PadType.replicate)); } else { Result = ImageFilter.Filter_double(cPlane, filter, PadType.replicate); } return(Result); }
//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); }
//Sharp image after some operations with default unsharp filter public static Bitmap FastSharpImage(Bitmap img) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); var ColorList = Helpers.GetPixels(img); var Rc = ColorList[0].Color; var Gc = ColorList[1].Color; var Bc = ColorList[2].Color; var resultR = (ImageFilter.Filter_double(Rc, "unsharp")).ArrayToUint8(); var resultG = (ImageFilter.Filter_double(Gc, "unsharp")).ArrayToUint8(); var resultB = (ImageFilter.Filter_double(Bc, "unsharp")).ArrayToUint8(); image = Helpers.SetPixels(image, resultR, resultG, resultB); return(image); }
// private static void LittleEdgeMethodVariant(Bitmap img, Edgevariant variant, double threshold) { string imgExtension = GetImageInfo.Imginfo(Imageinfo.Extension); string imgName = GetImageInfo.Imginfo(Imageinfo.FileName); string defPath = GetImageInfo.MyPath("Segmentation\\Edge"); Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); int[,] result = new int[img.Height, img.Width]; double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); string outName = String.Empty; double scale = 4; // for calculating the automatic threshold var imArray = MoreHelpers.BlackandWhiteProcessHelper(img); if ((imArray.GetLength(0) > 1 && imArray.GetLength(1) > 1) && (threshold >= 0 && threshold <= 1) && !(Checks.BinaryInput(img) && threshold == 0)) { if (variant == Edgevariant.var1) { result = EdgeHelperv1(scale, imArray, threshold, ImageFilter.Dx3FWindow("Sobel"), ImageFilter.Dx3FWindow("SobelT"), 8, EdgeDirection.both, imgName, imgExtension, EdgeTempName._EdgeDefaultVar1_temp); if (threshold == 0) { outName = defPath + imgName + "_EdgeDefV1" + imgExtension; } else { outName = defPath + imgName + "_EdgeDefV1" + "Th_" + threshold.ToString() + imgExtension; } } else { result = EdgeHelperv2(scale, imArray, threshold, ImageFilter.Dx3FWindow("Sobel"), ImageFilter.Dx3FWindow("SobelT"), 8, EdgeDirection.both); if (threshold == 0) { outName = defPath + imgName + "_EdgeDefV2" + imgExtension; } else { outName = defPath + imgName + "_EdgeDefV2" + "Th_" + threshold.ToString() + imgExtension; } } image = Helpers.SetPixels(image, result, result, result); if (Depth == 8) { PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } Helpers.SaveOptions(image, outName, imgExtension); } else { Console.WriteLine("Threshold must be in range [0..1]." + "\nOr may be Binary image at input and threshold = 0 - can`t process with such condition."); } }
private static Bitmap SaltPepperFilterProcess(Bitmap img, int m, int n, double filterOrder, SaltPepperfilterType spfiltType, bool unsharp) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); List <ArraysListInt> ColorList = Helpers.GetPixels(img); var Rc = ColorList[0].Color; var Gc = ColorList[1].Color; var Bc = ColorList[2].Color; double[,] filter; int[,] resultR = new int[img.Height, img.Width]; int[,] resultG = new int[img.Height, img.Width]; int[,] resultB = new int[img.Height, img.Width]; ArrGen <double> arrGen = new ArrGen <double>(); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); if (m >= 1 && n >= 1) { switch (spfiltType) { //Arithmetic mean filtering. //help with salt noize case SaltPepperfilterType.amean: filter = ImageFilter.FspecialSize(m, n, "average"); resultR = (ImageFilter.Filter_double(Rc, filter)).ArrayToUint8(); resultG = (ImageFilter.Filter_double(Gc, filter)).ArrayToUint8(); resultB = (ImageFilter.Filter_double(Bc, filter)).ArrayToUint8(); break; //Geometric mean filtering. //help with salt noize case SaltPepperfilterType.gmean: filter = arrGen.ArrOfSingle(m, n, 1); resultR = GmeanCount(Rc, filter, m, n); resultG = GmeanCount(Gc, filter, m, n); resultB = GmeanCount(Bc, filter, m, n); break; //harmonic mean filter //help with salt noize case SaltPepperfilterType.hmean: filter = arrGen.ArrOfSingle(m, n, 1); resultR = HmeanCount(Rc, filter, m, n); resultG = HmeanCount(Gc, filter, m, n); resultB = HmeanCount(Bc, filter, m, n); break; //contraharmonic mean filter Q>0 for pepper & <0 for salt case SaltPepperfilterType.chmean: filter = arrGen.ArrOfSingle(m, n, 1); resultR = CharmeanCount(Rc, filter, filterOrder); resultG = CharmeanCount(Gc, filter, filterOrder); resultB = CharmeanCount(Bc, filter, filterOrder); break; default: resultR = Rc; resultG = Gc; resultB = Bc; break; } image = Helpers.SetPixels(image, resultR, resultG, resultB); if (unsharp) //spfiltType == SaltPepperfilterType.chmean & unsharp { image = Helpers.FastSharpImage(image); } if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } if (Depth == 1) { image = PixelFormatWorks.ImageTo1BppBitmap(image, 0.5); } } else { Console.WriteLine("m and n parameters must be positive geater or equal 1. Recommended 2 & 2 and higher. Method >SaltandPapperFilter<"); } return(image); }
//shorter int public static int[,] Filter_int(int[,] arr, string filterType) { return(Filter_int(arr, ImageFilter.Ix3FWindow(filterType), PadType.replicate)); }
//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); }
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); }
private static int[,] EdgeHelperv1(double scale, int[,] im, double threshold, double[,] filter, double[,] filterT, double fdiv, EdgeDirection direction, string fName, string extension, EdgeTempName tempName) { int[,] result = new int[im.GetLength(0), im.GetLength(1)]; int[,] b = new int[im.GetLength(0), im.GetLength(1)]; double cutoff = 0; double tresh = 0; //Sobel approximation to derivative var bx = (ImageFilter.Filter_double(im, filterT, fdiv)).ArrayToUint8(); var by = (ImageFilter.Filter_double(im, filter, fdiv)).ArrayToUint8(); //compute the magnitude if (direction == EdgeDirection.horizontal) { b = by.PowArrayElements(2).Uint8Range(); } else if (direction == EdgeDirection.vertical) { b = bx.PowArrayElements(2).Uint8Range(); } else if (direction == EdgeDirection.both || direction == EdgeDirection.def) { var bt = by.PowArrayElements(2).Uint8Range(); //temp res, part of expression b = bx.PowArrayElements(2).Uint8Range().SumArrays(bt).Uint8Range(); } else { Console.WriteLine("Wrong direction"); } fName = fName + tempName.ToString() + extension; MoreHelpers.WriteImageToFile(b, b, b, fName, "Segmentation"); if (threshold == 0) { cutoff = scale * b.Cast <int>().ToArray().Average(); tresh = Math.Sqrt(cutoff); } else { cutoff = Math.Pow(threshold, 2); } for (int i = 0; i < im.GetLength(0); i++) { for (int j = 0; j < im.GetLength(1); j++) { if (b[i, j] > cutoff) { result[i, j] = 255; } else { result[i, j] = 0; } } } 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); }
public static double[,] Filter_double(double[,] arr, string filterType) { return(Filter_double(arr, ImageFilter.Dx3FWindow(filterType), PadType.replicate)); }