Ejemplo n.º 1
0
        private static Bitmap MirrorHelper(Bitmap img, MirrorOption side)
        {
            Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb);
            double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat);

            List <ArraysListInt> ColorList = Helpers.GetPixels(img);

            int[,] resultR = new int[img.Height, img.Width];
            int[,] resultG = new int[img.Height, img.Width];
            int[,] resultB = new int[img.Height, img.Width];

            PadMyArray <int> padArr = new PadMyArray <int>();

            if (side == MirrorOption.left || side == MirrorOption.right)
            {
                resultR = padArr.PadArray(ColorList[0].Color, 0, img.Width, PadType.symmetric, Direction.pre);
                resultG = padArr.PadArray(ColorList[1].Color, 0, img.Width, PadType.symmetric, Direction.pre);
                resultB = padArr.PadArray(ColorList[2].Color, 0, img.Width, PadType.symmetric, Direction.pre);
            }
            else
            {
                resultR = padArr.PadArray(ColorList[0].Color, img.Height, 0, PadType.symmetric, Direction.pre);
                resultG = padArr.PadArray(ColorList[1].Color, img.Height, 0, PadType.symmetric, Direction.pre);
                resultB = padArr.PadArray(ColorList[2].Color, img.Height, 0, PadType.symmetric, Direction.pre);
            }

            image = Helpers.SetPixels(image, resultR, resultG, resultB);

            if (Depth == 8)
            {
                image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image);
            }
            else if (Depth == 1)
            {
                image = PixelFormatWorks.ImageTo1BppBitmap(image);
            }

            return(image);
        }
