Esempio n. 1
0
        //convert RGB image to 24bpp BW and save to file
        public static void RGBtoBlackWhite24bpp(Bitmap img)
        {
            string imgExtension = GetImageInfo.Imginfo(Imageinfo.Extension);
            string imgName      = GetImageInfo.Imginfo(Imageinfo.FileName);
            string defPath      = GetImageInfo.MyPath("Rand");

            Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb);

            if (Checks.RGBinput(img))
            {
                var gray = Helpers.RGBToGrayArray(img);
                image = Helpers.SetPixels(image, gray, gray, gray);

                string outName = defPath + "_rgbToGray24bpp" + imgExtension;
                Helpers.SaveOptions(img, outName, imgExtension);
            }
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        public static void WriteImageToFile(int[,] r, int[,] g, int[,] b, string fileName, string directoryName)
        {
            string ImgExtension = Path.GetExtension(fileName).ToLower();

            fileName = Path.GetFileNameWithoutExtension(fileName);
            Checks.DirectoryExistance(Directory.GetCurrentDirectory() + "\\Rand");

            if (r.Length != g.Length || r.Length != b.Length)
            {
                Console.WriteLine("Image plane arrays size dismatch in hsv2rgb operation -> WriteImageToFile(int[,] R, int[,] G, int[,] B) <-");
            }
            else
            {
                Bitmap image = new Bitmap(r.GetLength(1), g.GetLength(0), PixelFormat.Format24bppRgb);
                image = Helpers.SetPixels(image, r, g, b);

                string outName = Directory.GetCurrentDirectory() + "\\" + directoryName + "\\" + fileName + ImgExtension;

                Helpers.SaveOptions(image, outName, ImgExtension);
            }
        }
Esempio n. 4
0
        //obtain array of BW(gray) data for some functions
        public static int[,] BlackandWhiteProcessHelper(Bitmap img)
        {
            int[,] empty = new int[1, 1];
            int[,] im    = new int[img.Height, img.Width];
            double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat);

            if (img.Height < 3 || img.Width < 3)
            {
                Console.WriteLine("Bad input. Image less then filter 3x3");
                return(empty);
            }
            else if (Depth == 8)
            {
                im = MoreHelpers.Obtain8bppdata(img);
            }
            else if (Depth == 24 || Depth == 32)
            {
                if (Checks.BlackandWhite24bppCheck(img))
                {
                    var ColorList = Helpers.GetPixels(img);
                    im = ColorList[0].Color;
                }
                else
                {
                    im = Helpers.RGBToGrayArray(img);
                }
            }
            else if (Depth == 1)
            {
                var ColorList = Helpers.GetPixels(img);
                im = ColorList[0].Color;
            }
            else
            {
                Console.WriteLine("Bad input. Image didn`t 8bit BW or 24bit RGB/BW");
                return(empty);
            }

            return(im);
        }
Esempio n. 5
0
        //default such as input format
        public static void SaveOptions(Bitmap image, string path, string imgExtension)
        {
            double Depth = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat);

            if (Depth == 8 || Depth == 1)
            {
                imgExtension = ".png";
            }

            ImageFormat imageFormat;

            switch (imgExtension)
            {
            case ".jpg":
                imageFormat = ImageFormat.Jpeg;
                break;

            case ".jpeg":
                imageFormat = ImageFormat.Jpeg;
                break;

            case ".bmp":
                imageFormat = ImageFormat.Bmp;
                break;

            case ".png":
                imageFormat = ImageFormat.Png;
                break;

            case ".tif":
                imageFormat = ImageFormat.Tiff;
                break;

            default:
                imageFormat = ImageFormat.Jpeg;
                break;
            }

            image.Save(Checks.OutputFileNames(path), imageFormat);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        //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);
        }
Esempio n. 9
0
        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);
        }
Esempio n. 10
0
        //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);
        }
