Exemplo n.º 1
0
        /// <summary>
        /// 与えられた画像からオブジェクトを含む様な矩形領域を検出し,それらの領域を矩形の列として返す.
        /// </summary>
        /// <param name="image">この画像の中からオブジェクトを検出する</param>
        /// <param name="cascade">Haar 分類器カスケード の内部表現</param>
        /// <param name="storage">オブジェクト候補の矩形が得られた場合に,その矩形列を保存するメモリストレージ</param>
        /// <param name="scaleFactor">スキャン毎に探索ウィンドウがスケーリングされる際のスケールファクタ. 例えばこの値が 1.1 ならば,ウィンドウが 10% 大きくなる</param>
        /// <param name="minNeighbors">(これから 1 を引いた値が)オブジェクトを構成する近傍矩形の最小数となる. min_neighbors-1 よりも少ない矩形しか含まないようなグループは全て棄却される. もし min_neighbors が 0 である場合,この関数はグループを一つも生成せず,候補となる矩形を全て返す.これはユーザがカスタマイズしたグループ化処理を適用したい場合に有用である. </param>
        /// <param name="flags">処理モード</param>
        /// <param name="minSize">最小ウィンドウサイズ.デフォルトでは分類器の学習に用いられたサンプルのサイズが設定される(顔検出の場合は,~20×20).</param>
        /// <param name="maxSize">探索窓の最大サイズ.デフォルトでは,画像サイズに設定されます.</param>
        /// <returns>CvAvgCompを要素とするCvSeq</returns>
#else
        /// <summary>
        /// Finds rectangular regions in the given image that are likely to contain objects the cascade has been trained for and returns those regions as a sequence of rectangles.
        /// </summary>
        /// <param name="image">Image to detect objects in. </param>
        /// <param name="cascade">Haar classifier cascade in internal representation. </param>
        /// <param name="storage">Memory storage to store the resultant sequence of the object candidate rectangles. </param>
        /// <param name="scaleFactor">The factor by which the search window is scaled between the subsequent scans, for example, 1.1 means increasing window by 10%. </param>
        /// <param name="minNeighbors">Minimum number (minus 1) of neighbor rectangles that makes up an object. All the groups of a smaller number of rectangles than min_neighbors-1 are rejected. If min_neighbors is 0, the function does not any grouping at all and returns all the detected candidate rectangles, which may be useful if the user wants to apply a customized grouping procedure. </param>
        /// <param name="flags">Mode of operation. Currently the only flag that may be specified is CV_HAAR_DO_CANNY_PRUNING. If it is set, the function uses Canny edge detector to reject some image regions that contain too few or too much edges and thus can not contain the searched object. The particular threshold values are tuned for face detection and in this case the pruning speeds up the processing. </param>
        /// <param name="minSize">Minimum window size. By default, it is set to the size of samples the classifier has been trained on (~20×20 for face detection). </param>
        /// <param name="maxSize">Minimum window size. By default, it is set to the Size(0,0)</param>
        /// <returns></returns>
#endif
        public static CvSeq <CvAvgComp> HaarDetectObjects(
            CvArr image, CvHaarClassifierCascade cascade, CvMemStorage storage,
            double scaleFactor, int minNeighbors, HaarDetectionType flags,
            CvSize minSize, CvSize maxSize)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }
            if (cascade == null)
            {
                throw new ArgumentNullException("cascade");
            }
            if (storage == null)
            {
                throw new ArgumentNullException("storage");
            }
            IntPtr result = NativeMethods.cvHaarDetectObjects(
                image.CvPtr, cascade.CvPtr, storage.CvPtr,
                scaleFactor, minNeighbors, flags, minSize, maxSize);

            KeepAlive(image, cascade, storage);
            if (result == IntPtr.Zero)
            {
                return(null);
            }
            else
            {
                return(new CvSeq <CvAvgComp>(result));
            }
        }
