Ejemplo n.º 1
0
        //create diamond with dist from center point 1 to each direction
        private static int[,] DiamondProcess(int dist)
        {
            int[,] result = new int[(dist * 2) + 1, (dist * 2) + 1];
            //int[] resVect = result.Cast<int>().ToArray();

            ArrGen <int> d = new ArrGen <int>();

            if (HelpMe(dist, dist))
            {
                result = d.ArrOfSingle(result.GetLength(0), result.GetLength(0), 1);

                for (int i = 0; i < result.GetLength(0); i++)
                {
                    for (int j = 0; j < result.GetLength(0); j++)
                    {
                        if (i < dist && j < dist - i || i < dist && j > i + dist)
                        {
                            result[i, j] = 0;
                        }
                        else if (i > dist && j < i - dist || i > dist && j > result.GetLength(0) - (i - dist + 1))
                        {
                            result[i, j] = 0;
                        }
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        //create octagon with dist from center point 1 to each direction
        private static int[,] OctagonProcess(int dist)
        {
            ArrGen <int> d = new ArrGen <int>();

            int[,] result = d.ArrOfSingle((dist * 2) + 1, (dist * 2) + 1, 1);

            var baka = dist / 3 * 2;

            if (HelpMe(dist, dist))
            {
                for (int i = 0; i < result.GetLength(0); i++)
                {
                    for (int j = 0; j < result.GetLength(0); j++)
                    {
                        if (i < baka && j < baka - i || i < baka && j >= result.GetLength(0) - baka + i)
                        {
                            result[i, j] = 0;
                        }
                        else if (i >= result.GetLength(0) - baka && j <= baka - (result.GetLength(0) - i) ||
                                 i >= result.GetLength(0) - baka && j >= result.GetLength(0) - (i - (baka * 2 + 1)) - 1)
                        {
                            result[i, j] = 0;
                        }
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        //create rectangle with entred height and width
        private static int[,] RectangleProcess(int height, int width)
        {
            ArrGen <int> d = new ArrGen <int>();

            if (HelpMe(height, width))
            {
                return(d.ArrOfSingle(height, width, 1));
            }
            return(new int[height, width]);
        }
Ejemplo n.º 4
0
        //create disk with radius
        private static int[,] DiskProcess(int radius)
        {
            int[,] result = new int[(radius * 2) + 1, (radius * 2) + 1];

            ArrGen <int> d = new ArrGen <int>();

            if (HelpMe(radius, radius))
            {
                //if (radius == 0)
                //    result = new int[1; 1] { { 1 } };
                if (radius == 1)
                {
                    result = new int[3, 3] {
                        { 0, 1, 0 }, { 1, 1, 1 }, { 0, 1, 0 }
                    }
                }
                ;
                else if (radius == 3)
                {
                    result = d.ArrOfSingle(5, 5, 1);
                }
                else
                {
                    if (radius != 2)
                    {
                        result = new int[(radius * 2) - 1, (radius * 2) - 1];
                    }

                    result = d.ArrOfSingle(result.GetLength(0), result.GetLength(0), 1);

                    for (int i = 0; i < result.GetLength(0); i++)
                    {
                        for (int j = 0; j < result.GetLength(0); j++)
                        {
                            if (i == 0 || i == result.GetLength(0) - 1)
                            {
                                if (j < 2 || j > result.GetLength(0) - 3)
                                {
                                    result[i, j] = 0;
                                }
                            }
                            else if (i == 1 || i == result.GetLength(0) - 2)
                            {
                                if (j < 1 || j > result.GetLength(0) - 2)
                                {
                                    result[i, j] = 0;
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
        //create square with entered square side size
        private static int[,] SquareProcess(int size)
        {
            ArrGen <int> d = new ArrGen <int>();

            if (HelpMe(size, size))
            {
                return(d.ArrOfSingle(size, size, 1));
            }

            return(new int[size, size]);
        }
Ejemplo n.º 6
0
        //create line with lenght and diretion
        private static int[,] LineProcess(int size, LineStructElementDegree lineType)
        {
            int[,] resultH = new int [1, size];
            int[,] resultV = new int [size, 1];
            int[,] tempRes = new int [size, size];
            ArrGen <int> d = new ArrGen <int>();

            if (HelpMe(size, size))
            {
                switch (lineType)
                {
                case LineStructElementDegree.horizontal:
                    resultH = d.ArrOfSingle(1, size, 1);
                    return(resultH);

                case LineStructElementDegree.vertical:
                    resultV = d.ArrOfSingle(size, 1, 1);
                    return(resultV);

                case LineStructElementDegree.plus45:
                    int m = size - 1;
                    int n = 0;
                    for (int i = 0; i < size; i++)
                    {
                        for (int j = 0; j < size; j++)
                        {
                            if (i == n && j == m)
                            {
                                tempRes[i, j] = 1;
                            }
                        }
                        m--;
                        n++;
                    }
                    return(tempRes);

                case LineStructElementDegree.minus45:
                    for (int i = 0; i < size; i++)
                    {
                        for (int j = 0; j < size; j++)
                        {
                            if (i == j)
                            {
                                tempRes[i, j] = 1;
                            }
                        }
                    }
                    return(tempRes);
                }
            }

            return(tempRes);
        }
Ejemplo n.º 7
0
        private static int[,] SaltandPepper(int[,] arr, double[,] noise, double densitySalt, double densityPepper, int white, SaltandPapperNoise noiseType)
        {
            int[,] result = new int[arr.GetLength(0), arr.GetLength(1)];
            ArrGen <int> d = new ArrGen <int>();

            var vectorArr   = arr.Cast <int>().ToArray();
            var vectorNoise = noise.Cast <double>().ToArray();

            switch (noiseType)
            {
            case SaltandPapperNoise.salt:
                for (int i = 0; i < noise.Length; i++)
                {
                    if (vectorNoise[i] >= (1 - densitySalt / 2))
                    {
                        vectorArr[i] = white;
                    }
                }
                result = d.VecorToArrayRowByRow(arr.GetLength(0), arr.GetLength(1), vectorArr);
                break;

            case SaltandPapperNoise.pepper:
                for (int i = 0; i < noise.Length; i++)
                {
                    if (vectorNoise[i] <= densityPepper / 2)
                    {
                        vectorArr[i] = 0;
                    }
                }
                result = d.VecorToArrayRowByRow(arr.GetLength(0), arr.GetLength(1), vectorArr);
                break;

            case SaltandPapperNoise.saltandpepper:
                for (int i = 0; i < noise.Length; i++)
                {
                    if (vectorNoise[i] >= (1 - densitySalt / 2))
                    {
                        vectorArr[i] = white;
                    }
                }
                for (int i = 0; i < noise.Length; i++)
                {
                    if (vectorNoise[i] <= densityPepper / 2)
                    {
                        vectorArr[i] = 0;
                    }
                }
                result = d.VecorToArrayRowByRow(arr.GetLength(0), arr.GetLength(1), vectorArr);
                break;
            }

            return(result);
        }
Ejemplo n.º 8
0
        //process histogram by applying lut
        private static int[,] HisteqHelper(int[,] cPlane)
        {
            //gen array of the same values
            ArrGen <double> d = new ArrGen <double>();

            double    eps    = 2.2204 * Math.Pow(10, -16);
            const int nDef   = 64;  //default
            const int nUint8 = 256; //for uint8 image. At this time operationg only with them

            int[,] Result;

            //hgram contain integer counts for equally spaced bins with intensity values
            //here count it based on input array

            //array on ones 1 x nDef mult by input array length dived by nDef
            //obtain vector 1 x nDef size
            var hgram = (d.ArrOfSingle(1, nDef, 1).Cast <double>().ToArray()).VectorMultByConst((cPlane.ArrayToDouble().Cast <double>().ToArray().Length) / (double)nDef);

            //Normalize hgram. hgram = hgram mult by input array dived by hgram elements sum
            hgram = hgram.VectorMultByConst((cPlane.ArrayToDouble().Cast <double>().ToArray().Length) / (hgram.Cast <double>().ToArray().Sum()));
            var m = hgram.Length;

            //compute Cumulative and Histogram
            var imhist = ImHist(cPlane); //imhist only for uint8 arrays realization ([0..255])

            double[] CumuSum = new double[nUint8];
            for (int i = 0; i < nUint8; i++)
            {
                if (i == 0)
                {
                    CumuSum[i] = imhist[i];
                }
                else
                {
                    CumuSum[i] = imhist[i] + CumuSum[i - 1];
                }
            }

            //cumulative distribution function
            //create Transformation To Intensity Image
            var cumdInput = hgram.VectorMultByConst((cPlane.ArrayToDouble().Cast <double>().ToArray().Length) / (hgram.Cast <double>().ToArray().Sum()));

            double[] cumd = new double[nDef];
            for (int i = 0; i < nDef; i++)
            {
                if (i == 0)
                {
                    cumd[i] = cumdInput[i];
                }
                else
                {
                    cumd[i] = cumdInput[i] + cumd[i - 1];
                }
            }

            //Create transformation to an intensity image by minimizing the error
            //between desired and actual cumulative histogram.
            //tol saturates equal fractions at low and high pixel values

            //sory for this wtf code
            var z       = new double[2 * imhist.Length];
            var partOne = imhist.ToList();

            partOne[nUint8 - 1] = 0;
            partOne.ToArray().CopyTo(z, 0);

            var partTwo = imhist.ToList();

            partTwo[0] = 0;
            partTwo.ToArray().CopyTo(z, imhist.Length);

            var tolPart = d.VecorToArrayRowByRow(1, nUint8, (d.VecorToArrayRowByRow(2, nUint8, z)).MinInColumns());

            var tol = (d.ArrOfSingle(m, 1, 1)).MultArrays(tolPart.ArrayDivByConst(2));

            //Error
            var cumdArr    = d.TransposeArray(d.VecorToArrayRowByRow(1, nDef, cumd));
            var CumuSumArr = d.VecorToArrayRowByRow(1, nUint8, CumuSum);

            var errPartOne = cumdArr.MultArrays(d.ArrOfSingle(1, nUint8, 1));

            var errPartTwo = (d.ArrOfSingle(m, 1, 1)).MultArrays(CumuSumArr);

            var err = errPartOne.SubArrays(errPartTwo).SumArrays(tol);

            //Find all places with error. Yep; wtf code continues!
            List <double> erroIndexes = new List <double>();

            //present array as vector col by col. Coz this is cool. Cast dont need!
            //Sorry; stupid copy step by step matlab logic
            //var errVector = d.ArrayToVectorColByCol(err);

            //with cast faster a little?
            var errVector = err.Cast <double>().ToArray();

            for (int i = 0; i < errVector.Length; i++)
            {
                if (errVector[i] < -(cPlane.Length * Math.Sqrt(eps)))
                {
                    erroIndexes.Add(i);
                }
            }

            //f**k the error values
            double[,] rest = new double[err.GetLength(0), err.GetLength(1)];
            if (erroIndexes.Any())
            {
                var newErrVector = errVector.ToList();
                var newValue     = (d.ArrOfSingle(erroIndexes.Count(), 1, 1).Cast <double>().ToArray()).VectorMultByConst(cPlane.Length); //coz erroIndexes - vector

                for (int i = 0; i < errVector.Length; i++)
                {
                    for (int j = 0; j < erroIndexes.Count(); j++)
                    {
                        if (i == erroIndexes[j])
                        {
                            newErrVector[i] = cPlane.Length; //newValue[j];
                        }
                    }
                }

                rest = d.VecorToArrayRowByRow(rest.GetLength(0), rest.GetLength(1), newErrVector.ToArray());
            }
            else
            {
                //err vector back to array
                rest = err;
            }

            //find row number of mins in each col rest array
            double[] lut = new double[rest.GetLength(1)];

            double min;
            int    index = 0;

            for (int i = 0; i < rest.GetLength(1); i++)
            {
                min   = rest[0, i];
                index = 0;
                for (int j = 0; j < rest.GetLength(0); j++)
                {
                    if (min > rest[j, i])
                    {
                        min = rest[j, i];

                        index = j;
                    }
                }
                lut[i] = index;
            }

            //count lut
            for (int i = 0; i < lut.Length; i++)
            {
                lut[i] = lut[i] / (m - 1);
            }

            Result = (InlutHisteq(cPlane, lut)).ArrayToUint8();

            return(Result);
        }
Ejemplo n.º 9
0
        private static Bitmap SaltPepperFilterProcess(Bitmap img, int m, int n, double filterOrder, SaltPepperfilterType spfiltType, bool unsharp)
        {
            Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb);

            List <ArraysListInt> ColorList = Helpers.GetPixels(img);
            var Rc = ColorList[0].Color;
            var Gc = ColorList[1].Color;
            var Bc = ColorList[2].Color;

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

            ArrGen <double> arrGen = new ArrGen <double>();
            double          Depth  = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat);

            if (m >= 1 && n >= 1)
            {
                switch (spfiltType)
                {
                //Arithmetic mean filtering.
                //help with salt noize
                case SaltPepperfilterType.amean:
                    filter = ImageFilter.FspecialSize(m, n, "average");

                    resultR = (ImageFilter.Filter_double(Rc, filter)).ArrayToUint8();
                    resultG = (ImageFilter.Filter_double(Gc, filter)).ArrayToUint8();
                    resultB = (ImageFilter.Filter_double(Bc, filter)).ArrayToUint8();
                    break;

                //Geometric mean filtering.
                //help with salt noize
                case SaltPepperfilterType.gmean:
                    filter = arrGen.ArrOfSingle(m, n, 1);

                    resultR = GmeanCount(Rc, filter, m, n);
                    resultG = GmeanCount(Gc, filter, m, n);
                    resultB = GmeanCount(Bc, filter, m, n);
                    break;

                //harmonic mean filter
                //help with salt noize
                case SaltPepperfilterType.hmean:
                    filter = arrGen.ArrOfSingle(m, n, 1);

                    resultR = HmeanCount(Rc, filter, m, n);
                    resultG = HmeanCount(Gc, filter, m, n);
                    resultB = HmeanCount(Bc, filter, m, n);
                    break;

                //contraharmonic mean filter Q>0 for pepper & <0 for salt
                case SaltPepperfilterType.chmean:
                    filter = arrGen.ArrOfSingle(m, n, 1);

                    resultR = CharmeanCount(Rc, filter, filterOrder);
                    resultG = CharmeanCount(Gc, filter, filterOrder);
                    resultB = CharmeanCount(Bc, filter, filterOrder);
                    break;

                default:
                    resultR = Rc; resultG = Gc; resultB = Bc;
                    break;
                }

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

                if (unsharp)  //spfiltType == SaltPepperfilterType.chmean & unsharp
                {
                    image = Helpers.FastSharpImage(image);
                }

                if (Depth == 8)
                {
                    image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image);
                }
                if (Depth == 1)
                {
                    image = PixelFormatWorks.ImageTo1BppBitmap(image, 0.5);
                }
            }
            else
            {
                Console.WriteLine("m and n parameters must be positive geater or equal 1. Recommended 2 & 2 and higher. Method >SaltandPapperFilter<");
            }

            return(image);
        }
Ejemplo n.º 10
0
        private static double[,] Conv2Process(double[,] inArray, double[] hrow, double[] hcol, Convback convback)
        {
            ArrGen <double> d = new ArrGen <double>();

            double[,] result = new double[inArray.GetLength(0) + hrow.Length - 1, inArray.GetLength(1) + hcol.Length - 1];

            List <double> tempColList = new List <double>();
            List <double> tempRowList = new List <double>();
            List <double> tempResults = new List <double>();

            var arrayCols = d.ReturnListof2DArrayCols(inArray);

            //first convolves columns with col vector
            for (int i = 0; i < inArray.GetLength(1); i++)
            {
                tempResults = Convolution.Conv(arrayCols[i].Vect, hrow, Convback.full).ToList();
                tempColList.AddRange(tempResults);
            }

            var colsResult = d.VecorToArrayColbyCol(result.GetLength(0), inArray.GetLength(1), tempColList.ToArray());
            var arrayRows  = d.ReturnListof2DArrayRows(colsResult);

            //convolves rows of the result with row vector
            for (int i = 0; i < colsResult.GetLength(0); i++)
            {
                tempResults = Convolution.Conv(arrayRows[i].Vect, hcol, Convback.full).ToList();
                tempRowList.AddRange(tempResults);
            }

            var convResult = d.VecorToArrayRowByRow(result.GetLength(0), result.GetLength(1), tempRowList.ToArray());

            result = convResult;

            if (convback == Convback.same)
            {
                int indexCols = 0;
                int indexRows = 0;

                if (hcol.Length % 2 != 0)
                {
                    indexCols = (hcol.Length - 1) / 2;
                }
                else
                {
                    indexCols = hcol.Length / 2;
                }

                if (hrow.Length % 2 != 0)
                {
                    indexRows = (hrow.Length - 1) / 2;
                }
                else
                {
                    indexRows = hrow.Length / 2;
                }

                tempResults = new List <double>();
                var RowsCut = d.ArrayToVectorColByCol(convResult).ToList();
                for (int i = 0; i < convResult.GetLength(1); i++)
                {
                    int index  = convResult.GetLength(0) * i + indexRows;
                    var rowPal = RowsCut.GetRange(index, inArray.GetLength(0));
                    tempResults.AddRange(rowPal);
                }

                var sameRows = d.VecorToArrayColbyCol(inArray.GetLength(0), convResult.GetLength(1), tempResults.ToArray());

                tempResults = new List <double>();
                var ColsCut = sameRows.Cast <double>().ToList();
                for (int i = 0; i < inArray.GetLength(0); i++)
                {
                    int index  = convResult.GetLength(1) * i + indexCols;
                    var colPal = ColsCut.GetRange(index, inArray.GetLength(1));
                    tempResults.AddRange(colPal);
                }

                result = new double[inArray.GetLength(0), inArray.GetLength(1)];
                result = d.VecorToArrayRowByRow(inArray.GetLength(0), inArray.GetLength(1), tempResults.ToArray());
            }

            result.DecimalCorrection();
            return(result);
        }
Ejemplo n.º 11
0
        //forgive for such implementation. Slow and ugly...
        private static double[,] Conv2Process(double[,] inArray, double[,] convArray, Convback convback)
        {
            ArrGen <double> d = new ArrGen <double>();

            double[,] result = new double[inArray.GetLength(0) + convArray.GetLength(0) - 1, inArray.GetLength(1) + convArray.GetLength(1) - 1];

            List <double> tempResults = new List <double>();

            double[] tempResult = new double[result.GetLength(0)];

            List <VectorsListDouble> aRListIn  = new List <VectorsListDouble>();
            List <VectorsListDouble> aRListOut = new List <VectorsListDouble>();
            List <VectorsListDouble> temp      = new List <VectorsListDouble>();
            List <VectorsListDouble> tempRes   = new List <VectorsListDouble>();

            int sameRow = convArray.GetLength(0); int sameRowPart = inArray.GetLength(0);
            int sameCol = convArray.GetLength(1); int sameColPart = inArray.GetLength(1);

            //lazy cheat
            if (inArray.GetLength(1) < convArray.GetLength(1))
            {
                var tempIn = inArray;
                inArray   = new double[convArray.GetLength(0), convArray.GetLength(1)];
                inArray   = convArray;
                convArray = new double[inArray.GetLength(0), inArray.GetLength(1)];
                convArray = tempIn;
            }

            var arrayCols = d.ReturnListof2DArrayCols(inArray);
            var convCols  = d.ReturnListof2DArrayCols(convArray);

            //conv col by col
            for (int i = 0; i < convArray.GetLength(1); i++)
            {
                for (int j = 0; j < inArray.GetLength(1); j++)
                {
                    tempResult = Convolution.Conv(arrayCols[j].Vect, convCols[i].Vect, Convback.full);
                    aRListIn.Add(new VectorsListDouble()
                    {
                        Vect = tempResult
                    });
                }
            }

            if (convArray.GetLength(1) == 1)
            {
                aRListOut = aRListIn;
            }
            else
            {
                var first = aRListIn[0].Vect;
                var last  = aRListIn[aRListIn.Count - 1].Vect;
                aRListOut.Add(new VectorsListDouble()
                {
                    Vect = first
                });

                aRListIn.RemoveAt(0); aRListIn.RemoveAt(aRListIn.Count - 1);

                int maxAmount      = convArray.GetLength(1);  //максимальное число суммируемых стобцов в один //max number of cols to one sum
                int maxSumCount    = 0;                       //количество maxAmount  //count maxAmount
                int simpleSumCount = 0;                       //количество остальных сумм //count another sums
                int sumCount       = result.GetLength(1) - 2; //кроличество всех сумм //count all sums
                int step           = 0;                       //шаг через который суммируются //sum step

                if (convArray.GetLength(1) == 2)
                {
                    simpleSumCount = inArray.GetLength(1) - 1;
                    step           = inArray.GetLength(1) - 1;

                    for (int i = 0; i < sumCount; i++)
                    {
                        aRListOut.Add(new VectorsListDouble()
                        {
                            Vect = aRListIn[i].Vect.SumVectors(aRListIn[i + step].Vect)
                        });
                    }
                }
                else
                {
                    step           = inArray.GetLength(1) - 2 + 1;
                    maxSumCount    = Math.Abs(inArray.GetLength(1) - convArray.GetLength(1)) + 1;
                    simpleSumCount = result.GetLength(1) - maxSumCount - 2;

                    tempResult = new double[result.GetLength(0)];
                    for (int i = 0; i < simpleSumCount / 2; i++)
                    {
                        int baka = i + 1;
                        int c    = 0;
                        for (int j = 0; j <= baka; j++)
                        {
                            temp.Add(new VectorsListDouble()
                            {
                                Vect = aRListIn[i + c].Vect
                            });
                            c = c + step;
                        }

                        for (int k = 0; k < temp.Count; k++)
                        {
                            tempResult = tempResult.SumVectors(temp[k].Vect);
                        }
                        aRListOut.Add(new VectorsListDouble()
                        {
                            Vect = tempResult
                        });

                        temp = new List <VectorsListDouble>(); tempResult = new double[result.GetLength(0)];
                    }

                    for (int i = 0; i < maxSumCount; i++)
                    {
                        int maxstep       = 0;
                        int maxFirstIndex = simpleSumCount / 2;
                        for (int j = 0; j < maxAmount; j++)
                        {
                            temp.Add(new VectorsListDouble()
                            {
                                Vect = aRListIn[i + maxFirstIndex + maxstep].Vect
                            });
                            maxstep = maxstep + step;
                        }

                        for (int k = 0; k < temp.Count; k++)
                        {
                            tempResult = tempResult.SumVectors(temp[k].Vect);
                        }
                        aRListOut.Add(new VectorsListDouble()
                        {
                            Vect = tempResult
                        });

                        temp = new List <VectorsListDouble>(); tempResult = new double[result.GetLength(0)];
                    }

                    for (int i = 0; i < simpleSumCount / 2; i++)
                    {
                        int baka = i + 1;
                        int c    = 0;
                        int kap  = aRListIn.Count - 1;
                        for (int j = 0; j <= baka; j++)
                        {
                            temp.Add(new VectorsListDouble()
                            {
                                Vect = aRListIn[kap - i - c].Vect
                            });
                            c = c + step;
                        }

                        for (int k = 0; k < temp.Count; k++)
                        {
                            tempResult = tempResult.SumVectors(temp[k].Vect);
                        }

                        tempRes.Add(new VectorsListDouble()
                        {
                            Vect = tempResult
                        });

                        temp = new List <VectorsListDouble>(); tempResult = new double[result.GetLength(0)];
                    }

                    for (int i = tempRes.Count - 1; i >= 0; i--)
                    {
                        aRListOut.Add(new VectorsListDouble()
                        {
                            Vect = tempRes[i].Vect
                        });
                    }
                }
                aRListOut.Add(new VectorsListDouble()
                {
                    Vect = last
                });
            }

            for (int i = 0; i < aRListOut.Count; i++)
            {
                var getCol = aRListOut[i].Vect;
                tempResults.AddRange(getCol);
            }

            var convResult = d.VecorToArrayColbyCol(result.GetLength(0), result.GetLength(1), tempResults.ToArray());

            result = convResult;

            if (convback == Convback.same)
            {
                int indexCols = 0;
                int indexRows = 0;

                if (sameCol % 2 != 0)
                {
                    indexCols = (sameCol - 1) / 2;
                }
                else
                {
                    indexCols = sameCol / 2;
                }

                if (sameRow % 2 != 0)
                {
                    indexRows = (sameRow - 1) / 2;
                }
                else
                {
                    indexRows = sameRow / 2;
                }

                tempResults = new List <double>();
                var RowsCut = d.ArrayToVectorColByCol(convResult).ToList();
                for (int i = 0; i < convResult.GetLength(1); i++)
                {
                    int index  = convResult.GetLength(0) * i + indexRows;
                    var rowPal = RowsCut.GetRange(index, sameRowPart);
                    tempResults.AddRange(rowPal);
                }

                var sameRows = d.VecorToArrayColbyCol(sameRowPart, convResult.GetLength(1), tempResults.ToArray());

                tempResults = new List <double>();
                var ColsCut = sameRows.Cast <double>().ToList();
                for (int i = 0; i < sameRowPart; i++)
                {
                    int index  = convResult.GetLength(1) * i + indexCols;
                    var colPal = ColsCut.GetRange(index, sameColPart);
                    tempResults.AddRange(colPal);
                }

                result = new double[sameRowPart, sameColPart];
                result = d.VecorToArrayRowByRow(sameRowPart, sameColPart, tempResults.ToArray());
            }

            result.DecimalCorrection();
            return(result);
        }
Ejemplo n.º 12
0
        //difference shown at original image by using alpha channel
        public static void Difference(Bitmap imgOrig, Bitmap imgMod, double coefOne, double coefTwo, double alpha)
        {
            string defPath = GetImageInfo.MyPath("Rand");

            int    width  = imgOrig.Width;
            int    height = imgOrig.Height;
            Bitmap image  = new Bitmap(width, height, PixelFormat.Format24bppRgb);

            double DepthOrig = System.Drawing.Image.GetPixelFormatSize(imgOrig.PixelFormat);
            double DepthMod  = System.Drawing.Image.GetPixelFormatSize(imgMod.PixelFormat);

            if (imgOrig.Width != imgMod.Width || imgOrig.Height != imgMod.Height)
            {
                Console.WriteLine("Origin and modified images dimentions dismatch or them different");
            }
            else if (alpha < 0 || alpha > 1)
            {
                Console.WriteLine("Alpha must be in range [0..1]");
            }
            else if (DepthOrig != DepthMod)
            {
                Console.WriteLine("Can`t obtain difference between 8bit and 24bit iamge");
            }
            else
            {
                List <ArraysListInt> cListOrigin = Helpers.GetPixels(imgOrig);
                List <ArraysListInt> cListMod    = Helpers.GetPixels(imgMod);

                //get difference
                var Rdif = (cListOrigin[0].Color).SubArrays(cListMod[0].Color).AbsArrayElements().ArraySubWithConst(coefOne).Uint8Range();
                var Gdif = (cListOrigin[1].Color).SubArrays(cListMod[1].Color).AbsArrayElements().ArraySubWithConst(coefOne).Uint8Range();
                var Bdif = (cListOrigin[2].Color).SubArrays(cListMod[2].Color).AbsArrayElements().ArraySubWithConst(coefOne).Uint8Range();

                Rdif = Rdif.ArrayMultByConst(coefTwo).Uint8Range();
                Gdif = Gdif.ArrayMultByConst(coefTwo).Uint8Range();
                Bdif = Bdif.ArrayMultByConst(coefTwo).Uint8Range();

                var fakeR = (cListOrigin[0].Color).ArrayToDouble().ArrayMultByConst(alpha).ArrayToUint8();
                var fakeG = (cListOrigin[1].Color).ArrayToDouble().ArrayMultByConst(alpha).ArrayToUint8();
                var fakeB = (cListOrigin[2].Color).ArrayToDouble().ArrayMultByConst(alpha).ArrayToUint8();

                //obtain indexes with difference
                List <int> rIndexes = new List <int>();

                var rVector = Rdif.Cast <int>().ToArray();
                for (int i = 0; i < rVector.Length; i++)
                {
                    if (rVector[i] > 0)
                    {
                        rIndexes.Add(i);
                    }
                }

                var newrVector = fakeR.Cast <int>().ToList();
                for (int i = 0; i < rVector.Length; i++)
                {
                    for (int j = 0; j < rIndexes.Count(); j++)
                    {
                        if (i == rIndexes[j])
                        {
                            newrVector[i] = rVector[i];
                        }
                    }
                }

                //*****************

                List <int> gIndexes = new List <int>();

                var gVector = Gdif.Cast <int>().ToArray();
                for (int i = 0; i < gVector.Length; i++)
                {
                    if (gVector[i] > 0)
                    {
                        gIndexes.Add(i);
                    }
                }

                var newgVector = fakeG.Cast <int>().ToList();
                for (int i = 0; i < gVector.Length; i++)
                {
                    for (int j = 0; j < gIndexes.Count(); j++)
                    {
                        if (i == gIndexes[j])
                        {
                            newgVector[i] = gVector[i];
                        }
                    }
                }

                //*****************

                List <int> bIndexes = new List <int>();

                var bVector = Bdif.Cast <int>().ToArray();
                for (int i = 0; i < bVector.Length; i++)
                {
                    if (bVector[i] > 0)
                    {
                        bIndexes.Add(i);
                    }
                }

                var newbVector = fakeB.Cast <int>().ToList();
                for (int i = 0; i < bVector.Length; i++)
                {
                    for (int j = 0; j < bIndexes.Count(); j++)
                    {
                        if (i == bIndexes[j])
                        {
                            newbVector[i] = bVector[i];
                        }
                    }
                }

                ArrGen <int> d = new ArrGen <int>();

                var R = d.VecorToArrayRowByRow(fakeR.GetLength(0), fakeR.GetLength(1), newrVector.ToArray());
                var G = d.VecorToArrayRowByRow(fakeR.GetLength(0), fakeR.GetLength(1), newgVector.ToArray());
                var B = d.VecorToArrayRowByRow(fakeR.GetLength(0), fakeR.GetLength(1), newbVector.ToArray());
                //lay difference on alpha image

                image = Helpers.SetPixels(image, R, G, B);
                string outName = defPath + "Difference.png";

                if (DepthOrig == 8)
                {
                    image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image);
                }

                image.Save(outName);
            }
        }
Ejemplo n.º 13
0
        //create CheckerBoard process
        private static int[,] CheckerBoardHelper(int n, int r, int c)
        {
            int[] wCell = new int[n];
            int[] bCell = new int[n];
            int[,] result = new int[n * r, n *c];

            for (int i = 0; i < n; i++)
            {
                wCell[i] = 255;
            }

            ArrGen <int> d = new ArrGen <int>();

            int[] temp1   = new int[n * c];
            int[] temp2   = new int[n * c];
            var   resVect = result.Cast <int>().ToArray();

            for (int i = 0; i < temp1.Length / n; i++)
            {
                if (i % 2 == 0)
                {
                    bCell.CopyTo(temp1, bCell.Length * i);
                }
                else
                {
                    wCell.CopyTo(temp1, wCell.Length * i);
                }
            }

            for (int i = 0; i < temp2.Length / n; i++)
            {
                if (i % 2 == 0)
                {
                    wCell.CopyTo(temp2, wCell.Length * i);
                }
                else
                {
                    bCell.CopyTo(temp2, bCell.Length * i);
                }
            }

            int count  = 0;
            int countn = 0;

            if (r % 2 == 0)
            {
                for (int i = 0; i < r / 2; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        temp1.CopyTo(resVect, temp1.Length * (i + countn));
                        countn++;
                    }

                    //count++;

                    for (int j = 0; j < n; j++)
                    {
                        temp2.CopyTo(resVect, temp2.Length * (i + countn));
                        countn++;
                    }

                    countn--;
                }
            }

            else
            {
                for (int i = 0; i < (r - 1) / 2; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        temp1.CopyTo(resVect, temp1.Length * (i + count));
                        count++;
                    }

                    //count++;

                    for (int j = 0; j < n; j++)
                    {
                        temp2.CopyTo(resVect, temp2.Length * (i + count));
                        count++;
                    }

                    count--;
                }
                int baka = n;
                for (int j = 0; j < n; j++)
                {
                    temp1.CopyTo(resVect, resVect.Length - (temp2.Length * baka));
                    baka--;
                }
            }

            return(d.VecorToArrayRowByRow(n * r, n * c, resVect)); //r;c; vector
        }
Ejemplo n.º 14
0
        //circular averaging filter
        //within the square matrix of side 2*radius+1
        //realization at the level "Bratiwka, ya tebe pokywat prines"
        private static double[,] DiskProcess(double radius)
        {
            double[,] result = new double[1, 1];
            ArrGen <double> d = new ArrGen <double>();

            if (radius > 0)
            {
                var crad = Math.Ceiling(radius - 0.5);
                int rc   = 0;
                for (int i = (int)(-crad); i <= crad; i++)
                {
                    rc++;
                }

                double[,] x = new double[rc, rc];
                double[,] y = new double[rc, rc];

                var trap = -crad;
                for (int i = 0; i < rc; i++)
                {
                    for (int j = 0; j < rc; j++)
                    {
                        x[i, j] = trap + j;
                        y[i, j] = trap + i;
                    }
                }

                var maxxy = x.AbsArrayElements().MaxTwoArrays(y.AbsArrayElements());
                var minxy = x.AbsArrayElements().MinTwoArrays(y.AbsArrayElements());

                var maxxyVector = maxxy.Cast <double>().ToArray();
                var minxyVector = minxy.Cast <double>().ToArray();

                //temp vectors and variables
                double[] temp1 = new double[rc * rc];
                double[] temp2 = new double[rc * rc];
                double   temp  = 0;
                Complex  tempComplex;

                //m1 count by partial
                double[] m1tempCusum = new double[rc * rc];

                for (int i = 0; i < m1tempCusum.Length; i++)
                {
                    //a = (rad^2 < (maxxy+0.5).^2 + (minxy-0.5).^2).*(minxy-0.5)
                    if (Math.Pow(radius, 2) < (Math.Pow(maxxyVector[i] + 0.5, 2) + Math.Pow(minxyVector[i] - 0.5, 2)))
                    {
                        temp = 1;
                    }
                    temp1[i] = temp * (minxyVector[i] - 0.5); temp = 0;

                    // b =(rad^2 >= (maxxy+0.5).^2 + (minxy-0.5).^2).* sqrt(rad ^ 2 - (maxxy + 0.5).^ 2)
                    if (Math.Pow(radius, 2) >= (Math.Pow(maxxyVector[i] + 0.5, 2) + Math.Pow(minxyVector[i] - 0.5, 2)))
                    {
                        temp = 1;
                    }
                    tempComplex = Complex.Sqrt(Math.Pow(radius, 2) - Math.Pow(maxxyVector[i] + 0.5, 2));
                    temp2[i]    = temp * (tempComplex.Real + tempComplex.Imaginary); temp = 0;
                }
                //a + b
                m1tempCusum = temp1.SumVectors(temp2);
                var m1 = d.VecorToArrayRowByRow(rc, rc, m1tempCusum);

                temp1 = new double[rc * rc]; temp2 = new double[rc * rc];
                //m2 count by partial
                double[] m2tempCusum = new double[rc * rc];

                for (int i = 0; i < m2tempCusum.Length; i++)
                {
                    //a = (rad^2 > (maxxy-0.5).^2 + (minxy+0.5).^2).*(minxy+0.5)
                    if (Math.Pow(radius, 2) > (Math.Pow(maxxyVector[i] - 0.5, 2) + Math.Pow(minxyVector[i] + 0.5, 2)))
                    {
                        temp = 1;
                    }
                    temp1[i] = temp * (minxyVector[i] + 0.5); temp = 0;

                    // b = (rad^2 <= (maxxy-0.5).^2 + (minxy+0.5).^2).* sqrt(rad ^ 2 - (maxxy - 0.5).^ 2)
                    if (Math.Pow(radius, 2) <= (Math.Pow(maxxyVector[i] - 0.5, 2) + Math.Pow(minxyVector[i] + 0.5, 2)))
                    {
                        temp = 1;
                    }
                    temp2[i] = temp * Math.Sqrt(Math.Pow(radius, 2) - Math.Pow(maxxyVector[i] - 0.5, 2)); temp = 0;
                }
                //a + b
                m2tempCusum = temp1.SumVectors(temp2);
                var m2 = d.VecorToArrayRowByRow(rc, rc, m2tempCusum);

                temp1 = new double[rc * rc]; temp2 = new double[rc * rc];
                //sgrid count by partial
                double[] sgridVector = new double[rc * rc];

                for (int i = 0; i < sgridVector.Length; i++)
                {
                    //rad^2*(0.5*(asin(m2/rad) - asin(m1/rad)) + 0.25 * (sin(2 * asin(m2 / rad)) - sin(2 * asin(m1 / rad)))) - (maxxy - 0.5).* (m2 - m1) + (m1 - minxy + 0.5)
                    temp1[i] = Math.Pow(radius, 2) * (0.5 * (Math.Asin(m2tempCusum[i] / radius) - Math.Asin(m1tempCusum[i] / radius)) +
                                                      0.25 * (Math.Sin(2 * Math.Asin(m2tempCusum[i] / radius)) - Math.Sin(2 * Math.Asin(m1tempCusum[i] / radius)))) -
                               (maxxyVector[i] - 0.5) * (m2tempCusum[i] - m1tempCusum[i]) +
                               (m1tempCusum[i] - minxyVector[i] + 0.5);

                    // ((rad^2 < (maxxy+0.5).^2 + (minxy+0.5).^2) & (rad ^ 2 > (maxxy - 0.5).^ 2 + (minxy - 0.5).^ 2)) | ((minxy == 0) & (maxxy - 0.5 < rad) & (maxxy + 0.5 >= rad))
                    if (Math.Pow(radius, 2) < (Math.Pow(maxxyVector[i] + 0.5, 2) + Math.Pow(minxyVector[i] + 0.5, 2)))
                    {
                        temp = 1;
                    }

                    if (Math.Pow(radius, 2) > (Math.Pow(maxxyVector[i] - 0.5, 2) + Math.Pow(minxyVector[i] - 0.5, 2)) && temp == 1)
                    {
                        temp = 1;
                    }
                    else
                    {
                        temp = 0;
                    }

                    if (((minxyVector[i] == 0) && (maxxyVector[i] - 0.5 < radius) && (maxxyVector[i] + 0.5 >= radius)) || temp == 1)
                    {
                        temp = 1;
                    }
                    else
                    {
                        temp = 0;
                    }

                    temp2[i] = temp; temp = 0;
                }

                //a * b
                sgridVector = temp1.MultVectors(temp2);

                for (int i = 0; i < sgridVector.Length; i++)
                {
                    if ((Math.Pow(maxxyVector[i] + 0.5, 2) + Math.Pow(minxyVector[i] + 0.5, 2)) < Math.Pow(radius, 2))
                    {
                        temp = 1;
                    }

                    sgridVector[i] = sgridVector[i] + temp; temp = 0;
                }

                var sgrid = d.VecorToArrayRowByRow(rc, rc, sgridVector);
                sgrid[(int)crad, (int)crad] = Math.Min(Math.PI * Math.Pow(radius, 2), Math.PI / 2);

                if ((crad > 0) && (radius > (crad - 0.5)) && (Math.Pow(radius, 2) < (Math.Pow(crad - 0.5, 2) + 0.25)))
                {
                    var tempm1  = Math.Sqrt(Math.Pow(radius, 2) - Math.Pow(crad - 0.5, 2));
                    var tempm1n = tempm1 / radius;
                    var sg0     = 2 * (Math.Pow(radius, 2) * (0.5 * Math.Asin(tempm1n) +
                                                              0.25 * Math.Sin(2 * Math.Asin(tempm1n))) - tempm1 * (crad - 0.5));

                    sgrid[2 * (int)crad, (int)crad] = sg0;
                    sgrid[(int)crad, 2 * (int)crad] = sg0;
                    sgrid[(int)crad, 0]             = sg0;
                    sgrid[0, (int)crad]             = sg0;

                    sgrid[2 * (int)crad - 1, (int)crad] = sgrid[(2 * (int)crad - 1), (int)crad] - sg0;
                    sgrid[(int)crad, 2 * (int)crad - 1] = sgrid[(int)crad, 2 * (int)crad - 1] - sg0;
                    sgrid[(int)crad, 1] = sgrid[(int)crad, 1] - sg0;
                    sgrid[1, (int)crad] = sgrid[1, (int)crad] - sg0;
                }
                sgrid[(int)crad, (int)crad] = Math.Min(sgrid[(int)crad, (int)crad], 1);

                result = new double[rc, rc];
                result = d.VecorToArrayRowByRow(rc, rc, sgrid.Cast <double>().ToArray().VectorDivByConst(sgrid.Cast <double>().ToArray().Sum()));
            }
            else
            {
                Console.WriteLine("Radius must be greater 0. Method: FSpecial.Radius(). Return black point.");
            }

            return(result);
        }
Ejemplo n.º 15
0
        //linear motion of a camera by Len pixels
        //with an angle of Theta degrees
        //realization at the level "Bratiwka, ya tebe pokywat prines"
        private static double[,] MotionProcess(uint len, double theta)
        {
            double[,] result = new double[1, 1];
            ArrGen <double> d = new ArrGen <double>();

            if (len > 1)
            {
                //Floating-point relative accuracy
                double eps = 2.2204 * Math.Pow(10, -16);

                //rotate half length around center
                var half = ((double)len - 1) / 2;
                var phi  = (theta % 180) / 180 * Math.PI;

                var cosPhi = Math.Cos(phi);
                var sinPhi = Math.Sin(phi);
                var xsign  = Math.Sign(cosPhi);
                if (xsign == 0 || xsign == -1)
                {
                    Console.WriteLine("Something wrong with input parameters. Return empty."); return(result);
                }
                double linewdt = 1;

                //define mesh for the half matrix, eps takes care of the right size for 0 & 90 rotation
                var sx = half * cosPhi + linewdt * xsign - len * eps;
                if (sx > 0)
                {
                    sx = Math.Floor(sx);
                }
                else
                {
                    sx = -(Math.Floor(-sx));
                }

                var sy = half * sinPhi + linewdt - len * eps;
                if (sy > 0)
                {
                    sy = Math.Floor(sy);
                }
                else
                {
                    sy = -(Math.Floor(-sy));
                }

                double[,] x = new double[(int)sy + 1, (int)sx + 1];
                double[,] y = new double[(int)sy + 1, (int)sx + 1];

                for (int i = 0; i <= sy; i++)
                {
                    for (int j = 0; j <= sx; j++)
                    {
                        x[i, j] = j;
                        y[i, j] = i;
                    }
                }

                //define shortest distance from a pixel to the rotated line
                var dist2line = y.ArrayMultByConst(cosPhi).SubArrays(x.ArrayMultByConst(sinPhi));
                var rad       = x.PowArrayElements(2).SumArrays(y.PowArrayElements(2)).SqrtArrayElements();

                //find points beyond the line's end-point but within the line width
                var tempGreater = rad.MarkGreaterEqual(half, BabaYaga.logic);
                var tempLess    = dist2line.AbsArrayElements().MarkLessEqual(linewdt, BabaYaga.logic);
                tempGreater = tempGreater.ArrayMultElements(tempLess);

                List <int> lastpix = new List <int>();

                var lastPixVector = d.ArrayToVectorColByCol(tempGreater);
                for (int i = 0; i < lastPixVector.Count(); i++)
                {
                    if (lastPixVector[i] != 0)
                    {
                        lastpix.Add(i);
                    }
                }

                //distance to the line's end-point parallel to the line
                //transform into vectors, for more convenient process (for me naturally :D)
                var xVector         = d.ArrayToVectorColByCol(x);
                var dist2lineVector = d.ArrayToVectorColByCol(dist2line);

                List <double> x2lastpix = new List <double>();
                for (int i = 0; i < lastpix.Count; i++)
                {
                    x2lastpix.Add(half - Math.Abs((xVector[lastpix[i]] + dist2lineVector[lastpix[i]] * sinPhi) / cosPhi));
                    dist2lineVector[lastpix[i]] = Math.Sqrt(Math.Pow(dist2lineVector[lastpix[i]], 2) + Math.Pow(x2lastpix[i], 2));
                }

                for (int i = 0; i < dist2lineVector.Length; i++)
                {
                    dist2lineVector[i] = linewdt + eps - Math.Abs(dist2lineVector[i]);
                    if (dist2lineVector[i] < 0)
                    {
                        dist2lineVector[i] = 0;                         //zero out anything beyond line width
                    }
                }

                //back to 2D
                dist2line = d.VecorToArrayColbyCol(dist2line.GetLength(0), dist2line.GetLength(1), dist2lineVector);

                //unfold half - matrix to the full size
                result = new double[dist2line.GetLength(0) * 2 - 1, dist2line.GetLength(1) * 2 - 1];
                var temp    = Rotate90.RotateArray90(dist2line, 2);
                var reverse = d.VecorToArrayRowByRow(temp.GetLength(0), temp.GetLength(1), temp.Cast <double>().Reverse().ToArray());

                for (int i = 0; i < temp.GetLength(0); i++)
                {
                    for (int j = 0; j < temp.GetLength(1); j++)
                    {
                        result[i, j] = temp[i, j];
                    }
                }

                for (int i = 0; i < reverse.GetLength(0); i++)
                {
                    for (int j = 0; j < reverse.GetLength(1); j++)
                    {
                        result[i + reverse.GetLength(0) - 1, j + reverse.GetLength(1) - 1] = reverse[i, j];
                    }
                }

                result = result.ArrayDivByConst(result.Cast <double>().Sum() + eps * len * len);

                if (cosPhi > 0)
                {
                    result = d.FlipArray(result);
                }
            }
            else
            {
                Console.WriteLine("Len must be positive integer value, greater than 1. Method: FSpecial.Motion(). Return black rectangle.");
            }

            return(result);
        }