Ejemplo n.º 2
0
        private static int[,] DilateProcess(int[,] arr, int[,] structElement)
        {
            int width  = arr.GetLength(1);
            int height = arr.GetLength(0);

            int[,] temp;
            PadMyArray <int> padArr = new PadMyArray <int>();

            int[,] result = new int[height, width];
            int[,] toConv = new int[structElement.GetLength(0), structElement.GetLength(1)];

            if (arr.Length < structElement.Length || arr.GetLength(0) < structElement.GetLength(0) || arr.GetLength(1) < structElement.GetLength(1))
            {
                Console.WriteLine("Cannot operate with image, less then structure element. Returned array with zeros. Method: DilatMe");
                return(result);
            }

            int padsizeR, padsizeC = 0;

            if (structElement.GetLength(0) % 2 == 0)
            {
                padsizeR = structElement.GetLength(0) / 2;
            }
            else
            {
                padsizeR = (structElement.GetLength(0) - 1) / 2;
            }

            if (structElement.GetLength(1) % 2 == 0)
            {
                padsizeC = structElement.GetLength(1) / 2;
            }
            else
            {
                padsizeC = (structElement.GetLength(1) - 1) / 2;
            }

            //form array at different structure element size at input
            //[1; 2]
            if (structElement.GetLength(0) == 1 && structElement.GetLength(1) == 2)
            {
                temp = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);
            }

            //[1; >2 & %2 == 0]
            else if (structElement.GetLength(0) == 1 && structElement.GetLength(1) % 2 == 0)
            {
                padsizeR = 0;
                padsizeC = structElement.GetLength(1) / 2;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                temp = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[2; 1]
            else if (structElement.GetLength(0) == 2 && structElement.GetLength(1) == 1)
            {
                temp = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);
            }

            //[>2 & %2 == 0; 1]
            else if (structElement.GetLength(0) % 2 == 0 && structElement.GetLength(1) == 1)
            {
                padsizeC = 0;
                padsizeR = structElement.GetLength(0) / 2;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                temp = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[2;2]
            else if (structElement.GetLength(0) == 2 && structElement.GetLength(1) == 2)
            {
                temp = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);
            }

            //[2; >2 & %2 == 0]
            else if (structElement.GetLength(0) == 2 && structElement.GetLength(1) % 2 == 0)
            {
                padsizeC = 0;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                padsizeR = 0;
                padsizeC = structElement.GetLength(1) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                temp = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[>2 & %2 == 0; 2]
            else if (structElement.GetLength(0) % 2 == 0 && structElement.GetLength(1) == 2)
            {
                padsizeR = 0;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                padsizeC = 0;
                padsizeR = structElement.GetLength(0) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                temp = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[>2 & %2 == 0; >2 & %2 == 0]
            else if (structElement.GetLength(0) % 2 == 0 && structElement.GetLength(1) % 2 == 0)
            {
                padsizeR = 0;
                padsizeC = structElement.GetLength(1) / 2;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                temp = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);

                padsizeC = 0;
                padsizeR = structElement.GetLength(0) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                temp = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[%2 != 0; 2]
            else if (structElement.GetLength(0) % 2 != 0 && structElement.GetLength(1) == 2)
            {
                padsizeR = 0;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                padsizeC = 0;
                padsizeR = (structElement.GetLength(0) - 1) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.both);
            }

            //[%2 !=0; >2 & %2 == 0]
            else if (structElement.GetLength(0) % 2 != 0 && structElement.GetLength(1) % 2 == 0)
            {
                padsizeR = 0;
                padsizeC = structElement.GetLength(1) / 2;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                temp = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);

                padsizeC = 0;
                padsizeR = (structElement.GetLength(0) - 1) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.both);
            }

            //[2; %2 != 0]
            else if (structElement.GetLength(0) == 2 && structElement.GetLength(1) % 2 != 0)
            {
                padsizeC = 0;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                padsizeR = 0;
                padsizeC = (structElement.GetLength(1) - 1) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.both);
            }

            //[>2 & %2 == 0; %2 != 0]
            else if (structElement.GetLength(0) % 2 == 0 && structElement.GetLength(1) % 2 != 0)
            {
                padsizeC = 0;
                padsizeR = structElement.GetLength(0) / 2;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                temp = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);

                padsizeR = 0;
                padsizeC = (structElement.GetLength(1) - 1) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.both);
            }

            //[%2 != 0; %2 != 0]
            else
            {
                temp = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.both);
            }

            //obtain part of image array for dilate by structure element
            try
            {
                for (int i = 1; i <= height; i++)
                {
                    for (int j = 1; j <= width; j++)
                    {
                        for (int m = 0; m < structElement.GetLength(0); m++)
                        {
                            for (int n = 0; n < structElement.GetLength(1); n++)
                            {
                                toConv[m, n] = temp[i + m - 1, j + n - 1];
                            }
                        }

                        int[,] convolution = new int[structElement.GetLength(0), structElement.GetLength(1)];
                        convolution        = toConv.ArrayMultElements(structElement);

                        result[i - 1, j - 1] = convolution.Cast <int>().Max();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem, most likely OutOfRangeException. Dilate Method. \nHere message: " +
                                  e.Message);
            }

            return(result);
        }
