/// <summary>
        ///
        /// </summary>
        /// <param name="clusterCount"></param>
        /// <param name="termcrit"></param>
        /// <param name="attempts"></param>
        /// <param name="flags"></param>
        public BOWKMeansTrainer(int clusterCount, TermCriteria?termcrit = null,
                                int attempts = 3, KMeansFlags flags = KMeansFlags.PpCenters)
        {
            var termCritValue = termcrit.GetValueOrDefault(new TermCriteria());

            ptr = NativeMethods.features2d_BOWKMeansTrainer_new(clusterCount, termCritValue, attempts, (int)flags);
        }
Beispiel #2
0
        /// <summary>
        /// Performs a mean-shift procedure and stores information about processed points (their colors and
        /// positions) in two images.
        /// </summary>
        /// <param name="src">Source image. Only CV_8UC4 images are supported for now.</param>
        /// <param name="dstr">Destination image containing the color of mapped points. The size and type is the same
        /// as src.</param>
        /// <param name="dstsp">Destination image containing the position of mapped points. The size is the same as
        /// src size.The type is CV_16SC2.</param>
        /// <param name="sp">Spatial window radius.</param>
        /// <param name="sr">Color window radius.</param>
        /// <param name="criteria">Termination criteria. See TermCriteria.</param>
        /// <param name="stream">Stream for the asynchronous version.</param>
        public static void meanShiftProc(InputArray src, OutputArray dstr, OutputArray dstsp, int sp, int sr,
                                         TermCriteria?criteria = null, Stream stream = null)
        {
            if (src == null)
            {
                throw new ArgumentNullException(nameof(src));
            }
            if (dstr == null)
            {
                throw new ArgumentNullException(nameof(dstr));
            }
            if (dstsp == null)
            {
                throw new ArgumentNullException(nameof(dstsp));
            }

            src.ThrowIfDisposed();
            dstr.ThrowIfNotReady();
            dstsp.ThrowIfNotReady();

            TermCriteria criteria0 = criteria.GetValueOrDefault(
                TermCriteria.Both(5, 1));

            NativeMethods.cuda_imgproc_meanShiftProc(src.CvPtr, dstr.CvPtr, dstr.CvPtr, sp, sr, criteria0, stream?.CvPtr ?? Stream.Null.CvPtr);
            GC.KeepAlive(src);
            GC.KeepAlive(dstr);
            GC.KeepAlive(dstsp);
            dstr.Fix();
            dstsp.Fix();
        }
Beispiel #3
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,
            InputArray prevPts, InputOutputArray nextPts,
            OutputArray status, OutputArray 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));
            }
            if (status == null)
            {
                throw new ArgumentNullException(nameof(status));
            }
            if (err == null)
            {
                throw new ArgumentNullException(nameof(err));
            }
            prevImg.ThrowIfDisposed();
            nextImg.ThrowIfDisposed();
            prevPts.ThrowIfDisposed();
            nextPts.ThrowIfNotReady();
            status.ThrowIfNotReady();
            err.ThrowIfNotReady();

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

            NativeMethods.HandleException(
                NativeMethods.video_calcOpticalFlowPyrLK_InputArray(
                    prevImg.CvPtr, nextImg.CvPtr, prevPts.CvPtr, nextPts.CvPtr,
                    status.CvPtr, err.CvPtr, winSize0, maxLevel,
                    criteria0, (int)flags, minEigThreshold));
            GC.KeepAlive(prevImg);
            GC.KeepAlive(nextImg);
            GC.KeepAlive(prevPts);
            nextPts.Fix();
            status.Fix();
            err.Fix();
        }
Beispiel #4
0
// ReSharper restore InconsistentNaming
#pragma warning restore 1591
        #endregion

        #region Init and Disposal

#if LANG_JP
        /// <summary>
        /// 初期化
        /// </summary>
        /// <param name="nClusters"></param>
        /// <param name="covMatType"></param>
        /// <param name="termCrit"></param>
#else
        /// <summary>
        /// Training constructor
        /// </summary>
        /// <param name="nClusters"></param>
        /// <param name="covMatType"></param>
        /// <param name="termCrit"></param>
#endif
        public EM(
            int nClusters           = DEFAULT_NCLUSTERS,
            EMCovMatType covMatType = EMCovMatType.Diagonal,
            TermCriteria?termCrit   = null)
        {
            var termCrit0 = termCrit.GetValueOrDefault(
                TermCriteria.Both(DEFAULT_MAX_ITERS, Double.Epsilon));

            ptr = NativeMethods.ml_EM_new(nClusters, (int)covMatType, termCrit0);
        }
Beispiel #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();
        }
Beispiel #6
0
        /// <summary>
        /// Finds the geometric transform (warp) between two images in terms of the ECC criterion @cite EP08 .
        /// </summary>
        /// <param name="templateImage">single-channel template image; CV_8U or CV_32F array.</param>
        /// <param name="inputImage">single-channel input image which should be warped with the final warpMatrix in
        /// order to provide an image similar to templateImage, same type as templateImage.</param>
        /// <param name="warpMatrix">floating-point \f$2\times 3\f$ or \f$3\times 3\f$ mapping matrix (warp).</param>
        /// <param name="motionType">parameter, specifying the type of motion</param>
        /// <param name="criteria">parameter, specifying the termination criteria of the ECC algorithm;
        /// criteria.epsilon defines the threshold of the increment in the correlation coefficient between two
        /// iterations(a negative criteria.epsilon makes criteria.maxcount the only termination criterion).
        /// Default values are shown in the declaration above.</param>
        /// <param name="inputMask">An optional mask to indicate valid values of inputImage.</param>
        /// <returns></returns>
        public static double FindTransformECC(
            InputArray templateImage,
            InputArray inputImage,
            InputOutputArray warpMatrix,
            MotionTypes motionType = MotionTypes.Affine,
            TermCriteria?criteria  = null,
            InputArray?inputMask   = null)
        {
            if (templateImage == null)
            {
                throw new ArgumentNullException(nameof(templateImage));
            }
            if (inputImage == null)
            {
                throw new ArgumentNullException(nameof(inputImage));
            }
            if (warpMatrix == null)
            {
                throw new ArgumentNullException(nameof(warpMatrix));
            }
            templateImage.ThrowIfDisposed();
            inputImage.ThrowIfDisposed();
            warpMatrix.ThrowIfDisposed();
            inputMask?.ThrowIfDisposed();

            var criteriaValue = criteria.GetValueOrDefault(new TermCriteria(CriteriaType.Count | CriteriaType.Eps, 50, 0.001));

            NativeMethods.HandleException(
                NativeMethods.video_findTransformECC2(
                    templateImage.CvPtr, inputImage.CvPtr, warpMatrix.CvPtr, (int)motionType,
                    criteriaValue, inputMask?.CvPtr ?? IntPtr.Zero, out var ret));

            GC.KeepAlive(templateImage);
            GC.KeepAlive(inputImage);
            GC.KeepAlive(warpMatrix);
            GC.KeepAlive(inputMask);
            return(ret);
        }