Esempio n. 11
0
        private static Bitmap ContrastRGBProcess(Bitmap img, double Rc_low_in, double Rc_high_in, double Gc_low_in, double Gc_high_in, double Bc_low_in, double Bc_high_in,
                                                 double Rc_low_out, double Rc_high_out, double Gc_low_out, double Gc_high_out, double Bc_low_out, double Bc_high_out, double gamma)
        {
            Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb);

            if (Checks.RGBinput(img))
            {
                List <ArraysListInt> ColorList = Helpers.GetPixels(img);
                string outName = String.Empty;

                //default
                //gamma - linear mapping gamma = 1

                //In values between low_in and high_in
                //Only positive values in range [0:1]
                //low and high specifying the In contrast limits
                double[] RcIn = new double[2] {
                    0, 1
                };
                double[] GcIn = new double[2] {
                    0, 1
                };
                double[] BcIn = 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[] RcOut = new double[2] {
                    0, 1
                };
                double[] GcOut = new double[2] {
                    0, 1
                };
                double[] BcOut = new double[2] {
                    0, 1
                };

                if (Rc_low_in > 1 || Rc_high_in > 1 || Gc_low_in > 1 || Gc_high_in > 1 || Bc_low_in > 1 || Bc_high_in > 1 ||
                    Rc_low_in < 0 || Rc_high_in < 0 || Gc_low_in < 0 || Gc_high_in < 0 || Bc_low_in < 0 || Bc_high_in < 0 ||
                    Rc_low_out > 1 || Rc_high_out > 1 || Gc_low_out > 1 || Gc_high_out > 1 || Bc_low_out > 1 || Bc_high_out > 1 ||
                    Rc_low_out < 0 || Rc_high_out < 0 || Gc_low_out < 0 || Gc_high_out < 0 || Bc_low_out < 0 || Bc_high_out < 0)
                {
                    Console.WriteLine("low_in limit and high_in limits must be in range [0:1]\n" +
                                      "low_out and high_out limits must be in range [0:1] or [1 0]");
                }
                else if (Rc_low_in >= Rc_high_in || Gc_low_in >= Gc_high_in || Bc_low_in >= Bc_high_in)
                {
                    Console.WriteLine("low_in limit must be less then high_in limit for each color plane");
                }
                else
                {
                    RcIn = new double[2] {
                        Rc_low_in, Rc_high_in
                    };
                    GcIn = new double[2] {
                        Gc_low_in, Gc_high_in
                    };
                    BcIn = new double[2] {
                        Bc_low_in, Bc_high_in
                    };

                    if (Rc_low_out != 0 || Rc_high_out != 0 || Gc_low_out != 0 || Gc_high_out != 0 ||
                        Bc_low_out != 0 || Bc_high_out != 0)
                    {
                        RcOut = new double[2] {
                            Rc_low_out, Rc_high_out
                        };
                        GcOut = new double[2] {
                            Gc_low_out, Gc_high_out
                        };
                        BcOut = new double[2] {
                            Bc_low_out, Bc_high_out
                        };
                    }
                    //prevent obtain black square if all low_out = high_out = 0  in this case used default Out { 0, 1 };

                    //Convert integer values using lookup table
                    var ContRc = ContrastProcess.InlutContrast(ColorList[0].Color, ContrastProcess.CountLut(RcIn, RcOut, gamma));
                    var ContGc = ContrastProcess.InlutContrast(ColorList[1].Color, ContrastProcess.CountLut(GcIn, GcOut, gamma));
                    var ContBc = ContrastProcess.InlutContrast(ColorList[2].Color, ContrastProcess.CountLut(BcIn, BcOut, gamma));

                    image = Helpers.SetPixels(image, ContRc, ContGc, ContBc);
                }
            }

            return(image);
        }
