Beispiel #1
0
    public void mapToSphere(Point point, Vector3f vector)
    {
        //Copy paramter into temp point
        Point2f tempPoint = new Point2f(point.X, point.Y);

        //Adjust point coords and scale down to range of [-1 ... 1]
        tempPoint.x = (tempPoint.x * this.adjustWidth) - 1.0f;
        tempPoint.y = 1.0f - (tempPoint.y * this.adjustHeight);

        //Compute the square of the length of the vector to the point from the center
        float length = (tempPoint.x * tempPoint.x) + (tempPoint.y * tempPoint.y);

        //If the point is mapped outside of the sphere... (length > radius squared)
        if (length > 1.0f)
        {
            //Compute a normalizing factor (radius / sqrt(length))
            float norm = (float) (1.0 / Math.Sqrt(length));

            //Return the "normalized" vector, a point on the sphere
            vector.x = tempPoint.x * norm;
            vector.y = tempPoint.y * norm;
            vector.z = 0.0f;
        }
        else    //Else it's on the inside
        {
            //Return a vector to a point mapped inside the sphere sqrt(radius squared - length)
            vector.x = tempPoint.x;
            vector.y = tempPoint.y;
            vector.z = (float) Math.Sqrt(1.0f - length);
        }
    }
Beispiel #2
0
 public Line(Graphics gs, Pen pen, Point2f x, Point2f y)
     : base(gs)
 {
     this.pen = pen;
     this.b = x;
     this.e = y;
 }
Beispiel #3
0
        public void DrawSpot(Point2f spot)
        {
            using (var graphics = Graphics.FromImage(streamBox.Image))
            {
                graphics.DrawEllipse(Pens.CornflowerBlue, spot.X - 2, spot.Y - 2, 4, 4);

                //streamBox.Image = new Bitmap(bitmap, streamBox.Size);
            }
        }
Beispiel #4
0
        public void Run()
        {

            Console.WriteLine("===== FlannTest =====");

            // creates data set
            using (Mat features = new Mat(10000, 2, MatType.CV_32FC1))
            {
                Random 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
                Point2f queryPoint = new Point2f(7777, 7777);
                Mat 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 (Index 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];
                        Point2f 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();
                    }
                    Knn.ToString();
                }
            }
            Console.Read();
        }
Beispiel #5
0
        /// <summary>
        /// returns 4 vertices of the rectangle
        /// </summary>
        /// <returns></returns>
        public Point2f[] Points()
        {
            double angle = Angle * Cv.PI / 180.0;
            float b = (float)Math.Cos(angle) * 0.5f;
            float a = (float)Math.Sin(angle) * 0.5f;

            Point2f[] pt = new Point2f[4];
            pt[0].X = Center.X - a * Size.Height - b * Size.Width;
            pt[0].Y = Center.Y + b * Size.Height - a * Size.Width;
            pt[1].X = Center.X + a * Size.Height - b * Size.Width;
            pt[1].Y = Center.Y - b * Size.Height - a * Size.Width;
            pt[2].X = 2 * Center.X - pt[0].X;
            pt[2].Y = 2 * Center.Y - pt[0].Y;
            pt[3].X = 2 * Center.X - pt[1].X;
            pt[3].Y = 2 * Center.Y - pt[1].Y;
            return pt;
        }
Beispiel #6
0
        public void SaveCallibration(string path, Point2f[] src, Point2f[] dst)
        {
            this.srcx = new List<float>();
            this.srcy = new List<float>();
            this.dstx = new List<float>();
            this.dsty = new List<float>();

            foreach (var p in src)
            {
                this.srcx.Add(p.X);
                this.srcy.Add(p.Y);
            }
            foreach (var q in dst)
            {
                this.dstx.Add(q.X);
                this.dsty.Add(q.Y);
            }
            this.Save(path);
        }
Beispiel #7
0
        public void LoadCallibration(string path, Point2f[] src, Point2f[] dst)
        {
            this.Load(path);

            src[0].X = srcx[0];
            src[1].X = srcx[1];
            src[2].X = srcx[2];
            src[3].X = srcx[3];
            src[0].Y = srcy[0];
            src[1].Y = srcy[1];
            src[2].Y = srcy[2];
            src[3].Y = srcy[3];

            dst[0].X = dstx[0];
            dst[1].X = dstx[1];
            dst[2].X = dstx[2];
            dst[3].X = dstx[3];
            dst[0].Y = dsty[0];
            dst[1].Y = dsty[1];
            dst[2].Y = dsty[2];
            dst[3].Y = dsty[3];
        }
Beispiel #8
0
 /// <summary>
 /// renders all qtrees
 /// </summary>
 public void RenderingPass(float FOV, float near, float far, Vector3f eyeVector, Point2f position)
 {
     for (int i = 0; i < QTrees.Count; i++)
     {
         RenderingPass(QTrees[i], FOV, near, far, eyeVector, position);
     }
 }
        private double CalculateHeadingAngleWithEyesFunc(ConnectedComponentCollection eyes, Point2f centroid)
        {
            Point2f headingPoint = new Point2f((eyes[0].Centroid.X + eyes[1].Centroid.X) / 2, (eyes[0].Centroid.Y + eyes[1].Centroid.Y) / 2);

            return(CalculateHeadingAngleWithPointsFunc(headingPoint, centroid));
        }