Ejemplo n.º 3
0
        private static int[,] ErodeProcess(int[,] arr, int[,] structElement)
        {
            int width  = arr.GetLength(1);
            int height = arr.GetLength(0);

            int[,] temp;
            PadMyArray <int> padArr = new PadMyArray <int>();

            int[,] result = new int[height, width];
            int[,] toConv = new int[structElement.GetLength(0), structElement.GetLength(1)];

            if (arr.Length < structElement.Length || arr.GetLength(0) < structElement.GetLength(0) || arr.GetLength(1) < structElement.GetLength(1))
            {
                Console.WriteLine("Cannot operate with image, less then structure element. Returned array with zeros. Method: ErodeMe");
                return(result);
            }

            int padsizeR, padsizeC = 0;

            if (structElement.GetLength(0) % 2 == 0)
            {
                padsizeR = structElement.GetLength(0) / 2;
            }
            else
            {
                padsizeR = (structElement.GetLength(0) - 1) / 2;
            }

            if (structElement.GetLength(1) % 2 == 0)
            {
                padsizeC = structElement.GetLength(1) / 2;
            }
            else
            {
                padsizeC = (structElement.GetLength(1) - 1) / 2;
            }

            //form array at different structure element size at input
            //[1; 2]
            if (structElement.GetLength(0) == 1 && structElement.GetLength(1) == 2)
            {
                temp = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[1; >2 & %2 == 0]
            else if (structElement.GetLength(0) == 1 && structElement.GetLength(1) % 2 == 0)
            {
                padsizeR = 0;
                padsizeC = (structElement.GetLength(1) - 2) / 2;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                padsizeC = structElement.GetLength(1) - padsizeC;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[2; 1]
            else if (structElement.GetLength(0) == 2 && structElement.GetLength(1) == 1)
            {
                temp = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[>2 & %2 == 0; 1]
            else if (structElement.GetLength(0) % 2 == 0 && structElement.GetLength(1) == 1)
            {
                padsizeC = 0;
                padsizeR = (structElement.GetLength(0) - 2) / 2;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                padsizeR = structElement.GetLength(1) - padsizeR;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[2;2]
            else if (structElement.GetLength(0) == 2 && structElement.GetLength(1) == 2)
            {
                temp = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[2; >2 & %2 == 0]
            else if (structElement.GetLength(0) == 2 && structElement.GetLength(1) % 2 == 0)
            {
                padsizeC = 0;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.post);

                padsizeR = 0;
                padsizeC = (structElement.GetLength(1) - 2) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                padsizeC = structElement.GetLength(1) - padsizeC;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[>2 & %2 == 0; 2]
            else if (structElement.GetLength(0) % 2 == 0 && structElement.GetLength(1) == 2)
            {
                padsizeR = 0;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.post);

                padsizeC = 0;
                padsizeR = (structElement.GetLength(0) - 2) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                padsizeR = structElement.GetLength(0) - padsizeR;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[>2 & %2 == 0; >2 & %2 == 0]
            else if (structElement.GetLength(0) % 2 == 0 && structElement.GetLength(1) % 2 == 0)
            {
                padsizeR = 0;
                padsizeC = (structElement.GetLength(1) - 2) / 2;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                padsizeC = structElement.GetLength(1) - padsizeC;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);

                padsizeC = 0;
                padsizeR = (structElement.GetLength(0) - 2) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                padsizeR = structElement.GetLength(0) - padsizeR;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);
            }

            //[%2 != 0; 2]
            else if (structElement.GetLength(0) % 2 != 0 && structElement.GetLength(1) == 2)
            {
                padsizeR = 0;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.post);

                padsizeC = 0;
                padsizeR = (structElement.GetLength(0) - 1) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.both);
            }

            //[%2 !=0; >2 & %2 == 0]
            else if (structElement.GetLength(0) % 2 != 0 && structElement.GetLength(1) % 2 == 0)
            {
                padsizeR = 0;
                padsizeC = (structElement.GetLength(1) - 2) / 2;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                padsizeC = structElement.GetLength(1) - padsizeC;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);

                padsizeC = 0;
                padsizeR = (structElement.GetLength(0) - 1) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.both);
            }

            //[2; %2 != 0]
            else if (structElement.GetLength(0) == 2 && structElement.GetLength(1) % 2 != 0)
            {
                padsizeC = 0;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.post);

                padsizeR = 0;
                padsizeC = (structElement.GetLength(1) - 1) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.both);
            }

            //[>2 & %2 == 0; %2 != 0]
            else if (structElement.GetLength(0) % 2 == 0 && structElement.GetLength(1) % 2 != 0)
            {
                padsizeC = 0;
                padsizeR = (structElement.GetLength(0) - 2) / 2;
                temp     = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.pre);

                padsizeR = structElement.GetLength(0) - padsizeR;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.post);

                padsizeR = 0;
                padsizeC = (structElement.GetLength(1) - 1) / 2;
                temp     = padArr.PadArray(temp, padsizeR, padsizeC, PadType.replicate, Direction.both);
            }

            //[%2 != 0; %2 != 0]
            else
            {
                temp = padArr.PadArray(arr, padsizeR, padsizeC, PadType.replicate, Direction.both);
            }

            //some some some... don`t know...
            int  strSum    = structElement.Cast <int>().ToArray().Sum();
            bool nonBinary = false; if (temp.Cast <int>().ToArray().Max() > 1)

            {
                nonBinary = true;
            }

            //obtain part of image array for erode by structure element
            try
            {
                for (int i = 1; i <= height; i++)
                {
                    for (int j = 1; j <= width; j++)
                    {
                        for (int m = 0; m < structElement.GetLength(0); m++)
                        {
                            for (int n = 0; n < structElement.GetLength(1); n++)
                            {
                                toConv[m, n] = temp[i + m - 1, j + n - 1];
                            }
                        }

                        int[,] convolution = new int[structElement.GetLength(0), structElement.GetLength(1)];
                        convolution        = toConv.ArrayMultElements(structElement);

                        int[] convCast = convolution.Cast <int>().ToArray();
                        if (convCast.All(s => s == 0))
                        {
                            result[i - 1, j - 1] = 0;
                        }
                        //suppose we work with non-binary image
                        else if (nonBinary)
                        {
                            result[i - 1, j - 1] = convolution.Cast <int>().Where(f => f > 0).Min();
                        }
                        //suppose we work with binary image. It`s important! Fail result if we here count with binary array represented as 0/255
                        else if (convCast.Sum() != strSum)
                        {
                            result[i - 1, j - 1] = 0;
                        }
                        else
                        {
                            result[i - 1, j - 1] = convolution.Cast <int>().Where(f => f > 0).Min();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem, most likely OutOfRangeException. Erode method. \nHere message: " +
                                  e.Message);
            }

            return(result);
        }
Ejemplo n.º 4
0
        public static int[,] Filter_int(int[,] arr, int[,] filter, PadType padType)
        {
            int width  = arr.GetLength(1);
            int height = arr.GetLength(0);

            int[,] temp;
            PadMyArray <int> padArr = new PadMyArray <int>();

            int[,] result = new int[height, width];
            int[,] toConv = new int[filter.GetLength(0), filter.GetLength(1)];

            if (arr.Length < filter.Length)
            {
                Console.WriteLine("Cannot filter image, less than filter window. Returned array with zeros. Method: filter_int");
                return(result);
            }

            int padsizeR, padsizeC = 0;

            if (filter.GetLength(0) % 2 == 0)
            {
                padsizeR = filter.GetLength(0) / 2;
            }
            else
            {
                padsizeR = (filter.GetLength(0) - 1) / 2;
            }

            if (filter.GetLength(1) % 2 == 0)
            {
                padsizeC = filter.GetLength(1) / 2;
            }
            else
            {
                padsizeC = (filter.GetLength(1) - 1) / 2;
            }

            temp = padArr.PadArray(arr, padsizeR, padsizeC, padType, Direction.both);

            //filtering
            //obtain part of image array and convolution with filter window
            try
            {
                for (int i = 1; i <= height; i++)
                {
                    for (int j = 1; j <= width; j++)
                    {
                        for (int m = 0; m < filter.GetLength(0); m++)
                        {
                            for (int n = 0; n < filter.GetLength(1); n++)
                            {
                                toConv[m, n] = temp[i + m - 1, j + n - 1];
                            }
                        }

                        //such size coz array mult by elements, and we take part same size as filter window
                        int[,] convolution = new int[filter.GetLength(0), filter.GetLength(1)];
                        convolution        = toConv.ArrayMultElements(filter);

                        //get elemt after filter
                        if (convolution.Cast <int>().Sum() < 0)
                        {
                            result[i - 1, j - 1] = 0;
                        }
                        else if (convolution.Cast <int>().Sum() > 255)
                        {
                            result[i - 1, j - 1] = 255;
                        }
                        else
                        {
                            result[i - 1, j - 1] = convolution.Cast <int>().Sum();
                        }
                        //result[i - 1, j - 1] = convolution.Cast<int>().Sum(); //get elemt after filter
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem in filter, most likely OutOfRangeException, here message:\n" +
                                  e.Message);
            }

            return(result);
        }