Esempio n. 12
0
        //obtain selected Color plane in file or their combo.
        public static void RGBcomponents(Bitmap img, ColorPlaneRGB colorPlane)
        {
            string imgExtension = GetImageInfo.Imginfo(Imageinfo.Extension);
            string imgName      = GetImageInfo.Imginfo(Imageinfo.FileName);
            string defPath      = GetImageInfo.MyPath("ColorSpace\\ColorSpacePlane");

            Bitmap image  = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb);
            Bitmap outRes = new Bitmap(img.Width, img.Height, PixelFormat.Format8bppIndexed);

            string outName = String.Empty;

            if (Checks.RGBinput(img))
            {
                //obtain color components
                var ColorList = Helpers.GetPixels(img);
                var Red       = ColorList[0].Color;
                var Green     = ColorList[1].Color;
                var Blue      = ColorList[2].Color;

                switch (colorPlane)
                {
                case ColorPlaneRGB.R:
                    image = MoreHelpers.SetColorPlanePixels(image, Red, ColorPlaneRGB.R);

                    outName = defPath + imgName + "_Rcplane" + imgExtension;
                    break;

                case ColorPlaneRGB.G:
                    image = MoreHelpers.SetColorPlanePixels(image, Green, ColorPlaneRGB.G);

                    outName = defPath + imgName + "_Gcplane" + imgExtension;
                    break;

                case ColorPlaneRGB.B:
                    image = MoreHelpers.SetColorPlanePixels(image, Blue, ColorPlaneRGB.B);

                    outName = defPath + imgName + "_Bcplane" + imgExtension;
                    break;

                case ColorPlaneRGB.RGB:
                    image   = MoreHelpers.SetColorPlanePixels(image, Red, ColorPlaneRGB.R);
                    outName = defPath + imgName + "_Rcplane" + imgExtension;
                    Helpers.SaveOptions(image, outName, imgExtension);

                    image   = MoreHelpers.SetColorPlanePixels(image, Green, ColorPlaneRGB.G);
                    outName = defPath + imgName + "_Gcplane" + imgExtension;
                    Helpers.SaveOptions(image, outName, imgExtension);

                    image   = MoreHelpers.SetColorPlanePixels(image, Blue, ColorPlaneRGB.B);
                    outName = defPath + imgName + "_Bcplane" + imgExtension;
                    Helpers.SaveOptions(image, outName, imgExtension);
                    break;

                case ColorPlaneRGB.RGcombo:
                    image = Helpers.SetPixels(image, Red, Green, new int[Blue.GetLength(0), Blue.GetLength(1)]);

                    outName = defPath + imgName + "_RGplanes" + imgExtension;
                    break;

                case ColorPlaneRGB.RBcombo:
                    image = Helpers.SetPixels(image, Red, new int[Blue.GetLength(0), Blue.GetLength(1)], Blue);

                    outName = defPath + imgName + "_RBplanes" + imgExtension;
                    break;

                case ColorPlaneRGB.GBcombo:
                    image = Helpers.SetPixels(image, new int[Blue.GetLength(0), Blue.GetLength(1)], Green, Blue);

                    outName = defPath + imgName + "_GBplanes" + imgExtension;
                    break;

                case ColorPlaneRGB.Rnarkoman:
                    image  = MoreHelpers.SetColorPlanePixels(image, Red, ColorPlaneRGB.R);
                    outRes = MoreHelpers.Narko8bppPalette(image);

                    outName = defPath + imgName + "_RcplaneNarkoman" + imgExtension;
                    Helpers.SaveOptions(image, outName, imgExtension);
                    break;

                case ColorPlaneRGB.Gnarkoman:
                    image  = MoreHelpers.SetColorPlanePixels(image, Green, ColorPlaneRGB.G);
                    outRes = MoreHelpers.Narko8bppPalette(image);

                    outName = defPath + imgName + "_GcplaneNarkoman" + imgExtension;
                    Helpers.SaveOptions(image, outName, imgExtension);
                    break;

                case ColorPlaneRGB.Bnarkoman:
                    image  = MoreHelpers.SetColorPlanePixels(image, Blue, ColorPlaneRGB.B);
                    outRes = MoreHelpers.Narko8bppPalette(image);

                    outName = defPath + imgName + "_BcplaneNarkoman" + imgExtension;
                    Helpers.SaveOptions(image, outName, imgExtension);
                    break;

                default:

                    break;
                }

                if (colorPlane != ColorPlaneRGB.RGB && colorPlane != ColorPlaneRGB.Rnarkoman &&
                    colorPlane != ColorPlaneRGB.Gnarkoman && colorPlane != ColorPlaneRGB.Bnarkoman)
                {
                    Helpers.SaveOptions(image, outName, imgExtension);
                }
            }
        }
