Example #1
0
        public void ApplyMinMaxCalc(cv.Mat input)
        {
            double zMax = double.MinValue;
            double zMin = double.MaxValue;

            for (int x = 0; x < xSize; ++x)
            {
                for (int z = 0; z < zSize; ++z)
                {
                    if (zMax < input.At <double>(x, z))
                    {
                        zMax = input.At <double>(x, z);
                    }

                    if (zMin > input.At <double>(x, z))
                    {
                        zMin = input.At <double>(x, z);
                    }
                }
            }

            for (int x = 0; x < xSize; ++x)
            {
                for (int z = 0; z < zSize; ++z)
                {
                    input.Set <float>(x, z, input.At <float>(x, z) - (float)zMin);
                    input.Set <float>(x, z, input.At <float>(x, z) * 1000.0f / (float)(zMax - zMin));
                    MeshDataSeries[z, x] = input.Get <float>(x, z);
                }
            }
        }
Example #2
0
        public double[] GetRadiusCenter3P()
        {
            int    dataSize = positionInfo[annotIdx].points.Count;
            double a, b, r;

            cv.Mat A = cv.Mat.Zeros(dataSize, 3, cv.MatType.CV_64FC1);
            cv.Mat B = cv.Mat.Zeros(dataSize, 1, cv.MatType.CV_64FC1);
            cv.Mat X = cv.Mat.Zeros(dataSize, 1, cv.MatType.CV_64FC1);

            for (int i = 0; i < dataSize; i++)
            {
                A.Set <double>(i, 0, positionInfo[annotIdx].points[i].x);
                A.Set <double>(i, 1, positionInfo[annotIdx].points[i].z);
                A.Set <double>(i, 2, 1);
                B.Set <double>(i, -Math.Pow(positionInfo[annotIdx].points[i].x, 2) - Math.Pow(positionInfo[annotIdx].points[i].z, 2));
            }

            cv.Cv2.Solve(A, B, X, cv.DecompTypes.SVD);

            a = -X.At <double>(0, 0) / 2;
            b = -X.At <double>(1, 0) / 2;
            r = Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2) - X.At <double>(2, 0));

            double[] data = { a, b, r };

            return(data);
        }
Example #3
0
            //  the function TestPerformance() measures the running time of
            //  the nearest neighbor search in an ordered dataset of points;
            //
            //  the test emulates the computation of the distance transform;
            //  it calculates the minimum distance from each point in
            //  the given rectangle to a point in the input dataset;
            static public cv.Mat TestPerformance
            (
                int rect_width,
                int rect_height,
                List <Point> test_points
            )
            {
                cv.Mat        dist         = new cv.Mat(rect_height, rect_width, cv.MatType.CV_32F);
                PointComparer pnt_comparer = new PointComparer();

                test_points.Sort(pnt_comparer);

                Stopwatch watch = new Stopwatch();

                watch.Start();

                for (int x = 0; x < rect_width; ++x)
                {
                    for (int y = 0; y < rect_height; ++y)
                    {
                        float nextVal = (float)MinDistanceOrderedSet(new Point(x, y), pnt_comparer, test_points);
                        dist.Set <float>(y, x, nextVal);
                    }
                }

                watch.Stop();
                Console.WriteLine("execution time of ordered dataset algorithm = {0} ms ;", watch.ElapsedMilliseconds);

                return(dist);
            }
Example #4
0
        static cv.Mat MatInverse(cv.Mat m)
        {
            // assumes determinant is not 0
            // that is, the matrix does have an inverse
            int n = m.Rows;

            cv.Mat result = m.Clone();

            cv.Mat lum;                         // combined lower & upper
            int[]  perm;                        // out parameter
            MatDecompose(m, out lum, out perm); // ignore return

            double[] b = new double[n];
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    if (i == perm[j])
                    {
                        b[j] = 1.0;
                    }
                    else
                    {
                        b[j] = 0.0;
                    }
                }

                double[] x = Reduce(lum, b); //
                for (int j = 0; j < n; ++j)
                {
                    result.Set <double>(j, i, x[j]);
                }
            }
            return(result);
        }
