//Ahtung! for HSV & Lab lost in accuracy, coz convert double->int->double //Equalize histogram for image by using separate color plane and save to file public static void Equalize(Bitmap img, HisteqColorSpace cSpace) { string imgExtension = GetImageInfo.Imginfo(Imageinfo.Extension); string imgName = GetImageInfo.Imginfo(Imageinfo.FileName); string defPath = GetImageInfo.MyPath("Sharp\\Histeq"); Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); image = EqualizeHelper(img, cSpace); string outName = defPath + imgName + HisteqVariants.ElementAt((int)cSpace) + imgExtension; Helpers.SaveOptions(image, outName, imgExtension); }
////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); }
////Equalize histogram for image by using separate color plane and return bitmap public static Bitmap EqualizeBitmap(Bitmap img, HisteqColorSpace cSpace) { return(EqualizeHelper(img, cSpace)); }