1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> #include <tesseract/baseapi.h> #include <leptonica/allheaders.h> int main() { // Initialize Tesseract API tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); if (api->Init(NULL, "eng")) { // Initialize with the English language std::cerr << "Could not initialize tesseract.\n"; return 1; } // Load an image from file Pix *image = pixRead("image.png"); // Replace with your image file path if (image == NULL) { std::cerr << "Could not open image file.\n"; api->End(); return 1; } // Set the image for OCR processing api->SetImage(image); // Perform OCR char *text = api->GetUTF8Text(); if (text == NULL) { std::cerr << "Could not perform OCR.\n"; pixDestroy(&image); api->End(); return 1; } // Output the extracted text std::cout << "Extracted Text:\n" << text << std::endl; // Clean up delete[] text; pixDestroy(&image); api->End(); return 0; } |
Explanation:
- Include Tesseract and Leptonica Headers:
#include <tesseract/baseapi.h>
: Includes the Tesseract API for OCR.#include <leptonica/allheaders.h>
: Includes Leptonica library headers for image processing.
- Initialize Tesseract API:
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
: Creates an instance of the Tesseract API.api->Init(NULL, "eng");
: Initializes the Tesseract API with the English language. You may specify the path to trained data files if they are not in the default location.
- Load Image:
Pix *image = pixRead("image.png");
: Loads the image from the file. Replace"image.png"
with the path to your image file.- Checks if the image was loaded successfully.
- Set Image for OCR:
api->SetImage(image);
: Sets the loaded image for OCR processing.
- Perform OCR:
char *text = api->GetUTF8Text();
: Extracts text from the image. The result is a UTF-8 encoded string.
- Output Extracted Text:
- Prints the extracted text to the console.
- Clean Up:
delete[] text;
: Frees the memory allocated for the extracted text.pixDestroy(&image);
: Frees the memory used by the image.api->End();
: Cleans up and releases resources used by the Tesseract API.
Prerequisites:
- Tesseract OCR: Ensure Tesseract OCR is installed on your system.
- Leptonica Library: Ensure Leptonica (a library used by Tesseract for image processing) is installed.
Compilation and Execution:
Compile with Tesseract and Leptonica:
1 |
g++ -o ocr_example ocr_example.cpp `pkg-config --cflags --libs tesseract lept` |
Run the Program:
1 |
./ocr_example |