Smart and powerful OCR tools

Working with multi-page images

How to check if an image consists of several pages?

It is easy to do: Load the image to an IMG object (for example, using the "Img_LoadFile" function), and then call the "Img_GetPageCount" function, which returns the number of pages in the image. If the value returned by the second function is more than 1, it means you have loaded a multi-page image. Only a few image formats support multiple pages, the most popular of which is TIFF.

How to process some or all pages of a multipage image?

The simplest way is to process pages one by one:

int i, PageCnt;
Img_LoadFile(ImgObj, "c:\\SomeImage.tiff");
PageCnt = Img_GetPageCount(ImgObj);
for (i = 0; i < PageCnt; i++)
Img_SetPage(ImgObj, i); //select page
//page OCRed, get its text, analyze results or add page to SVR object.

If you want to process only one page with index N (0 <= N < PageCnt), use this code:

Img_LoadFile(ImgObj, "c:\\SomeImage.tiff");
Img_SetPage(ImgObj, N);

What about the OCR speed?

Processing pages one by one is not the fastest way if the CPU has several cores, because NSOCR cannot use several CPU cores completely when processing one page. However, it will work faster on a multi-core CPU, because NSOCR can parallelize some of the OCR algorithms if the "Main/NumKernels" option is set to 0 or more than 1 (by default, it is 0, which means that all CPU cores can be used when necessary).

To improve performance, process every page on a separate CPU core. Use several threads, each one to process its own page. The "Ocr_ProcessPages" function uses this approach, so the following code works faster:

Img_LoadFile(ImgObj, "c:\\SomeImage.tiff");
Ocr_ProcessPages(ImgObj, SvrObj, 0, -1, 0, FMT_EXACTCOPY); //OCR all pages, create as many threads as the number of CPU cores.

Note that the "Ocr_ProcessPages" function saves recognized pages to the SVR object, which sometimes is useful, but other times is not. If you only need to perform OCR for all pages and save them as a TXT, RTF, or PDF file, this function is very useful. Unfortunately, it does not allow you to perform any advanced processing/analyzing. For example, you cannot count the number of zones in every recognized page, or find out which text is in a block on the page. If you need such things, use the first approach: Call "Img_SetPage" + "Img_OCR" for every page, and analyze results.

Advanced processing and maximum speed

Yes, it is possible, though it will take more than a few lines of code. Actually, you need to implement the "Ocr_ProcessPages" function, that is, to create several threads. Each thread will process its own page. When the recognition of a page is complete, the thread can analyze the results, and then start processing the next page (if available).
The "CSharp ProcessPages" sample project from the NSOCR SDK shows how to do it.

Tips for developers:

  • Use an appropriate method to work with multi-page images. It is not a good idea to create a multi-threading application like "CSharp ProcessPages Sample" if all you need to do is OCR an image and save the results to an output PDF file, or just get text strings. If that is the case, use "Ocr_ProcessPages" instead.
  • If you perform OCR from different threads, create separate OCR and IMG objects for every thread.