Example #5
0
        static cv.Mat MatProduct(cv.Mat matA, cv.Mat matB)
        {
            int aRows = matA.Rows;
            int aCols = matA.Cols;
            int bRows = matB.Rows;
            int bCols = matB.Cols;

            if (aCols != bRows)
            {
                throw new Exception("Non-conformable matrices");
            }

            cv.Mat result = new cv.Mat(aRows, bCols, cv.MatType.CV_64F, 0);

            for (int i = 0; i < aRows; ++i)         // each row of A
            {
                for (int j = 0; j < bCols; ++j)     // each col of B
                {
                    for (int k = 0; k < aCols; ++k) // could use bRows
                    {
                        result.Set <double>(i, j, result.At <double>(i, j) + matA.At <double>(i, k) * matB.At <double>(k, j));
                    }
                }
            }

            return(result);
        }
Example #6
0
        public void Run()
        {
            Console.WriteLine("===== FlannTest =====");

            // creates data set
            using (var features = new Mat(10000, 2, MatType.CV_32FC1))
            {
                var rand = new Random();
                for (int i = 0; i < features.Rows; i++)
                {
                    features.Set<float>(i, 0, rand.Next(10000));
                    features.Set<float>(i, 1, rand.Next(10000));
                }

                // query
                var queryPoint = new Point2f(7777, 7777);
                var queries = new Mat(1, 2, MatType.CV_32FC1);
                queries.Set<float>(0, 0, queryPoint.X);
                queries.Set<float>(0, 1, queryPoint.Y);
                Console.WriteLine("query:({0}, {1})", queryPoint.X, queryPoint.Y);
                Console.WriteLine("-----");

                // knnSearch
                using (var nnIndex = new Index(features, new KDTreeIndexParams(4)))
                {
                    const int Knn = 1;
                    int[] indices;
                    float[] dists;
                    nnIndex.KnnSearch(queries, out indices, out dists, Knn, new SearchParams(32));

                    for (int i = 0; i < Knn; i++)
                    {
                        int index = indices[i];
                        float dist = dists[i];
                        var pt = new Point2f(features.Get<float>(index, 0), features.Get<float>(index, 1));
                        Console.Write("No.{0}\t", i);
                        Console.Write("index:{0}", index);
                        Console.Write(" distance:{0}", dist);
                        Console.Write(" data:({0}, {1})", pt.X, pt.Y);
                        Console.WriteLine();
                    }
                }
            }
            Console.Read();
        }
Example #7
0
        static cv.Mat ExtractLower(cv.Mat lum)
        {
            // lower part of an LU Crout's decomposition
            // (dummy 1.0s on diagonal, 0.0s above)
            int n = lum.Rows;

            cv.Mat result = lum.Clone().SetTo(0);
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    if (i == j)
                    {
                        result.Set <double>(i, j, 1.0);
                    }
                    else if (i > j)
                    {
                        result.Set <double>(i, j, lum.At <double>(i, j));
                    }
                }
            }
            return(result);
        }
Example #8
0
        public void Start(cv.Mat color, cv.Mat result1, int filterSize, int levels)
        {
            int[] intensityBin = new int[levels];

            int filterOffset = (filterSize - 1) / 2;
            int currentIntensity = 0, maxIntensity = 0, maxIndex = 0;

            for (int offsetY = filterOffset; offsetY < color.Height - filterOffset; offsetY++)
            {
                for (int offsetX = filterOffset; offsetX < color.Width - filterOffset; offsetX++)
                {
                    maxIntensity = maxIndex = 0;

                    intensityBin = new int[levels];
                    cv.Vec3i[] bins = new cv.Vec3i[levels];

                    for (int y = offsetY - filterOffset; y < offsetY + filterOffset; y++)
                    {
                        for (int x = offsetX - filterOffset; x < offsetX + filterOffset; x++)
                        {
                            cv.Vec3b rgb = color.Get <cv.Vec3b>(y, x);
                            currentIntensity = (int)(Math.Round((Double)(rgb.Item0 + rgb.Item1 + rgb.Item2) / 3.0 * (levels - 1)) / 255.0);

                            intensityBin[currentIntensity] += 1;
                            bins[currentIntensity].Item0   += rgb.Item0;
                            bins[currentIntensity].Item1   += rgb.Item1;
                            bins[currentIntensity].Item2   += rgb.Item2;

                            if (intensityBin[currentIntensity] > maxIntensity)
                            {
                                maxIntensity = intensityBin[currentIntensity];
                                maxIndex     = currentIntensity;
                            }
                        }
                    }

                    if (maxIntensity == 0)
                    {
                        maxIntensity = 1;
                    }
                    double blue  = bins[maxIndex].Item0 / maxIntensity;
                    double green = bins[maxIndex].Item1 / maxIntensity;
                    double red   = bins[maxIndex].Item2 / maxIntensity;

                    result1.Set <cv.Vec3b>(offsetY, offsetX, new cv.Vec3b(ClipByte(blue), ClipByte(green), ClipByte(red)));
                }
            }
        }
