FOSSredder 0.5.0
Loading...
Searching...
No Matches
OpenCvDocumentImageProcessorAdapter.cpp
Go to the documentation of this file.
1
5
15
16#include <opencv2/opencv.hpp>
17#include <filesystem>
18
20public:
21 OpenCvDocumentImageProcessorAdapter(std::shared_ptr<core::ports::diagnostics::IDiagnostics> dbg = nullptr) : debugger(std::move(dbg)) {}
22
27
29 if (req.cancelFlag && req.cancelFlag->load()) return {};
30 return opencv::MaskAdapter::mask(req, debugger);
31 }
32
35 try {
36 if (req.cancelFlag && req.cancelFlag->load()) return res;
37 cv::Mat img;
38 std::string label;
39 if (!req.imageBytes.empty()) {
40 img = cv::imdecode(req.imageBytes, cv::IMREAD_GRAYSCALE);
41 label = std::string("<bytes>");
42 }
43
44 std::filesystem::path p = req.imagePath;
45 if (img.empty()) {
46 if (!std::filesystem::exists(p)) return res;
47 img = cv::imread(p.string(), cv::IMREAD_GRAYSCALE);
48 label = p.string();
49 }
50 if (img.empty()) return res;
51
53 auto blocks = opencv::DetectAdapter::detectTextBlocks(img, debugger);
54 if (!blocks.empty()) {
55 res.detected = true;
56 res.textBlocks.reserve(blocks.size());
57 for (const auto &b : blocks) {
59 r.x = b.x; r.y = b.y; r.width = b.width; r.height = b.height;
60 res.textBlocks.push_back(r);
61 }
62 }
63 return res;
64 }
65
66 auto tables = opencv::DetectAdapter::detectTables(img, label, debugger);
67 if (!tables.empty()) {
68 res.table = tables[0];
69 res.detected = true;
70 }
71 } catch (...) {
72 if (debugger && debugger->enabled()) debugger->writeText("opencv/error.txt", "OpenCvDocumentImageProcessorAdapter::detect failed");
73 }
74 return res;
75 }
76
79 if (req.cancelFlag && req.cancelFlag->load()) return res;
80 try {
81 cv::Mat img;
82 if (!req.imageBytes.empty()) {
83 img = cv::imdecode(req.imageBytes, cv::IMREAD_COLOR);
84 }
85
86 std::filesystem::path p = req.imagePath;
87 if (img.empty()) {
88 if (!std::filesystem::exists(p)) return res;
89 img = cv::imread(p.string(), cv::IMREAD_COLOR);
90 }
91 if (img.empty()) return res;
92
93 std::vector<core::ports::document_image_processing::Rect> rects; rects.push_back(req.bbox);
94 std::string prefix;
95 if (!req.uniqIdPrefix.empty()) {
96 prefix = req.uniqIdPrefix;
97 if (!req.filePrefix.empty()) prefix += "_" + req.filePrefix;
98 } else {
99 prefix = req.filePrefix.empty() ? std::string("opencv_crop") : req.filePrefix;
100 }
101
102 res.croppedImageBytes.clear();
103 res.croppedImagePaths = opencv::CropAdapter::cropImages(img, rects, req.outputDir, req.outputFormat, req.jpegQuality, &res.croppedImageBytes, debugger, prefix);
104 } catch (...) {
105 if (debugger && debugger->enabled()) debugger->writeText("opencv/error.txt", "OpenCvDocumentImageProcessorAdapter::crop failed");
106 }
107 return res;
108 }
109
110private:
111 std::shared_ptr<core::ports::diagnostics::IDiagnostics> debugger;
112};
113
114std::shared_ptr<core::ports::document_image_processing::IDocumentImageProcessor> createDocumentImageProcessorAdapter(std::shared_ptr<core::ports::diagnostics::IDiagnostics> debugger) {
115 return std::make_shared<OpenCvDocumentImageProcessorAdapter>(std::move(debugger));
116}
117
Declares the OpenCV crop adapter used by the import workflow.
Declares the OpenCV denoise adapter used by the import workflow.
Declares the OpenCV detection adapter used by the import workflow.
Request types for document image processing operations.
Result types for document image processing operations.
Declares the diagnostics artifact writing port.
Document image processing port for import workflows.
Declares the OpenCV masking adapter used by the import workflow.
std::shared_ptr< core::ports::document_image_processing::IDocumentImageProcessor > createDocumentImageProcessorAdapter(std::shared_ptr< core::ports::diagnostics::IDiagnostics > debugger)
core::ports::document_image_processing::DenoiseResult denoise(const core::ports::document_image_processing::DenoiseRequest &req) const override
Removes noise from an image.
OpenCvDocumentImageProcessorAdapter(std::shared_ptr< core::ports::diagnostics::IDiagnostics > dbg=nullptr)
core::ports::document_image_processing::DetectResult detect(const core::ports::document_image_processing::DetectRequest &req) const override
Detects table or text regions in an image.
core::ports::document_image_processing::CropResult crop(const core::ports::document_image_processing::CropRequest &req) const override
Crops an image into one or more regions.
core::ports::document_image_processing::MaskResult mask(const core::ports::document_image_processing::MaskRequest &req) const override
Applies a mask to an image.
Processes document images for the import pipeline.
static std::vector< std::filesystem::path > cropImages(const std::string &imagePath, const std::vector< ports::Rect > &rects, const std::filesystem::path &outputDir, ports::CropRequest::OutputFormat fmt, int jpegQuality=92, std::shared_ptr< core::ports::diagnostics::IDiagnostics > debugger=nullptr, const std::string &filePrefix="")
Crops multiple rectangles from an image file and writes the results to disk.
static ports::DenoiseResult denoise(const ports::DenoiseRequest &req, std::shared_ptr< core::ports::diagnostics::IDiagnostics > debugger=nullptr)
Applies the configured denoise operation to the input image.
static std::vector< cv::Rect > detectTextBlocks(const cv::Mat &img, std::shared_ptr< core::ports::diagnostics::IDiagnostics > debugger=nullptr)
Detects text blocks in a source image.
static std::vector< ports::Table > detectTables(const cv::Mat &img, const std::string &imagePath, std::shared_ptr< core::ports::diagnostics::IDiagnostics > debugger=nullptr)
Detects table structures in a source image.
static ports::MaskResult mask(const ports::MaskRequest &req, std::shared_ptr< core::ports::diagnostics::IDiagnostics > debugger=nullptr)
Builds a whiteout mask for the requested image region.
Precompiled header for the document-image-processing infrastructure module.
enum core::ports::document_image_processing::CropRequest::OutputFormat outputFormat
enum core::ports::document_image_processing::DetectRequest::DetectKind kind