From 593aa26451aff1e05e53f1449193dc0d226ee005 Mon Sep 17 00:00:00 2001 From: Ger Hobbelt Date: Sat, 5 Aug 2023 16:20:40 +0200 Subject: [PATCH] added example code from https://github.com/tesseract-ocr/tesseract/pull/4070 : GetGradient() --- src/examples/get-page-gradient.cpp | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/examples/get-page-gradient.cpp diff --git a/src/examples/get-page-gradient.cpp b/src/examples/get-page-gradient.cpp new file mode 100644 index 0000000000..d5a4c23deb --- /dev/null +++ b/src/examples/get-page-gradient.cpp @@ -0,0 +1,58 @@ +// example as part of https://github.com/tesseract-ocr/tesseract/pull/4070 + +#include +#include + + +#if defined(BUILD_MONOLITHIC) +# define main tesseract_get_page_gradient_main + +# ifdef __cplusplus +extern "C" { +# endif + +int main(int argc, const char **argv); + +# ifdef __cplusplus +} +# endif + +#endif + +int main(int argc, const char **argv) { + tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); + // Initialize tesseract-ocr with English, without specifying tessdata path + if (api->InitSimple(NULL, "eng")) { + fprintf(stderr, "Could not initialize tesseract.\n"); + return 1; + } + + // Open input image with leptonica library + std::string filepath; + if (argc > 1 && argv[1] != nullptr) { + filepath = argv[1]; + } + if (filepath.empty()) { + filepath = "rotate_image.png"; + } + Pix *image = pixRead(filepath.c_str()); + if (!image) { + fprintf(stderr, "Could not open image file: %s\n", filepath.c_str()); + return 1; + } + + api->SetImage(image); + + // Find lines, get average gradient + api->AnalyseLayout(); + float gradient = api->GetGradient(); + + printf("Average Gradient: %f\n", gradient); + + // Destroy used object and release memory + api->End(); + delete api; + pixDestroy(&image); + + return 0; +}