Beispiel #10
0
 internal static extern bool ON_Mesh_SetTextureCoordinates(IntPtr pMesh, int count, ref Point2f tcs, [MarshalAs(UnmanagedType.U1)]bool append);
    // Update is called once per frame
    void Update()
    {
        //string name = "Form";



        //Texture2D inputTexture = (Texture2D)Resources.Load("DocumentScanner/" + name);

        //Texture2D inputTexture = new Texture2D(webcamTexture.width, webcamTexture.height);


        //inputTexture.SetPixels(webcamTexture.GetPixels());
        //inputTexture.Apply();


        scanner.Settings.NoiseReduction = noiseReduction_test;                                          // real-world images are quite noisy, this value proved to be reasonable
        scanner.Settings.EdgesTight     = EdgesTight_test;                                              // higher value cuts off "noise" as well, this time smaller and weaker edges
        scanner.Settings.ExpectedArea   = ExpectedArea_test;                                            // we expect document to be at least 20% of the total image area

        scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.Grayscale;


/*         if (!isCaptured)
 *      {
 *          inputPlane.GetComponent<Renderer>().material.mainTexture = webcamTexture;
 *          inputPlane.transform.localScale = new Vector3((float)webcamTexture.width / (float)webcamTexture.height, 1, 1);
 *      } */

        //Input.GetKeyDown(KeyCode.Space)
        if (InteractWithScanner.isNewImage && InteractWithScanner.isNewMode)
        {
            //string name = "Form";
            //Texture2D inputTexture = (Texture2D)Resources.Load("DocumentScanner/" + name);

            /* Texture2D inputTexture = new Texture2D(webcamTexture.width, webcamTexture.height);
             *
             *
             * inputTexture.SetPixels(webcamTexture.GetPixels());
             * inputTexture.Apply();
             *
             * inputPlane.GetComponent<Renderer>().material.mainTexture = inputTexture;
             * inputPlane.transform.localScale = new Vector3((float)inputTexture.width / (float)inputTexture.height, 1, 1); */


            Texture2D inputTexture = (Texture2D)inputPlane.GetComponent <Renderer>().material.mainTexture;


            if (InteractWithScanner.modeSelected == 0 || InteractWithScanner.modeSelected == 1 || InteractWithScanner.modeSelected == 2)
            {
                isCaptured = true;

                //savedTexture = inputTexture;
                savedTexture     = InteractWithScanner.virtualPhoto;
                beginningTexture = InteractWithScanner.virtualPhoto;

                beginingMat = OpenCvSharp.Unity.TextureToMat(beginningTexture);

                allSelectedPoints = new Point[4];

                pointCounter = 0;

                autoFailed = false;
            }

            if (InteractWithScanner.modeSelected == 1 || InteractWithScanner.modeSelected == 2)
            {
                scanner.Input = OpenCvSharp.Unity.TextureToMat(inputTexture);


                // should we fail, there is second try - HSV might help to detect paper by color difference
                // if (!scanner.Success)
                //     // this will drop current result and re-fetch it next time we query for 'Success' flag or actual data
                //     scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.HueGrayscale;

                if (!scanner.Success)
                {
                    Mat          failMat      = OpenCvSharp.Unity.TextureToMat(inputTexture);
                    var          textData     = string.Format("{0}", "No contour guess. Use Manual");
                    HersheyFonts textFontFace = HersheyFonts.HersheyPlain;
                    failMat.PutText(textData, new Point(failMat.Width / 2, failMat.Height / 2), textFontFace, 20, Scalar.White);

                    Texture2D outputTexture = OpenCvSharp.Unity.MatToTexture(failMat);

                    inputPlane.GetComponent <Renderer>().material.mainTexture = outputTexture;

                    //hardcoded values need to be changed

/*                     inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0f, 1f);
 *                  inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(1f, -1f); */

                    /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                     * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f);  */


                    //inputPlane.transform.localScale = new Vector3( currScale.x*((float)resultContoured.Width / (float)resultContoured.Height), currScale.y, currScale.z);

                    textureNew = outputTexture;

                    savedTexture = outputTexture;

                    autoFailed = true;

                    isCaptured = false;
                }
                else
                {
                    Mat resultContoured = null;



                    for (int i = 0; i < scanner.PaperShape.Length; i++)
                    {
                        float currPointX = (float)scanner.PaperShape[i].X;
                        float currPointY = (float)scanner.PaperShape[i].Y;


                        currPointX *= (float)inputTexture.width;
                        currPointY *= (float)inputTexture.height;

                        currPointY = inputTexture.height - currPointY;

/*                         currPointX /= (float)inputTexture.width;
 *                      currPointY /= (float)inputTexture.height;
 *
 *                      currPointX *= (float)inputTexture.width/2f;
 *                      currPointY *= (float)inputTexture.height/2f;
 *
 *
 *                      currPointY += ((float)inputTexture.width/2.0f)*0.375f;
 *                      currPointX += ((float)inputTexture.width/2.0f)*0.5f; */

                        scanner.PaperShape[i] = new Point((int)currPointX, (int)currPointY);
                    }



                    resultContoured = JustImageContour(scanner.Input, scanner.PaperShape);

                    allSelectedPoints = scanner.PaperShape;

                    Point2f[] currPointsCon = new Point2f[4];

                    for (int i = 0; i < allSelectedPoints.Length; i++)
                    {
                        resultContoured.DrawMarker(allSelectedPoints[i].X, allSelectedPoints[i].Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 50, LineTypes.Link8, 5);

                        currPointsCon[i] = allSelectedPoints[i];
                    }


                    Texture2D outputTexture = OpenCvSharp.Unity.MatToTexture(resultContoured);

                    inputPlane.GetComponent <Renderer>().material.mainTexture = outputTexture;

                    //hardcoded values need to be changed

                    /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                     * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                    //inputPlane.transform.localScale = new Vector3( currScale.x*((float)resultContoured.Width / (float)resultContoured.Height), currScale.y, currScale.z);

                    textureNew = outputTexture;

                    /* outputPlane.GetComponent<Renderer>().material.mainTexture = webcamTexture;
                     * outputPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1); */



                    Mat test = OpenCvSharp.Demo.MatUtilities.UnwrapShape(beginingMat, currPointsCon);


                    //Mat resultUnwarpped = null;

                    //Point[] noContour = new Point[1];

                    //resultUnwarpped = JustImageContour(scanner.Output, noContour);
                    Texture2D unwrappedTexture = OpenCvSharp.Unity.MatToTexture(test);

                    unwrappedPlane.GetComponent <Renderer>().material.mainTexture = unwrappedTexture;



                    //unwrappedPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1);

                    //isCaptured = true;


                    savedTexture = outputTexture;
                }
            }



            if (!autoFailed)
            {
                InteractWithScanner.isNewImage = false;
                InteractWithScanner.isNewMode  = false;
            }
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            isCaptured = false;
        }

        if (isCaptured)
        {
            if (InteractWithScanner.modeSelected == 0 || InteractWithScanner.modeSelected == 1)
            {
                RaycastHit hit;
                //var ray = Camera.main.ScreenPointToRay(Input.mousePosition);



                Mat matCurrent = null;

                if (Physics.Raycast(casterObj_scanner.transform.position, -casterObj_scanner.transform.forward, out hit, 100) && hit.transform.name == "inputPlane")
                {
                    inputPlane.GetComponent <Renderer>().material.mainTexture = savedTexture;

                    //hardcoded values need to be changed

                    /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                     * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */

                    //inputPlane.transform.localScale = new Vector3( currScale.x*((float)savedTexture.width / (float)savedTexture.height), currScale.y, currScale.z);


                    Renderer rend = hit.transform.GetComponent <Renderer>();

                    Texture2D tex     = rend.material.mainTexture as Texture2D;
                    Vector2   pixelUV = hit.textureCoord;

                    //pixelUV.x += pixelUV.x*0.75f;
                    pixelUV.y = 1 - pixelUV.y;

                    pixelUV.x *= tex.width;
                    pixelUV.y *= tex.height;


                    /* pixelUV.y += (tex.width/2)*0.375f;
                     * pixelUV.x += (tex.width/2)*0.5f;
                     */

                    matCurrent = OpenCvSharp.Unity.TextureToMat(tex);
                    Point currPoint = new Point(pixelUV.x, pixelUV.y);


                    if (mouseMoveHeld)
                    {
                        matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(0, 255, 0), MarkerStyle.Cross, 50, LineTypes.Link8, 5);
                    }
                    else
                    {
                        matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(255, 0, 0), MarkerStyle.Cross, 50, LineTypes.Link8, 5);
                    }



                    textureNew = OpenCvSharp.Unity.MatToTexture(matCurrent);

                    inputPlane.GetComponent <Renderer>().material.mainTexture = textureNew;
                    //hardcoded values need to be changed

                    /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                     * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                    //inputPlane.transform.localScale = new Vector3( currScale.x*((float)textureNew.width / (float)textureNew.height), currScale.y, currScale.z);


                    if (raySelect.GetStateDown(handType_right))
                    {
                        if (InteractWithScanner.modeSelected == 0)
                        {
                            if (pointCounter <= 4)
                            {
                                matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 50, LineTypes.Link8, 5);


                                if (pointCounter > 3)
                                {
                                    pointCounter = 0;

                                    allSelectedPoints = new Point[4];

                                    matCurrent = OpenCvSharp.Unity.TextureToMat(beginningTexture);

                                    matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 50, LineTypes.Link8, 5);
                                }


                                allSelectedPoints[pointCounter] = currPoint;

                                textureNew = OpenCvSharp.Unity.MatToTexture(matCurrent);



                                inputPlane.GetComponent <Renderer>().material.mainTexture = textureNew;
                                //hardcoded values need to be changed

                                /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                                 * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                                //inputPlane.transform.localScale = new Vector3( currScale.x*((float)textureNew.width / (float)textureNew.height), currScale.y, currScale.z);

                                savedTexture = textureNew;
                            }
                            if (pointCounter == 3)
                            {
                                //Point[] allSelectedPoints_order = new Point[4];

                                allSelectedPoints = SortCorners(allSelectedPoints);



                                Mat resultContoured = null;
                                resultContoured = JustImageContour(matCurrent, allSelectedPoints);

                                Texture2D contourTexture = OpenCvSharp.Unity.MatToTexture(resultContoured);

                                inputPlane.GetComponent <Renderer>().material.mainTexture = contourTexture;
                                //hardcoded values need to be changed

                                /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                                 * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                                //inputPlane.transform.localScale = new Vector3( currScale.x*((float)resultContoured.Width / (float)resultContoured.Height), currScale.y, currScale.z);


                                savedTexture = contourTexture;

                                Point2f[] currPointsCon = new Point2f[4];

                                for (int i = 0; i < allSelectedPoints.Length; i++)
                                {
                                    currPointsCon[i] = allSelectedPoints[i];
                                }

                                Mat test = OpenCvSharp.Demo.MatUtilities.UnwrapShape(beginingMat, currPointsCon);

                                Texture2D unwrappedTexture = OpenCvSharp.Unity.MatToTexture(test);

                                unwrappedPlane.GetComponent <Renderer>().material.mainTexture = unwrappedTexture;
                                //unwrappedPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1);
                            }


                            pointCounter++;
                        }
                        else if (InteractWithScanner.modeSelected == 1 && !selectedForChange)
                        {
                            closePoint = new Point(-1, -1);
                            foundIndex = -1;

                            // First  selected for now maybe change later
                            for (int i = 0; i < allSelectedPoints.Length; i++)
                            {
                                float distBetweenP = Mathf.Sqrt(Mathf.Pow((currPoint.X - allSelectedPoints[i].X), 2f) + Mathf.Pow((currPoint.Y - allSelectedPoints[i].Y), 2f));
                                if (distBetweenP < distTresh)
                                {
                                    closePoint = allSelectedPoints[i];

                                    foundIndex = i;

                                    selectedForChange = true;

                                    break;
                                }
                            }

                            if (selectedForChange)
                            {
                                inputPlane.GetComponent <Renderer>().material.mainTexture = beginningTexture;
                                //hardcoded values need to be changed

                                /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                                 * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                                //inputPlane.transform.localScale = new Vector3( currScale.x*((float)beginningTexture.width / (float)beginningTexture.height), currScale.y, currScale.z);

                                Mat currMat = OpenCvSharp.Unity.TextureToMat(beginningTexture);



                                for (int i = 0; i < allSelectedPoints.Length; i++)
                                {
                                    if (i != foundIndex)
                                    {
                                        currMat.DrawMarker(allSelectedPoints[i].X, allSelectedPoints[i].Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 50, LineTypes.Link8, 5);
                                    }
                                }


                                Texture2D changedTexture = OpenCvSharp.Unity.MatToTexture(currMat);

                                inputPlane.GetComponent <Renderer>().material.mainTexture = changedTexture;
                                //hardcoded values need to be changed

                                /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                                 * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                                //inputPlane.transform.localScale = new Vector3( currScale.x*((float)textureNew.width / (float)textureNew.height), currScale.y, currScale.z);

                                savedTexture = changedTexture;
                            }
                        }
                    }

                    if (raySelect.GetState(handType_right) && selectedForChange)
                    {
                        mouseMoveHeld = true;
                    }

                    if (raySelect.GetStateUp(handType_right) && selectedForChange)
                    {
                        allSelectedPoints[foundIndex] = currPoint;



                        Mat currMat = OpenCvSharp.Unity.TextureToMat(beginningTexture);

                        currMat = JustImageContour(currMat, allSelectedPoints);


                        Point2f[] currPointsCon = new Point2f[4];

                        for (int i = 0; i < allSelectedPoints.Length; i++)
                        {
                            currMat.DrawMarker(allSelectedPoints[i].X, allSelectedPoints[i].Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 50, LineTypes.Link8, 5);

                            currPointsCon[i] = allSelectedPoints[i];
                        }


                        Texture2D changedTexture = OpenCvSharp.Unity.MatToTexture(currMat);

                        inputPlane.GetComponent <Renderer>().material.mainTexture = changedTexture;
                        //hardcoded values need to be changed

                        /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                         * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                        //inputPlane.transform.localScale = new Vector3( currScale.x*((float)textureNew.width / (float)textureNew.height), currScale.y, currScale.z);

                        savedTexture = changedTexture;


                        Mat test = OpenCvSharp.Demo.MatUtilities.UnwrapShape(beginingMat, currPointsCon);

                        Texture2D unwrappedTexture = OpenCvSharp.Unity.MatToTexture(test);

                        unwrappedPlane.GetComponent <Renderer>().material.mainTexture = unwrappedTexture;
                        //unwrappedPlane.transform.localScale = new Vector3((float)test.Width / (float)test.Height, 1, 1);



                        mouseMoveHeld = false;

                        selectedForChange = false;
                    }
                }


                if (clearSelect.GetLastStateDown(handType_right))
                {
                    pointCounter      = 0;
                    allSelectedPoints = new Point[4];


                    inputPlane.GetComponent <Renderer>().material.mainTexture = beginningTexture;
                    //hardcoded values need to be changed

                    /* inputPlane.GetComponent<Renderer>().material.mainTextureOffset = new Vector2(0.25f, 0.75f);
                     * inputPlane.GetComponent<Renderer>().material.mainTextureScale = new Vector2(0.5f, -0.5f); */


                    //inputPlane.transform.localScale = new Vector3( currScale.x*((float)beginningTexture.width / (float)beginningTexture.height), currScale.y, currScale.z);

                    savedTexture = beginningTexture;

                    if (InteractWithScanner.modeSelected == 1)
                    {
                        InteractWithScanner.isNewImage = true;
                        InteractWithScanner.isNewMode  = true;

                        isCaptured = false;
                    }
                }
            }
        }


        //if (operationMode == 1)
        //{
        //    scanner.Input = OpenCvSharp.Unity.TextureToMat(webcamTexture);


        //    // should we fail, there is second try - HSV might help to detect paper by color difference
        //    if (!scanner.Success)
        //        // this will drop current result and re-fetch it next time we query for 'Success' flag or actual data
        //        scanner.Settings.GrayMode = PaperScanner.ScannerSettings.ColorMode.HueGrayscale;



        //    Mat resultContoured = null;

        //    resultContoured = JustImageContour(scanner.Input, scanner.PaperShape);



        //    Texture2D outputTexture = OpenCvSharp.Unity.MatToTexture(resultContoured);

        //    outputPlane.GetComponent<Renderer>().material.mainTexture = outputTexture;
        //    outputPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1);

        //    inputPlane.GetComponent<Renderer>().material.mainTexture = webcamTexture;
        //    inputPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1);


        //    Mat resultUnwarpped = null;

        //    Point[] noContour = new Point[1];

        //    resultUnwarpped = JustImageContour(scanner.Output, noContour);
        //    Texture2D unwrappedTexture = OpenCvSharp.Unity.MatToTexture(resultUnwarpped);

        //    unwrappedPlane.GetComponent<Renderer>().material.mainTexture = unwrappedTexture;
        //    unwrappedPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1);
        //}
        //else
        //{

        //    if (!isCaptured)
        //    {
        //        inputPlane.GetComponent<Renderer>().material.mainTexture = webcamTexture;
        //        inputPlane.transform.localScale = new Vector3((float)webcamTexture.width / (float)webcamTexture.height, 1, 1);
        //    }



        //    if (Input.GetKeyDown(KeyCode.Space))
        //    {


        //        Texture2D inputTexture = new Texture2D(webcamTexture.width, webcamTexture.height);


        //        inputTexture.SetPixels(webcamTexture.GetPixels());
        //        inputTexture.Apply();

        //        inputPlane.GetComponent<Renderer>().material.mainTexture = inputTexture;
        //        inputPlane.transform.localScale = new Vector3((float)webcamTexture.width / (float)webcamTexture.height, 1, 1);

        //        isCaptured = true;

        //        savedTexture = inputTexture;

        //        beginningTexture = inputTexture;

        //        allSelectedPoints = new Point[4];

        //        pointCounter = 0;

        //    }

        //    if (Input.GetKeyDown(KeyCode.Escape))
        //    {
        //        isCaptured = false;
        //    }


        //    if (isCaptured)
        //    {


        //        RaycastHit hit;
        //        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        //        Mat matCurrent = null;

        //        if (Physics.Raycast(ray, out hit) && hit.transform.name == "inputPlane")
        //        {

        //            inputPlane.GetComponent<Renderer>().material.mainTexture = savedTexture;
        //            inputPlane.transform.localScale = new Vector3((float)savedTexture.width / (float)savedTexture.height, 1, 1);

        //            Renderer rend = hit.transform.GetComponent<Renderer>();

        //            Texture2D tex = rend.material.mainTexture as Texture2D;
        //            Vector2 pixelUV = hit.textureCoord;
        //            pixelUV.x *= tex.width;
        //            pixelUV.y *= tex.height;

        //            matCurrent = OpenCvSharp.Unity.TextureToMat(tex);
        //            Point currPoint = new Point(pixelUV.x, matCurrent.Height - pixelUV.y);


        //            matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(255, 0, 0), MarkerStyle.Cross, 100, LineTypes.Link8, 5);


        //            Texture2D textureNew = OpenCvSharp.Unity.MatToTexture(matCurrent);

        //            inputPlane.GetComponent<Renderer>().material.mainTexture = textureNew;
        //            inputPlane.transform.localScale = new Vector3((float)textureNew.width / (float)textureNew.height, 1, 1);


        //            if (Input.GetMouseButtonDown(0))
        //            {


        //                //if (pointCounter > 4)
        //                //{
        //                //    pointCounter = 0;

        //                //    allSelectedPoints = new Point[4];


        //                //    //inputPlane.GetComponent<Renderer>().material.mainTexture = beginningTexture;
        //                //    //inputPlane.transform.localScale = new Vector3((float)beginningTexture.width / (float)beginningTexture.height, 1, 1);



        //                //}


        //                //if (Physics.Raycast(ray, out hit) && hit.transform.name == "inputPlane")
        //                //{



        //                //Debug.Log(pixelUV);

        //                //Mat matCurrent = OpenCvSharp.Unity.TextureToMat(tex);
        //                //Point currPoint = new Point(pixelUV.x, matCurrent.Height - pixelUV.y);



        //                //else if (pointCounter > 3)
        //                //{
        //                //    pointCounter = 0;

        //                //    allSelectedPoints = new Point[4];


        //                //    //inputPlane.GetComponent<Renderer>().material.mainTexture = beginningTexture;
        //                //    //inputPlane.transform.localScale = new Vector3((float)beginningTexture.width / (float)beginningTexture.height, 1, 1);

        //                //    savedTexture = beginningTexture;
        //                //}
        //                if (pointCounter <=4)
        //                {

        //                    matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 100, LineTypes.Link8, 5);


        //                    if (pointCounter>3)
        //                    {
        //                        pointCounter = 0;

        //                        allSelectedPoints = new Point[4];

        //                        matCurrent = OpenCvSharp.Unity.TextureToMat(beginningTexture);

        //                        matCurrent.DrawMarker(currPoint.X, currPoint.Y, new Scalar(0, 0, 255), MarkerStyle.Cross, 100, LineTypes.Link8, 5);
        //                    }


        //                    allSelectedPoints[pointCounter] = currPoint;

        //                    textureNew = OpenCvSharp.Unity.MatToTexture(matCurrent);



        //                    inputPlane.GetComponent<Renderer>().material.mainTexture = textureNew;
        //                    inputPlane.transform.localScale = new Vector3((float)textureNew.width / (float)textureNew.height, 1, 1);

        //                    savedTexture = textureNew;



        //                }
        //                if (pointCounter == 3)
        //                {
        //                    Debug.Log("HERE");
        //                    Mat resultContoured = null;
        //                    resultContoured = JustImageContour(matCurrent, allSelectedPoints);

        //                    Texture2D contourTexture = OpenCvSharp.Unity.MatToTexture(resultContoured);

        //                    inputPlane.GetComponent<Renderer>().material.mainTexture = contourTexture;
        //                    inputPlane.transform.localScale = new Vector3((float)resultContoured.Width / (float)resultContoured.Height, 1, 1);


        //                    savedTexture = contourTexture;



        //                }


        //                pointCounter++;

        //                //if (pointCounter > 3)
        //                //{
        //                //    //pointCounter = 0;

        //                //    //allSelectedPoints = new Point[4];


        //                //    //inputPlane.GetComponent<Renderer>().material.mainTexture = beginningTexture;
        //                //    //inputPlane.transform.localScale = new Vector3((float)beginningTexture.width / (float)beginningTexture.height, 1, 1);

        //                //   // savedTexture = beginningTexture;
        //                //}



        //                //tex.SetPixel((int)pixelUV.x, (int)pixelUV.y, Color.black);
        //                //tex.Apply();
        //                //}
        //            }


        //        }

        //        if (Input.GetMouseButtonDown(1))
        //        {
        //            pointCounter = 0;
        //            allSelectedPoints = new Point[4];


        //            inputPlane.GetComponent<Renderer>().material.mainTexture = beginningTexture;
        //            inputPlane.transform.localScale = new Vector3((float)beginningTexture.width / (float)beginningTexture.height, 1, 1);

        //        }



        //    }


        //}
    }