Exemplo n.º 2
0
        //public virtual bool read( const FileNode& node );

        /// <summary>
        /// Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
        /// </summary>
        /// <param name="image">Matrix of the type CV_8U containing an image where objects are detected.</param>
        /// <param name="scaleFactor">Parameter specifying how much the image size is reduced at each image scale.</param>
        /// <param name="minNeighbors">Parameter specifying how many neighbors each candidate rectangle should have to retain it.</param>
        /// <param name="flags">Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects.
        /// It is not used for a new cascade.</param>
        /// <param name="minSize">Minimum possible object size. Objects smaller than that are ignored.</param>
        /// <param name="maxSize">Maximum possible object size. Objects larger than that are ignored.</param>
        /// <returns>Vector of rectangles where each rectangle contains the detected object.</returns>
        public virtual Rect[] DetectMultiScale(
            Mat image,
            double scaleFactor      = 1.1,
            int minNeighbors        = 3,
            HaarDetectionType flags = 0,
            Size?minSize            = null,
            Size?maxSize            = null)
        {
            ThrowIfDisposed();
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }
            image.ThrowIfDisposed();

            Size minSize0 = minSize.GetValueOrDefault(new Size());
            Size maxSize0 = maxSize.GetValueOrDefault(new Size());

            using (var objectsVec = new VectorOfRect())
            {
                NativeMethods.objdetect_CascadeClassifier_detectMultiScale1(
                    ptr, image.CvPtr, objectsVec.CvPtr,
                    scaleFactor, minNeighbors, (int)flags, minSize0, maxSize0);
                GC.KeepAlive(this);
                GC.KeepAlive(image);
                return(objectsVec.ToArray());
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
        /// </summary>
        /// <param name="image">Matrix of the type CV_8U containing an image where objects are detected.</param>
        /// <param name="rejectLevels"></param>
        /// <param name="levelWeights"></param>
        /// <param name="scaleFactor">Parameter specifying how much the image size is reduced at each image scale.</param>
        /// <param name="minNeighbors">Parameter specifying how many neighbors each candidate rectangle should have to retain it.</param>
        /// <param name="flags">Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects.
        /// It is not used for a new cascade.</param>
        /// <param name="minSize">Minimum possible object size. Objects smaller than that are ignored.</param>
        /// <param name="maxSize">Maximum possible object size. Objects larger than that are ignored.</param>
        /// <param name="outputRejectLevels"></param>
        /// <returns>Vector of rectangles where each rectangle contains the detected object.</returns>
        public virtual Rect[] DetectMultiScale(
            Mat image,
            out int[] rejectLevels,
            out double[] levelWeights,
            double scaleFactor      = 1.1,
            int minNeighbors        = 3,
            HaarDetectionType flags = 0,
            Size?minSize            = null,
            Size?maxSize            = null,
            bool outputRejectLevels = false)
        {
            ThrowIfDisposed();
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }
            image.ThrowIfDisposed();

            Size minSize0 = minSize.GetValueOrDefault(new Size());
            Size maxSize0 = maxSize.GetValueOrDefault(new Size());

            using (var objectsVec = new VectorOfRect())
                using (var rejectLevelsVec = new VectorOfInt32())
                    using (var levelWeightsVec = new VectorOfDouble())
                    {
                        NativeMethods.objdetect_CascadeClassifier_detectMultiScale2(
                            ptr, image.CvPtr, objectsVec.CvPtr, rejectLevelsVec.CvPtr, levelWeightsVec.CvPtr,
                            scaleFactor, minNeighbors, (int)flags, minSize0, maxSize0, outputRejectLevels ? 1 : 0);

                        rejectLevels = rejectLevelsVec.ToArray();
                        levelWeights = levelWeightsVec.ToArray();
                        return(objectsVec.ToArray());
                    }
        }
