Example #1
0
        /// <summary>
        /// utility function: convert one image to another with optional vertical flip
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dst"></param>
        /// <param name="flags"></param>
        public static void ConvertImage(Mat src, Mat dst, ConvertImageFlag flags = ConvertImageFlag.None)
        {
            if (src == null)
            {
                throw new ArgumentNullException(nameof(src));
            }
            if (dst == null)
            {
                throw new ArgumentNullException(nameof(dst));
            }
            src.ThrowIfDisposed();
            dst.ThrowIfDisposed();

            dst.Create(src.Size(), MatType.CV_8UC3);
            NativeMethods.highgui_cvConvertImage_Mat(src.CvPtr, dst.CvPtr, (int)flags);

            GC.KeepAlive(src);
            GC.KeepAlive(dst);
        }
Example #2
0
        private static void Main(string[] args)
        {
            string fileName = "C:\\perspective_chessboard.jpg";

            // Cv
            using (CvMat chessboard = new CvMat(fileName))
            {
                Cv.ShowImage("Input_Cv", chessboard);

                CvPoint2D32f[] corners;
                if (Cv.FindChessboardCorners(chessboard, new Size(10, 7), out corners))
                {
                    using (CvMat dest = new CvMat(chessboard.Rows, chessboard.Cols, MatrixType.U8C3))
                    {
                        CvPoint2D32f[] a = 
                        {
                            corners[0],
                            corners[60],
                            corners[69],
                            corners[9]
                        };

                        foreach (var corner in a)
                        {
                            Cv.Circle(chessboard, corner, 5, new CvScalar(0, 0, 255));
                        }
                        Cv.ShowImage("RectangleVertices_Cv", chessboard);

                        CvPoint2D32f[] b =
                        {
                            new CvPoint2D32f(0, 0),
                            new CvPoint2D32f(0, chessboard.Height),
                            new CvPoint2D32f(chessboard.Width, chessboard.Height),
                            new CvPoint2D32f(chessboard.Width, 0)
                        };

                        CvMat map_matrix;
                        Cv.GetPerspectiveTransform(a, b, out map_matrix);
                        Cv.WarpPerspective(chessboard, dest, map_matrix, Interpolation.Linear | Interpolation.FillOutliers, CvScalar.ScalarAll(255)); //Succeed
                        Cv.ReleaseMat(map_matrix);

                        Cv.ShowImage("Output_Cv", dest);
                    }

                    //Cv.WaitKey();
                }
            }

            //Cv2
            using (Mat chessboard = new Mat(fileName))
            {
                Point2f[] corners;
                if (Cv2.FindChessboardCorners(chessboard, new Size(10, 7), out corners))
                {
                    Point2f[] a =
                    {
                        corners[0],
                        corners[60],
                        corners[69],
                        corners[9]
                    };

                    foreach (var corner in a)
                    {
                        chessboard.Circle(corner, 5, new Scalar(0, 0, 255));
                    }
                    Cv2.ImShow("RectangleVertices_Cv2", chessboard);
                    //Cv2.WaitKey();

                    Point2f[] b =
                    {
                        new Point2f(0, 0),
                        new Point2f(0, chessboard.Height),
                        new Point2f(chessboard.Width, chessboard.Height),
                        new Point2f(chessboard.Width, 0)
                    };

                    using (Mat map_matrix = Cv2.GetPerspectiveTransform(a, b))
                    using (Mat dest = new Mat(new Size(640, 480), MatType.CV_8UC3))
                    {
                        Cv2.WarpPerspective(chessboard, dest, map_matrix, dest.Size(), Interpolation.Linear | Interpolation.FillOutliers, BorderType.Default, Scalar.All(255)); //AccessViolation
                        Cv2.ImShow("Output_Cv2", dest);
                    }

                    ////Another way (Using Mat.WarpPerspective())
                    //using (Mat map_matrix = Cv2.GetPerspectiveTransform(a, b))
                    //using (Mat dest = chessboard.WarpPerspective(map_matrix, new Size(640, 480), Interpolation.Linear | Interpolation.FillOutliers, BorderType.Default, Scalar.All(255))) //AccessViolation
                    //{
                    //    Cv2.ImShow("Output_Cv2", dest);
                    //}

                    Cv2.WaitKey();
                }
            }

            //Track();
            //Run();
        }
Example #3
0
        private void GetInitWrap(Mat image)
        {
            this.src[0] = new Point2f(0, 0);
            this.src[1] = new Point2f(0, image.Height);
            this.src[2] = new Point2f(image.Width, image.Height);
            this.src[3] = new Point2f(image.Width, 0);

            this.dst[0] = new Point2f(0, 0);
            this.dst[1] = new Point2f(0, image.Height);
            this.dst[2] = new Point2f(image.Width, image.Height);
            this.dst[3] = new Point2f(image.Width, 0);

            this.SetCanvasCenter(this.SrcMarkList[0], 0, 0);
            this.SetCanvasCenter(this.SrcMarkList[1], 0, image.Height);
            this.SetCanvasCenter(this.SrcMarkList[2], image.Width, image.Height);
            this.SetCanvasCenter(this.SrcMarkList[3], image.Width, 0);

            this.SetCanvasCenter(this.DstMarkList[0], 0, 0);
            this.SetCanvasCenter(this.DstMarkList[1], 0, image.Height);
            this.SetCanvasCenter(this.DstMarkList[2], image.Width, image.Height);
            this.SetCanvasCenter(this.DstMarkList[3], image.Width, 0);
            this.UpdateLines();

            this.m_Mask = new Mat(image.Size(), MatType.CV_8UC1, new Scalar(0));
            List<List<Point>> polygons = new List<List<Point>>();
            List<Point> polygon = new List<Point>();
            polygons.Add(polygon);
            foreach (var p in this.dst)
            {
                polygon.Add(new Point(p.X, p.Y));
            }
            Cv2.FillPoly(this.m_Mask, polygons, new Scalar(255));

            this.SetWarp();
        }