Skip to content

Commit

Permalink
added example code from tesseract-ocr#4070 : GetGradient()
Browse files Browse the repository at this point in the history
  • Loading branch information
GerHobbelt committed Aug 5, 2023
1 parent c1d030a commit 593aa26
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/examples/get-page-gradient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// example as part of https://github.com/tesseract-ocr/tesseract/pull/4070

#include <allheaders.h>
#include <tesseract/baseapi.h>


#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;
}

0 comments on commit 593aa26

Please sign in to comment.