Beispiel #12
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="ptvec"></param>
 public void Insert(Point2f[] ptvec)
 {
     if(disposed)
         throw new ObjectDisposedException("Subdiv2D", "");
     if(ptvec == null)
         throw new ArgumentNullException("ptvec");
     NativeMethods.imgproc_Subdiv2D_insert(ptr, ptvec, ptvec.Length);
 }
Beispiel #13
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="facetList"></param>
        /// <param name="facetCenters"></param>
        public void GetVoronoiFacetList(IEnumerable<int> idx, out Point2f[][] facetList, out Point2f[] facetCenters)
        {
            if (disposed)
                throw new ObjectDisposedException("Subdiv2D", "");

            IntPtr facetListPtr, facetCentersPtr;
            if (idx == null)
            {
                NativeMethods.imgproc_Subdiv2D_getVoronoiFacetList(ptr, IntPtr.Zero, 0, out facetListPtr, out facetCentersPtr);
            }
            else
            {
                int[] idxArray = EnumerableEx.ToArray(idx);
                NativeMethods.imgproc_Subdiv2D_getVoronoiFacetList(ptr, idxArray, idxArray.Length, out facetListPtr, out facetCentersPtr);
            }

            using (VectorOfVectorPoint2f facetListVec = new VectorOfVectorPoint2f(facetListPtr))
            {
                facetList = facetListVec.ToArray();
            }
            using (VectorOfPoint2f facetCentersVec = new VectorOfPoint2f(facetCentersPtr))
            {
                facetCenters = facetCentersVec.ToArray();
            }
        }
 /// <summary>
 /// Retrieves a pixel rectangle from an image with sub-pixel accuracy.
 /// </summary>
 /// <param name="image">Source image.</param>
 /// <param name="patchSize">Size of the extracted patch.</param>
 /// <param name="center">Floating point coordinates of the center of the extracted rectangle 
 /// within the source image. The center must be inside the image.</param>
 /// <param name="patch">Extracted patch that has the size patchSize and the same number of channels as src .</param>
 /// <param name="patchType">Depth of the extracted pixels. By default, they have the same depth as src.</param>
 public static void GetRectSubPix(InputArray image, Size patchSize, Point2f center, 
     OutputArray patch, int patchType = -1)
 {
     if (image == null)
         throw new ArgumentNullException("image");
     if (patch == null)
         throw new ArgumentNullException("patch");
     image.ThrowIfDisposed();
     patch.ThrowIfNotReady();
     NativeMethods.imgproc_getRectSubPix(image.CvPtr, patchSize, center, patch.CvPtr, patchType);
     patch.Fix();
 }
 /// <summary>
 /// Checks if the point is inside the contour. Optionally computes the signed distance from the point to the contour boundary
 /// </summary>
 /// <param name="contour"></param>
 /// <param name="pt"></param>
 /// <param name="measureDist"></param>
 /// <returns></returns>
 public static double PointPolygonTest(InputArray contour, Point2f pt, bool measureDist)
 {
     if (contour == null)
         throw new ArgumentNullException("contour");
     contour.ThrowIfDisposed();
     return NativeMethods.imgproc_pointPolygonTest_InputArray(contour.CvPtr, pt, measureDist ? 1 : 0);
 }
Beispiel #16
0
 public static void Scale(ref Point2f value, double factor)
 {
     value.X *= (float)factor;
     value.Y *= (float)factor;
 }
