Example #1
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);
        }
Example #2
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);
        }
Example #3
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);
        }
Example #4
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);
        }
Example #5
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);
            }
        }
Example #6
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
        }
Example #7
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);
        }
Example #8
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);
        }