Esempio n. 13
0
        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);
        }
Esempio n. 14
0
        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);
        }
Esempio n. 15
0
        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);
        }
Esempio n. 16
0
        //
        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);
        }
Esempio n. 17
0
        //
        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.");
            }
        }
Esempio n. 18
0
        private static Bitmap RGB2Gray8bppConveter(Bitmap img)
        {
            int          r, ic, oc, bmpStride, outputStride;
            ColorPalette palette;
            BitmapData   inputData, outputData;
            Bitmap       image = new Bitmap(img.Width, img.Height, PixelFormat.Format8bppIndexed);
            double       Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat);

            if (Depth == 8)
            {
                Console.WriteLine("Image already 8bit. Method RGB2Gray8bpp. Return themself");
                return(img);
            }

            if (Checks.RGBinput(img))
            {
                //Build a grayscale color Palette
                palette = image.Palette;
                for (int i = 0; i < 256; i++)
                {
                    Color tmp = Color.FromArgb(255, i, i, i);
                    palette.Entries[i] = Color.FromArgb(255, i, i, i);
                }
                image.Palette = palette;

                //Lock the images
                inputData    = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, img.PixelFormat);
                outputData   = image.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
                bmpStride    = inputData.Stride;
                outputStride = outputData.Stride;

                try
                {
                    //Traverse each pixel of the image
                    unsafe
                    {
                        byte *bmpPtr    = (byte *)inputData.Scan0.ToPointer();
                        var   outputPtr = (byte *)outputData.Scan0.ToPointer();

                        //Convert the pixel to it's luminance using the formula:
                        // L = .299*R + .587*G + .114*B
                        //Note that ic is the input column and oc is the output column
                        for (r = 0; r < img.Height; r++)
                        {
                            for (ic = oc = 0; oc < img.Width; ic += 3, ++oc)
                            {
                                outputPtr[r * outputStride + oc] = (byte)(int)
                                                                   (0.299f * bmpPtr[r * bmpStride + ic] +
                                                                    0.587f * bmpPtr[r * bmpStride + ic + 1] +
                                                                    0.114f * bmpPtr[r * bmpStride + ic + 2]);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Some problems while using unsafe code. Method: RGB2Gray8bpp. \nMessage: " + e.Message);
                }
                finally
                {
                    //Unlock the images
                    img.UnlockBits(inputData);
                    image.UnlockBits(outputData);
                }
            }

            return(image);
        }
Esempio n. 19
0
        ////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);
        }
