FOSSredder 0.5.0
Loading...
Searching...
No Matches
DenoiseAdapter.cpp
Go to the documentation of this file.
1
5
9#include <opencv2/opencv.hpp>
10#include <filesystem>
11
12namespace opencv {
13namespace ports = core::ports::document_image_processing;
14
15namespace {
16cv::Mat applyDenoise(const cv::Mat& img, ports::DenoiseRequest::Method method) {
17 if (img.empty()) return cv::Mat();
18 cv::Mat out;
19 switch (method) {
20 case ports::DenoiseRequest::Method::Median:
21 cv::medianBlur(img, out, 3);
22 break;
23 case ports::DenoiseRequest::Method::Gaussian:
24 cv::GaussianBlur(img, out, cv::Size(3, 3), 0);
25 break;
26 case ports::DenoiseRequest::Method::Bilateral:
27 cv::bilateralFilter(img, out, 5, 50, 50);
28 break;
29 }
30 return out.empty() ? img : out;
31}
32}
33
34ports::DenoiseResult DenoiseAdapter::denoise(const ports::DenoiseRequest& req, std::shared_ptr<core::ports::diagnostics::IDiagnostics> debugger) {
35 ports::DenoiseResult res;
36 try {
37 const auto path = std::filesystem::path(req.imagePath);
38 if (!std::filesystem::exists(path)) return res;
39
40 cv::Mat src = cv::imread(path.string(), cv::IMREAD_COLOR);
41 if (src.empty()) return res;
42 cv::Mat denoised = applyDenoise(src, req.method);
43
44 auto stem = path.stem().string();
45 auto parent = path.parent_path();
46 auto outPath = parent / (stem + "_denoised.png");
47 if (cv::imwrite(outPath.string(), denoised)) {
48 res.denoisedImagePath = outPath;
49 if (debugger && debugger->enabled()) {
50 std::vector<uint8_t> buf;
51 if (cv::imencode(".png", denoised, buf)) {
52 debugger->writeBytes("opencv/" + stem + "/denoised.png", buf);
53 }
54 }
55 }
56 } catch (...) {
57 if (debugger && debugger->enabled()) debugger->writeText("opencv/error.txt", "DenoiseAdapter::denoise failed");
58 }
59 return res;
60}
61
62} // namespace opencv
Declares the OpenCV denoise adapter used by the import workflow.
Declares the diagnostics artifact writing port.
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.
Precompiled header for the document-image-processing infrastructure module.