Пример #1
0
        public void FishEyeCalibrate()
        {
            var patternSize = new Size(10, 7);

            using (var image = Image("calibration/00.jpg"))
                using (var corners = new MatOfPoint2f())
                {
                    Cv2.FindChessboardCorners(image, patternSize, corners);

                    var objectPointsArray = Create3DChessboardCorners(patternSize, 1.0f).ToArray();
                    var imagePointsArray  = corners.ToArray();

                    using (var objectPoints = MatOfPoint3f.FromArray(objectPointsArray))
                        using (var imagePoints = MatOfPoint2f.FromArray(imagePointsArray))
                            using (var cameraMatrix = new MatOfDouble(Mat.Eye(3, 3, MatType.CV_64FC1)))
                                using (var distCoeffs = new MatOfDouble())
                                {
                                    var rms = Cv2.FishEye.Calibrate(new[] { objectPoints }, new[] { imagePoints }, image.Size(), cameraMatrix,
                                                                    distCoeffs, out var rotationVectors, out var translationVectors,
                                                                    FishEyeCalibrationFlags.None);

                                    var distCoeffValues = distCoeffs.ToArray();
                                    Assert.Equal(55.15, rms, 2);
                                    Assert.Contains(distCoeffValues, d => Math.Abs(d) > 1e-20);
                                    Assert.NotEmpty(rotationVectors);
                                    Assert.NotEmpty(translationVectors);
                                }
                }
        }
Пример #2
0
        public void BuildPatternFromImage(Mat image, Pattern pattern)
        {
            // Store original image in pattern structure
            pattern.size  = new Size(image.Cols, image.Rows);
            pattern.frame = image.Clone();
            GetGray(image, pattern.grayImg);

            // Build 2d and 3d contours (3d contour lie in XY plane since it's planar)
            List <Point2f> points2dList = new List <Point2f>(4);
            List <Point3f> points3dList = new List <Point3f>(4);

            // Image dimensions
            float w = image.Cols;
            float h = image.Rows;

            // Normalized dimensions:
            points2dList.Add(new Point2f(0, 0));
            points2dList.Add(new Point2f(w, 0));
            points2dList.Add(new Point2f(w, h));
            points2dList.Add(new Point2f(0, h));

            pattern.points2d = MatOfPoint2f.FromArray(points2dList);

            points3dList.Add(new Point3f(-0.5f, -0.5f, 0));
            points3dList.Add(new Point3f(+0.5f, -0.5f, 0));
            points3dList.Add(new Point3f(+0.5f, +0.5f, 0));
            points3dList.Add(new Point3f(-0.5f, +0.5f, 0));

            pattern.points3d = MatOfPoint3f.FromArray(points3dList);

            ExtractFeatures(pattern.grayImg, ref pattern.keypoints, pattern.descriptors);
            Train(pattern);
        }
Пример #3
0
        public static void TransformTv()
        {
            Mat img    = Cv2.ImRead(@"C:\prog\consoleHomography\ConsoleApp1\ConsoleApp1\images\input\in.jpg");
            Mat imgOut = new Mat();

            Point2f[] source =
            {
                new Point2f(62f,   59f),
                new Point2f(420f, 112f),
                new Point2f(425f, 300f),
                new Point2f(50f, 265f)
            };
            MatOfPoint2f src = MatOfPoint2f.FromArray(source);

            Point2f[] destination =
            {
                new Point2f(0f,                0f),
                new Point2f(img.Width,         0f),
                new Point2f(img.Width, img.Height),
                new Point2f(0f,        img.Height)
            };
            MatOfPoint2f dest = MatOfPoint2f.FromArray(destination);

            Mat m = Cv2.FindHomography(src, dest);

            Cv2.WarpPerspective(img, imgOut, m, img.Size());

            string imgName = $"{DateTime.Now:dd_MM_yyyy_HH_mm_ss}_out.jpg";

            Cv2.ImWrite(@"C:\prog\consoleHomography\ConsoleApp1\ConsoleApp1\images\output\" + imgName, imgOut);
        }
Пример #4
0
 // fixed FromArray behavior
 static Point2d[] MyPerspectiveTransform2(Point2f[] yourData, Mat transformationMatrix)
 {
     using (MatOfPoint2f s = MatOfPoint2f.FromArray(yourData))
         using (MatOfPoint2f d = new MatOfPoint2f())
         {
             Cv2.PerspectiveTransform(s, d, transformationMatrix);
             Point2f[] f = d.ToArray();
             return(f.Select(Point2fToPoint2d).ToArray());
         }
 }
Пример #5
0
        static bool RefineMatchesWithHomography(
            KeyPoint[] queryKeypoints,
            KeyPoint[] trainKeypoints,
            float reprojectionThreshold,
            DMatch[] matches,
            Mat homography
            )
        {
            int minNumberMatchesAllowed = 8;

            if (matches.Length < minNumberMatchesAllowed)
            {
                return(false);
            }

            List <Point2f> srcPointsList = new List <Point2f>(matches.Length);
            List <Point2f> dstPointsList = new List <Point2f>(matches.Length);

            for (int i = 0; i < matches.Length; i++)
            {
                srcPointsList.Add(trainKeypoints[matches[i].TrainIdx].Pt);
                dstPointsList.Add(queryKeypoints[matches[i].QueryIdx].Pt);
            }

            using (MatOfPoint2f srcPoints = MatOfPoint2f.FromArray(srcPointsList))
                using (MatOfPoint2f dstPoints = MatOfPoint2f.FromArray(dstPointsList))
                    using (MatOfByte inliersMask = new MatOfByte())
                    {
                        Cv2.FindHomography(srcPoints, dstPoints, HomographyMethods.Ransac, reprojectionThreshold, inliersMask).CopyTo(homography);

                        if (homography.Rows != 3 || homography.Cols != 3)
                        {
                            return(false);
                        }

                        byte[] inliersMaskList = inliersMask.ToArray();

                        List <DMatch> inliers = new List <DMatch>();
                        for (int i = 0; i < inliersMaskList.Length; i++)
                        {
                            if (inliersMaskList[i] == 1)
                            {
                                inliers.Add(matches[i]);
                            }
                        }

                        matches = inliers.ToArray();
                    }
            return(matches.Length > minNumberMatchesAllowed);
        }