Esempio n. 20
0
        //if direct from file. Alert: don`t recommended to use
        public static void ColorSpaceToFileDirectFromImage(Bitmap img, ColorSpaceType colorSpace)
        {
            string imgExtension = GetImageInfo.Imginfo(Imageinfo.Extension);
            string imgName      = GetImageInfo.Imginfo(Imageinfo.FileName);
            string defPath      = GetImageInfo.MyPath("ColorSpace");

            Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb);

            //back result [0 .. 255]
            int[,] colorPlaneOne   = new int[img.Height, img.Width];
            int[,] colorPlaneTwo   = new int[img.Height, img.Width];
            int[,] colorPlaneThree = new int[img.Height, img.Width];

            List <ArraysListInt> rgbResult = new List <ArraysListInt>();
            string outName = String.Empty;

            if (Checks.RGBinput(img))
            {
                switch (colorSpace.ToString())
                {
                case "rgb2hsv":
                    var hsvResult = RGBandHSV.RGB2HSV(img);

                    colorPlaneOne   = (hsvResult[0].Color).ArrayDivByConst(360).ImageDoubleToUint8();
                    colorPlaneTwo   = (hsvResult[1].Color).ImageDoubleToUint8();
                    colorPlaneThree = (hsvResult[2].Color).ImageDoubleToUint8();

                    outName = defPath + imgName + "_rgb2hsv" + imgExtension;
                    break;

                case "hsv2rgb":
                    rgbResult = RGBandHSV.HSV2RGB(img);

                    colorPlaneOne   = rgbResult[0].Color;
                    colorPlaneTwo   = rgbResult[1].Color;
                    colorPlaneThree = rgbResult[2].Color;

                    outName = defPath + imgName + "_hsv2rgb" + imgExtension;
                    break;

                case "rgb2ntsc":
                    var ntscResult = RGBandNTSC.RGB2NTSC(img);

                    colorPlaneOne   = (ntscResult[0].Color).ArrayToUint8();
                    colorPlaneTwo   = (ntscResult[1].Color).ArrayToUint8();
                    colorPlaneThree = (ntscResult[2].Color).ArrayToUint8();

                    //if we want to save rgb2ntsc result in file
                    //approximate result in file, coz we lost negative values in I and Q
                    outName = defPath + imgName + "_rgb2ntsc" + imgExtension;
                    break;

                case "ntsc2rgb":
                    rgbResult = RGBandNTSC.NTSC2RGB(img);

                    colorPlaneOne   = rgbResult[0].Color;
                    colorPlaneTwo   = rgbResult[1].Color;
                    colorPlaneThree = rgbResult[2].Color;

                    //when ntsc2rgb from file
                    //approximate result in file, coz we lost negative values in I and Q when saving ntsc result in file [0..255]
                    outName = defPath + imgName + "_ntsc2rgb" + imgExtension;
                    break;

                case "rgb2cmy":
                    var cmyResult = RGBandCMY.RGB2CMY(img);

                    colorPlaneOne   = (cmyResult[0].Color).ImageDoubleToUint8();
                    colorPlaneTwo   = (cmyResult[1].Color).ImageDoubleToUint8();
                    colorPlaneThree = (cmyResult[2].Color).ImageDoubleToUint8();

                    outName = defPath + imgName + "_rgb2cmy" + imgExtension;
                    break;

                case "cmy2rgb":
                    rgbResult = RGBandCMY.CMY2RGB(img);

                    colorPlaneOne   = rgbResult[0].Color;
                    colorPlaneTwo   = rgbResult[1].Color;
                    colorPlaneThree = rgbResult[2].Color;

                    outName = defPath + imgName + "_cmy2rgb" + imgExtension;
                    break;

                case "rgb2YCbCr":
                    var YCbCrResult = RGBandYCbCr.RGB2YCbCr(img);

                    colorPlaneOne   = (YCbCrResult[0].Color).ArrayToUint8();
                    colorPlaneTwo   = (YCbCrResult[1].Color).ArrayToUint8();
                    colorPlaneThree = (YCbCrResult[2].Color).ArrayToUint8();

                    outName = defPath + imgName + "_rgb2YCbCr" + imgExtension;
                    break;

                case "YCbCr2rgb":
                    rgbResult = RGBandYCbCr.YCbCr2RGB(img);

                    colorPlaneOne   = rgbResult[0].Color;
                    colorPlaneTwo   = rgbResult[1].Color;
                    colorPlaneThree = rgbResult[2].Color;

                    outName = defPath + imgName + "_YCbCr2rgb" + imgExtension;
                    break;

                case "rgb2xyz":
                    var xyzrgbResult = RGBandXYZ.RGB2XYZ(img);

                    colorPlaneOne   = (xyzrgbResult[0].Color).ArrayToUint8();
                    colorPlaneTwo   = (xyzrgbResult[1].Color).ArrayToUint8();
                    colorPlaneThree = (xyzrgbResult[2].Color).ArrayToUint8();

                    //approximate result in file, coz we lost values after comma in saving ntsc result in file [0..255] and heavy round them
                    outName = defPath + imgName + "_rgb2xyz" + imgExtension;
                    break;

                case "xyz2rgb":
                    rgbResult = RGBandXYZ.XYZ2RGB(img);

                    colorPlaneOne   = rgbResult[0].Color;
                    colorPlaneTwo   = rgbResult[1].Color;
                    colorPlaneThree = rgbResult[2].Color;

                    //bad when from file, coz using heavy rounded X Y Z values; when writing them to file
                    outName = defPath + imgName + "_xyz2rgb" + imgExtension;
                    break;

                case "xyz2lab":
                    var xyzlabResult = XYZandLab.XYZ2Lab(img);

                    colorPlaneOne   = (xyzlabResult[0].Color).ArrayToUint8();
                    colorPlaneTwo   = (xyzlabResult[1].Color).ArrayToUint8();
                    colorPlaneThree = (xyzlabResult[2].Color).ArrayToUint8();

                    //bad when from file, coz xyz values rounded; and lost negative value in a & b when saving in [0..255] range into file
                    outName = defPath + imgName + "_xyz2lab" + imgExtension;
                    break;

                case "lab2xyz":
                    var labxyzResult = XYZandLab.Lab2XYZ(img);

                    colorPlaneOne   = (labxyzResult[0].Color).ArrayToUint8();
                    colorPlaneTwo   = (labxyzResult[1].Color).ArrayToUint8();
                    colorPlaneThree = (labxyzResult[2].Color).ArrayToUint8();

                    //bad when from file, coz lost a and b negative value when save to file. And lost X Y Z values when round before save in [0..255] range into file
                    outName = defPath + imgName + "_lab2xyz" + imgExtension;
                    break;

                case "rgb2lab":
                    var rgblabResult = RGBandLab.RGB2Lab(img);

                    colorPlaneOne   = (rgblabResult[0].Color).ArrayToUint8();
                    colorPlaneTwo   = (rgblabResult[1].Color).ArrayToUint8();
                    colorPlaneThree = (rgblabResult[2].Color).ArrayToUint8();

                    //bad, coz lost negative value in a & b when saving in [0..255] range into file
                    outName = defPath + imgName + "_rgb2lab" + imgExtension;
                    break;

                case "rgb2lab1976":
                    var rgblab1976Result = RGBandLab.RGB2Lab1976(img);

                    colorPlaneOne   = (rgblab1976Result[0].Color).ArrayToUint8();
                    colorPlaneTwo   = (rgblab1976Result[1].Color).ArrayToUint8();
                    colorPlaneThree = (rgblab1976Result[2].Color).ArrayToUint8();

                    //bad, coz lost negative value in a & b when saving in [0..255] range into file
                    outName = defPath + imgName + "_rgb2lab1976" + imgExtension;
                    break;

                case "lab2rgb":
                    rgbResult = RGBandLab.Lab2RGB(img);

                    colorPlaneOne   = rgbResult[0].Color;
                    colorPlaneTwo   = rgbResult[1].Color;
                    colorPlaneThree = rgbResult[2].Color;

                    //very bad, coz lost a lot in converting and round everywhere...
                    outName = defPath + imgName + "_lab2rgb" + imgExtension;
                    break;

                default:
                    colorPlaneOne   = Helpers.GetPixels(img)[0].Color;
                    colorPlaneTwo   = Helpers.GetPixels(img)[1].Color;
                    colorPlaneThree = Helpers.GetPixels(img)[2].Color;

                    outName = defPath + imgName + "_defaultNonColorSpace" + imgExtension;
                    break;
                }

                image = Helpers.SetPixels(image, colorPlaneOne, colorPlaneTwo, colorPlaneThree);

                Helpers.SaveOptions(image, outName, imgExtension);
            }
        }