Finding line bounding boxes
Contents
[
Hide
]
Aspose.OCR allows you to automatically find the coordinates of image rectangles containing text lines. This can be useful for highlighting detected areas when previewing an image or extracting individual blocks of text.
To get bounding boxes of all lines of the image, use one of the following methods:
Method | Description |
---|---|
get_rectangles_number() |
Get the total number of lines in the image. |
get_rectangles_number_from_raw_bytes() |
Get the total number of lines in the image provided as an array of pixels. |
get_rectangles_number_from_uri() |
Get the total number of lines in the image provided by URI. This method is not supported in the Linux version of Aspose.OCR for C++. |
get_rectangles() |
Get coordinates of all line in the image. Make sure to use the same parameters as in get_rectangles_number() method. |
get_rectangles_from_raw_bytes() |
Get coordinates of all line in the image provided as an array of pixels. Make sure to use the same parameters as in get_rectangles_number_from_raw_bytes() method. |
get_rectangles_from_uri() |
Get coordinates of all line in the image provided by URI. Make sure to use the same parameters as in get_rectangles_number_from_uri() method.This method is not supported in the Linux version of Aspose.OCR for C++. |
Set type
parameter of the method to areas_type::lines
.
std::string image_path = "source.png";
// calculate the number of lines
size_t res_len = aspose::ocr::get_rectangles_number(image_path.c_str(), areas_type::lines, false);
std::wcout << "Number of lines: " << res_len << std::endl;
// get line bounding boxes
rect* rectangles = new rect[res_len];
res_len = aspose::ocr::get_rectangles(image_path.c_str(), areas_type::lines, false, rectangles, res_len);
std::wcout << "lines: " << std::endl;
for (size_t i = 0; i < res_len; i++)
{
std::wcout << " x: " << rectangles[i].x << " y: " << rectangles[i].y << " width: " << rectangles[i].width << " height: " << rectangles[i].height << std::endl;
}
Coordinates of each line (top-left corner, width and height) are returned as an array of rect
objects.
Important consideration
Line detection works differently depending on the all_image
parameter of the method:
detectAreas | Behavior |
---|---|
true |
The OCR engine tries to break the content into paragraphs and then extracts lines from the found paragraphs. Best suited for multi-column texts - adjacent lines in different columns will be treated as separate lines rather than a single line. |
false |
The OCR engine ignores the columns and combines adjacent lines from different columns into a single line. This can be useful when concatenating text from table rows. |