Example #9
0
        static cv.Mat ExtractUpper(cv.Mat lum)
        {
            // upper part of an LU (lu values on diagional and above, 0.0s below)
            int n = lum.Rows;

            cv.Mat result = lum.Clone().SetTo(0);
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    if (i <= j)
                    {
                        result.Set <double>(i, j, lum.At <double>(i, j));
                    }
                }
            }
            return(result);
        }
        // Use this for initialization
        void Start()
        {
            Mat mat = Unity.TextureToMat(this.texture);

            for (int yi = 0; yi < mat.Height; yi++)
            {
                for (int xi = 0; xi < mat.Width; xi++)
                {
                    Vec3b v = mat.At <Vec3b>(yi, xi);
                    v[0] = (byte)(ReduceColor(v[0]));
                    v[1] = (byte)(ReduceColor(v[1]));
                    v[2] = (byte)(ReduceColor(v[2]));
                    mat.Set <Vec3b>(yi, xi, v);
                }
            }
            Texture2D changedTex = Unity.MatToTexture(mat);

            GetComponent <RawImage>().texture = changedTex;
        }
        // Use this for initialization
        void Start()
        {
            Mat mat = Unity.TextureToMat(this.texture);

            for (int yi = 0; yi < mat.Height; yi++)
            {
                for (int xi = 0; xi < mat.Width; xi++)
                {
                    Vec3b v  = mat.At <Vec3b>(yi, xi);
                    float gr = 0.2126f * v[2] + 0.7152f * v[1] + 0.0722f * v[0];
                    v[0] = (byte)gr;
                    v[1] = (byte)gr;
                    v[2] = (byte)gr;
                    mat.Set <Vec3b>(yi, xi, v);
                }
            }
            Texture2D changedTex = Unity.MatToTexture(mat);

            GetComponent <RawImage>().texture = changedTex;
        }
        // Use this for initialization
        void Start()
        {
            Mat mat = Unity.TextureToMat(this.texture);

            for (int yi = 0; yi < 16; yi++)
            {
                for (int xi = 0; xi < 16; xi++)
                {
                    Vec3b max = new Vec3b();
                    for (int yj = 0; yj < 8; yj++)
                    {
                        for (int xj = 0; xj < 8; xj++)
                        {
                            Vec3b v = mat.At <Vec3b>(yi * 8 + yj, xi * 8 + xj);
                            if (max[0] < v[0])
                            {
                                max[0] = v[0];
                            }
                            if (max[1] < v[1])
                            {
                                max[1] = v[1];
                            }
                            if (max[2] < v[2])
                            {
                                max[2] = v[2];
                            }
                        }
                    }
                    for (int yj = 0; yj < 8; yj++)
                    {
                        for (int xj = 0; xj < 8; xj++)
                        {
                            mat.Set <Vec3b>(yi * 8 + yj, xi * 8 + xj, max);
                        }
                    }
                }
            }
            Texture2D changedTex = Unity.MatToTexture(mat);

            GetComponent <RawImage>().texture = changedTex;
        }
        // Use this for initialization
        void Start()
        {
            Mat mat         = Unity.TextureToMat(this.texture);
            Mat changedMat  = new Mat();
            Mat changedMat1 = new Mat();

            Cv2.CvtColor(mat, changedMat, ColorConversionCodes.BGR2HSV);
            for (int yi = 0; yi < mat.Height; yi++)
            {
                for (int xi = 0; xi < mat.Width; xi++)
                {
                    Vec3b v = changedMat.At <Vec3b>(yi, xi);
                    Debug.Log(v[0]);
                    v[0] = (byte)((v[0] - 180) % 360);
                    changedMat.Set <Vec3b>(yi, xi, v);
                }
            }
            Cv2.CvtColor(changedMat, changedMat1, ColorConversionCodes.HSV2BGR);
            Texture2D changedTex = Unity.MatToTexture(changedMat1);

            GetComponent <RawImage>().texture = changedTex;
        }
        // Use this for initialization
        void Start()
        {
            Mat mat = Unity.TextureToMat(this.texture);

            for (int yi = 0; yi < 16; yi++)
            {
                for (int xi = 0; xi < 16; xi++)
                {
                    Vector3 sum = new Vector3();
                    for (int yj = 0; yj < 8; yj++)
                    {
                        for (int xj = 0; xj < 8; xj++)
                        {
                            Vec3b v = mat.At <Vec3b>(yi * 8 + yj, xi * 8 + xj);
                            sum[0] += v[0];
                            sum[1] += v[1];
                            sum[2] += v[2];
                        }
                    }
                    Debug.Log(sum[0]);
                    Vec3b ave = new Vec3b();
                    ave[0] = (byte)(sum[0] / 64);
                    ave[1] = (byte)(sum[1] / 64);
                    ave[2] = (byte)(sum[2] / 64);

                    for (int yj = 0; yj < 8; yj++)
                    {
                        for (int xj = 0; xj < 8; xj++)
                        {
                            mat.Set <Vec3b>(yi * 8 + yj, xi * 8 + xj, ave);
                        }
                    }
                }
            }
            Texture2D changedTex = Unity.MatToTexture(mat);

            GetComponent <RawImage>().texture = changedTex;
        }
        // Use this for initialization
        void Start()
        {
            Mat mat = Unity.TextureToMat(this.texture);

            float[] results = new float[256];
            float[,] grs = new float[mat.Height, mat.Width];
            for (int yi = 0; yi < mat.Height; yi++)
            {
                for (int xi = 0; xi < mat.Width; xi++)
                {
                    Vec3b v  = mat.At <Vec3b>(yi, xi);
                    float gr = 0.2126f * v[2] + 0.7152f * v[1] + 0.0722f * v[0];
                    grs[yi, xi] = gr;
                }
            }
            for (int thi = 1; thi < 255; thi++)
            {
                int   w0 = 0;
                int   w1 = 0;
                float M0 = 0;
                float M1 = 0;
                foreach (float gr in grs)
                {
                    if (gr < thi)
                    {
                        w0++;
                        M0 += gr;
                    }
                    else
                    {
                        w1++;
                        M1 += gr;
                    }
                }
                Debug.Log(w0 + w1);
                float tmp0 = w0 == 0 ? 0 : M0 / w0;
                float tmp1 = w1 == 0 ? 0 : M1 / w1;
                results[thi] = ((float)w0 / (mat.Height * mat.Width)) * ((float)w1 / (mat.Height * mat.Width)) * Mathf.Pow(tmp0 - tmp1, 2);
            }
            int z = 0;

            for (int i = 1; i < 255; i++)
            {
                if (results[i] > results[z])
                {
                    z = i;
                }
            }
            for (int yi = 0; yi < mat.Height; yi++)
            {
                for (int xi = 0; xi < mat.Width; xi++)
                {
                    if (grs[yi, xi] < z)
                    {
                        Vec3b v = new Vec3b();
                        v[0] = (byte)0; v[1] = (byte)0; v[2] = (byte)0;
                        mat.Set <Vec3b>(yi, xi, v);
                    }
                    else
                    {
                        Vec3b v = new Vec3b();
                        v[0] = (byte)255; v[1] = (byte)255; v[2] = (byte)255;
                        mat.Set <Vec3b>(yi, xi, v);
                    }
                }
            }
            Texture2D changedTex = Unity.MatToTexture(mat);

            GetComponent <RawImage>().texture = changedTex;
        }
