//create CheckerBoard and return as bitmap object public static Bitmap CheckerBoardBitmap(int n, int rows, int cols, OutType type) { int[,] result = new int[n * rows, n *cols]; Bitmap image = new Bitmap(result.GetLength(1), result.GetLength(0), PixelFormat.Format24bppRgb); if (rows > 0 && cols > 0) { result = CheckerBoardHelper(n, rows, cols); image = Helpers.SetPixels(image, result, result, result); if (type == OutType.OneBpp) { image = PixelFormatWorks.ImageTo1BppBitmap(image); } else if (type == OutType.EightBpp) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } } else { Console.WriteLine("r and c must be positive values greater than 1. Method: CheckerBoard"); } return(image); }
//write to file for some methods public static void WriteImageToFile(int[,] r, int[,] g, int[,] b, string outName, OutType type) { string ImgExtension = Path.GetExtension(outName).ToLower(); if (r.Length != g.Length || r.Length != b.Length) { Console.WriteLine("Image plane arrays size dismatch in operation -> WriteImageToFile(int[,] R, int[,] G, int[,] B, string fileName, OutType type) <-"); } else { Bitmap image = new Bitmap(r.GetLength(1), g.GetLength(0), PixelFormat.Format24bppRgb); image = Helpers.SetPixels(image, r, g, b); if (type == OutType.OneBpp) { image = PixelFormatWorks.ImageTo1BppBitmap(image, 0.5); } else if (type == OutType.EightBpp) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } Helpers.SaveOptions(image, outName, ImgExtension); } }
private static void ShapkaProcess(Bitmap img, double[,] tform, int r, int c, string fileName, bool ext) { string ImgExtension = GetImageInfo.Imginfo(Imageinfo.Extension); string imgName = GetImageInfo.Imginfo(Imageinfo.FileName); string defPath = GetImageInfo.MyPath("Affine"); Bitmap image = new Bitmap(c, r, PixelFormat.Format24bppRgb); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); List <ArraysListInt> ColorList = Helpers.GetPixels(img); int[,] resultR = new int[r, c]; int[,] resultG = new int[r, c]; int[,] resultB = new int[r, c]; if (Depth == 1 || Depth == 8) { if (ext) { resultR = ExtenstionCount(ColorList[0].Color, tform, r, c); } else { resultR = AffineCount(ColorList[0].Color, tform, r, c); } resultG = resultR; resultB = resultR; } else { if (ext) { resultR = ExtenstionCount(ColorList[0].Color, tform, r, c); resultG = ExtenstionCount(ColorList[1].Color, tform, r, c); resultB = ExtenstionCount(ColorList[2].Color, tform, r, c); } else { resultR = AffineCount(ColorList[0].Color, tform, r, c); resultG = AffineCount(ColorList[1].Color, tform, r, c); resultB = AffineCount(ColorList[2].Color, tform, r, c); } } image = Helpers.SetPixels(image, resultR, resultG, resultB); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } if (Depth == 1) { image = PixelFormatWorks.ImageTo1BppBitmap(image, 0.5); } string outName = defPath + imgName + fileName + ImgExtension; Helpers.SaveOptions(image, outName, ImgExtension); }
//find lines public static void Lines(Bitmap img, LineDirection lineDirection) { string imgName = GetImageInfo.Imginfo(Imageinfo.FileName); string defPath = GetImageInfo.MyPath("Segmentation\\Lines"); Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); int[,] lineRes = new int[img.Height, img.Width]; string outName = String.Empty; var imArray = MoreHelpers.BlackandWhiteProcessHelper(img); if (imArray.GetLength(0) > 1 && imArray.GetLength(1) > 1) { //choose filter and use it switch (lineDirection) { case LineDirection.horizontal: double[,] horisontalFilter = { { -1, -1, -1 }, { 2, 2, 2 }, { -1, -1, -1 } }; lineRes = FindLineHelper(imArray, horisontalFilter); outName = defPath + imgName + "_HorisontalLine.png"; break; case LineDirection.vertical: double[,] verticalFilter = { { -1, 2, -1 }, { -1, 2, -1 }, { -1, 2, -1 } }; lineRes = FindLineHelper(imArray, verticalFilter); outName = defPath + imgName + "_VerticalLine.png"; break; case LineDirection.plus45: double[,] plus45Filter = { { -1, -1, 2 }, { -1, 2, -1 }, { 2, -1, -1 } }; lineRes = FindLineHelper(imArray, plus45Filter); outName = defPath + imgName + "_Plus45Line.png"; break; case LineDirection.minus45: double[,] minus45Filter = { { 2, -1, -1 }, { -1, 2, -1 }, { -1, -1, 2 } }; lineRes = FindLineHelper(imArray, minus45Filter); outName = defPath + imgName + "_Minus45Line.png"; break; } image = Helpers.SetPixels(image, lineRes, lineRes, lineRes); image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); Helpers.SaveOptions(image, outName, ".png"); } }
//L component CIE1976 experiment to file how it`s look like public static void FakeCIE1976LtoFile(Bitmap img) { string imgName = GetImageInfo.Imginfo(Imageinfo.FileName); string defPath = GetImageInfo.MyPath("ColorSpace\\ColorSpacePlane"); int[,] fake = ColorSpace.FakeCIE1976L(img).ArrayToUint8(); img = Helpers.SetPixels(img, fake, fake, fake); img = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(img); string outName = defPath + imgName + "_FakeCIE1976L.png"; Helpers.SaveOptions(img, outName, ".png"); }
private static void SpeckleShapkaProcess(Bitmap img, double variance) { string imgExtension = GetImageInfo.Imginfo(Imageinfo.Extension); string imgName = GetImageInfo.Imginfo(Imageinfo.FileName); string defPath = GetImageInfo.MyPath("Noise"); Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); List <ArraysListInt> ColorList = Helpers.GetPixels(img); int[,] resultR = new int[img.Height, img.Width]; int[,] resultG = new int[img.Height, img.Width]; int[,] resultB = new int[img.Height, img.Width]; if (variance > 0) { double[,] noise = Helpers.RandArray(img.Height, img.Width); if (Depth != 1 && Depth != 8) { resultR = Speckle(ColorList[0].Color, variance, noise); resultG = Speckle(ColorList[1].Color, variance, noise); resultB = Speckle(ColorList[2].Color, variance, noise); } else { Depth = 8; resultR = Speckle(ColorList[0].Color, variance, noise); resultG = resultR; resultB = resultR; } image = Helpers.SetPixels(image, resultR, resultG, resultB); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } } else { Console.WriteLine("Variance must be greater 0. Method NoiseTry.SpeckleNoise(). Return black rectangle."); } string outName = defPath + imgName + "_speckleNoise_variance_" + variance + imgExtension; Helpers.SaveOptions(image, outName, imgExtension); }
private static Bitmap MorphOperationProcess(Bitmap img, MorphOp operation, int[,] structureElement) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); bool type = true; //array, 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]; var ColorList = Helpers.GetPixels(img); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); if (Depth == 8 || Depth == 1 || Checks.BlackandWhite24bppCheck(ColorList)) { type = false; } if (type) { resultR = MorphOperationHelper(ColorList[0].Color, operation, structureElement); resultG = MorphOperationHelper(ColorList[1].Color, operation, structureElement); resultB = MorphOperationHelper(ColorList[2].Color, operation, structureElement); } else { resultR = MorphOperationHelper(ColorList[0].Color, operation, structureElement); resultG = resultR; resultB = resultR; } image = Helpers.SetPixels(image, resultR, resultG, resultB); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } if (Depth == 1) { image = PixelFormatWorks.ImageTo1BppBitmap(image, 0.5); } return(image); }
//return result as 1/8/24 bitmap public static Bitmap BwmorphBitmap(Bitmap img, BwmorphOpearion operation, int repeat, BwmorphOut type) { var result = BwmorphHelper(img, operation, repeat); Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); image = Helpers.SetPixels(image, result, result, result); if (type == BwmorphOut.OneBpp) { image = PixelFormatWorks.ImageTo1BppBitmap(image, 0.5); } else if (type == BwmorphOut.EightBpp) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } return(image); }
private static Bitmap HitMissBitmapHelper(Bitmap img, int[,] FirstStructureElement, int[,] SecondStructureElement) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); int[,] count = BWHitMissProcess(img, FirstStructureElement, SecondStructureElement); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); image = Helpers.SetPixels(image, count, count, count); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } if (Depth == 1) { image = PixelFormatWorks.ImageTo1BppBitmap(image, 0.5); } return(image); }
private static Bitmap InverseBinaryHelper(Bitmap img, OutType type) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); int[,] result = new int[img.Height, img.Width]; if (Checks.BWinput(img)) { List <ArraysListInt> ColorList = Helpers.GetPixels(img); result = MoreHelpers.InvertBinaryArray(ColorList[0].Color); if (result.Cast <int>().Max() == 1) { for (int i = 0; i < result.GetLength(0); i++) { for (int j = 0; j < result.GetLength(1); j++) { if (result[i, j] == 1) { result[i, j] = 255; } } } } image = Helpers.SetPixels(image, result, result, result); if (type == OutType.OneBpp) { image = PixelFormatWorks.ImageTo1BppBitmap(image, 0.5); } else if (type == OutType.EightBpp) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } } return(image); }
private static Bitmap MirrorHelper(Bitmap img, MirrorOption side) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); List <ArraysListInt> ColorList = Helpers.GetPixels(img); int[,] resultR = new int[img.Height, img.Width]; int[,] resultG = new int[img.Height, img.Width]; int[,] resultB = new int[img.Height, img.Width]; PadMyArray <int> padArr = new PadMyArray <int>(); if (side == MirrorOption.left || side == MirrorOption.right) { resultR = padArr.PadArray(ColorList[0].Color, 0, img.Width, PadType.symmetric, Direction.pre); resultG = padArr.PadArray(ColorList[1].Color, 0, img.Width, PadType.symmetric, Direction.pre); resultB = padArr.PadArray(ColorList[2].Color, 0, img.Width, PadType.symmetric, Direction.pre); } else { resultR = padArr.PadArray(ColorList[0].Color, img.Height, 0, PadType.symmetric, Direction.pre); resultG = padArr.PadArray(ColorList[1].Color, img.Height, 0, PadType.symmetric, Direction.pre); resultB = padArr.PadArray(ColorList[2].Color, img.Height, 0, PadType.symmetric, Direction.pre); } image = Helpers.SetPixels(image, resultR, resultG, resultB); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } else if (Depth == 1) { image = PixelFormatWorks.ImageTo1BppBitmap(image); } return(image); }
private static void MorphOperationArray2FileShapka(int[,] arr, MorphOp operation, int[,] structureElement, string elementInf) { string defPath = GetImageInfo.MyPath("Morph"); string outName = string.Empty; Bitmap image = new Bitmap(arr.GetLength(1), arr.GetLength(0), PixelFormat.Format24bppRgb); int[,] result = new int[arr.GetLength(0), arr.GetLength(1)]; result = MorphOperationHelper(arr, operation, structureElement); image = Helpers.SetPixels(image, result, result, result); if (string.IsNullOrEmpty(elementInf)) { outName = defPath + "_Array2File_" + operation.ToString() + ".png"; } else { outName = defPath + "_Array2File_" + operation.ToString() + elementInf + ".png"; } image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); Helpers.SaveOptions(image, outName, ".png"); }
private static Bitmap MakeNegativeAndBackHelper(Bitmap img) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); List <ArraysListInt> ColorList = Helpers.GetPixels(img); var Rcn = ColorList[0].Color.ConstSubArrayElements(255); //R plane var Gcn = ColorList[1].Color.ConstSubArrayElements(255); //G plane var Bcn = ColorList[2].Color.ConstSubArrayElements(255); //B plane image = Helpers.SetPixels(image, Rcn, Gcn, Bcn); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } else if (Depth == 1) { image = PixelFormatWorks.ImageTo1BppBitmap(image); } 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 GraythreshProcess(Bitmap img, Bitmap adaptOrig, bool adaptive) { 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); if (!Checks.BinaryInput(img)) { var im = MoreHelpers.BlackandWhiteProcessHelper(img); if (im.GetLength(0) > 1 && im.GetLength(1) > 1) { double T = 0.5 * (im.Cast <int>().ToArray().Min() + im.Cast <int>().ToArray().Max()); bool done = false; double Tnext = 0; List <double> tempTrue = new List <double>(); List <double> tempFalse = new List <double>(); while (!done) { for (int i = 0; i < im.GetLength(0); i++) { for (int j = 0; j < im.GetLength(1); j++) { if (im[i, j] >= T) { tempTrue.Add(im[i, j]); } else { tempFalse.Add(im[i, j]); } } } Tnext = 0.5 * (tempTrue.Average() + tempFalse.Average()); if (Math.Abs(T - Tnext) < 0.5) { done = true; } T = Tnext; tempTrue = new List <double>(); tempFalse = new List <double>(); } if (adaptive) { im = im.ArraySumWithConst(T); var origCheck = MoreHelpers.BlackandWhiteProcessHelper(adaptOrig); for (int i = 0; i < im.GetLength(0); i++) { for (int j = 0; j < im.GetLength(1); j++) { if (origCheck[i, j] > im[i, j]) { result[i, j] = 255; } else { result[i, j] = 0; } } } } else { for (int i = 0; i < im.GetLength(0); i++) { for (int j = 0; j < im.GetLength(1); j++) { if (im[i, j] > T) { result[i, j] = 255; } else { result[i, j] = 0; } } } } image = Helpers.SetPixels(image, result, result, result); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } } } else { Console.WriteLine("What did you expected to make Graythresh with binary image? Return black square."); } return(image); }
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); }
//search difference between origianl and moded image //difference writed to file public static void DifferenceLight(Bitmap imgOrig, Bitmap imgMod) { string defPath = GetImageInfo.MyPath("Rand"); Bitmap difference; int width, height; bool identical = true; double DepthOrig = System.Drawing.Image.GetPixelFormatSize(imgOrig.PixelFormat); double DepthMod = System.Drawing.Image.GetPixelFormatSize(imgMod.PixelFormat); if (imgOrig.Width != imgMod.Width || imgOrig.Height != imgMod.Height) { Console.WriteLine("Origin and modified images dimentions dismatch or them different"); } else if (DepthOrig != DepthMod) { Console.WriteLine("Can`t obtain difference between 8bit and 24bit iamge"); } else { List <ArraysListInt> cListOrigin = Helpers.GetPixels(imgOrig); List <ArraysListInt> cListMod = Helpers.GetPixels(imgMod); width = imgOrig.Width; height = imgOrig.Height; difference = new Bitmap(width, height, PixelFormat.Format24bppRgb); var Rdif = (cListOrigin[0].Color).SubArrays(cListMod[0].Color).AbsArrayElements().ArraySubWithConst(40).Uint8Range(); var Gdif = (cListOrigin[1].Color).SubArrays(cListMod[1].Color).AbsArrayElements().ArraySubWithConst(40).Uint8Range(); var Bdif = (cListOrigin[2].Color).SubArrays(cListMod[2].Color).AbsArrayElements().ArraySubWithConst(40).Uint8Range(); Rdif = Rdif.ArrayMultByConst(20).Uint8Range(); Gdif = Gdif.ArrayMultByConst(20).Uint8Range(); Bdif = Bdif.ArrayMultByConst(20).Uint8Range(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (Rdif[y, x] == 0) { difference.SetPixel(x, y, Color.White); } else { difference.SetPixel(x, y, Color.FromArgb(Rdif[y, x], Gdif[y, x], Bdif[y, x])); identical = false; } } } if (identical) { Console.WriteLine("Images are identical"); } else { string outName = defPath + "_differenceLight.png"; if (DepthOrig == 8) { difference = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(difference); } Helpers.SaveOptions(difference, outName, ".png"); } } }
//difference shown at original image by using alpha channel public static void Difference(Bitmap imgOrig, Bitmap imgMod, double coefOne, double coefTwo, double alpha) { string defPath = GetImageInfo.MyPath("Rand"); int width = imgOrig.Width; int height = imgOrig.Height; Bitmap image = new Bitmap(width, height, PixelFormat.Format24bppRgb); double DepthOrig = System.Drawing.Image.GetPixelFormatSize(imgOrig.PixelFormat); double DepthMod = System.Drawing.Image.GetPixelFormatSize(imgMod.PixelFormat); if (imgOrig.Width != imgMod.Width || imgOrig.Height != imgMod.Height) { Console.WriteLine("Origin and modified images dimentions dismatch or them different"); } else if (alpha < 0 || alpha > 1) { Console.WriteLine("Alpha must be in range [0..1]"); } else if (DepthOrig != DepthMod) { Console.WriteLine("Can`t obtain difference between 8bit and 24bit iamge"); } else { List <ArraysListInt> cListOrigin = Helpers.GetPixels(imgOrig); List <ArraysListInt> cListMod = Helpers.GetPixels(imgMod); //get difference var Rdif = (cListOrigin[0].Color).SubArrays(cListMod[0].Color).AbsArrayElements().ArraySubWithConst(coefOne).Uint8Range(); var Gdif = (cListOrigin[1].Color).SubArrays(cListMod[1].Color).AbsArrayElements().ArraySubWithConst(coefOne).Uint8Range(); var Bdif = (cListOrigin[2].Color).SubArrays(cListMod[2].Color).AbsArrayElements().ArraySubWithConst(coefOne).Uint8Range(); Rdif = Rdif.ArrayMultByConst(coefTwo).Uint8Range(); Gdif = Gdif.ArrayMultByConst(coefTwo).Uint8Range(); Bdif = Bdif.ArrayMultByConst(coefTwo).Uint8Range(); var fakeR = (cListOrigin[0].Color).ArrayToDouble().ArrayMultByConst(alpha).ArrayToUint8(); var fakeG = (cListOrigin[1].Color).ArrayToDouble().ArrayMultByConst(alpha).ArrayToUint8(); var fakeB = (cListOrigin[2].Color).ArrayToDouble().ArrayMultByConst(alpha).ArrayToUint8(); //obtain indexes with difference List <int> rIndexes = new List <int>(); var rVector = Rdif.Cast <int>().ToArray(); for (int i = 0; i < rVector.Length; i++) { if (rVector[i] > 0) { rIndexes.Add(i); } } var newrVector = fakeR.Cast <int>().ToList(); for (int i = 0; i < rVector.Length; i++) { for (int j = 0; j < rIndexes.Count(); j++) { if (i == rIndexes[j]) { newrVector[i] = rVector[i]; } } } //***************** List <int> gIndexes = new List <int>(); var gVector = Gdif.Cast <int>().ToArray(); for (int i = 0; i < gVector.Length; i++) { if (gVector[i] > 0) { gIndexes.Add(i); } } var newgVector = fakeG.Cast <int>().ToList(); for (int i = 0; i < gVector.Length; i++) { for (int j = 0; j < gIndexes.Count(); j++) { if (i == gIndexes[j]) { newgVector[i] = gVector[i]; } } } //***************** List <int> bIndexes = new List <int>(); var bVector = Bdif.Cast <int>().ToArray(); for (int i = 0; i < bVector.Length; i++) { if (bVector[i] > 0) { bIndexes.Add(i); } } var newbVector = fakeB.Cast <int>().ToList(); for (int i = 0; i < bVector.Length; i++) { for (int j = 0; j < bIndexes.Count(); j++) { if (i == bIndexes[j]) { newbVector[i] = bVector[i]; } } } ArrGen <int> d = new ArrGen <int>(); var R = d.VecorToArrayRowByRow(fakeR.GetLength(0), fakeR.GetLength(1), newrVector.ToArray()); var G = d.VecorToArrayRowByRow(fakeR.GetLength(0), fakeR.GetLength(1), newgVector.ToArray()); var B = d.VecorToArrayRowByRow(fakeR.GetLength(0), fakeR.GetLength(1), newbVector.ToArray()); //lay difference on alpha image image = Helpers.SetPixels(image, R, G, B); string outName = defPath + "Difference.png"; if (DepthOrig == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } image.Save(outName); } }
private static void SaltandPepperShapkaProcess(Bitmap img, double densitySalt, double densityPepper, SaltandPapperNoise noiseType) { string imgExtension = GetImageInfo.Imginfo(Imageinfo.Extension); string imgName = GetImageInfo.Imginfo(Imageinfo.FileName); string defPath = GetImageInfo.MyPath("Noise"); Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); string outName = string.Empty; List <ArraysListInt> ColorList = Helpers.GetPixels(img); int[,] resultR = new int[img.Height, img.Width]; int[,] resultG = new int[img.Height, img.Width]; int[,] resultB = new int[img.Height, img.Width]; if (densitySalt > 0 && densitySalt <= 1 && densityPepper > 0 && densityPepper <= 1) { int white = 1; double[,] noise = Helpers.RandArray(img.Height, img.Width); if (Depth != 1 && Depth != 8) { white = 255; resultR = SaltandPepper(ColorList[0].Color, noise, densitySalt, densityPepper, white, noiseType); resultG = SaltandPepper(ColorList[1].Color, noise, densitySalt, densityPepper, white, noiseType); resultB = SaltandPepper(ColorList[2].Color, noise, densitySalt, densityPepper, white, noiseType); } else { resultR = SaltandPepper(ColorList[0].Color, noise, densitySalt, densityPepper, white, noiseType); resultG = resultR; resultB = resultR; } image = Helpers.SetPixels(image, resultR, resultG, resultB); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } if (Depth == 1) { image = PixelFormatWorks.ImageTo1BppBitmap(image, 0.5); } } else { Console.WriteLine("Density must be in range [0..1]. Method NoiseTry.SaltandPepperNoise(). Return black rectangle."); } if (noiseType == SaltandPapperNoise.salt) { outName = defPath + imgName + "_SaltNoise_Density_" + densitySalt + imgExtension; } else if (noiseType == SaltandPapperNoise.pepper) { outName = defPath + imgName + "_PepperNoise_Density_" + densityPepper + imgExtension; } else { outName = defPath + imgName + "_SalptandPepperNoise_SaltDensity_" + densitySalt + "_PepperDensity_" + densityPepper + imgExtension; } Helpers.SaveOptions(image, outName, imgExtension); }
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 FSpecialHelper(Bitmap img, double[,] filter, FSpecialColorSpace cSpace, FSpecialFilterType 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 FSpecialColorSpace.RGB: if (Depth == 8) { var bw = FSpecialFilterHelper(ColorList[0].Color.ArrayToDouble(), filter, filterType).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 = FSpecialFilterHelper(ColorList[0].Color.ArrayToDouble(), filter, filterType).ArrayToUint8() }); //R Result.Add(new ArraysListInt() { Color = FSpecialFilterHelper(ColorList[1].Color.ArrayToDouble(), filter, filterType).ArrayToUint8() }); //G Result.Add(new ArraysListInt() { Color = FSpecialFilterHelper(ColorList[2].Color.ArrayToDouble(), filter, filterType).ArrayToUint8() }); //B } break; case FSpecialColorSpace.HSV: var hsvd = RGBandHSV.RGB2HSV(img); var hsvd_temp = FSpecialFilterHelper((hsvd[2].Color).ArrayMultByConst(100), filter, filterType); //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 FSpecialColorSpace.Lab: var labd = RGBandLab.RGB2Lab(img); var labd_temp = FSpecialFilterHelper(labd[0].Color, filter, filterType); //Filter by L - lightness Result = RGBandLab.Lab2RGB(labd_temp.ToBorderGreaterZero(255), labd[1].Color, labd[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("I don`t wont process binary image. Return black rectangle."); } return(image); }
//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 ContrastBlackWhiteProcess(Bitmap img, double low_in, double high_in, double low_out, double high_out, double gamma) { int[,] GrayC = new int[img.Height, img.Width]; Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); if (Depth == 8 || Checks.BlackandWhite24bppCheck(img)) { GrayC = MoreHelpers.BlackandWhiteProcessHelper(img); //default //gamma - linear mapping gamma = 1 //Make In intensity to values between low_in and high_in //Only positive values in range [0:1] double[] In = new double[2] { 0, 1 }; //make Out intensity to values between low_out and high_out //Only positive values in range [0:1; 0:1] //If high_out < low_out, the output image is reversed, as in a photographic negative. double[] Out = new double[2] { 0, 1 }; int[,] Cont = new int[img.Height, img.Width]; if (low_in > 1 || high_in > 1 || low_in < 0 || high_in < 0 || low_out > 1 || high_out > 1 || low_out < 0 || high_out < 0) { Console.WriteLine("low_in and high_in limits must be in range [0:1] \n" + "low_out and high_out limits must be in range [1:0] or [0 1]"); } else if (low_in >= high_in) { Console.WriteLine("low_in must be less then high_in"); } else { In = new double[2] { low_in, high_in }; //low and high specifying the contrast limits //Find In limits to contrast image Based on it`s intensity //No sence for values 0.5 and more. 0.01 - here use only default value if (low_in == 0.98 && high_in == 0.99 && low_out == 0.99 && high_out == 0.99) //so bad { In = ContrastProcess.Stretchlims(GrayC, 0.01); //number - intensity in % pixels saturated at low and high intensities of image } else if (low_out != 0 || high_out != 0) { Out = new double[2] { low_out, high_out } } ; //prevent obtain black square if low_out = high_out = 0 in this case used default Out { 0, 1 }; Cont = ContrastProcess.InlutContrast(GrayC, ContrastProcess.CountLut(In, Out, gamma)); //Convert integer values using lookup table image = Helpers.SetPixels(image, Cont, Cont, Cont); if (Depth != 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } } } else { Console.WriteLine("There non 8bit or 24bit black and white image at input. Method: ContrastBlackandWhite()"); } return(image); }
private static Bitmap BrightTransformationHelper(Bitmap img, string operation, BrightConvPlane plane, double constOne, double constTwo) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); int[,] resultR = new int[img.Height, img.Width]; int[,] resultG = new int[img.Height, img.Width]; int[,] resultB = new int[img.Height, img.Width]; List <ArraysListInt> ColorList = Helpers.GetPixels(img); if (constOne > 0 && constTwo > 0) { if (!Checks.BinaryInput(img)) { if (Depth == 8) { if (operation == "stretch") { resultR = ColorList[0].Color.StretchContrastFunc(constOne, constTwo); } else if (operation == "log") { resultR = ColorList[0].Color.LogTransformationFunc(constOne); } else if (operation == "gamma") { resultR = ColorList[0].Color.GammaCorrectionFunc(constOne, constTwo); } resultG = resultR; resultB = resultR; } else { switch (plane) { case BrightConvPlane.Rplane: //R plane if (operation == "stretch") { resultR = ColorList[0].Color.StretchContrastFunc(constOne, constTwo); } else if (operation == "log") { resultR = ColorList[0].Color.LogTransformationFunc(constOne); } else if (operation == "gamma") { resultR = ColorList[0].Color.GammaCorrectionFunc(constOne, constTwo); } resultG = ColorList[1].Color; resultB = ColorList[2].Color; break; case BrightConvPlane.Gplane: //G plane if (operation == "stretch") { resultG = ColorList[1].Color.StretchContrastFunc(constOne, constTwo); } else if (operation == "log") { resultG = ColorList[1].Color.LogTransformationFunc(constOne); } else if (operation == "gamma") { resultG = ColorList[1].Color.GammaCorrectionFunc(constOne, constTwo); } resultR = ColorList[0].Color; resultB = ColorList[2].Color; break; case BrightConvPlane.Bplane: //B plane if (operation == "stretch") { resultB = ColorList[2].Color.StretchContrastFunc(constOne, constTwo); } else if (operation == "log") { resultB = ColorList[2].Color.LogTransformationFunc(constOne); } else if (operation == "gamma") { resultB = ColorList[2].Color.GammaCorrectionFunc(constOne, constTwo); } resultR = ColorList[0].Color; resultG = ColorList[1].Color; break; case BrightConvPlane.RGB: if (operation == "stretch") { resultR = ColorList[0].Color.StretchContrastFunc(constOne, constTwo); resultG = ColorList[1].Color.StretchContrastFunc(constOne, constTwo); resultB = ColorList[2].Color.StretchContrastFunc(constOne, constTwo); } else if (operation == "log") { resultR = ColorList[0].Color.LogTransformationFunc(constOne); resultG = ColorList[1].Color.LogTransformationFunc(constOne); resultB = ColorList[2].Color.LogTransformationFunc(constOne); } else if (operation == "gamma") { resultR = ColorList[0].Color.GammaCorrectionFunc(constOne, constTwo); resultG = ColorList[1].Color.GammaCorrectionFunc(constOne, constTwo); resultB = ColorList[2].Color.GammaCorrectionFunc(constOne, constTwo); } break; } } image = Helpers.SetPixels(image, resultR, resultG, resultB); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } } else { Console.WriteLine("What did you expected to make log transformation with binary image? Return black square."); } } else { Console.WriteLine("Input constants must be positive value. 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); }
////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); }
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); }