Exemplo n.º 4
0
        private OpenCvSharp.Rect [] DetectFacesInImage(Mat img, HaarDetectionType detectionType, OpenCvSharp.Size minSize)
        {
            if (classifier == null)
            {
                classifier = new CascadeClassifier(@"../../../haarcascade_frontalface_alt.xml");
//                var nestedCascade = new CascadeClassifier(@"..\..\Data\haarcascade_eye_tree_eyeglasses.xml");
            }

            OpenCvSharp.Rect [] faces = classifier.DetectMultiScale(
                image: img,
                scaleFactor: 1.1,
                minNeighbors: 5,
//                 flags: HaarDetectionType.DoRoughSearch | HaarDetectionType.ScaleImage,
//                 flags: HaarDetectionType.FindBiggestObject | HaarDetectionType.ScaleImage,
                flags: detectionType | HaarDetectionType.ScaleImage,
                minSize: minSize);
            return(faces);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
        /// </summary>
        /// <param name="image">Matrix of the type CV_8U containing an image where objects are detected.</param>
        /// <param name="rejectLevels"></param>
        /// <param name="levelWeights"></param>
        /// <param name="scaleFactor">Parameter specifying how much the image size is reduced at each image scale.</param>
        /// <param name="minNeighbors">Parameter specifying how many neighbors each candidate rectangle should have to retain it.</param>
        /// <param name="flags">Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects.
        /// It is not used for a new cascade.</param>
        /// <param name="minSize">Minimum possible object size. Objects smaller than that are ignored.</param>
        /// <param name="maxSize">Maximum possible object size. Objects larger than that are ignored.</param>
        /// <param name="outputRejectLevels"></param>
        /// <returns>Vector of rectangles where each rectangle contains the detected object.</returns>
        public virtual Rect[] DetectMultiScale(
            Mat image,
            out int[] rejectLevels,
            out double[] levelWeights,
            double scaleFactor      = 1.1,
            int minNeighbors        = 3,
            HaarDetectionType flags = 0,
            Size?minSize            = null,
            Size?maxSize            = null,
            bool outputRejectLevels = false)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("CascadeClassifier");
            }
            if (image == null)
            {
                throw new ArgumentNullException("nameof(image)");
            }
            _stopWatch.Start();
            image.ThrowIfDisposed();
            _stopWatch.Stop();
            Debug.Log("FF_throw: " + _stopWatch.ElapsedMilliseconds + "ms");
            _stopWatch.Reset();

            Size minSize0 = minSize.GetValueOrDefault(new Size());
            Size maxSize0 = maxSize.GetValueOrDefault(new Size());

            using (var objectsVec = new VectorOfRect())
                using (var rejectLevelsVec = new VectorOfInt32())
                    using (var levelWeightsVec = new VectorOfDouble())
                    {
                        NativeMethods.objdetect_CascadeClassifier_detectMultiScale2(
                            ptr, image.CvPtr, objectsVec.CvPtr, rejectLevelsVec.CvPtr, levelWeightsVec.CvPtr,
                            scaleFactor, minNeighbors, (int)flags, minSize0, maxSize0, outputRejectLevels ? 1 : 0);

                        rejectLevels = rejectLevelsVec.ToArray();
                        levelWeights = levelWeightsVec.ToArray();
                        return(objectsVec.ToArray());
                    }
        }
