Class OcrInputBase
Base class for OCR input variants
Implements
Namespace: IronOcr
Assembly: IronOcr.dll
Syntax
public abstract class OcrInputBase : Object, IDisposable
Constructors
OcrInputBase()
Declaration
protected OcrInputBase()
Properties
TargetDPI
The resolution that low resolution images will be enhanced to. To disable upscaling, set this to 0 (will affect read quality).
TargetDPI also determines the resolution at which PDF documents will be sampled.
Declaration
public int TargetDPI { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Int32 |
Title
A title for the OcrInput document. This is relevant as it becomes metadata when exporting searchable PDFs and HOCR files from IronTesseract results.
See SaveAsSearchablePdf(String, Boolean) and SaveAsHocrFile(String)
Declaration
public string Title { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
Methods
AdaptiveThreshold(Nullable<Single>)
Applies Bradley Adaptive Threshold to the image.
Adaptive thresholding is the method where the threshold value is calculated for smaller regions and therefore, there will be different threshold values for different regions.
Declaration
public OcrInputBase AdaptiveThreshold(Nullable<float> thresholdLimit = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Nullable<System.Single> | thresholdLimit | Threshold limit (0.0-1.0) to consider for binarization. 0.0 to consider threshold is completely white 1.0 to consider threshold is completely black |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
ApplyMultipleFilters(OcrFilters, Double, Int32, Int32, Int32, Boolean, Nullable<Int32>)
Apply multiple imaging filters using the specified parameters
Declaration
public void ApplyMultipleFilters(OcrFilters filters, double Rotation = 0, int MaxDeskewAngle = 45, int MaxWidth = 0, int MaxHeight = 0, bool Use3x3 = false, Nullable<int> thresholdLimit = null)
Parameters
| Type | Name | Description |
|---|---|---|
| OcrFilters | filters | Filters to apply |
| System.Double | Rotation | Rotation amount. Required when using the Rotation filter. Rotate |
| System.Int32 | MaxDeskewAngle | Optional MaxDeskewAngle amount when using the Deskew filter. Defaults to 45 degrees.Deskew |
| System.Int32 | MaxWidth | Maximum width. Required when using the Scale filter. Scale |
| System.Int32 | MaxHeight | Maximum height. Required when using the Scale filter. Scale |
| System.Boolean | Use3x3 | Optional morphology when using the Despeckle (DeNoise), Dilate, or Erode filter. DeNoise, Dilate, or Erode |
| System.Nullable<System.Int32> | thresholdLimit | Optional Threshold limit (0.0-1.0) to consider for binarization when using the Bradley Adaptive Threshold. |
Remarks
This method serves as an alternative way to apply multiple filters. Filters are applied in what is typically the optimal order.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
Binarize()
This image filter turns every pixel black or white with no middle ground. May Improve OCR performance cases of very low contrast of text to background.
Declaration
public OcrInputBase Binarize()
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
Close(Boolean)
Advanced Morphology.
Closing is reverse of Opening, Dilation followed by Erosion. It is useful in closing small holes inside the foreground objects.
Declaration
public OcrInputBase Close(bool use3x3 = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Boolean | use3x3 | 2x2 is default morphology |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
Contrast(Single)
Increases contrast automatically. This filter often improves OCR speed and accuracy in low contrast scans. Flattens Alpha channels to white.
Declaration
public OcrInputBase Contrast(float amount = 1.1F)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | amount | Amount which is used to adjust contrast. A value of 0 will create an image that is completely gray. A value of 1 leaves the input unchanged. Amount values greater than 0 increase contrast making light areas lighter and dark areas darker. Amount values less than 0 decrease contrast - decreasing variety of contrast. |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
DeNoise(Boolean)
Removes digital noise. This filter should only be used where noise is expected. Flattens Alpha channels to white.
Declaration
public OcrInputBase DeNoise(bool use3x3 = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Boolean | use3x3 | 2x2 is default morphology |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
See Also
Deskew(Int32)
Rotates an image so it is the right way up and orthogonal. This is very useful for OCR because Tesseract tolerance for skewed scans can be as low as 5 degrees.
This also helps when producing searchable PDF documents from IronTesseract because the pages will likely all be the right way up.
This version uses only Hough transform to make minor correction. Example: pages that where put in a scanner at a slight angle.
Declaration
public bool Deskew(int MaxDeskewAngle = 45)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | MaxDeskewAngle | Maximum angle of skew to correct for. Higher values can lead to more opportunity for correction, but may be slower and more prone to error including upside down pages. |
Returns
| Type | Description |
|---|---|
| System.Boolean | Returns a boolean result of whether or not IronOCR was able to detect image orientation. True = Deskew was applied. False = Failed to detect image orientation and image remains unchanged. |
See Also
Despeckle(Boolean)
DeSpeckle as an alias of DeNoise.
Alias of DeNoise(Boolean) to make this method easily to find in Intensense.
Declaration
public OcrInputBase Despeckle(bool use3x3 = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Boolean | use3x3 | 2x2 is default morphology |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
DetectPageOrientation(OrientationDetectionMode)
Detects the page orientation for each image in a collection using specified detection modes.
Declaration
public IEnumerable<OcrPageOrientationResult> DetectPageOrientation(OrientationDetectionMode OrientedDetMode)
Parameters
| Type | Name | Description |
|---|---|---|
| OrientationDetectionMode | OrientedDetMode |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<OcrPageOrientationResult> | An array of OcrPageOrientationResult where each element represents the detected orientation for an image in the collection. |
Remarks
Higher text density within the image or PDF generally leads to more accurate orientation detection.
The correctional rotation angle can be directly used to rotate the image to the correct orientation.
Dilate(Boolean)
Advanced Morphology. Dilation is the opposite of Erosion, instead of shrinking it expands the foreground object.
Opposite of Erode(Boolean).
Declaration
public OcrInputBase Dilate(bool use3x3 = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Boolean | use3x3 | 2x2 is default morphology |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
Dispose()
Disposes of this OcrInput object.
Declaration
public void Dispose()
EnhanceResolution(Int32)
Enhances the resolution of low quality images. This filter is not often needed because TargetDPI will automatically catch and resolve low resolution inputs.
May not work for all images if their metadata is corrupted.
Declaration
public OcrInputBase EnhanceResolution(int TargetDPI = 225)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | TargetDPI | The target DPI to resample to. |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
Erode(Boolean)
Advanced Morphology. Erosion is the morphological operation used to diminish the size of the foreground object.
Opposite of Erode(Boolean).
Declaration
public OcrInputBase Erode(bool use3x3 = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Boolean | use3x3 | 2x2 is default morphology |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
Finalize()
OcrInput has a safe finaliser that cleans up undisposed native images in memory.
Declaration
protected override void Finalize()
FindMultipleTextRegions()
Use computer vision to detect areas which contain text elements and divide the input into separate images based on text regions.
Declaration
public void FindMultipleTextRegions()
Remarks
Useful for generating several OCR results from a single image/page
FindTextRegion()
Use computer vision to detect regions which contain text elements on each page
Declaration
public void FindTextRegion()
GetPages()
Get all OcrInputPage within this OcrInput
Declaration
public IEnumerable<OcrInputPage> GetPages()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<OcrInputPage> |
HighlightTextAndSaveAsImages(IronTesseract, String, ResultHighlightType)
Based on the ResultHighlightType, will draw red boxes around characters/words/lines/paragraphs detected, and save to a PNG image.
For best results, perform all filters before calling.
Declaration
public void HighlightTextAndSaveAsImages(IronTesseract tesseract, string filename, ResultHighlightType type)
Parameters
| Type | Name | Description |
|---|---|---|
| IronTesseract | tesseract | IronTesseract instance used to scan the OcrInput. |
| System.String | filename | File will be saved as : 'filename_page_0.png'. You may use an absolute or relative path. |
| ResultHighlightType | type | Choose whether each box represents a character, word, line, paragraph. |
HoughTransformStraighten(Int32)
Uses a Hough Transform to rotate and image to the nearest 90 degrees of straightness. This is very useful for OCR because Tesseract tolerance for skewed scans can be as low as 5 degrees.
A Synonym of Deskew(Int32)
Declaration
public OcrInputBase HoughTransformStraighten(int MaxDeskewAngle = 45)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | MaxDeskewAngle | Maximum angle of skew to correct for. Higher values can lead to more opportunity for correction, but may be slower and more prone to error including upside down pages. |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
See Also
Invert(Boolean)
Inverts every color. E.g. White becomes black : black becomes white.
Declaration
public OcrInputBase Invert(bool GrayScale = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Boolean | GrayScale | Optionally remove all color channels and return a GrayScale image. |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
Open(Boolean)
Advanced Morphology.
Opening is just another name of erosion followed by dilation. It is useful in removing noise.
Declaration
public OcrInputBase Open(bool use3x3 = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Boolean | use3x3 | 2x2 is default morphology |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
PageCount()
The number of OcrInputPages currently present in this OcrInput
Declaration
public int PageCount()
Returns
| Type | Description |
|---|---|
| System.Int32 |
RemovePage(Int32)
Removes a OcrInputPage within this OcrInput at the specified index.
Declaration
public bool RemovePage(int pageIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | pageIndex | Indexes of a page in this OcrInput to remove. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if page was removed sucessfully. False if removal failed. |
RemovePages(IEnumerable<Int32>)
Removes all OcrInputPage within this OcrInput at the specified indexes.
Declaration
public bool RemovePages(IEnumerable<int> pageIndexes)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<System.Int32> | pageIndexes | Indexes of pages in this OcrInput to remove. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if ALL page indexes were removed sucessfully. False if one or more failed. |
ReplaceColor(Color, Color, Int32)
Replace current color to new color in Image
Declaration
public OcrInputBase ReplaceColor(Color currentColor, Color newColor, int tolerance = 10)
Parameters
| Type | Name | Description |
|---|---|---|
| IronSoftware.Drawing.Color | currentColor | Current IronSoftware.Drawing.Color |
| IronSoftware.Drawing.Color | newColor | New IronSoftware.Drawing.Color |
| System.Int32 | tolerance | Tolerance Value |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
Rotate(Double)
Rotates images by a number of degrees clockwise. For anti-clockwise, use negative numbers. Also see Deskew(Int32)
Declaration
public OcrInputBase Rotate(double Degrees)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | Degrees | A number of clockwise degrees. |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
SaveAsImages(String, AnyBitmap.ImageFormat)
Exports an OcrInput object as Images
Declaration
public string[] SaveAsImages(string Prefix = "export_of_page", AnyBitmap.ImageFormat Extension)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | Prefix | Will save images at {Prefix}_(page_number).{Extension}. May include a fully qualified file path. |
| IronSoftware.Drawing.AnyBitmap.ImageFormat | Extension | Output file extension in lower-case. |
Returns
| Type | Description |
|---|---|
| System.String[] | Array of saved image file names. Can be multiple if OcrInput used has multiple pages. |
Exceptions
| Type | Condition |
|---|---|
| System.Exception | Throws an exception if there are no pages. See OcrInputPage |
Scale(Int32, Boolean)
Scales OCRInput pages proportionally.
Declaration
public OcrInputBase Scale(int Percentage, bool ScaleCropArea = true)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | Percentage | The percentage scale. 100 = no effect. |
| System.Boolean | ScaleCropArea | Should associated crop areas also be scaled proportionally (recommended true) |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
Scale(Int32, Int32, Boolean)
Scales the OCRInput pages up in size.
Declaration
public OcrInputBase Scale(int MaxWidth, int MaxHeight, bool ScaleCropArea = true)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | MaxWidth | Maximum width in pixels. |
| System.Int32 | MaxHeight | Maximum height in pixels. |
| System.Boolean | ScaleCropArea | Should associated crop areas also be scaled proportionally (recommended true) |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
SelectTextColor(Color, Int32)
Binarize an image to read pixels of a color (with threshold) as text and ignore other colors as background.
This is useful if you image has many colors and a normal binarize will not work. It will turn all text of the color specified into black and the rest of the image to white.
Declaration
public OcrInputBase SelectTextColor(Color selectColor, int tolerance = 10)
Parameters
| Type | Name | Description |
|---|---|---|
| IronSoftware.Drawing.Color | selectColor | IronSoftware.Drawing.Color of text to isolate from background. |
| System.Int32 | tolerance | [0,255]; Acceptable range of the difference between PixelColor and selectColor for each R, G, and B value |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
SelectTextColors(IEnumerable<Color>, Int32)
Binarize an image to read pixels of only selected colors (with thresholds) as text and ignore other colors as background.
This is useful if you image has many colors and a normal binarize will not work. It will turn all text of the color specified into black and the rest of the image to white.
Declaration
public OcrInputBase SelectTextColors(IEnumerable<Color> selectColors, int tolerance = 10)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<IronSoftware.Drawing.Color> | selectColors | IronSoftware.Drawing.Color of text to isolate from background. |
| System.Int32 | tolerance | [0,255]; Acceptable range of the difference between PixelColor and selectColor for each R, G, and B value |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
Sharpen()
Sharpens blurred OCR Documents. Applies a Gaussian sharpening filter to image.
Declaration
public OcrInputBase Sharpen()
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
StampCropRectangle(OcrInput, Rectangle, Color)
Returns a new OcrInput object with a rectangle applied and to visualize and debug where a Crop Rectangle will be applied when using IronSoftware.Drawing.Rectangle on your image.
Declaration
public static OcrInput StampCropRectangle(OcrInput input, Rectangle cropRectangle, Color rectangleColor)
Parameters
| Type | Name | Description |
|---|---|---|
| OcrInput | input | OcrInput object to return a copy of stamped |
| IronSoftware.Drawing.Rectangle | cropRectangle | Use a IronSoftware.Drawing.Rectangle to debug the area that will be scanned on an image. |
| IronSoftware.Drawing.Color | rectangleColor | Color of rectangle drawn. Red is recommended for easy contrast. |
Returns
| Type | Description |
|---|---|
| OcrInput |
StampCropRectangleAndSaveAs(Rectangle, Color, String, AnyBitmap.ImageFormat)
Saves a copy of the image with a rectangle applied and to visualize and debug where Crop Rectangle will be applied when using IronSoftware.Drawing.Rectangle on your image.
Declaration
public string[] StampCropRectangleAndSaveAs(Rectangle cropRectangle, Color rectangleColor, string Prefix = "rectangle_on_page", AnyBitmap.ImageFormat extension)
Parameters
| Type | Name | Description |
|---|---|---|
| IronSoftware.Drawing.Rectangle | cropRectangle | Use a IronSoftware.Drawing.Rectangle to debug the area that will be scanned on an image. |
| IronSoftware.Drawing.Color | rectangleColor | Color of rectangle drawn. Red is recommended for easy contrast. |
| System.String | Prefix | Will save images at {Prefix}_(page_number).{Extension}. May include a fully qualified file path. |
| IronSoftware.Drawing.AnyBitmap.ImageFormat | extension | Output file extension in lower-case. |
Returns
| Type | Description |
|---|---|
| System.String[] | Array of saved image file names. Can be multiple if OcrInput used has multiple pages. |
Exceptions
| Type | Condition |
|---|---|
| System.Exception | Throws an exception if there are no pages. See OcrInputPage |
StampCropRectangles(OcrInput, Rectangle[], Color)
Returns a new OcrInput object with a rectangle applied and to visualize and debug where the Rectangle will be applied when using IronSoftware.Drawing.Rectangle on your image.
Declaration
public static OcrInput StampCropRectangles(OcrInput input, Rectangle[] cropRectangles, Color rectangleColor)
Parameters
| Type | Name | Description |
|---|---|---|
| OcrInput | input | OcrInput object to return a copy of stamped |
| IronSoftware.Drawing.Rectangle[] | cropRectangles | Use an array of IronSoftware.Drawing.Rectangle to debug the areas that will be scanned on an image. |
| IronSoftware.Drawing.Color | rectangleColor | Color of rectangle drawn. Red is recommended for easy contrast. |
Returns
| Type | Description |
|---|---|
| OcrInput |
StampCropRectanglesAndSaveAs(Rectangle[], Color, String, AnyBitmap.ImageFormat)
Saves a copy of the image with all rectangles applied and to visualize and debug where Crop Rectangles will be applied when using IronSoftware.Drawing.Rectangle on your image.
Declaration
public string[] StampCropRectanglesAndSaveAs(Rectangle[] cropRectangles, Color rectangleColor, string Prefix = "rectangle_on_page", AnyBitmap.ImageFormat extension)
Parameters
| Type | Name | Description |
|---|---|---|
| IronSoftware.Drawing.Rectangle[] | cropRectangles | Use an array of IronSoftware.Drawing.Rectangle to debug the areas that will be scanned on an image. |
| IronSoftware.Drawing.Color | rectangleColor | Color of rectangles drawn. Red is recommended for easy contrast. |
| System.String | Prefix | Will save images at {Prefix}_(page_number).{Extension}. May include a fully qualified file path. |
| IronSoftware.Drawing.AnyBitmap.ImageFormat | extension | Output file extension in lower-case. |
Returns
| Type | Description |
|---|---|
| System.String[] | Array of saved image file names. Can be multiple if OcrInput used has multiple pages. |
Exceptions
| Type | Condition |
|---|---|
| System.Exception | Throws an exception if there are no pages. See OcrInputPage |
ToGrayScale()
This image filter turns every pixel into a shade of grayscale. Unlikely to improve OCR accuracy but may improve speed.
Declaration
public OcrInputBase ToGrayScale()
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |
WithTitle(String)
Adds a Title to the OcrInput Document. This title will be used when calling SaveAsHocrFile(String) and
Declaration
public OcrInputBase WithTitle(string Title)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | Title | The document title as a string. |
Returns
| Type | Description |
|---|---|
| OcrInputBase | This OcrInput object allowing for LINQ style fluent notation. |