Exemplo n.º 1
0
        public static bool FindChessboardCorners(Mat grayTexMat, Vector2Int innerCornerCount, ref MatOfPoint2f cornerPoints, bool fastAndImprecise = false)
        {
            Vector2IntToSize(innerCornerCount, ref _tempSize);
            if (cornerPoints == null)
            {
                cornerPoints = new MatOfPoint2f();
            }

            if (!fastAndImprecise)
            {
                // TODO: Enable CALIB_CB_LARGER and CALIB_CB_MARKER when they are supported.
                // https://forum.unity.com/threads/released-opencv-for-unity.277080/page-47#post-5665141

                const int flagsSB =
                    Calib3d.CALIB_CB_EXHAUSTIVE;                                // Run an exhaustive search to improve detection rate. (Note: this seems to have very positive impact).
                //Calib3d.CALIB_CB_LARGER			// The detected pattern is allowed to be larger than patternSize
                //Calib3d.CALIB_CB_MARKER			// The detected pattern must have a marker
                //Calib3d.CALIB_CB_NORMALIZE_IMAGE	// Normalize the image gamma with equalizeHist before detection

                // findChessboardCornersSB() is supposed to work better than combined findChessboardCorners() and cornerSubPix().
                return(Calib3d.findChessboardCornersSB(grayTexMat, _tempSize, cornerPoints, flagsSB));
            }

            // Normally, you would use find4QuadCornersSubpix after findChessboardCorners to improve the result, but
            // we just want fast.
            const int flags =
                Calib3d.CALIB_CB_FAST_CHECK;                // |
            //Calib3d.CALIB_CB_NORMALIZE_IMAGE |
            //Calib3d.CALIB_CB_FILTER_QUADS |
            //Calib3d.CALIB_CB_ADAPTIVE_THRESH;
            bool success = Calib3d.findChessboardCorners(grayTexMat, _tempSize, cornerPoints, flags);

            // Because the old version prefers to start at black tile corner vs findChessboardCornersSB prefering to start
            // at white tile corner, we have to reverse the order of points for the old version to match up.
            if (success)
            {
                ReverseOrder(cornerPoints);
            }

            return(success);
        }