コード例 #1
0
        private static int[,] BWHitMissProcess(Bitmap img, int[,] FirstStructureElement, int[,] SecondStructureElement)
        {
            int[,] temp   = new int[img.Height, img.Width];
            int[,] result = new int[img.Height, img.Width];

            //Bitmap read binary images as 0/255
            int[,] original = Helpers.Image2BinaryArray(img);
            int[,] inverted = MoreHelpers.InvertBinaryArray(original);

            temp   = MorphOperationsCall.MorphOperationArray(original, MorphOp.Erode, FirstStructureElement);
            result = MorphOperationsCall.MorphOperationArray(inverted, MorphOp.Erode, SecondStructureElement);
            result = result.ArrayMultElements(temp);

            //make result saveble
            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;
                    }
                }
            }

            return(result);
        }
コード例 #2
0
        //create CheckerBoard

        //n - pixels per cell
        public static void CheckerBoard(int n, OutType type)
        {
            string defPath = GetImageInfo.MyPath("Rand");

            int[,] result = new int[n * 8, n * 8]; //8 -default cells
            result        = CheckerBoardHelper(n, 8, 8);

            string outName = defPath + "checkerboardClassik_" + "pixelsPerCell_" + n + ".png";

            MoreHelpers.WriteImageToFile(result, result, result, outName, type);
        }
コード例 #3
0
        private static void HitMissShapkaProcess(Bitmap img, int[,] FirstStructureElement, int[,] SecondStructureElement, OutType type)
        {
            string imgExtension = GetImageInfo.Imginfo(Imageinfo.Extension);
            string imgName      = GetImageInfo.Imginfo(Imageinfo.FileName);
            string defPath      = GetImageInfo.MyPath("Morph\\BWHitMiss");

            int[,] result = BWHitMissProcess(img, FirstStructureElement, SecondStructureElement);
            string outName = defPath + imgName + "_BWHitMiss" + imgExtension;

            MoreHelpers.WriteImageToFile(result, result, result, outName, type);
        }
コード例 #4
0
        //Some morph operations with binary image, or represented as binary
        public static void Bwmorph(Bitmap img, BwmorphOpearion operation, int repeat, OutType type)
        {
            string imgExtension = GetImageInfo.Imginfo(Imageinfo.Extension);
            string imgName      = GetImageInfo.Imginfo(Imageinfo.FileName);
            string defPath      = GetImageInfo.MyPath("Morph\\BWMorph");

            var    result  = BwmorphHelper(img, operation, repeat);
            string outName = defPath + imgName + "_" + operation.ToString() + "_repeat_" + repeat + imgExtension;

            MoreHelpers.WriteImageToFile(result, result, result, outName, type);
        }
コード例 #5
0
ファイル: FindLines.cs プロジェクト: Grimhill/Image
        //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");
            }
        }
コード例 #6
0
ファイル: Helpers.cs プロジェクト: Grimhill/Image
        //Look SomeLittle -> ImageTo1Bpp to obtain binary image with write to file
        public static int[,] Image2BinaryArray(Bitmap img)
        {
            Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format1bppIndexed);

            int[,] result = new int[img.Height, img.Width];

            double level = 0.5; //default
            var    im    = MoreHelpers.BlackandWhiteProcessHelper(img);

            result = im.Uint8ArrayToBinary(level);

            return(result);
        }
コード例 #7
0
ファイル: Helpers.cs プロジェクト: Grimhill/Image
        public static int[,] Image2BinaryArray(Bitmap img, double level)
        {
            Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format1bppIndexed);

            int[,] result = new int[img.Height, img.Width];

            if (level > 1 || level < 0)
            {
                Console.WriteLine("Level value must be in range 0..1. Set to default 0.5");
                level = 0.5;
            }

            var im = MoreHelpers.BlackandWhiteProcessHelper(img);

            result = im.Uint8ArrayToBinary(level);

            return(result);
        }
コード例 #8
0
        //rows, cols, n - pixels per cell
        public static void CheckerBoard(int n, int rows, int cols, OutType type)
        {
            string defPath = GetImageInfo.MyPath("Rand");

            int[,] result = new int[n * rows, n *cols];

            if (rows > 0 && cols > 0)
            {
                result = CheckerBoardHelper(n, rows, cols);

                string outName = defPath + "checkerboard_" + "rows_" + rows + "_cols_" + cols + "_pixelsPerCell_" + n + ".png";

                MoreHelpers.WriteImageToFile(result, result, result, outName, type);
            }
            else
            {
                Console.WriteLine("r and c must be positive values greater than 1. Method: CheckerBoard");
            }
        }
コード例 #9
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);
        }
コード例 #10
0
ファイル: SomeLittle.cs プロジェクト: Grimhill/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);
        }
コード例 #11
0
ファイル: GrayThreash.cs プロジェクト: Grimhill/Image
        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);
        }
コード例 #12
0
ファイル: Contour.cs プロジェクト: Grimhill/Image
        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);
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #15
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.");
            }
        }
コード例 #16
0
        private static int[,] EdgeHelperv1(double scale, int[,] im, double threshold, double[,] filter,
                                           double[,] filterT, double fdiv, EdgeDirection direction, string fName, string extension, EdgeTempName tempName)
        {
            int[,] result = new int[im.GetLength(0), im.GetLength(1)];
            int[,] b      = new int[im.GetLength(0), im.GetLength(1)];

            double cutoff = 0;
            double tresh  = 0;

            //Sobel approximation to derivative
            var bx = (ImageFilter.Filter_double(im, filterT, fdiv)).ArrayToUint8();
            var by = (ImageFilter.Filter_double(im, filter, fdiv)).ArrayToUint8();

            //compute the magnitude
            if (direction == EdgeDirection.horizontal)
            {
                b = by.PowArrayElements(2).Uint8Range();
            }
            else if (direction == EdgeDirection.vertical)
            {
                b = bx.PowArrayElements(2).Uint8Range();
            }
            else if (direction == EdgeDirection.both || direction == EdgeDirection.def)
            {
                var bt = by.PowArrayElements(2).Uint8Range(); //temp res, part of expression
                b = bx.PowArrayElements(2).Uint8Range().SumArrays(bt).Uint8Range();
            }
            else
            {
                Console.WriteLine("Wrong direction");
            }

            fName = fName + tempName.ToString() + extension;
            MoreHelpers.WriteImageToFile(b, b, b, fName, "Segmentation");

            if (threshold == 0)
            {
                cutoff = scale * b.Cast <int>().ToArray().Average();
                tresh  = Math.Sqrt(cutoff);
            }
            else
            {
                cutoff = Math.Pow(threshold, 2);
            }

            for (int i = 0; i < im.GetLength(0); i++)
            {
                for (int j = 0; j < im.GetLength(1); j++)
                {
                    if (b[i, j] > cutoff)
                    {
                        result[i, j] = 255;
                    }
                    else
                    {
                        result[i, j] = 0;
                    }
                }
            }

            return(result);
        }
コード例 #17
0
ファイル: ColorSpacePlane.cs プロジェクト: Grimhill/Image
        //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);
                }
            }
        }