Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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.");
            }
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }