Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="facetList"></param>
        /// <param name="facetCenters"></param>
        public void GetVoronoiFacetList(IEnumerable <int> idx, out Point2f[][] facetList, out Point2f[] facetCenters)
        {
            ThrowIfDisposed();

            IntPtr facetListPtr, facetCentersPtr;

            if (idx == null)
            {
                NativeMethods.imgproc_Subdiv2D_getVoronoiFacetList(ptr, IntPtr.Zero, 0, out facetListPtr, out facetCentersPtr);
            }
            else
            {
                int[] idxArray = EnumerableEx.ToArray(idx);
                NativeMethods.imgproc_Subdiv2D_getVoronoiFacetList(ptr, idxArray, idxArray.Length, out facetListPtr, out facetCentersPtr);
            }

            using (VectorOfVectorPoint2f facetListVec = new VectorOfVectorPoint2f(facetListPtr))
            {
                facetList = facetListVec.ToArray();
            }
            using (VectorOfPoint2f facetCentersVec = new VectorOfPoint2f(facetCentersPtr))
            {
                facetCenters = facetCentersVec.ToArray();
            }
        }
Example #2
0
        /// <summary>
        /// Decodes QR code in image once it's found by the detect() method.
        /// Returns UTF8-encoded output string or empty string if the code cannot be decoded.
        /// </summary>
        /// <param name="img">grayscale or color (BGR) image containing QR code.</param>
        /// <param name="points">Quadrangle vertices found by detect() method (or some other algorithm).</param>
        /// <param name="straightQrCode">The optional output image containing rectified and binarized QR code</param>
        /// <returns></returns>
        public string Decode(InputArray img, IEnumerable <Point2f> points, OutputArray?straightQrCode = null)
        {
            if (img == null)
            {
                throw new ArgumentNullException(nameof(img));
            }
            if (points == null)
            {
                throw new ArgumentNullException(nameof(points));
            }
            img.ThrowIfDisposed();
            straightQrCode?.ThrowIfNotReady();

            using var pointsVec    = new VectorOfPoint2f(points);
            using var resultString = new StdString();
            NativeMethods.HandleException(
                NativeMethods.objdetect_QRCodeDetector_decode(
                    ptr, img.CvPtr, pointsVec.CvPtr, Cv2.ToPtr(straightQrCode), resultString.CvPtr));

            GC.KeepAlive(img);
            GC.KeepAlive(points);
            GC.KeepAlive(straightQrCode);
            GC.KeepAlive(this);

            return(resultString.ToString());
        }
Example #3
0
        /// <summary>
        /// Both detects and decodes QR code
        /// </summary>
        /// <param name="img">grayscale or color (BGR) image containing QR code.</param>
        /// <param name="points">opiotnal output array of vertices of the found QR code quadrangle. Will be empty if not found.</param>
        /// <param name="straightQrcode">The optional output image containing rectified and binarized QR code</param>
        /// <returns></returns>
        public string DetectAndDecode(InputArray img, out Point2f[] points, OutputArray straightQrcode = null)
        {
            if (img == null)
            {
                throw new ArgumentNullException(nameof(img));
            }
            img.ThrowIfDisposed();
            straightQrcode?.ThrowIfNotReady();

            string result;

            using (var pointsVec = new VectorOfPoint2f())
                using (var resultString = new StdString())
                {
                    NativeMethods.objdetect_QRCodeDetector_detectAndDecode(
                        ptr, img.CvPtr, pointsVec.CvPtr, Cv2.ToPtr(straightQrcode), resultString.CvPtr);
                    points = pointsVec.ToArray();
                    result = resultString.ToString();
                }

            GC.KeepAlive(img);
            GC.KeepAlive(straightQrcode);
            GC.KeepAlive(this);

            return(result);
        }
Example #4
0
        /// <summary>
        /// Returns a list of all Voronoi facets.
        /// </summary>
        /// <param name="idx">Vector of vertices IDs to consider. For all vertices you can pass empty vector.</param>
        /// <param name="facetList">Output vector of the Voronoi facets.</param>
        /// <param name="facetCenters">Output vector of the Voronoi facets center points.</param>
        public void GetVoronoiFacetList(IEnumerable <int>?idx, out Point2f[][] facetList, out Point2f[] facetCenters)
        {
            ThrowIfDisposed();

            int[]? idxArray           = idx as int[] ?? idx?.ToArray();
            using var facetListVec    = new VectorOfVectorPoint2f();
            using var facetCentersVec = new VectorOfPoint2f();
            NativeMethods.HandleException(
                NativeMethods.imgproc_Subdiv2D_getVoronoiFacetList(
                    ptr, idxArray, idxArray?.Length ?? 0, facetListVec.CvPtr, facetCentersVec.CvPtr));
            GC.KeepAlive(this);
            facetList    = facetListVec.ToArray();
            facetCenters = facetCentersVec.ToArray();
        }
Example #5
0
        /// <summary>
        /// computes sparse optical flow using multi-scale Lucas-Kanade algorithm
        /// </summary>
        /// <param name="prevImg"></param>
        /// <param name="nextImg"></param>
        /// <param name="prevPts"></param>
        /// <param name="nextPts"></param>
        /// <param name="status"></param>
        /// <param name="err"></param>
        /// <param name="winSize"></param>
        /// <param name="maxLevel"></param>
        /// <param name="criteria"></param>
        /// <param name="flags"></param>
        /// <param name="minEigThreshold"></param>
        public static void CalcOpticalFlowPyrLK(
            InputArray prevImg,
            InputArray nextImg,
            Point2f[] prevPts,
            ref Point2f[] nextPts,
            out byte[] status,
            out float[] err,
            Size?winSize           = null,
            int maxLevel           = 3,
            TermCriteria?criteria  = null,
            OpticalFlowFlags flags = OpticalFlowFlags.None,
            double minEigThreshold = 1e-4)
        {
            if (prevImg == null)
            {
                throw new ArgumentNullException(nameof(prevImg));
            }
            if (nextImg == null)
            {
                throw new ArgumentNullException(nameof(nextImg));
            }
            if (prevPts == null)
            {
                throw new ArgumentNullException(nameof(prevPts));
            }
            if (nextPts == null)
            {
                throw new ArgumentNullException(nameof(nextPts));
            }
            prevImg.ThrowIfDisposed();
            nextImg.ThrowIfDisposed();

            var winSize0  = winSize.GetValueOrDefault(new Size(21, 21));
            var criteria0 = criteria.GetValueOrDefault(
                TermCriteria.Both(30, 0.01));

            using var nextPtsVec = new VectorOfPoint2f(nextPts);
            using var statusVec  = new VectorOfByte();
            using var errVec     = new VectorOfFloat();
            NativeMethods.HandleException(
                NativeMethods.video_calcOpticalFlowPyrLK_vector(
                    prevImg.CvPtr, nextImg.CvPtr, prevPts, prevPts.Length,
                    nextPtsVec.CvPtr, statusVec.CvPtr, errVec.CvPtr,
                    winSize0, maxLevel, criteria0, (int)flags, minEigThreshold));
            GC.KeepAlive(prevImg);
            GC.KeepAlive(nextImg);
            nextPts = nextPtsVec.ToArray();
            status  = statusVec.ToArray();
            err     = errVec.ToArray();
        }
Example #6
0
        /// <summary>
        /// Decodes QR codes in image once it's found by the detect() method.
        /// Returns UTF8-encoded output string or empty string if the code cannot be decoded.
        /// </summary>
        /// <param name="img">grayscale or color (BGR) image containing QR code.</param>
        /// <param name="points">Quadrangle vertices found by detect() method (or some other algorithm).</param>
        /// <param name="decodedInfo">UTF8-encoded output vector of string or empty vector of string if the codes cannot be decoded. </param>
        /// <param name="straightQrCode">The optional output image containing rectified and binarized QR code</param>
        /// <param name="isOutputStraightQrCode"><see langword="true"/> to output <paramref name="straightQrCode"/></param>
        /// <returns></returns>
        protected bool DecodeMulti(InputArray img, IEnumerable <Point2f> points, out string?[] decodedInfo, out Mat[] straightQrCode, bool isOutputStraightQrCode)
        {
            if (img == null)
            {
                throw new ArgumentNullException(nameof(img));
            }
            if (points == null)
            {
                throw new ArgumentNullException(nameof(points));
            }

            img.ThrowIfDisposed();

            using var decodedInfoVec = new VectorOfString();
            using var pointsVec      = new VectorOfPoint2f(points);

            int ret;

            if (isOutputStraightQrCode)
            {
                NativeMethods.HandleException(
                    NativeMethods.objdetect_QRCodeDetector_decodeMulti(
                        ptr, img.CvPtr, pointsVec.CvPtr, decodedInfoVec.CvPtr, out var straightQrCodePtr, out ret));

                using var straightQrCodeVec = new VectorOfMat(straightQrCodePtr);
                straightQrCode = straightQrCodeVec.ToArray();
            }
            else
            {
                NativeMethods.HandleException(
                    NativeMethods.objdetect_QRCodeDetector_decodeMulti_NoStraightQrCode(
                        ptr, img.CvPtr, pointsVec.CvPtr, decodedInfoVec.CvPtr, out ret));
                straightQrCode = Array.Empty <Mat>();
            }

            // decode utf-8 bytes.
            decodedInfo = decodedInfoVec.ToArray();

            GC.KeepAlive(img);
            GC.KeepAlive(points);
            GC.KeepAlive(this);

            return(ret != 0);
        }
Example #7
0
        /// <summary>
        /// Detects QR code in image and returns the quadrangle containing the code.
        /// </summary>
        /// <param name="img">grayscale or color (BGR) image containing (or not) QR code.</param>
        /// <param name="points">Output vector of vertices of the minimum-area quadrangle containing the code.</param>
        /// <returns></returns>
        public bool Detect(InputArray img, out Point2f[] points)
        {
            if (img == null)
            {
                throw new ArgumentNullException(nameof(img));
            }
            img.ThrowIfDisposed();

            using var pointsVec = new VectorOfPoint2f();

            NativeMethods.HandleException(
                NativeMethods.objdetect_QRCodeDetector_detect(ptr, img.CvPtr, pointsVec.CvPtr, out var ret));
            points = pointsVec.ToArray();

            GC.KeepAlive(img);
            GC.KeepAlive(this);

            return(ret != 0);
        }
Example #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="matches1to2"></param>
        /// <param name="correctMatches1to2Mask"></param>
        /// <returns>recallPrecisionCurve</returns>
        public static Point2f[] ComputeRecallPrecisionCurve(
            DMatch[][] matches1to2, byte[][] correctMatches1to2Mask)
        {
            if (matches1to2 == null)
            {
                throw new ArgumentNullException(nameof(matches1to2));
            }
            if (correctMatches1to2Mask == null)
            {
                throw new ArgumentNullException(nameof(correctMatches1to2Mask));
            }

            using var dm     = new ArrayAddress2 <DMatch>(matches1to2);
            using var cm     = new ArrayAddress2 <byte>(correctMatches1to2Mask);
            using var recall = new VectorOfPoint2f();
            NativeMethods.HandleException(
                NativeMethods.features2d_computeRecallPrecisionCurve(
                    dm.Pointer, dm.Dim1Length, dm.Dim2Lengths,
                    cm.Pointer, cm.Dim1Length, cm.Dim2Lengths,
                    recall.CvPtr));
            return(recall.ToArray());
        }