Beispiel #17
0
 public static DB::UV ToUV(this Point2f value, double factor)
 {
     return(factor == 1.0 ?
            new DB::UV(value.X, value.Y) :
            new DB::UV(value.X * factor, value.Y * factor));
 }
Beispiel #18
0
        public static DB::UV ToUV(this Point2f value)
        {
            double factor = UnitConverter.ToHostUnits;

            return(new DB::UV(value.X * factor, value.Y * factor));
        }
Beispiel #19
0
        public static Dictionary <string, double> Find(Mat img, System.Windows.Point squares, System.Windows.Point roiCenter, System.Windows.Point roiSize, bool export)
        {
            using var _img = img.Clone();
            int im_width  = _img.Cols / 2;
            int im_height = _img.Rows / 2;

            var clarity = VarianceOfLaplacian(img);

            var roi = new Rect(im_width - (int)roiCenter.X - (int)roiSize.X / 2, im_height - (int)roiCenter.Y - (int)roiSize.Y / 2, (int)roiSize.X, (int)roiSize.Y);

            using var _img_crop = new Mat(_img, roi);
            using var _img_gray = _img_crop.Clone();
            _img_crop.ConvertTo(_img_gray, -1, 1, 0);

            Cv2.CvtColor(_img_gray, _img_gray, ColorConversionCodes.BGR2GRAY);

            int chessboardCornersPerCol = (int)squares.X - 1;
            int chessboardCornersPerRow = (int)squares.Y - 1;
            var board_sz = new Size(chessboardCornersPerRow, chessboardCornersPerCol);

            bool found = Cv2.FindChessboardCorners(_img_gray, board_sz, out Point2f[] corners, ChessboardFlags.AdaptiveThresh | ChessboardFlags.NormalizeImage);

            CameraProperties cam = new CameraProperties();

            if (found)
            {
                var       termcrit     = new TermCriteria(CriteriaTypes.Eps | CriteriaTypes.Count, 30, 0.001);
                Point2f[] cornerSubPix = Cv2.CornerSubPix(_img_gray, corners, new Size(11, 11), new Size(-1, -1), termcrit);

                var chessImg = new Mat(_img_crop.Cols, _img_crop.Rows, MatType.CV_8UC3, new Scalar(0.0, 0.0, 0.0, 255.0));


                Cv2.DrawChessboardCorners(chessImg, board_sz, cornerSubPix, found);

                var matCorners = new Mat(rows: chessboardCornersPerRow, cols: chessboardCornersPerCol, type: MatType.CV_32FC2, data: cornerSubPix);

                var pointRow  = matCorners.Reduce(ReduceDimension.Row, ReduceTypes.Avg, -1);
                var pointCol  = matCorners.Reduce(ReduceDimension.Column, ReduceTypes.Avg, -1);
                var matCenter = pointCol.Reduce(ReduceDimension.Row, ReduceTypes.Avg, -1);

                if (export)
                {
                    _ = PrintMatAsync(matCorners);
                }


                var    vecRow = pointRow.At <Point2f>(0, pointRow.Cols - 1) - pointRow.At <Point2f>(0, 0);
                var    vecCol = pointCol.At <Point2f>(pointCol.Rows - 1, 0) - pointCol.At <Point2f>(0, 0);
                double rotationAngleRadians = Math.Abs(vecRow.X) > Math.Abs(vecCol.X) ? Math.Atan(vecRow.Y / vecRow.X) : Math.Atan(vecCol.Y / vecCol.X);

                cam.Rotation = 180 * rotationAngleRadians / Math.PI;

                float[] arrRow = { vecRow.X, vecRow.Y };
                float[] arrCol = { vecCol.X, vecCol.Y };

                var res_x = 1.0 / Math.Sqrt(Cv2.Norm(InputArray.Create(arrRow)) / (chessboardCornersPerRow - 1) * Cv2.Norm(InputArray.Create(arrRow)) / (chessboardCornersPerRow - 1));
                var res_y = 1.0 / Math.Sqrt(Cv2.Norm(InputArray.Create(arrCol)) / (chessboardCornersPerCol - 1) * Cv2.Norm(InputArray.Create(arrCol)) / (chessboardCornersPerCol - 1));

                cam.Resolution = new System.Windows.Point(res_x, res_y);

                var center = new Point2f(matCenter.At <Point2f>(0).X, matCenter.At <Point2f>(0).Y);

                cam.Center = new System.Windows.Point((center.X - roi.Width / 2) * cam.Resolution.X, (center.Y - roi.Height / 2) * cam.Resolution.Y);

                //CameraCalibration(_img_crop, board_sz, cornerSubPix);

                var roi_img = new Mat(img, roi);
                chessImg.CopyTo(roi_img);
            }

            Cv2.Rectangle(img, roi, new Scalar(0, 0, 255));

            var dict = new Dictionary <string, double>
            {
                { "X um / px", cam.Resolution.X * 1000 },
                { "Y um / px", cam.Resolution.Y * 1000 },
                { "Rotation [*]", cam.Rotation },
                { "Center X [um]", cam.Center.X * 1000 },
                { "Center Y [um]", cam.Center.Y * 1000 },
                { "Clarity", clarity }
            };

            return(dict);
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="point"></param>
 /// <returns></returns>
 public static Vec2d ToVec2d(this Point2f point)
 {
     return(new Vec2d(point.X, point.Y));
 }
 /// <summary>
 /// Finds the minimum area circle enclosing a 2D point set.
 /// </summary>
 /// <param name="points">The input 2D point set, represented by CV_32SC2 or CV_32FC2 matrix.</param>
 /// <param name="center">The output center of the circle</param>
 /// <param name="radius">The output radius of the circle</param>
 public static void MinEnclosingCircle(IEnumerable<Point2f> points, out Point2f center, out float radius)
 {
     if (points == null)
         throw new ArgumentNullException("points");
     Point2f[] pointsArray = EnumerableEx.ToArray(points);
     NativeMethods.imgproc_minEnclosingCircle_Point2f(pointsArray, pointsArray.Length, out center, out radius);
 }
Beispiel #22
0
        /// <summary>
        /// renders the QTree in current openGl context
        /// </summary>
        private void RenderingPass(QTreeWrapper qtree, float FOV, float near, float far, Vector3f eyeVector, Point2f position)
        {
            QTree lasTree = qtree.qtree;

            if (lasTree != null)
            {
                lasTree.RenderingPass(FOV, near, far, eyeVector, position - qtree.positionOffset);

                /*
                 *                      Point3D tempPoint = new Point3D();
                 *                      int pointSize = Marshal.SizeOf(tempPoint);
                 *
                 *                      int allowedPointsInMemory = dedicatedPointMemory / pointSize;
                 *
                 *                      float percent = (float)lasTree.numberOfLoadedPoints / (float)allowedPointsInMemory;
                 *
                 *                      //allow discrepancy of 15%
                 *                      if (Math.Abs(percent - LOD) > 0.1)
                 *                      {
                 *                              LOD = (float)allowedPointsInMemory / (float)qtree.lasFile.header.NumberOfPointRecords;
                 *                              //lasTree.CascadeLOD(LOD);
                 *                      }
                 */
            }
        }
        /// <summary>
        /// adjusts the corner locations with sub-pixel accuracy to maximize the certain cornerness criteria
        /// </summary>
        /// <param name="image">Input image.</param>
        /// <param name="inputCorners">Initial coordinates of the input corners and refined coordinates provided for output.</param>
        /// <param name="winSize">Half of the side length of the search window.</param>
        /// <param name="zeroZone">Half of the size of the dead region in the middle of the search zone 
        /// over which the summation in the formula below is not done. It is used sometimes to avoid possible singularities 
        /// of the autocorrelation matrix. The value of (-1,-1) indicates that there is no such a size.</param>
        /// <param name="criteria">Criteria for termination of the iterative process of corner refinement. 
        /// That is, the process of corner position refinement stops either after criteria.maxCount iterations 
        /// or when the corner position moves by less than criteria.epsilon on some iteration.</param>
        /// <returns></returns>
        public static Point2f[] CornerSubPix(InputArray image, IEnumerable<Point2f> inputCorners,
            Size winSize, Size zeroZone, CvTermCriteria criteria)
        {
            if (image == null)
                throw new ArgumentNullException("image");
            if (inputCorners == null)
                throw new ArgumentNullException("inputCorners");
            image.ThrowIfDisposed();

            var inputCornersSrc = Util.ToArray(inputCorners);
            var inputCornersCopy = new Point2f[inputCornersSrc.Length];
            Array.Copy(inputCornersSrc, inputCornersCopy, inputCornersSrc.Length);
            using (var vector = new VectorOfPoint2f(inputCornersCopy))
            {
                NativeMethods.imgproc_cornerSubPix(image.CvPtr, vector.CvPtr, winSize, zeroZone, criteria);
                return vector.ToArray();
            }
        }
Beispiel #24
0
 static bool Contains(Contour contour, Point2f point)
 {
     return(contour != null && CV.PointPolygonTest(contour, point, false) > 0);
 }
Beispiel #25
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="pt"></param>
 /// <returns></returns>
 public int FindNearest(Point2f pt)
 {
     Point2f nearestPt;
     return FindNearest(pt, out nearestPt);
 }
        void Canvas_KeyDown(object sender, KeyEventArgs e)
        {
            if (dragging)
            {
                return;
            }
            if (e.KeyCode == Keys.PageUp)
            {
                ImageScale += ScaleIncrement;
            }
            if (e.KeyCode == Keys.PageDown)
            {
                ImageScale -= ScaleIncrement;
            }
            if (e.Control && e.KeyCode == Keys.Z)
            {
                commandExecutor.Undo();
            }
            if (e.Control && e.KeyCode == Keys.Y)
            {
                commandExecutor.Redo();
            }
            if (e.Control && e.KeyCode == Keys.V)
            {
                var roiText = (string)Clipboard.GetData(DataFormats.Text);
                try
                {
                    var mousePosition = PointToClient(MousePosition);
                    var offset        = NormalizedLocation(mousePosition.X, mousePosition.Y);
                    var roiData       = (float[])ArrayConvert.ToArray(roiText, 1, typeof(float));
                    var center        = new Point2f(offset.X, offset.Y);
                    var size          = new Size2f(roiData[0], roiData[1]);
                    var roi           = new RotatedRect(center, size, 0);

                    var selection = selectedRoi;
                    commandExecutor.Execute(
                        () => AddRegion(roi),
                        () => { regions.Remove(roi); SelectedRegion = selection; });
                }
                catch (ArgumentException) { }
                catch (InvalidCastException) { }
                catch (FormatException) { }
            }

            if (selectedRoi.HasValue)
            {
                if (e.Control && e.KeyCode == Keys.C)
                {
                    var roi     = regions[selectedRoi.Value];
                    var roiData = new[] { roi.Size.Width, roi.Size.Height };
                    Clipboard.SetData(DataFormats.Text, ArrayConvert.ToString(roiData));
                }

                if (e.KeyCode == Keys.Delete)
                {
                    var selection = selectedRoi.Value;
                    var region    = regions[selection];
                    commandExecutor.Execute(
                        () => { regions.RemoveAt(selection); SelectedRegion = null; },
                        () => { regions.Insert(selection, region); SelectedRegion = selection; });
                }
            }
        }
Beispiel #27
0
        /// <summary>
        /// Finds centers in the grid of circles.
        /// </summary>
        /// <param name="image">grid view of input circles; it must be an 8-bit grayscale or color image.</param>
        /// <param name="patternSize">number of circles per row and column ( patternSize = Size(points_per_row, points_per_colum) ).</param>
        /// <param name="centers">output array of detected centers.</param>
        /// <param name="flags">various operation flags that can be one of the FindCirclesGridFlag values</param>
        /// <param name="blobDetector">feature detector that finds blobs like dark circles on light background.</param>
        /// <returns></returns>
        public static bool FindCirclesGrid(
            InputArray image,
            Size patternSize,
            out Point2f[] centers,
            FindCirclesGridFlag flags = FindCirclesGridFlag.SymmetricGrid,
            FeatureDetector blobDetector = null)
        {
            if (image == null)
                throw new ArgumentNullException("image");
            image.ThrowIfDisposed();

            using (var centersVec = new VectorOfPoint2f())
            {
                int ret = NativeMethods.calib3d_findCirclesGrid_InputArray(
                image.CvPtr, patternSize, centersVec.CvPtr, (int)flags, ToPtr(blobDetector));
                centers = centersVec.ToArray();
                return ret != 0;
            }
        }
Beispiel #28
0
        Vector3 triangulate(int j, HyperMegaStuff.HyperMegaLines drawer = null)
        {
            Ray[] rays         = new Ray[2];
            Mat   workingImage = new Mat(calibrationDevices[j].webcam.leftImage.Height,
                                         calibrationDevices[j].webcam.leftImage.Width,
                                         calibrationDevices[j].webcam.leftImage.Type(), 0);

            for (int i = 0; i < 2; i++)
            {
                Mat curMat = i == 0 ? calibrationDevices[j].webcam.leftImage :
                             calibrationDevices[j].webcam.rightImage;

                if (calibrationDevices[j].subtractionImage[i] != null)
                {
                    // Subtract the background from the curMat
                    Cv2.Subtract(curMat, calibrationDevices[j].subtractionImage[i], workingImage);

                    // Threshold the image to separate black and white
                    Cv2.Threshold(workingImage, workingImage, blobThreshold, 255, ThresholdTypes.BinaryInv); // TODO MAKE THRESHOLD TUNABLE

                    // Detect Blobs using the Mask
                    var settings = new SimpleBlobDetector.Params();
                    settings.FilterByArea        = false;
                    settings.FilterByColor       = false;
                    settings.FilterByInertia     = true;
                    settings.FilterByConvexity   = true;
                    settings.FilterByCircularity = false;
                    SimpleBlobDetector detector = SimpleBlobDetector.Create();
                    KeyPoint[]         blobs    = detector.Detect(workingImage, calibrationDevices[j].maskImage[i]);
                    Cv2.DrawKeypoints(workingImage, blobs, workingImage, 255);
                    int biggest = -1; float size = 0;
                    for (int k = 0; k < blobs.Length; k++)
                    {
                        if (blobs[k].Size > size)
                        {
                            biggest = k;
                            size    = blobs[k].Size;
                        }
                    }

                    // If there's only one blob in this image, assume it's the white circle
                    if (blobs.Length > 0)
                    {
                        float[] pointArr         = { blobs[biggest].Pt.X, blobs[biggest].Pt.Y };
                        Mat     point            = new Mat(1, 1, MatType.CV_32FC2, pointArr);
                        Mat     undistortedPoint = new Mat(1, 1, MatType.CV_32FC2, 0);
                        Cv2.UndistortPoints(point, undistortedPoint, calibrationDevices[j].calibration.cameras[i].cameraMatrixMat,
                                            calibrationDevices[j].calibration.cameras[i].distCoeffsMat,
                                            calibrationDevices[j].calibration.cameras[i].rectificationMatrixMat);
                        Point2f[] rectilinear = new Point2f[1];
                        undistortedPoint.GetArray(0, 0, rectilinear);
                        Transform camera = i == 0 ? calibrationDevices[j].LeftCamera : calibrationDevices[j].RightCamera;
                        rays[i] = new Ray(camera.position, camera.TransformDirection(
                                              new Vector3(-rectilinear[0].X, rectilinear[0].Y, 1f)));
                        if (drawer != null)
                        {
                            drawer.color = ((j == 0) != (i == 0)) ? Color.cyan : Color.red;
                            drawer.DrawRay(rays[i].origin, rays[i].direction);
                        }
                    }
                }
            }
            workingImage.Release();

            // Only accept the triangulated point if the rays match up closely enough
            if (rays[0].origin != Vector3.zero &&
                rays[1].origin != Vector3.zero)
            {
                Vector3 point1 = RayRayIntersection(rays[0], rays[1]);
                Vector3 point2 = RayRayIntersection(rays[1], rays[0]);

                if (Vector3.Distance(point1, point2) < 0.005f)
                {
                    return((point1 + point2) * 0.5f);
                }
                else
                {
                    return(Vector3.zero);
                }
            }
            else
            {
                return(Vector3.zero);
            }
        }
Beispiel #29
0
 /// <summary>
 /// Finds the minimum area circle enclosing a 2D point set.
 /// The input is 2D point set, represented by CV_32SC2 or CV_32FC2 matrix.
 /// </summary>
 /// <param name="center">The output center of the circle</param>
 /// <param name="radius">The output radius of the circle</param>
 public void MinEnclosingCircle(out Point2f center, out float radius)
 {
     Cv2.MinEnclosingCircle(this, out center, out radius);
 }
Beispiel #30
0
 public static Vector2 ToHost(this Point2f point)
 {
     return(new Vector2(point.X, point.Y));
 }
Beispiel #31
0
 public static extern ExceptionStatus core_FileNode_read_Point2f(IntPtr node, out Point2f returnValue);
Beispiel #32
0
        public override IObservable <KeyPointOpticalFlow> Process(IObservable <Tuple <KeyPointCollection, IplImage> > source)
        {
            return(Observable.Defer(() =>
            {
                IplImage previousImage = null;
                IplImage previousPyramid = null;
                IplImage currentPyramid = null;
                return source.Select(input =>
                {
                    var previous = input.Item1;
                    var currentImage = input.Item2;
                    var currentKeyPoints = new KeyPointCollection(currentImage);
                    if (previous.Count == 0)
                    {
                        return new KeyPointOpticalFlow(previous, currentKeyPoints);
                    }

                    if (currentPyramid == null || currentPyramid.Size != currentImage.Size)
                    {
                        previousImage = null;
                        previousPyramid = new IplImage(currentImage.Size, currentImage.Depth, currentImage.Channels);
                        currentPyramid = new IplImage(currentImage.Size, currentImage.Depth, currentImage.Channels);
                    }

                    var maxIterations = MaxIterations;
                    var epsilon = Epsilon;
                    var terminationType = TermCriteriaType.None;
                    if (maxIterations > 0)
                    {
                        terminationType |= TermCriteriaType.MaxIter;
                    }
                    if (epsilon > 0)
                    {
                        terminationType |= TermCriteriaType.Epsilon;
                    }
                    var termCriteria = new TermCriteria(terminationType, maxIterations, epsilon);
                    var flags = previousImage == previous.Image ? LKFlowFlags.PyrAReady : LKFlowFlags.None;

                    var previousFeatures = new Point2f[previous.Count];
                    for (int i = 0; i < previousFeatures.Length; i++)
                    {
                        previousFeatures[i] = previous[i];
                    }

                    var currentFeatures = new Point2f[previousFeatures.Length];
                    var status = new byte[previousFeatures.Length];
                    var trackError = new float[previousFeatures.Length];
                    CV.CalcOpticalFlowPyrLK(
                        previous.Image,
                        currentImage,
                        previousPyramid,
                        currentPyramid,
                        previousFeatures,
                        currentFeatures,
                        WindowSize,
                        Level,
                        status,
                        trackError,
                        termCriteria,
                        flags);

                    var previousKeyPoints = new KeyPointCollection(previous.Image);
                    for (int i = 0; i < status.Length; i++)
                    {
                        if (status[i] == 0 ||
                            trackError[i] > MaxError ||
                            currentFeatures[i].X <0 ||
                                                  currentFeatures[i].Y <0 ||
                                                                        currentFeatures[i].X> currentImage.Width - 1 ||
                                                  currentFeatures[i].Y> currentImage.Height - 1)
                        {
                            continue;
                        }

                        previousKeyPoints.Add(previousFeatures[i]);
                        currentKeyPoints.Add(currentFeatures[i]);
                    }

                    var temp = currentPyramid;
                    currentPyramid = previousPyramid;
                    previousPyramid = temp;
                    previousImage = currentImage;
                    return new KeyPointOpticalFlow(previousKeyPoints, currentKeyPoints);
                });
            }));
        }
Beispiel #33
0
        static void Main1(string[] args)
        {
            Process[] processes  = Process.GetProcesses();
            Process   wzqProcess = null;

            foreach (var item in processes)
            {
                if (item.MainWindowTitle == "五子棋")
                {
                    Console.WriteLine(item.ProcessName);
                    Console.WriteLine(item.Id);
                    //窗口名
                    Console.WriteLine(item.MainWindowTitle);
                    Console.WriteLine(item.MainModule.FileName);
                    Console.WriteLine(item.MainModule.FileVersionInfo.FileVersion);
                    Console.WriteLine(item.MainModule.FileVersionInfo.FileDescription);
                    Console.WriteLine(item.MainModule.FileVersionInfo.Comments);
                    Console.WriteLine(item.MainModule.FileVersionInfo.CompanyName);
                    Console.WriteLine(item.MainModule.FileVersionInfo.FileName);
                    //产品名
                    Console.WriteLine(item.MainModule.FileVersionInfo.ProductName);
                    Console.WriteLine(item.MainModule.FileVersionInfo.ProductVersion);
                    Console.WriteLine(item.StartTime);
                    Console.WriteLine(item.MainWindowHandle);
                    wzqProcess = item;
                    break;
                }
            }
            Bitmap bitmap = CaptureImage.Captuer(wzqProcess);

            if (bitmap == null)
            {
                return;
            }

            //bitmap.Save("a.bmp");
            //Process.Start("mspaint", "a.bmp");
            //左上角
            //227 129
            //右下角
            //721 621
            int width  = 721 - 227;
            int height = 621 - 129;
            int step   = width * 15 / 14 / 15;

            Bitmap   wzqBoardImage = new Bitmap(width * 15 / 14, height * 15 / 14);
            Graphics g             = Graphics.FromImage(wzqBoardImage);

            //
            // 摘要:
            //     在指定位置并且按指定大小绘制指定的 System.Drawing.Image 的指定部分。
            //
            // 参数:
            //   image:
            //     要绘制的 System.Drawing.Image。
            //
            //   destRect:
            //     System.Drawing.Rectangle 结构,它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。
            //
            //   srcRect:
            //     System.Drawing.Rectangle 结构,它指定 image 对象中要绘制的部分。
            //
            //   srcUnit:
            //     System.Drawing.GraphicsUnit 枚举的成员,它指定 srcRect 参数所用的度量单位。
            g.DrawImage(bitmap,
                        new Rectangle(0, 0, wzqBoardImage.Width, wzqBoardImage.Height),
                        new Rectangle(227 - step / 2, 129 - step / 2, wzqBoardImage.Width, wzqBoardImage.Height),
                        GraphicsUnit.Pixel);
            g.Dispose();

            //把Bitmap转换成Mat
            Mat boardMat = BitmapConverter.ToMat(wzqBoardImage);

            //因为霍夫圆检测对噪声比较敏感,所以首先对图像做一个中值滤波或高斯滤波(噪声如果没有可以不做)
            Mat blurBoardMat = new Mat();

            Cv2.MedianBlur(boardMat, blurBoardMat, 9);

            //转为灰度图像
            Mat grayBoardMat = new Mat();

            Cv2.CvtColor(blurBoardMat, grayBoardMat, ColorConversionCodes.BGR2GRAY);

            //3:霍夫圆检测:使用霍夫变换查找灰度图像中的圆。
            CircleSegment[] circleSegments = Cv2.HoughCircles(grayBoardMat, HoughMethods.Gradient, 1, step * 0.4, 70, 30, (int)(step * 0.3), (int)(step * 0.5));

            foreach (var circleSegment in circleSegments)
            {
                Cv2.Circle(boardMat, (int)circleSegment.Center.X, (int)circleSegment.Center.Y, (int)circleSegment.Radius, Scalar.Red, 1, LineTypes.AntiAlias);
            }



            //判断棋子位置,遍历棋盘上的每个位置
            int rows = 15;
            List <Tuple <int, int, int> > chessPointList = new List <Tuple <int, int, int> >();
            //计算棋子颜色的阈值
            Scalar scalarLower = new Scalar(128, 128, 128);
            Scalar scalarUpper = new Scalar(255, 255, 255);

            //行
            for (int i = 0; i < rows; i++)
            {
                //列
                for (int j = 0; j < rows; j++)
                {
                    //棋盘棋子坐标
                    Point2f point = new Point2f(j * step + 0.5f * step, i * step + 0.5f * step);
                    foreach (var circleSegment in circleSegments)
                    {
                        //有棋子
                        if (circleSegment.Center.DistanceTo(point) < 0.5 * step)
                        {
                            //检查棋子的颜色
                            //以棋子中心为中心点,截取一部分图片(圆内切正方形),来计算图片颜色
                            //r^2 = a^2 + a^2
                            //--> a= ((r^2)/2)^-2

                            double len       = Math.Sqrt(circleSegment.Radius * circleSegment.Radius / 2);
                            Rect   rect      = new Rect((int)(circleSegment.Center.X - len), (int)(circleSegment.Center.Y - len), (int)(len * 2), (int)(len * 2));
                            Mat    squareMat = new Mat(grayBoardMat, rect);

                            //计算颜色
                            Mat calculatedMat = new Mat();
                            Cv2.InRange(squareMat, scalarLower, scalarUpper, calculatedMat);
                            float result = 100f * Cv2.CountNonZero(calculatedMat) / (calculatedMat.Width * calculatedMat.Height);

                            chessPointList.Add(new Tuple <int, int, int>(i + 1, j + 1, result < 50 ? 0 : 1));
                            break;
                        }
                    }
                }
            }

            foreach (var item in chessPointList)
            {
                Console.WriteLine($"{item.Item1},{item.Item2},{item.Item3}");
            }
            Cv2.ImShow("boardMat", boardMat);
            Cv2.WaitKey();
        }
Beispiel #34
0
        /// <summary>
        /// computes sparse optical flow using multi-scale Lucas-Kanade algorithm
        /// </summary>
        /// <param name="prevImg"></param>
        /// <param name="nextImg"></param>
        /// <param name="prevPts"></param>
        /// <param name="nextPts"></param>
        /// <param name="status"></param>
        /// <param name="err"></param>
        /// <param name="winSize"></param>
        /// <param name="maxLevel"></param>
        /// <param name="criteria"></param>
        /// <param name="flags"></param>
        /// <param name="minEigThreshold"></param>
        public static void CalcOpticalFlowPyrLK(
            InputArray prevImg, InputArray nextImg,
            Point2f[] prevPts, ref Point2f[] nextPts,
            out byte[] status, out float[] err,
            Size? winSize = null,
            int maxLevel = 3,
            TermCriteria? criteria = null,
            OpticalFlowFlags flags = OpticalFlowFlags.None,
            double minEigThreshold = 1e-4)
        {
            if (prevImg == null)
                throw new ArgumentNullException("prevImg");
            if (nextImg == null)
                throw new ArgumentNullException("nextImg");
            if (prevPts == null)
                throw new ArgumentNullException("prevPts");
            if (nextPts == null)
                throw new ArgumentNullException("nextPts");
            prevImg.ThrowIfDisposed();
            nextImg.ThrowIfDisposed();

            Size winSize0 = winSize.GetValueOrDefault(new Size(21, 21));
            TermCriteria criteria0 = criteria.GetValueOrDefault(
                TermCriteria.Both(30, 0.01));

            using (var nextPtsVec = new VectorOfPoint2f())
            using (var statusVec = new VectorOfByte())
            using (var errVec = new VectorOfFloat())
            {
                NativeMethods.video_calcOpticalFlowPyrLK_vector(
                    prevImg.CvPtr, nextImg.CvPtr, prevPts, prevPts.Length,
                    nextPtsVec.CvPtr, statusVec.CvPtr, errVec.CvPtr, 
                    winSize0, maxLevel, criteria0, (int)flags, minEigThreshold);
                nextPts = nextPtsVec.ToArray();
                status = statusVec.ToArray();
                err = errVec.ToArray();
            }
        }
        //MyMSER
        static List <Point[][]> My_MSER(int my_delta, int my_minArea, int my_maxArea, double my_maxVariation, Mat img, ref Mat img_rgb, int big_flag)
        {
            //img.SaveImage("img_detected.jpg");

            List <Point[][]> final_area = new List <Point[][]>();

            Point[][] contours;
            Rect[]    bboxes;
            MSER      mser = MSER.Create(delta: my_delta, minArea: my_minArea, maxArea: my_maxArea, maxVariation: my_maxVariation);

            mser.DetectRegions(img, out contours, out bboxes);

            //====================================Local Majority Vote

            // to speed up, create four shift image first
            var shift_mat = set_shift_image(ref img);

            Mat[] neighbor_img = new Mat[4];
            for (int i = 0; i < 4; i++)
            {
                neighbor_img[i] = new Mat();
                var imageCenter = new Point2f(img.Cols / 2f, img.Rows / 2f);
                var rotationMat = Cv2.GetRotationMatrix2D(imageCenter, 100, 1.3);
                Cv2.WarpAffine(img, neighbor_img[i], shift_mat[i], img.Size());
                //neighbor_img[i].SaveImage("./shift_image" + i + ".jpg");
            }

            //for each contour, apply local majority vote
            foreach (Point[] now_contour in contours)
            {
                OpenCvSharp.Point[][] temp = new Point[1][];

                Point[] Convex_hull = Cv2.ConvexHull(now_contour);
                Point[] Approx      = Cv2.ApproxPolyDP(now_contour, 0.5, true);

                RotatedRect rotateRect = Cv2.MinAreaRect(Approx);
                //Debug
                //Console.WriteLine(Cv2.ContourArea(Approx)+" "+ rotateRect.Size.Height / rotateRect.Size.Width+ " "+rotateRect.Size.Width / rotateRect.Size.Height);

                if (Cv2.ContourArea(Approx) > 10000 || (Cv2.ContourArea(Approx) < stop1_inner_defect_size_min || ((rotateRect.Size.Height / rotateRect.Size.Width)) > stop1_arclength_area_ratio || ((rotateRect.Size.Width / rotateRect.Size.Height)) > stop1_arclength_area_ratio))
                {
                    continue;
                }

                //======================intensity in the area
                temp[0] = Approx;
                double mean_in_area_temp = 0, min_in_area_temp = 0;
                Mat    mask_img_temp = Mat.Zeros(img.Size(), MatType.CV_8UC1);
                Cv2.DrawContours(mask_img_temp, temp, -1, 255, thickness: -1);//notice the difference between temp = Approx and Convex_hull
                mean_in_area_temp = img.Mean(mask_img_temp)[0];
                img.MinMaxLoc(out min_in_area_temp, out _, out _, out _, mask_img_temp);
                //Console.WriteLine(min_in_area_temp + " " + mean_in_area_temp);
                if (min_in_area_temp > 100 || mean_in_area_temp > 130)
                {
                    continue;
                }

                // Convex hull
                temp[0] = Approx;
                if (big_flag == 0)//small area: local majority vote
                {
                    //Cv2.Polylines(img_rgb, temp, true, new Scalar(0, 0, 255), 1);
                    //inside the area
                    double mean_in_area = 0, min_in_area = 0;
                    Mat    mask_img = Mat.Zeros(img.Size(), MatType.CV_8UC1);
                    Cv2.DrawContours(mask_img, temp, -1, 255, thickness: -1);//notice the difference between temp = Approx and Convex_hull
                    mean_in_area = img.Mean(mask_img)[0];
                    img.MinMaxLoc(out min_in_area, out _, out _, out _, mask_img);

                    //Console.WriteLine(min_in_area + " " + mean_in_area);

                    //test

                    /*
                     * Mat mask2 = img.LessThan(230);
                     * for (int i = 0; i < img.Cols; i++) {
                     *  for (int j = 0; j < img.Rows; j++)
                     *      if(mask2.At<bool>(i, j)==false)
                     *          Console.Write(mask2.At<bool>(i,j)+ " ");
                     *
                     *  Console.Write("\n");
                     *
                     * }
                     */
                    //neighbor
                    double[] mean_neighbor = { 255, 255, 255, 255 };
                    double[] min_neighbor  = { 255, 255, 255, 255 };
                    for (int i = 0; i < 4; i++)
                    {
                        //先把 img > 230 的變成 0,再餵進 shift 裡面
                        //先把 mask 乘上另一個mask(>230的mask)
                        //Mat mask_neighbor_img = neighbor_img[i].GreaterThan(0);
                        //Console.WriteLine(mask_neighbor_img.At<int>(0,1));
                        // create final mask
                        Mat mask2 = neighbor_img[i].LessThan(225).ToMat();
                        mask2.ConvertTo(mask2, MatType.CV_8U, 1.0 / 255.0);

                        Mat mask_final = Mat.Zeros(img.Size(), MatType.CV_8UC1);
                        mask_img.CopyTo(mask_final, mask2);

                        //mask_final.SaveImage("./mask" + i + ".jpg");

                        mean_neighbor[i] = neighbor_img[i].Mean(mask_final)[0];
                        //compute min:
                        //neighbor_img[i].MinMaxLoc(out min_neighbor[i], out _, out _, out _, mask_img);
                        //Console.WriteLine(min_neighbor[i] + " " + mean_neighbor[i]);
                    }
                    int vote = 0;
                    for (int i = 0; i < 4; i++)
                    {
                        if (mean_in_area > mean_neighbor[i])
                        {
                            vote++;
                        }
                    }
                    if (vote > 2 || min_in_area > 100 || mean_in_area > 130)
                    {
                        //Debug
                        //Console.WriteLine(vote + " " + min_in_area + " ", min_in_area);
                        continue;
                    }
                    else
                    {
                        //Cv2.Polylines(img_rgb, temp, true, new Scalar(0, 0, 255), 1);
                        //Console.WriteLine("--");
                        final_area.Add(temp);
                    }
                }
                else
                {
                    //Console.WriteLine("--");
                    //Cv2.Polylines(img_rgb, temp, true, new Scalar(0, 0, 255), 1);
                    final_area.Add(temp);
                }
            }
            Console.WriteLine(final_area.Count);
            return(final_area);
        }
 public static double[] ToArray(this Point2f pt)
 {
     return(new double[] { pt.X, pt.Y });
 }
 public static extern void video_calcOpticalFlowPyrLK_vector(
     IntPtr prevImg, IntPtr nextImg,
     Point2f[] prevPts, int prevPtsSize,
     IntPtr nextPts, IntPtr status, IntPtr err,
     Size winSize, int maxLevel, TermCriteria criteria,
     int flags, double minEigThreshold);
Beispiel #38
0
 internal static extern void cvLogPolar(Arr src, Arr dst, Point2f center, double M, WarpFlags flags);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="center"></param>
        /// <param name="angle"></param>
        /// <param name="scale"></param>
        /// <returns></returns>
        public static Mat GetRotationMatrix2D(Point2f center, double angle, double scale)
        {
            IntPtr ret = NativeMethods.imgproc_getRotationMatrix2D(center, angle, scale);
            return new Mat(ret);

        }
Beispiel #40
0
 internal static extern void cvLinearPolar(Arr src, Arr dst, Point2f center, double maxRadius, WarpFlags flags);
 /// <summary>
 /// Finds the minimum area circle enclosing a 2D point set.
 /// </summary>
 /// <param name="points">The input 2D point set, represented by CV_32SC2 or CV_32FC2 matrix.</param>
 /// <param name="center">The output center of the circle</param>
 /// <param name="radius">The output radius of the circle</param>
 public static void MinEnclosingCircle(InputArray points, out Point2f center, out float radius)
 {
     if (points == null)
         throw new ArgumentNullException("points");
     points.ThrowIfDisposed();
     NativeMethods.imgproc_minEnclosingCircle_InputArray(points.CvPtr, out center, out radius);
 }
Beispiel #42
0
 internal static extern void cvGetRectSubPix(Arr src, Arr dst, Point2f center);
        /// <summary>
        /// finds intersection of two convex polygons
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <param name="p12"></param>
        /// <param name="handleNested"></param>
        /// <returns></returns>
        public static float IntersectConvexConvex(IEnumerable<Point2f> p1, IEnumerable<Point2f> p2,
            out Point2f[] p12, bool handleNested = true)
        {
            if (p1 == null)
                throw new ArgumentNullException("p1");
            if (p2 == null)
                throw new ArgumentNullException("p2");
            Point2f[] p1Array = EnumerableEx.ToArray(p1);
            Point2f[] p2Array = EnumerableEx.ToArray(p2);
            IntPtr p12Ptr;
            float ret = NativeMethods.imgproc_intersectConvexConvex_Point2f(p1Array, p1Array.Length, p2Array, p2Array.Length,
                out p12Ptr, handleNested ? 1 : 0);

            using (var p12Vec = new VectorOfPoint2f(p12Ptr))
            {
                p12 = p12Vec.ToArray();
            }

            return ret;
        }
Beispiel #44
0
 internal static extern int cvMinEnclosingCircle(CVHandle points, out Point2f center, out float radius);
 /// <summary>
 /// Checks if the point is inside the contour. 
 /// Optionally computes the signed distance from the point to the contour boundary.
 /// </summary>
 /// <param name="contour">Input contour.</param>
 /// <param name="pt">Point tested against the contour.</param>
 /// <param name="measureDist">If true, the function estimates the signed distance 
 /// from the point to the nearest contour edge. Otherwise, the function only checks 
 /// if the point is inside a contour or not.</param>
 /// <returns>Positive (inside), negative (outside), or zero (on an edge) value.</returns>
 public static double PointPolygonTest(IEnumerable<Point2f> contour, Point2f pt, bool measureDist)
 {
     if (contour == null)
         throw new ArgumentNullException("contour");
     Point2f[] contourArray = EnumerableEx.ToArray(contour);
     return NativeMethods.imgproc_pointPolygonTest_Point2f(contourArray, contourArray.Length, pt, measureDist ? 1 : 0);
 }
Beispiel #46
0
 internal static extern double cvPointPolygonTest(CVHandle contour, Point2f pt, int measure_dist);
Beispiel #47
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="pt"></param>
 /// <returns></returns>
 public int Insert(Point2f pt)
 {
     if(disposed)
         throw new ObjectDisposedException("Subdiv2D", "");
     return NativeMethods.imgproc_Subdiv2D_insert(ptr, pt);
 }
Beispiel #48
0
 internal static extern IntPtr cv2DRotationMatrix(Point2f center, double angle, double scale, Mat map_matrix);
Beispiel #49
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="pt"></param>
 /// <param name="edge"></param>
 /// <param name="vertex"></param>
 /// <returns></returns>
 public int Locate(Point2f pt, out int edge, out int vertex)
 {
     if (disposed)
         throw new ObjectDisposedException("Subdiv2D", "");
     return NativeMethods.imgproc_Subdiv2D_locate(ptr, pt, out edge, out vertex);
 }
Beispiel #50
0
        public void Locate(Mat mat, MyFlags localiseFlags)
        {
            if (mat == null || mat.Empty())
            {
                return;
            }
            if (localiseFlags == null)
            {
                return;
            }
            using (Mat hsv = new Mat())
                using (Mat car1 = new Mat())
                    using (Mat car2 = new Mat())
                        using (Mat merged = new Mat())
                            using (Mat black = new Mat(mat.Size(), MatType.CV_8UC1))
                            {
                                Cv2.CvtColor(mat, hsv, ColorConversionCodes.RGB2HSV);
                                MyFlags.LocConfigs configs = localiseFlags.configs;
                                Cv2.InRange(hsv,
                                            new Scalar(configs.hue1Lower, configs.saturation1Lower, configs.valueLower),
                                            new Scalar(configs.hue1Upper, 255, 255),
                                            car1);
                                Cv2.InRange(hsv,
                                            new Scalar(configs.hue2Lower, configs.saturation2Lower, configs.valueLower),
                                            new Scalar(configs.hue2Upper, 255, 255),
                                            car2);

                                Point2i[][] contours1, contours2;

                                contours1 = Cv2.FindContoursAsArray(car1, RetrievalModes.External, ContourApproximationModes.ApproxSimple);
                                contours2 = Cv2.FindContoursAsArray(car2, RetrievalModes.External, ContourApproximationModes.ApproxSimple);

                                foreach (Point2i[] c1 in contours1)
                                {
                                    Point2i centre  = new Point2i();
                                    Moments moments = Cv2.Moments(c1);
                                    centre.X = (int)(moments.M10 / moments.M00);
                                    centre.Y = (int)(moments.M01 / moments.M00);
                                    double area = moments.M00;
                                    if (area < configs.areaLower)
                                    {
                                        continue;
                                    }
                                    centres1.Add(centre);
                                }
                                foreach (Point2i[] c2 in contours2)
                                {
                                    Point2i centre  = new Point2f();
                                    Moments moments = Cv2.Moments(c2);
                                    centre.X = (int)(moments.M10 / moments.M00);
                                    centre.Y = (int)(moments.M01 / moments.M00);
                                    double area = moments.M00;
                                    if (area < configs.areaLower)
                                    {
                                        continue;
                                    }
                                    centres2.Add(centre);
                                }

                                foreach (Point2i c1 in centres1)
                                {
                                    Cv2.Circle(mat, c1, 10, new Scalar(0x1b, 0xff, 0xa7), -1);
                                }
                                foreach (Point2i c2 in centres2)
                                {
                                    Cv2.Circle(mat, c2, 10, new Scalar(0x00, 0x98, 0xff), -1);
                                }
                                if (localiseFlags.gameState != GameState.Unstart)
                                {
                                    for (int i = 0; i < localiseFlags.currPassengerNum; ++i)
                                    {
                                        int  x1       = localiseFlags.posPassengerEnd[i].X;
                                        int  y1       = localiseFlags.posPassengerEnd[i].Y;
                                        int  x2       = localiseFlags.posPassengerStart[i].X;
                                        int  y2       = localiseFlags.posPassengerStart[i].Y;
                                        int  x10      = x1 - 8;
                                        int  y10      = y1 - 8;
                                        int  x20      = x2 - 8;
                                        int  y20      = y2 - 8;
                                        Rect rectDest = new Rect(x10, y10, 16, 16);
                                        Rect rectSrc  = new Rect(x20, y20, 16, 16);
                                        switch (localiseFlags.passengerState[i])
                                        {
                                        case Camp.CampA:
                                            Cv2.Rectangle(mat, rectDest, new Scalar(0x1b, 0xff, 0xa7), -1);
                                            break;

                                        case Camp.CampB:
                                            Cv2.Rectangle(mat, rectDest, new Scalar(0x00, 0x98, 0xff), -1);
                                            break;

                                        case Camp.None:
                                            // Cv2.Circle(mat, localiseFlags.posPassengerStart[i], 5, new Scalar(0xd8, 0x93, 0xce), -1);
                                            if (i != 4)
                                            {
                                                Cv2.Rectangle(mat, rectSrc, new Scalar(0xf3, 0x96, 0x21), -1);
                                            }
                                            else
                                            {
                                                Cv2.Rectangle(mat, rectSrc, new Scalar(0x58, 0xee, 0xff), -1);
                                            }
                                            break;

                                        default:
                                            break;
                                        }
                                    }
                                }

                                Cv2.Merge(new Mat[] { car1, car2, black }, merged);
                                Cv2.ImShow("binary", merged);
                            }
        }
Beispiel #51
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="pt"></param>
 /// <param name="nearestPt"></param>
 /// <returns></returns>
 public int FindNearest(Point2f pt, out Point2f nearestPt)
 {
     if (disposed)
         throw new ObjectDisposedException("Subdiv2D", "");
     return NativeMethods.imgproc_Subdiv2D_findNearest(ptr, pt, out nearestPt);
 }
        glTFLoader.Schema.Buffer CreateTextureCoordinatesBuffer(Rhino.Geometry.Collections.MeshTextureCoordinateList texCoords, out Point2f min, out Point2f max)
        {
            byte[] bytes = GetTextureCoordinatesBytes(texCoords, out min, out max);

            return(new glTFLoader.Schema.Buffer()
            {
                Uri = Constants.TextBufferHeader + Convert.ToBase64String(bytes),
                ByteLength = bytes.Length,
            });
        }
Beispiel #53
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="edge"></param>
 /// <param name="dstpt"></param>
 /// <returns></returns>
 public int EdgeDst(int edge, out Point2f dstpt)
 {
     if (disposed)
         throw new ObjectDisposedException("Subdiv2D", "");
     return NativeMethods.imgproc_Subdiv2D_edgeDst(ptr, edge, out dstpt);
 }
        private byte[] GetTextureCoordinatesBytes(Rhino.Geometry.Collections.MeshTextureCoordinateList texCoords, out Point2f min, out Point2f max)
        {
            Point2f texCoordsMin = new Point2f()
            {
                X = float.PositiveInfinity, Y = float.PositiveInfinity
            };
            Point2f texCoordsMax = new Point2f()
            {
                X = float.NegativeInfinity, Y = float.NegativeInfinity
            };

            List <float> coordinates = new List <float>(texCoords.Count * 2);

            foreach (Point2f coordinate in texCoords)
            {
                coordinates.AddRange(new float[] { coordinate.X, coordinate.Y });

                texCoordsMin.X = Math.Min(texCoordsMin.X, coordinate.X);
                texCoordsMax.X = Math.Max(texCoordsMax.X, coordinate.X);

                texCoordsMin.Y = Math.Min(texCoordsMin.Y, coordinate.Y);
                texCoordsMax.Y = Math.Max(texCoordsMax.Y, coordinate.Y);
            }

            IEnumerable <byte> bytesEnumerable = coordinates.SelectMany(value => BitConverter.GetBytes(value));

            min = texCoordsMin;
            max = texCoordsMax;

            return(bytesEnumerable.ToArray());
        }
Beispiel #55
0
        /// <summary>
        /// Finds the positions of internal corners of the chessboard.
        /// </summary>
        /// <param name="image">Source chessboard view. It must be an 8-bit grayscale or color image.</param>
        /// <param name="patternSize">Number of inner corners per a chessboard row and column 
        /// ( patternSize = Size(points_per_row,points_per_colum) = Size(columns, rows) ).</param>
        /// <param name="corners">Output array of detected corners.</param>
        /// <param name="flags">Various operation flags that can be zero or a combination of the ChessboardFlag values</param>
        /// <returns>The function returns true if all of the corners are found and they are placed in a certain order (row by row, left to right in every row). 
        /// Otherwise, if the function fails to find all the corners or reorder them, it returns false.</returns>
        public static bool FindChessboardCorners(
            InputArray image,
            Size patternSize,
            out Point2f[] corners,
            ChessboardFlag flags = ChessboardFlag.AdaptiveThresh | ChessboardFlag.NormalizeImage)
        {
            if (image == null)
                throw new ArgumentNullException("image");
            image.ThrowIfDisposed();

            using (var cornersVec = new VectorOfPoint2f())
            {
                int ret = NativeMethods.calib3d_findChessboardCorners_InputArray(
                    image.CvPtr, patternSize, cornersVec.CvPtr, (int)flags);
                corners = cornersVec.ToArray();
                return ret != 0;
            }
        }
Beispiel #56
0
 public static UV AsUV(Point2f value)
 {
     return(new UV(value.X, value.Y));
 }
Beispiel #57
0
 /// <summary>
 /// Retrieves a pixel rectangle from an image with sub-pixel accuracy.
 /// </summary>
 /// <param name="patchSize">Size of the extracted patch.</param>
 /// <param name="center">Floating point coordinates of the center of the extracted rectangle 
 /// within the source image. The center must be inside the image.</param>
 /// <param name="patchType">Depth of the extracted pixels. By default, they have the same depth as src.</param>
 /// <returns>Extracted patch that has the size patchSize and the same number of channels as src .</returns>
 public Mat GetRectSubPix(Size patchSize, Point2f center, int patchType = -1)
 {
     var dst = new Mat();
     Cv2.GetRectSubPix(this, patchSize, center, dst, patchType);
     return dst;
 }
Beispiel #58
0
 public static Point2d Point2fToPoint2d(Point2f pf)
 {
     return(new Point2d(((int)pf.X), ((int)pf.Y)));
 }
Beispiel #59
0
 /// <summary>
 /// Checks if the point is inside the contour. 
 /// Optionally computes the signed distance from the point to the contour boundary.
 /// </summary>
 /// <param name="pt">Point tested against the contour.</param>
 /// <param name="measureDist">If true, the function estimates the signed distance 
 /// from the point to the nearest contour edge. Otherwise, the function only checks 
 /// if the point is inside a contour or not.</param>
 /// <returns>Positive (inside), negative (outside), or zero (on an edge) value.</returns>
 public double PointPolygonTest(Point2f pt, bool measureDist)
 {
     return Cv2.PointPolygonTest(this, pt, measureDist);
 }
Beispiel #60
0
        public void Run()
        {
            // Training data

            var points    = new Point2f[500];
            var responses = new int[points.Length];
            var rand      = new Random();

            for (int i = 0; i < responses.Length; i++)
            {
                float x = rand.Next(0, 300);
                float y = rand.Next(0, 300);
                points[i]    = new Point2f(x, y);
                responses[i] = (y > f(x)) ? 1 : 2;
            }

            // Show training data and f(x)
            using (Mat pointsPlot = Mat.Zeros(300, 300, MatType.CV_8UC3))
            {
                for (int i = 0; i < points.Length; i++)
                {
                    int    x     = (int)points[i].X;
                    int    y     = (int)(300 - points[i].Y);
                    int    res   = responses[i];
                    Scalar color = (res == 1) ? Scalar.Red : Scalar.GreenYellow;
                    pointsPlot.Circle(x, y, 2, color, -1);
                }
                // f(x)
                for (int x = 1; x < 300; x++)
                {
                    int y1 = (int)(300 - f(x - 1));
                    int y2 = (int)(300 - f(x));
                    pointsPlot.Line(x - 1, y1, x, y2, Scalar.LightBlue, 1);
                }
                Window.ShowImages(pointsPlot);
            }

            // Train
            var dataMat = new Mat(points.Length, 2, MatType.CV_32FC1, points);
            var resMat  = new Mat(responses.Length, 1, MatType.CV_32SC1, responses);

            using (var svm = SVM.Create())
            {
                // normalize data
                dataMat /= 300.0;

                // SVM parameters
                svm.Type         = SVM.Types.CSvc;
                svm.KernelType   = SVM.KernelTypes.Rbf;
                svm.TermCriteria = TermCriteria.Both(1000, 0.000001);
                svm.Degree       = 100.0;
                svm.Gamma        = 100.0;
                svm.Coef0        = 1.0;
                svm.C            = 1.0;
                svm.Nu           = 0.5;
                svm.P            = 0.1;

                svm.Train(dataMat, SampleTypes.RowSample, resMat);
                svm.Save("svm.xml");

                // Predict for each 300x300 pixel
                using (Mat retPlot = Mat.Zeros(300, 300, MatType.CV_8UC3))
                {
                    for (int x = 0; x < 300; x++)
                    {
                        for (int y = 0; y < 300; y++)
                        {
                            float[] sample    = { x / 300f, y / 300f };
                            var     sampleMat = new Mat(1, 2, MatType.CV_32FC1, sample);
                            int     ret       = (int)svm.Predict(sampleMat);
                            var     plotRect  = new Rect(x, 300 - y, 1, 1);
                            if (ret == 1)
                            {
                                retPlot.Rectangle(plotRect, Scalar.Red);
                            }
                            else if (ret == 2)
                            {
                                retPlot.Rectangle(plotRect, Scalar.GreenYellow);
                            }
                        }
                    }

                    for (int x = 1; x < 300; x++)
                    {
                        int y1 = (int)(300 - f(x - 1));
                        int y2 = (int)(300 - f(x));
                        retPlot.Line(x - 1, y1, x, y2, Scalar.LightBlue, 1);
                    }



                    Window.ShowImages(retPlot);
                }
            }

            //using (var svm = SVM.Load("svm.xml"))
            //{

            //    using (Mat retPlot = Mat.Zeros(300, 300, MatType.CV_8UC3))
            //    {
            //        for (int x = 0; x < 300; x++)
            //        {
            //            for (int y = 0; y < 300; y++)
            //            {
            //                float[] sample = { x / 300f, y / 300f };
            //                var sampleMat = new Mat(1, 2, MatType.CV_32FC1, sample);
            //                int ret = (int)svm.Predict(sampleMat);
            //                var plotRect = new Rect(x, 300 - y, 1, 1);
            //                if (ret == 1)
            //                    retPlot.Rectangle(plotRect, Scalar.Red);
            //                else if (ret == 2)
            //                    retPlot.Rectangle(plotRect, Scalar.GreenYellow);
            //            }
            //        }

            //        for (int x = 1; x < 300; x++)
            //        {
            //            int y1 = (int)(300 - f(x - 1));
            //            int y2 = (int)(300 - f(x));
            //            retPlot.Line(x - 1, y1, x, y2, Scalar.LightBlue, 1);
            //        }



            //        Window.ShowImages(retPlot);
            //    }



            //}
        }