Main Content

locateText

Locate text pattern

Description

bboxes = locateText(ocrText,pattern) returns the location and size of bounding boxes stored in the ocrText object. The locateText function returns only the locations of bounding boxes which correspond to text within an image that exactly match the input pattern.

bboxes = locateText(ocrText,pattern,Name=Value) specifies options using one or more name-value arguments in addition to any combination of arguments from previous syntaxes. For example, IgnoreCase=true specifies case-insensitive text locations.

example

Examples

collapse all

Load an image containing text into the workspace.

businessCard = imread("businessCard.png");
ocrResults = ocr(businessCard);
bboxes = locateText(ocrResults,"Math",IgnoreCase=true);
Iocr = insertShape(businessCard,"FilledRectangle",bboxes);
figure
imshow(Iocr)

Figure contains an axes object. The hidden axes object contains an object of type image.

Load an image containing text into the workspace.

     businessCard = imread("businessCard.png");
     ocrResults   = ocr(businessCard);
     bboxes = locateText(ocrResults, "www.*com","UseRegexp", true);
     img    = insertShape(businessCard, "FilledRectangle", bboxes);
     figure
     imshow(img)

Figure contains an axes object. The hidden axes object contains an object of type image.

Input Arguments

collapse all

Recognized text and metrics, returned as an ocrText object. The object contains the recognized text, the location of the recognized text within the input image, and the metrics indicating the confidence of the results. The confidence values, representing the probabilities, range between 0 and 1. When you specify an M-by-4 roi, the function returns ocrText as an M-by-1 array of ocrText objects.

OCR character vector pattern, specified as a single character vector, string scalar, cell array of character vectors, or a string array. The object function returns only the locations of bounding boxes which correspond to text within an image that exactly match the input pattern.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: IgnoreCase=true specifies case-insensitive text locations

Logical to treat pattern as regular expression, specified as a logical scalar. When you set this property to true, the object function treats the pattern as a regular expression. For more information about regular expressions, see regexp.

Case sensitivity, specified as a logical scalar. When you set this property to true, the object function performs case-insensitive text location.

Output Arguments

collapse all

Text bounding boxes, returned as an M-by-4 matrix. Each row of the matrix contains a four-element vector, [x y width height]. The [x y] elements correspond to the upper-left corner of the bounding box. The [width height] elements correspond to the size of the rectangular region in pixels. The bounding boxes enclose text found in an image using the ocr function. The ocr function stores OCR results in the ocrText object.

Version History

Introduced in R2014a