Exemplo n.º 6
0
        //public virtual bool read( const FileNode& node );

        /// <summary>
        /// Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
        /// </summary>
        /// <param name="image">Matrix of the type CV_8U containing an image where objects are detected.</param>
        /// <param name="scaleFactor">Parameter specifying how much the image size is reduced at each image scale.</param>
        /// <param name="minNeighbors">Parameter specifying how many neighbors each candidate rectangle should have to retain it.</param>
        /// <param name="flags">Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects.
        /// It is not used for a new cascade.</param>
        /// <param name="minSize">Minimum possible object size. Objects smaller than that are ignored.</param>
        /// <param name="maxSize">Maximum possible object size. Objects larger than that are ignored.</param>
        /// <returns>Vector of rectangles where each rectangle contains the detected object.</returns>
        public virtual Rect[] DetectMultiScale(
            Mat image,
            double scaleFactor      = 1.1,
            int minNeighbors        = 3,
            HaarDetectionType flags = 0,
            Size?minSize            = null,
            Size?maxSize            = null)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("CascadeClassifier");
            }
            if (image == null)
            {
                throw new ArgumentNullException("nameof(image)");
            }
            _stopWatch.Start();

            image.ThrowIfDisposed();

            Size minSize0 = minSize.GetValueOrDefault(new Size());
            Size maxSize0 = maxSize.GetValueOrDefault(new Size());

            _stopWatch.Stop();
            //Debug.Log("FF_throw: " + _stopWatch.ElapsedMilliseconds + "ms");
            _stopWatch.Reset();

            using (var objectsVec = new VectorOfRect())
            {
                _stopWatch.Start();
                NativeMethods.objdetect_CascadeClassifier_detectMultiScale1(
                    ptr, image.CvPtr, objectsVec.CvPtr,
                    scaleFactor, minNeighbors, (int)flags, minSize0, maxSize0);
                _stopWatch.Stop();
                //Debug.Log("FF_detect1: " + _stopWatch.ElapsedMilliseconds + "ms");
                _stopWatch.Reset();
                return(objectsVec.ToArray());
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 与えられた画像からオブジェクトを含む様な矩形領域を検出し,それらの領域を矩形の列として返す.
        /// </summary>
        /// <param name="image">この画像の中からオブジェクトを検出する</param>
        /// <param name="storage">オブジェクト候補の矩形が得られた場合に,その矩形列を保存するメモリストレージ</param>
        /// <param name="scaleFactor">スキャン毎に探索ウィンドウがスケーリングされる際のスケールファクタ. 例えばこの値が 1.1 ならば,ウィンドウが 10% 大きくなる</param>
        /// <param name="minNeighbors">(これから 1 を引いた値が)オブジェクトを構成する近傍矩形の最小数となる. min_neighbors-1 よりも少ない矩形しか含まないようなグループは全て棄却される. もし min_neighbors が 0 である場合,この関数はグループを一つも生成せず,候補となる矩形を全て返す.これはユーザがカスタマイズしたグループ化処理を適用したい場合に有用である. </param>
        /// <param name="flags">処理モード</param>
        /// <param name="minSize">最小ウィンドウサイズ.デフォルトでは分類器の学習に用いられたサンプルのサイズが設定される(顔検出の場合は,~20×20).</param>
        /// <returns>CvAvgCompを要素とするCvSeq</returns>
#else
        /// <summary>
        /// Finds rectangular regions in the given image that are likely to contain objects the cascade has been trained for and returns those regions as a sequence of rectangles.
        /// </summary>
        /// <param name="image">Image to detect objects in. </param>
        /// <param name="storage">Memory storage to store the resultant sequence of the object candidate rectangles. </param>
        /// <param name="scaleFactor">The factor by which the search window is scaled between the subsequent scans, for example, 1.1 means increasing window by 10%. </param>
        /// <param name="minNeighbors">Minimum number (minus 1) of neighbor rectangles that makes up an object. All the groups of a smaller number of rectangles than min_neighbors-1 are rejected. If min_neighbors is 0, the function does not any grouping at all and returns all the detected candidate rectangles, which may be useful if the user wants to apply a customized grouping procedure. </param>
        /// <param name="flags">Mode of operation. Currently the only flag that may be specified is CV_HAAR_DO_CANNY_PRUNING. If it is set, the function uses Canny edge detector to reject some image regions that contain too few or too much edges and thus can not contain the searched object. The particular threshold values are tuned for face detection and in this case the pruning speeds up the processing. </param>
        /// <param name="minSize">Minimum window size. By default, it is set to the size of samples the classifier has been trained on (~20×20 for face detection). </param>
        /// <returns></returns>
#endif
        public CvSeq HaarDetectObjects(CvArr image, CvMemStorage storage, double scaleFactor, int minNeighbors, HaarDetectionType flags, CvSize minSize)
        {
            return Cv.HaarDetectObjects(image, this, storage, scaleFactor, minNeighbors, flags, minSize);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 与えられた画像からオブジェクトを含む様な矩形領域を検出し,それらの領域を矩形の列として返す.
        /// </summary>
        /// <param name="image">この画像の中からオブジェクトを検出する</param>
        /// <param name="cascade">Haar 分類器カスケード の内部表現</param>
        /// <param name="storage">オブジェクト候補の矩形が得られた場合に,その矩形列を保存するメモリストレージ</param>
        /// <param name="scaleFactor">スキャン毎に探索ウィンドウがスケーリングされる際のスケールファクタ. 例えばこの値が 1.1 ならば,ウィンドウが 10% 大きくなる</param>
        /// <param name="minNeighbors">(これから 1 を引いた値が)オブジェクトを構成する近傍矩形の最小数となる. min_neighbors-1 よりも少ない矩形しか含まないようなグループは全て棄却される. もし min_neighbors が 0 である場合,この関数はグループを一つも生成せず,候補となる矩形を全て返す.これはユーザがカスタマイズしたグループ化処理を適用したい場合に有用である. </param>
        /// <param name="flags">処理モード</param>
        /// <returns>CvAvgCompを要素とするCvSeq</returns>
#else
        /// <summary>
        /// Finds rectangular regions in the given image that are likely to contain objects the cascade has been trained for and returns those regions as a sequence of rectangles.
        /// </summary>
        /// <param name="image">Image to detect objects in. </param>
        /// <param name="cascade">Haar classifier cascade in internal representation. </param>
        /// <param name="storage">Memory storage to store the resultant sequence of the object candidate rectangles. </param>
        /// <param name="scaleFactor">The factor by which the search window is scaled between the subsequent scans, for example, 1.1 means increasing window by 10%. </param>
        /// <param name="minNeighbors">Minimum number (minus 1) of neighbor rectangles that makes up an object. All the groups of a smaller number of rectangles than min_neighbors-1 are rejected. If min_neighbors is 0, the function does not any grouping at all and returns all the detected candidate rectangles, which may be useful if the user wants to apply a customized grouping procedure. </param>
        /// <param name="flags">Mode of operation. Currently the only flag that may be specified is CV_HAAR_DO_CANNY_PRUNING. If it is set, the function uses Canny edge detector to reject some image regions that contain too few or too much edges and thus can not contain the searched object. The particular threshold values are tuned for face detection and in this case the pruning speeds up the processing. </param>
        /// <returns></returns>
#endif
        public static CvSeq <CvAvgComp> HaarDetectObjects(CvArr image, CvHaarClassifierCascade cascade, CvMemStorage storage, double scaleFactor, int minNeighbors, HaarDetectionType flags)
        {
            return(HaarDetectObjects(image, cascade, storage, scaleFactor, minNeighbors, flags, new CvSize(0, 0)));
        }
Exemplo n.º 9
0
        /// <summary>
        /// 与えられた画像からオブジェクトを含む様な矩形領域を検出し,それらの領域を矩形の列として返す.
        /// </summary>
        /// <param name="image">この画像の中からオブジェクトを検出する</param>
        /// <param name="cascade">Haar 分類器カスケード の内部表現</param>
        /// <param name="storage">オブジェクト候補の矩形が得られた場合に,その矩形列を保存するメモリストレージ</param>
        /// <param name="scaleFactor">スキャン毎に探索ウィンドウがスケーリングされる際のスケールファクタ. 例えばこの値が 1.1 ならば,ウィンドウが 10% 大きくなる</param>
        /// <param name="minNeighbors">(これから 1 を引いた値が)オブジェクトを構成する近傍矩形の最小数となる. min_neighbors-1 よりも少ない矩形しか含まないようなグループは全て棄却される. もし min_neighbors が 0 である場合,この関数はグループを一つも生成せず,候補となる矩形を全て返す.これはユーザがカスタマイズしたグループ化処理を適用したい場合に有用である. </param>
        /// <param name="flags">処理モード</param>
        /// <param name="minSize">最小ウィンドウサイズ.デフォルトでは分類器の学習に用いられたサンプルのサイズが設定される(顔検出の場合は,~20×20).</param>
        /// <returns>CvAvgCompを要素とするCvSeq</returns>
#else
        /// <summary>
        /// Finds rectangular regions in the given image that are likely to contain objects the cascade has been trained for and returns those regions as a sequence of rectangles.
        /// </summary>
        /// <param name="image">Image to detect objects in. </param>
        /// <param name="cascade">Haar classifier cascade in internal representation. </param>
        /// <param name="storage">Memory storage to store the resultant sequence of the object candidate rectangles. </param>
        /// <param name="scaleFactor">The factor by which the search window is scaled between the subsequent scans, for example, 1.1 means increasing window by 10%. </param>
        /// <param name="minNeighbors">Minimum number (minus 1) of neighbor rectangles that makes up an object. All the groups of a smaller number of rectangles than min_neighbors-1 are rejected. If min_neighbors is 0, the function does not any grouping at all and returns all the detected candidate rectangles, which may be useful if the user wants to apply a customized grouping procedure. </param>
        /// <param name="flags">Mode of operation. Currently the only flag that may be specified is CV_HAAR_DO_CANNY_PRUNING. If it is set, the function uses Canny edge detector to reject some image regions that contain too few or too much edges and thus can not contain the searched object. The particular threshold values are tuned for face detection and in this case the pruning speeds up the processing. </param>
        /// <param name="minSize">Minimum window size. By default, it is set to the size of samples the classifier has been trained on (~20×20 for face detection). </param>
        /// <returns></returns>
#endif
        public static CvSeq<CvAvgComp> HaarDetectObjects(CvArr image, CvHaarClassifierCascade cascade, CvMemStorage storage, double scaleFactor, int minNeighbors, HaarDetectionType flags, CvSize minSize)
        {
            if (image == null)
                throw new ArgumentNullException("image");
            if (cascade == null)
                throw new ArgumentNullException("cascade");
            if (storage == null)
                throw new ArgumentNullException("storage");
            IntPtr result = NativeMethods.cvHaarDetectObjects(image.CvPtr, cascade.CvPtr, storage.CvPtr, scaleFactor, minNeighbors, flags, minSize);
            if (result == IntPtr.Zero)
                return null;
            else
                return new CvSeq<CvAvgComp>(result);
        }
Exemplo n.º 10
0
        /// <summary>
        /// 与えられた画像からオブジェクトを含む様な矩形領域を検出し,それらの領域を矩形の列として返す.
        /// </summary>
        /// <param name="image">この画像の中からオブジェクトを検出する</param>
        /// <param name="cascade">Haar 分類器カスケード の内部表現</param>
        /// <param name="storage">オブジェクト候補の矩形が得られた場合に,その矩形列を保存するメモリストレージ</param>
        /// <param name="scaleFactor">スキャン毎に探索ウィンドウがスケーリングされる際のスケールファクタ. 例えばこの値が 1.1 ならば,ウィンドウが 10% 大きくなる</param>
        /// <param name="minNeighbors">(これから 1 を引いた値が)オブジェクトを構成する近傍矩形の最小数となる. min_neighbors-1 よりも少ない矩形しか含まないようなグループは全て棄却される. もし min_neighbors が 0 である場合,この関数はグループを一つも生成せず,候補となる矩形を全て返す.これはユーザがカスタマイズしたグループ化処理を適用したい場合に有用である. </param>
        /// <param name="flags">処理モード</param>
        /// <returns>CvAvgCompを要素とするCvSeq</returns>
#else
        /// <summary>
        /// Finds rectangular regions in the given image that are likely to contain objects the cascade has been trained for and returns those regions as a sequence of rectangles.
        /// </summary>
        /// <param name="image">Image to detect objects in. </param>
        /// <param name="cascade">Haar classifier cascade in internal representation. </param>
        /// <param name="storage">Memory storage to store the resultant sequence of the object candidate rectangles. </param>
        /// <param name="scaleFactor">The factor by which the search window is scaled between the subsequent scans, for example, 1.1 means increasing window by 10%. </param>
        /// <param name="minNeighbors">Minimum number (minus 1) of neighbor rectangles that makes up an object. All the groups of a smaller number of rectangles than min_neighbors-1 are rejected. If min_neighbors is 0, the function does not any grouping at all and returns all the detected candidate rectangles, which may be useful if the user wants to apply a customized grouping procedure. </param>
        /// <param name="flags">Mode of operation. Currently the only flag that may be specified is CV_HAAR_DO_CANNY_PRUNING. If it is set, the function uses Canny edge detector to reject some image regions that contain too few or too much edges and thus can not contain the searched object. The particular threshold values are tuned for face detection and in this case the pruning speeds up the processing. </param>
        /// <returns></returns>
#endif
        public static CvSeq<CvAvgComp> HaarDetectObjects(CvArr image, CvHaarClassifierCascade cascade, CvMemStorage storage, double scaleFactor, int minNeighbors, HaarDetectionType flags)
        {
            return HaarDetectObjects(image, cascade, storage, scaleFactor, minNeighbors, flags, new CvSize(0, 0));
        }
Exemplo n.º 11
0
        /// <summary>
        /// 与えられた画像からオブジェクトを含む様な矩形領域を検出し,それらの領域を矩形の列として返す.
        /// </summary>
        /// <param name="image">この画像の中からオブジェクトを検出する</param>
        /// <param name="storage">オブジェクト候補の矩形が得られた場合に,その矩形列を保存するメモリストレージ</param>
        /// <param name="scaleFactor">スキャン毎に探索ウィンドウがスケーリングされる際のスケールファクタ. 例えばこの値が 1.1 ならば,ウィンドウが 10% 大きくなる</param>
        /// <param name="minNeighbors">(これから 1 を引いた値が)オブジェクトを構成する近傍矩形の最小数となる. min_neighbors-1 よりも少ない矩形しか含まないようなグループは全て棄却される. もし min_neighbors が 0 である場合,この関数はグループを一つも生成せず,候補となる矩形を全て返す.これはユーザがカスタマイズしたグループ化処理を適用したい場合に有用である. </param>
        /// <param name="flags">処理モード</param>
        /// <param name="minSize">最小ウィンドウサイズ.デフォルトでは分類器の学習に用いられたサンプルのサイズが設定される(顔検出の場合は,~20×20).</param>
        /// <returns>CvAvgCompを要素とするCvSeq</returns>
#else
        /// <summary>
        /// Finds rectangular regions in the given image that are likely to contain objects the cascade has been trained for and returns those regions as a sequence of rectangles.
        /// </summary>
        /// <param name="image">Image to detect objects in. </param>
        /// <param name="storage">Memory storage to store the resultant sequence of the object candidate rectangles. </param>
        /// <param name="scaleFactor">The factor by which the search window is scaled between the subsequent scans, for example, 1.1 means increasing window by 10%. </param>
        /// <param name="minNeighbors">Minimum number (minus 1) of neighbor rectangles that makes up an object. All the groups of a smaller number of rectangles than min_neighbors-1 are rejected. If min_neighbors is 0, the function does not any grouping at all and returns all the detected candidate rectangles, which may be useful if the user wants to apply a customized grouping procedure. </param>
        /// <param name="flags">Mode of operation. Currently the only flag that may be specified is CV_HAAR_DO_CANNY_PRUNING. If it is set, the function uses Canny edge detector to reject some image regions that contain too few or too much edges and thus can not contain the searched object. The particular threshold values are tuned for face detection and in this case the pruning speeds up the processing. </param>
        /// <param name="minSize">Minimum window size. By default, it is set to the size of samples the classifier has been trained on (~20×20 for face detection). </param>
        /// <returns></returns>
#endif
        public CvSeq HaarDetectObjects(CvArr image, CvMemStorage storage, double scaleFactor, int minNeighbors, HaarDetectionType flags, CvSize minSize)
        {
            return(Cv.HaarDetectObjects(image, this, storage, scaleFactor, minNeighbors, flags, minSize));
        }
Exemplo n.º 12
0
        /// <summary>
        /// Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
        /// </summary>
        /// <param name="image">Matrix of the type CV_8U containing an image where objects are detected.</param>
        /// <param name="rejectLevels"></param>
        /// <param name="levelWeights"></param>
        /// <param name="scaleFactor">Parameter specifying how much the image size is reduced at each image scale.</param>
        /// <param name="minNeighbors">Parameter specifying how many neighbors each candidate rectangle should have to retain it.</param>
        /// <param name="flags">Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. 
        /// It is not used for a new cascade.</param>
        /// <param name="minSize">Minimum possible object size. Objects smaller than that are ignored.</param>
        /// <param name="maxSize">Maximum possible object size. Objects larger than that are ignored.</param>
        /// <param name="outputRejectLevels"></param>
        /// <returns>Vector of rectangles where each rectangle contains the detected object.</returns>
        public virtual Rect[] DetectMultiScale(
            Mat image,
            out int[] rejectLevels,
            out double[] levelWeights,
            double scaleFactor = 1.1,
            int minNeighbors = 3,
            HaarDetectionType flags = HaarDetectionType.Zero,
            Size? minSize = null,
            Size? maxSize = null,
            bool outputRejectLevels = false)
        {
            if (disposed)
                throw new ObjectDisposedException("CascadeClassifier");
            if (image == null)
                throw new ArgumentNullException("image");
            image.ThrowIfDisposed();

            Size minSize0 = minSize.GetValueOrDefault(new Size());
            Size maxSize0 = maxSize.GetValueOrDefault(new Size());

            using (var objectsVec = new VectorOfRect())
            using (var rejectLevelsVec = new VectorOfInt32())
            using (var levelWeightsVec = new VectorOfDouble())
            {
                NativeMethods.objdetect_CascadeClassifier_detectMultiScale(
                    ptr, image.CvPtr, objectsVec.CvPtr, rejectLevelsVec.CvPtr, levelWeightsVec.CvPtr,
                    scaleFactor, minNeighbors, (int)flags, minSize0, maxSize0, outputRejectLevels ? 1 : 0);

                rejectLevels = rejectLevelsVec.ToArray();
                levelWeights = levelWeightsVec.ToArray();
                return objectsVec.ToArray();
            }
        }
Exemplo n.º 13
0
        //public virtual bool read( const FileNode& node );

        /// <summary>
        /// Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
        /// </summary>
        /// <param name="image">Matrix of the type CV_8U containing an image where objects are detected.</param>
        /// <param name="scaleFactor">Parameter specifying how much the image size is reduced at each image scale.</param>
        /// <param name="minNeighbors">Parameter specifying how many neighbors each candidate rectangle should have to retain it.</param>
        /// <param name="flags">Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. 
        /// It is not used for a new cascade.</param>
        /// <param name="minSize">Minimum possible object size. Objects smaller than that are ignored.</param>
        /// <param name="maxSize">Maximum possible object size. Objects larger than that are ignored.</param>
        /// <returns>Vector of rectangles where each rectangle contains the detected object.</returns>
        public virtual Rect[] DetectMultiScale(
            Mat image,
            double scaleFactor = 1.1,
            int minNeighbors = 3,
            HaarDetectionType flags = 0,
            Size? minSize = null,
            Size? maxSize = null)
        {
            if (disposed)
                throw new ObjectDisposedException("CascadeClassifier");
            if (image == null)
                throw new ArgumentNullException("image");
            image.ThrowIfDisposed();

            Size minSize0 = minSize.GetValueOrDefault(new Size());
            Size maxSize0 = maxSize.GetValueOrDefault(new Size());

            using (var objectsVec = new VectorOfRect())
            {
                NativeMethods.objdetect_CascadeClassifier_detectMultiScale1(
                    ptr, image.CvPtr, objectsVec.CvPtr, 
                    scaleFactor, minNeighbors, (int)flags, minSize0, maxSize0);
                return objectsVec.ToArray();
            }
        }
        /// <summary>
        /// 与えられた画像からオブジェクトを含む様な矩形領域を検出し,それらの領域を矩形の列として返す.
        /// </summary>
        /// <param name="image">この画像の中からオブジェクトを検出する</param>
        /// <param name="storage">オブジェクト候補の矩形が得られた場合に,その矩形列を保存するメモリストレージ</param>
        /// <param name="scale_factor">スキャン毎に探索ウィンドウがスケーリングされる際のスケールファクタ. 例えばこの値が 1.1 ならば,ウィンドウが 10% 大きくなる</param>
        /// <param name="min_neighbors">(これから 1 を引いた値が)オブジェクトを構成する近傍矩形の最小数となる. min_neighbors-1 よりも少ない矩形しか含まないようなグループは全て棄却される. もし min_neighbors が 0 である場合,この関数はグループを一つも生成せず,候補となる矩形を全て返す.これはユーザがカスタマイズしたグループ化処理を適用したい場合に有用である. </param>
        /// <param name="flags">処理モード</param>
        /// <returns>CvAvgCompを要素とするCvSeq</returns>
#else
        /// <summary>
        /// Finds rectangular regions in the given image that are likely to contain objects the cascade has been trained for and returns those regions as a sequence of rectangles.
        /// </summary>
        /// <param name="image">Image to detect objects in. </param>
        /// <param name="storage">Memory storage to store the resultant sequence of the object candidate rectangles. </param>
        /// <param name="scale_factor">The factor by which the search window is scaled between the subsequent scans, for example, 1.1 means increasing window by 10%. </param>
        /// <param name="min_neighbors">Minimum number (minus 1) of neighbor rectangles that makes up an object. All the groups of a smaller number of rectangles than min_neighbors-1 are rejected. If min_neighbors is 0, the function does not any grouping at all and returns all the detected candidate rectangles, which may be useful if the user wants to apply a customized grouping procedure. </param>
        /// <param name="flags">Mode of operation. Currently the only flag that may be specified is CV_HAAR_DO_CANNY_PRUNING. If it is set, the function uses Canny edge detector to reject some image regions that contain too few or too much edges and thus can not contain the searched object. The particular threshold values are tuned for face detection and in this case the pruning speeds up the processing. </param>
        /// <returns></returns>
#endif
        public CvSeq HaarDetectObjects(CvArr image, CvMemStorage storage, double scale_factor, int min_neighbors, HaarDetectionType flags)
        {
            return Cv.HaarDetectObjects(image, this, storage, scale_factor, min_neighbors, flags);
        }
Exemplo n.º 15
0
        public List <Bitmap> Detect(Bitmap img, double FaceScale = 1.0, double scaleFactor = 1.1, int minNeighbors = 3, HaarDetectionType flags = 0, Size?minSize = null, Size?maxSize = null)
        {
            //メンバーと引数チェック
            if (Classifier == null)
            {
                throw new ClassfierUnLoadedException();
            }
            if (img == null)
            {
                throw new ArgumentNullException();
            }

            //引数の変換
            OpenCvSharp.Size?cvMinSize = null;
            if (minSize != null)
            {
                cvMinSize = Utils.ConvertFdSizeToCvSize(minSize.Value);
            }

            OpenCvSharp.Size?cvMaxSize = null;
            if (maxSize != null)
            {
                cvMaxSize = Utils.ConvertFdSizeToCvSize(maxSize.Value);
            }

            //処理
            List <Bitmap> dst = new List <Bitmap>();

            using (Mat src = BitmapConverter.ToMat(img))
                using (Mat gray = new Mat())
                {
                    Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);

                    var faceRects = Classifier.DetectMultiScale(gray, scaleFactor, minNeighbors, (OpenCvSharp.HaarDetectionType)flags, cvMinSize, cvMaxSize);
                    if (faceRects.Count() == 0)
                    {
                        return(null);
                    }

                    foreach (var r in faceRects)
                    {
                        using (Mat face = new Mat())
                        {
                            Utils.CropRect(src, face, r, FaceScale);
                            dst.Add(BitmapConverter.ToBitmap(face));
                        }
                    }
                }

            return(dst);
        }