Esempio n. 1
0
        public Bitmap FixPerspective()
        {
            double maxWidth  = 0;
            double maxHeight = 0;

            OpenCV.Core.Point pTopLeft, pBottomLeft, pBottomRight, pTopRight;
            GetImageMaxSize(out maxWidth, out maxHeight, out pTopLeft, out pBottomLeft, out pBottomRight, out pTopRight);

            if (maxWidth <= 0 || maxHeight <= 0)
            {
                return(null);
            }

            CreateSrcDstArrays(maxWidth, maxHeight, pTopLeft, pBottomLeft, pBottomRight, pTopRight, out MatOfPoint2f src, out MatOfPoint2f dst);

            Mat undistortedMat = new Mat(new OpenCV.Core.Size(maxWidth, maxHeight), OpenCV.Core.CvType.Cv8uc4);
            Mat ptMat          = Imgproc.GetPerspectiveTransform(src, dst);

            Imgproc.WarpPerspective(originalMat, undistortedMat, ptMat, new OpenCV.Core.Size(maxWidth, maxHeight));
            Bitmap undistortedBitmap = Bitmap.CreateBitmap((int)maxWidth, (int)maxHeight, Bitmap.Config.Argb8888);

            OpenCV.Android.Utils.MatToBitmap(undistortedMat, undistortedBitmap);

            return(undistortedBitmap);
        }
        /// <summary>
        /// Creates the transformation matrix (in both directions) to wrap the zone in front of the car.
        /// </summary>
        /// <param name="src"> source image </param>
        /// <param name="transform"> transformation matrix </param>
        /// <param name="inv_transform"> inverse transformation matrix </param>
        /// <param name="warp"> wrapped zone of interest </param>
        public void GetBirdEye(Mat bin, Mat src, float marginX, float marginY, out Mat transform, out Mat inv_transform, out Mat warp)
        {
            // Create perspective in front of the car
            Core.Point[] sourcePeaks =
            {
                new Core.Point(bin.Width() * marginX,       bin.Height() * marginY),
                new Core.Point(bin.Width() * (1 - marginX), bin.Height() * marginY),
                new Core.Point(bin.Width(),                 bin.Height()),
                new Core.Point(0,                           bin.Height()),
                new Core.Point(bin.Width() * marginX,       bin.Height() * marginY)
            };
            MatOfPoint2f sourceMat = new MatOfPoint2f(
                sourcePeaks[0],
                sourcePeaks[1],
                sourcePeaks[2],
                sourcePeaks[3]
                );

            Core.Point[] targetPeaks =
            {
                new Core.Point(0,                      0),
                new Core.Point(bin.Width(),            0),
                new Core.Point(bin.Width(), bin.Height()),
                new Core.Point(0,           bin.Height()),
                new Core.Point(0, 0)
            };
            // Generate transformation matrix in both directions
            MatOfPoint2f targetMat = new MatOfPoint2f(
                targetPeaks[0],
                targetPeaks[1],
                targetPeaks[2],
                targetPeaks[3]
                );

            transform     = Imgproc.GetPerspectiveTransform(sourceMat, targetMat);
            inv_transform = Imgproc.GetPerspectiveTransform(targetMat, sourceMat);
            warp          = new Mat(bin.Size(), bin.Type());
            Imgproc.WarpPerspective(bin, warp, transform, bin.Size(), Imgproc.InterLinear);

            // Draw
            for (int iPeak = 0; iPeak < sourcePeaks.GetLength(0) - 1; iPeak++)
            {
                Imgproc.Line(src, sourcePeaks[iPeak], sourcePeaks[iPeak + 1], new Scalar(0, 255, 0), 3);
            }
        }