Ejemplo n.º 1
0
        /// <summary>
        /// computes the best-fit perspective transformation mapping srcPoints to dstPoints.
        /// </summary>
        /// <param name="srcPoints">Coordinates of the points in the original plane</param>
        /// <param name="dstPoints">Coordinates of the points in the target plane</param>
        /// <param name="method">Method used to computed a homography matrix.</param>
        /// <param name="ransacReprojThreshold">Maximum allowed reprojection error to treat a point pair as an inlier (used in the RANSAC method only)</param>
        /// <param name="mask"> Optional output mask set by a robust method ( CV_RANSAC or CV_LMEDS ). Note that the input mask values are ignored.</param>
        /// <returns></returns>
        public static Mat FindHomography(IEnumerable<Point2d> srcPoints, IEnumerable<Point2d> dstPoints,
            HomographyMethods method = HomographyMethods.None, double ransacReprojThreshold = 3,
            OutputArray mask = null)
        {
            if (srcPoints == null)
                throw new ArgumentNullException("srcPoints");
            if (dstPoints == null)
                throw new ArgumentNullException("dstPoints");

            Point2d[] srcPointsArray = EnumerableEx.ToArray(srcPoints);
            Point2d[] dstPointsArray = EnumerableEx.ToArray(dstPoints);

            IntPtr mat = NativeMethods.calib3d_findHomography_vector(srcPointsArray, srcPointsArray.Length,
                dstPointsArray, dstPointsArray.Length, (int)method, ransacReprojThreshold, ToPtr(mask));

            if (mask != null)
                mask.Fix();
            return new Mat(mat);
        }
        /// <summary>
        /// Refines the matches with homography.
        /// </summary>
        /// <returns><c>true</c>, if matches with homography was refined, <c>false</c> otherwise.</returns>
        /// <param name="queryKeypoints">Query keypoints.</param>
        /// <param name="trainKeypoints">Train keypoints.</param>
        /// <param name="reprojectionThreshold">Reprojection threshold.</param>
        /// <param name="matches">Matches.</param>
        /// <param name="homography">Homography.</param>
        static bool refineMatchesWithHomography
        (
            KeyPoint[] queryKeypoints,
            KeyPoint[] trainKeypoints,
            float reprojectionThreshold,
            ref DMatch[] matches,
            ref Mat homography,
            HomographyMethods methods
        )
        {
//              Debug.Log ("matches " + matches.ToString ());

            int minNumberMatchesAllowed = 8;

            List <KeyPoint> queryKeypointsList = queryKeypoints.ToList();
            List <KeyPoint> trainKeypointsList = trainKeypoints.ToList();
            List <DMatch>   matchesList        = matches.ToList();

            if (matchesList.Count < minNumberMatchesAllowed)
            {
                return(false);
            }

            // Prepare data for cv::findHomography
            List <Point2f> srcPointsList = new List <Point2f> (matchesList.Count);
            List <Point2f> dstPointsList = new List <Point2f> (matchesList.Count);

            for (int i = 0; i < matchesList.Count; i++)
            {
                srcPointsList.Add(trainKeypointsList [matchesList [i].TrainIdx].Pt);
                dstPointsList.Add(queryKeypointsList [matchesList [i].QueryIdx].Pt);
            }
            // Find homography matrix and get inliers mask


            //              Debug.Log ("srcPoints " + srcPoints.ToString ());
            //              Debug.Log ("dstPoints " + dstPoints.ToString ());
            using (Mat srcPoints = new Mat(srcPointsList.Count, 2, MatType.CV_32F, srcPointsList.ToArray()))
                using (Mat dstPoints = new Mat(dstPointsList.Count, 2, MatType.CV_32F, dstPointsList.ToArray()))
                    using (var inliersMask = new Mat(srcPointsList.Count, 1, MatType.CV_8U))
                    {
                        //homography=Cv2.FindHomography(InputArray.Create( srcPointsList), InputArray.Create(dstPointsList), HomographyMethods.Ransac, reprojectionThreshold, inliersMask);
                        homography = Cv2.FindHomography(srcPoints, dstPoints, methods, reprojectionThreshold, inliersMask);
                        if (homography.Rows != 3 || homography.Cols != 3)
                        {
                            return(false);
                        }


                        List <DMatch> inliers = new List <DMatch>();
                        for (int i = 0; i < inliersMask.Rows; i++)
                        {
                            if (inliersMask.Get <byte>(i, 0) == 1)
                            {
                                inliers.Add(matchesList[i]);
                            }
                        }
                        matches = inliers.ToArray();
                    }



            return(matchesList.Count > minNumberMatchesAllowed);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// computes the best-fit perspective transformation mapping srcPoints to dstPoints.
        /// </summary>
        /// <param name="srcPoints">Coordinates of the points in the original plane, a matrix of the type CV_32FC2</param>
        /// <param name="dstPoints">Coordinates of the points in the target plane, a matrix of the type CV_32FC2</param>
        /// <param name="method">Method used to computed a homography matrix.</param>
        /// <param name="ransacReprojThreshold">Maximum allowed reprojection error to treat a point pair as an inlier (used in the RANSAC method only)</param>
        /// <param name="mask"> Optional output mask set by a robust method ( CV_RANSAC or CV_LMEDS ). Note that the input mask values are ignored.</param>
        /// <returns></returns>
        public static Mat FindHomography(InputArray srcPoints, InputArray dstPoints,
            HomographyMethods method = HomographyMethods.None, double ransacReprojThreshold = 3,
            OutputArray mask = null)
        {
            if (srcPoints == null)
                throw new ArgumentNullException("srcPoints");
            if (dstPoints == null)
                throw new ArgumentNullException("dstPoints");
            srcPoints.ThrowIfDisposed();
            dstPoints.ThrowIfDisposed();

            IntPtr mat = NativeMethods.calib3d_findHomography_InputArray(srcPoints.CvPtr, dstPoints.CvPtr, (int)method,
                ransacReprojThreshold, ToPtr(mask));

            if (mask != null)
                mask.Fix();
            return new Mat(mat);
        }