Example #16
0
        static int MatDecompose(cv.Mat m, out cv.Mat lum, out int[] perm)
        {
            // Crout's LU decomposition for matrix determinant and inverse
            // stores combined lower & upper in lum[][]
            // stores row permuations into perm[]
            // returns +1 or -1 according to even or odd number of row permutations
            // lower gets dummy 1.0s on diagonal (0.0s above)
            // upper gets lum values on diagonal (0.0s below)

            int toggle = +1; // even (+1) or odd (-1) row permutatuions
            int n      = m.Rows;

            // make a copy of m[][] into result lum[][]
            lum = m.Clone();

            // make perm[]
            perm = new int[n];
            for (int i = 0; i < n; ++i)
            {
                perm[i] = i;
            }

            for (int j = 0; j < n - 1; ++j) // process by column. note n-1
            {
                double max = Math.Abs(lum.At <double>(j, j));
                int    piv = j;

                for (int i = j + 1; i < n; ++i) // find pivot index
                {
                    double xij = Math.Abs(lum.At <double>(i, j));
                    if (xij > max)
                    {
                        max = xij;
                        piv = i;
                    }
                } // i

                if (piv != j)
                {
                    cv.Mat tmp = lum.Row(piv).Clone(); // swap rows j, piv
                    lum.Row(j).CopyTo(lum.Row(piv));
                    tmp.CopyTo(lum.Row(j));

                    int t = perm[piv]; // swap perm elements
                    perm[piv] = perm[j];
                    perm[j]   = t;

                    toggle = -toggle;
                }

                double xjj = lum.At <double>(j, j);
                if (xjj != 0.0)
                {
                    for (int i = j + 1; i < n; ++i)
                    {
                        double xij = lum.At <double>(i, j) / xjj;
                        lum.Set <double>(i, j, xij);
                        for (int k = j + 1; k < n; ++k)
                        {
                            lum.Set <double>(i, k, lum.At <double>(i, k) - xij * lum.At <double>(j, k));
                        }
                    }
                }
            }

            return(toggle);  // for determinant
        }
