/// <summary> /// Find groups of Extremal Regions that are organized as text blocks. /// </summary> /// <param name="image">The image where ER grouping is to be perform on</param> /// <param name="channels">Array of single channel images from which the regions were extracted</param> /// <param name="erstats">Vector of ER’s retrieved from the ERFilter algorithm from each channel</param> /// <param name="groupingTrainedFileName">The XML or YAML file with the classifier model (e.g. trained_classifier_erGrouping.xml)</param> /// <param name="minProbability">The minimum probability for accepting a group.</param> /// <param name="groupMethods">The grouping methods</param> /// <returns>The output of the algorithm that indicates the text regions</returns> public static System.Drawing.Rectangle[] ERGrouping(IInputArray image, IInputArrayOfArrays channels, VectorOfERStat[] erstats, GroupingMethod groupMethods = GroupingMethod.OrientationHoriz, String groupingTrainedFileName = null, float minProbability = 0.5f) { IntPtr[] erstatPtrs = new IntPtr[erstats.Length]; for (int i = 0; i < erstatPtrs.Length; i++) { erstatPtrs[i] = erstats[i].Ptr; } using (VectorOfVectorOfPoint regionGroups = new VectorOfVectorOfPoint()) using (VectorOfRect groupsBoxes = new VectorOfRect()) using (InputArray iaImage = image.GetInputArray()) using (InputArray iaChannels = channels.GetInputArray()) using (CvString s = (groupingTrainedFileName == null ? new CvString() : new CvString(groupingTrainedFileName))) { GCHandle erstatsHandle = GCHandle.Alloc(erstatPtrs, GCHandleType.Pinned); TextInvoke.cveERGrouping( iaImage, iaChannels, erstatsHandle.AddrOfPinnedObject(), erstatPtrs.Length, regionGroups, groupsBoxes, groupMethods, s, minProbability); erstatsHandle.Free(); return(groupsBoxes.ToArray()); } }
/// <summary> /// Release all the unmanaged memory associate with this ERFilter /// </summary> protected override void DisposeObject() { if (_ptr != IntPtr.Zero) { TextInvoke.cveERFilterRelease(ref _ptr, ref _sharedPtr); } }
/// <summary> /// Release all the unmanaged memory associate with this TextDetector /// </summary> protected override void DisposeObject() { if (_sharedPtr != IntPtr.Zero) { TextInvoke.cveTextDetectorCNNRelease(ref _sharedPtr); _ptr = IntPtr.Zero; } }
/// <summary> /// Create an Extremal Region Filter for the 1st stage classifier of N&M algorithm /// </summary> /// <param name="classifierFileName">The file name of the classifier</param> /// <param name="thresholdDelta">Threshold step in subsequent thresholds when extracting the component tree.</param> /// <param name="minArea">The minimum area (% of image size) allowed for retreived ER’s.</param> /// <param name="maxArea">The maximum area (% of image size) allowed for retreived ER’s.</param> /// <param name="minProbability">The minimum probability P(er|character) allowed for retreived ER’s.</param> /// <param name="nonMaxSuppression">Whenever non-maximum suppression is done over the branch probabilities.</param> /// <param name="minProbabilityDiff">The minimum probability difference between local maxima and local minima ERs.</param> public ERFilterNM1( String classifierFileName, int thresholdDelta = 1, float minArea = 0.00025f, float maxArea = 0.13f, float minProbability = 0.4f, bool nonMaxSuppression = true, float minProbabilityDiff = 0.1f) { using (CvString s = new CvString(classifierFileName)) _ptr = TextInvoke.cveERFilterNM1Create(s, thresholdDelta, minArea, maxArea, minProbability, nonMaxSuppression, minProbabilityDiff, ref _sharedPtr); }
/// <summary> /// Creates an instance of the TextDetectorCNN class using the provided parameters. /// </summary> /// <param name="modelArchFilename">The relative or absolute path to the prototxt file describing the classifiers architecture.</param> /// <param name="modelWeightsFilename">The relative or absolute path to the file containing the pretrained weights of the model in caffe-binary form.</param> /// <param name="detectionSizes">A list of sizes for multi-scale detection. The values[(300,300),(700,500),(700,300),(700,700),(1600,1600)] are recommended to achieve the best quality.</param> public TextDetectorCNN(String modelArchFilename, String modelWeightsFilename, Size[] detectionSizes = null) { using (CvString csModelArchFilename = new CvString(modelArchFilename)) using (CvString csModelWeightsFilename = new CvString(modelWeightsFilename)) { if (detectionSizes == null) { _ptr = TextInvoke.cveTextDetectorCNNCreate(csModelArchFilename, csModelWeightsFilename, ref _sharedPtr); } else { using (VectorOfSize vs = new VectorOfSize(detectionSizes)) _ptr = TextInvoke.cveTextDetectorCNNCreate2(csModelArchFilename, csModelWeightsFilename, vs, ref _sharedPtr); } } }
/// <summary> /// Find bounding boxes of text words given an input image. /// </summary> /// <param name="inputImage">An image expected to be a CV_U8C3 of any size</param> /// <returns>The text regions found.</returns> public TextRegion[] Detect(IInputArray inputImage) { using (InputArray iaImage = inputImage.GetInputArray()) using (VectorOfRect vr = new VectorOfRect()) using (VectorOfFloat vf = new VectorOfFloat()) { TextInvoke.cveTextDetectorCNNDetect(_ptr, iaImage, vr, vf); Rectangle[] bboxes = vr.ToArray(); float[] confidents = vf.ToArray(); TextRegion[] regions = new TextRegion[bboxes.Length]; for (int i = 0; i < regions.Length; i++) { TextRegion tr = new TextRegion(); tr.BBox = bboxes[i]; tr.Confident = confidents[i]; regions[i] = tr; } return(regions); } }
/// <summary> /// Takes image on input and returns the selected regions in a vector of ERStat only distinctive ERs which correspond to characters are selected by a sequential classifier /// </summary> /// <param name="image">Sinle channel image CV_8UC1</param> /// <param name="regions">Output for the 1st stage and Input/Output for the 2nd. The selected Extremal Regions are stored here.</param> public void Run(IInputArray image, VectorOfERStat regions) { using (InputArray iaImage = image.GetInputArray()) TextInvoke.cveERFilterRun(_ptr, iaImage, regions); }
/// <summary> /// Create an Extremal Region Filter for the 2nd stage classifier of N&M algorithm /// </summary> /// <param name="classifierFileName">The file name of the classifier</param> /// <param name="minProbability">The minimum probability P(er|character) allowed for retreived ER’s.</param> public ERFilterNM2(String classifierFileName, float minProbability = 0.3f) { using (CvString s = new CvString(classifierFileName)) _ptr = TextInvoke.cveERFilterNM2Create(s, minProbability, ref _sharedPtr); }