Exemplo n.º 1
0
        /// <summary>
        /// Find rectangular regions in the given image that are likely to contain objects of
        /// loaded classes (models) and corresponding confidence levels.
        /// </summary>
        /// <param name="image">An image.</param>
        /// <param name="overlapThreshold">Threshold for the non-maximum suppression algorithm.</param>
        /// <param name="numThreads">Number of threads used in parallel version of the algorithm.</param>
        /// <returns>The detections: rectangulars, scores and class IDs.</returns>
        public virtual ObjectDetection[] Detect(Mat image,
                                                float overlapThreshold = 0.5f, int numThreads = -1)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("LatentSvmDetector");
            }
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }
            image.ThrowIfDisposed();

            using (var odVec = new VectorOfVec6d())
            {
                NativeMethods.objdetect_LatentSvmDetector_detect(
                    ptr, image.CvPtr, odVec.CvPtr, overlapThreshold, numThreads);

                return(EnumerableEx.SelectToArray(odVec.ToArray(), v =>
                                                  new ObjectDetection
                {
                    Rect = new Rect((int)v.Item0, (int)v.Item1, (int)v.Item2, (int)v.Item3),
                    Score = (float)v.Item4,
                    ClassId = (int)v.Item5
                }
                                                  ));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Draws matches of keypints from two images on output image.
        /// </summary>
        /// <param name="img1"></param>
        /// <param name="keypoints1"></param>
        /// <param name="img2"></param>
        /// <param name="keypoints2"></param>
        /// <param name="matches1To2"></param>
        /// <param name="outImg"></param>
        /// <param name="matchColor"></param>
        /// <param name="singlePointColor"></param>
        /// <param name="matchesMask"></param>
        /// <param name="flags"></param>
        public static void DrawMatches(Mat img1, IEnumerable <KeyPoint> keypoints1,
                                       Mat img2, IEnumerable <KeyPoint> keypoints2,
                                       IEnumerable <IEnumerable <DMatch> > matches1To2, Mat outImg,
                                       Scalar?matchColor = null, Scalar?singlePointColor = null,
                                       IEnumerable <IEnumerable <byte> > matchesMask = null,
                                       DrawMatchesFlags flags = DrawMatchesFlags.Default)
        {
            if (img1 == null)
            {
                throw new ArgumentNullException(nameof(img1));
            }
            if (img2 == null)
            {
                throw new ArgumentNullException(nameof(img2));
            }
            if (outImg == null)
            {
                throw new ArgumentNullException(nameof(outImg));
            }
            if (keypoints1 == null)
            {
                throw new ArgumentNullException(nameof(keypoints1));
            }
            if (keypoints2 == null)
            {
                throw new ArgumentNullException(nameof(keypoints2));
            }
            if (matches1To2 == null)
            {
                throw new ArgumentNullException(nameof(matches1To2));
            }
            img1.ThrowIfDisposed();
            img2.ThrowIfDisposed();
            outImg.ThrowIfDisposed();

            KeyPoint[] keypoints1Array  = EnumerableEx.ToArray(keypoints1);
            KeyPoint[] keypoints2Array  = EnumerableEx.ToArray(keypoints2);
            DMatch[][] matches1To2Array = EnumerableEx.SelectToArray(matches1To2, EnumerableEx.ToArray);
            int        matches1To2Size1 = matches1To2Array.Length;

            int[]  matches1To2Size2  = EnumerableEx.SelectToArray(matches1To2Array, dm => dm.Length);
            Scalar matchColor0       = matchColor.GetValueOrDefault(Scalar.All(-1));
            Scalar singlePointColor0 = singlePointColor.GetValueOrDefault(Scalar.All(-1));

            using (var matches1To2Ptr = new ArrayAddress2 <DMatch>(matches1To2Array))
            {
                if (matchesMask == null)
                {
                    NativeMethods.features2d_drawMatches2(img1.CvPtr, keypoints1Array, keypoints1Array.Length,
                                                          img2.CvPtr, keypoints2Array, keypoints2Array.Length,
                                                          matches1To2Ptr, matches1To2Size1, matches1To2Size2,
                                                          outImg.CvPtr, matchColor0, singlePointColor0,
                                                          null, 0, null, (int)flags);
                }
                else
                {
                    byte[][] matchesMaskArray = EnumerableEx.SelectToArray(matchesMask, EnumerableEx.ToArray);
                    int      matchesMaskSize1 = matches1To2Array.Length;
                    int[]    matchesMaskSize2 = EnumerableEx.SelectToArray(matchesMaskArray, dm => dm.Length);
                    using (var matchesMaskPtr = new ArrayAddress2 <byte>(matchesMaskArray))
                    {
                        NativeMethods.features2d_drawMatches2(img1.CvPtr, keypoints1Array, keypoints1Array.Length,
                                                              img2.CvPtr, keypoints2Array, keypoints2Array.Length,
                                                              matches1To2Ptr.Pointer, matches1To2Size1, matches1To2Size2,
                                                              outImg.CvPtr, matchColor0, singlePointColor0,
                                                              matchesMaskPtr, matchesMaskSize1, matchesMaskSize2, (int)flags);
                    }
                }
            }
        }