Example #17
0
        /// <summary>
        /// Classical Multidimensional Scaling
        /// </summary>
        public void Run()
        {
            // creates distance matrix
            int size = CityDistance.GetLength(0);
            Mat t = new Mat(size, size, MatType.CV_64FC1, CityDistance);
            // adds Torgerson's additive constant to t
            double torgarson = Torgerson(t);
            t += torgarson;
            // squares all elements of t
            t = t.Mul(t);

            // centering matrix G
            Mat g = CenteringMatrix(size);
            // calculates inner product matrix B
            Mat b = g * t * g.T() * -0.5;
            // calculates eigenvalues and eigenvectors of B
            Mat values = new Mat();
            Mat vectors = new Mat();
            Cv2.Eigen(b, values, vectors);
            for (int r = 0; r < values.Rows; r++)
            {
                if (values.Get<double>(r) < 0)
                    values.Set<double>(r, 0);
            }

            //Console.WriteLine(values.Dump());

            // multiplies sqrt(eigenvalue) by eigenvector
            Mat result = vectors.RowRange(0, 2);
            {
                var at = result.GetGenericIndexer<double>();
                for (int r = 0; r < result.Rows; r++)
                {
                    for (int c = 0; c < result.Cols; c++)
                    {
                        at[r, c] *= Math.Sqrt(values.Get<double>(r));
                    }
                }
            }

            // scaling
            Cv2.Normalize(result, result, 0, 800, NormTypes.MinMax);

            // opens a window
            using (Mat img = Mat.Zeros(600, 800, MatType.CV_8UC3))
            using (Window window = new Window("City Location Estimation"))
            {
                var at = result.GetGenericIndexer<double>();
                for (int c = 0; c < size; c++)
                {
                    double x = at[0, c];
                    double y = at[1, c];
                    x = x * 0.7 + img.Width * 0.1;
                    y = y * 0.7 + img.Height * 0.1;
                    img.Circle((int)x, (int)y, 5, Scalar.Red, -1);
                    Point textPos = new Point(x + 5, y + 10);
                    img.PutText(CityNames[c], textPos, HersheyFonts.HersheySimplex, 0.5, Scalar.White);
                }
                window.Image = img;
                Cv2.WaitKey();
            }
        }