/// <summary> /// Finds lines in the input image. /// This is the output of the default parameters of the algorithm on the above shown image. /// </summary> /// <param name="image">A grayscale (CV_8UC1) input image. </param> /// <param name="lines">A vector of Vec4i or Vec4f elements specifying the beginning and ending point of a line. /// Where Vec4i/Vec4f is (x1, y1, x2, y2), point 1 is the start, point 2 - end. Returned lines are strictly oriented depending on the gradient.</param> /// <param name="width">Vector of widths of the regions, where the lines are found. E.g. Width of line.</param> /// <param name="prec">Vector of precisions with which the lines are found.</param> /// <param name="nfa">Vector containing number of false alarms in the line region, /// with precision of 10%. The bigger the value, logarithmically better the detection.</param> public virtual void Detect(InputArray image, out Vec4f[] lines, out double[] width, out double[] prec, out double[] nfa) { if (image == null) { throw new ArgumentNullException("nameof(image)"); } image.ThrowIfDisposed(); using (var linesVec = new VectorOfVec4f()) using (var widthVec = new VectorOfDouble()) using (var precVec = new VectorOfDouble()) using (var nfaVec = new VectorOfDouble()) { NativeMethods.imgproc_LineSegmentDetector_detect_vector(ptr, image.CvPtr, linesVec.CvPtr, widthVec.CvPtr, precVec.CvPtr, nfaVec.CvPtr); lines = linesVec.ToArray(); width = widthVec.ToArray(); prec = precVec.ToArray(); nfa = nfaVec.ToArray(); } GC.KeepAlive(image); }
/// <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()); } }
/// <summary> /// Groups the object candidate rectangles. /// </summary> /// <param name="rectList"></param> /// <param name="rejectLevels"></param> /// <param name="levelWeights"></param> /// <param name="groupThreshold"></param> /// <param name="eps"></param> public static void GroupRectangles(IList <Rect> rectList, out int[] rejectLevels, out double[] levelWeights, int groupThreshold, double eps = 0.2) { if (rectList == null) { throw new ArgumentNullException(nameof(rectList)); } using (var rectListVec = new VectorOfRect(rectList)) using (var rejectLevelsVec = new VectorOfInt32()) using (var levelWeightsVec = new VectorOfDouble()) { NativeMethods.objdetect_groupRectangles4(rectListVec.CvPtr, rejectLevelsVec.CvPtr, levelWeightsVec.CvPtr, groupThreshold, eps); ClearAndAddRange(rectList, rectListVec.ToArray()); rejectLevels = rejectLevelsVec.ToArray(); levelWeights = levelWeightsVec.ToArray(); } }
/// <summary> /// /// </summary> /// <param name="image">Input image CV_8UC1 or CV_8UC3 with a single letter</param> /// <param name="classes">The classifier returns the character class categorical label, or list of class labels, to which the input image corresponds</param> /// <param name="confidences">The classifier returns the probability of the input image corresponding to each classes in out_class</param> public void Eval(InputArray image, out int[] classes, out double[] confidences) { if (image == null) { throw new ArgumentNullException("nameof(image)"); } image.ThrowIfDisposed(); using (var vecClass = new VectorOfInt32()) using (var vecConfd = new VectorOfDouble()) { NativeMethods.text_OCRHMMDecoder_ClassifierCallback_eval(ptr, image.CvPtr, vecClass.CvPtr, vecConfd.CvPtr); classes = vecClass.ToArray(); confidences = vecConfd.ToArray(); } GC.KeepAlive(image); }
/// <summary> /// Groups the object candidate rectangles. /// </summary> /// <param name="rectList"></param> /// <param name="groupThreshold"></param> /// <param name="eps"></param> /// <param name="weights"></param> /// <param name="levelWeights"></param> public static void GroupRectangles(IList <Rect> rectList, int groupThreshold, double eps, out int[] weights, out double[] levelWeights) { if (rectList == null) { throw new ArgumentNullException(nameof(rectList)); } using var rectListVec = new VectorOfRect(rectList); using var weightsVec = new VectorOfInt32(); using var levelWeightsVec = new VectorOfDouble(); NativeMethods.HandleException( NativeMethods.objdetect_groupRectangles3( rectListVec.CvPtr, groupThreshold, eps, weightsVec.CvPtr, levelWeightsVec.CvPtr)); ClearAndAddRange(rectList, rectListVec.ToArray()); weights = weightsVec.ToArray(); levelWeights = levelWeightsVec.ToArray(); }
/// <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()); } }
/// <summary> /// /// </summary> /// <param name="rectList"></param> /// <param name="foundWeights"></param> /// <param name="foundScales"></param> /// <param name="detectThreshold"></param> /// <param name="winDetSize"></param> public static void GroupRectanglesMeanshift(IList <Rect> rectList, out double[] foundWeights, out double[] foundScales, double detectThreshold = 0.0, Size?winDetSize = null) { if (rectList == null) { throw new ArgumentNullException(nameof(rectList)); } Size winDetSize0 = winDetSize.GetValueOrDefault(new Size(64, 128)); using (var rectListVec = new VectorOfRect(rectList)) using (var foundWeightsVec = new VectorOfDouble()) using (var foundScalesVec = new VectorOfDouble()) { NativeMethods.objdetect_groupRectangles_meanshift( rectListVec.CvPtr, foundWeightsVec.CvPtr, foundScalesVec.CvPtr, detectThreshold, winDetSize0); ClearAndAddRange(rectList, rectListVec.ToArray()); foundWeights = foundWeightsVec.ToArray(); foundScales = foundScalesVec.ToArray(); } }