9#include <opencv2/opencv.hpp>
13namespace ports = core::ports::document_image_processing;
16cv::Mat applyDenoise(
const cv::Mat& img, ports::DenoiseRequest::Method method) {
17 if (img.empty())
return cv::Mat();
20 case ports::DenoiseRequest::Method::Median:
21 cv::medianBlur(img, out, 3);
23 case ports::DenoiseRequest::Method::Gaussian:
24 cv::GaussianBlur(img, out, cv::Size(3, 3), 0);
26 case ports::DenoiseRequest::Method::Bilateral:
27 cv::bilateralFilter(img, out, 5, 50, 50);
30 return out.empty() ? img : out;
34ports::DenoiseResult
DenoiseAdapter::denoise(
const ports::DenoiseRequest& req, std::shared_ptr<core::ports::diagnostics::IDiagnostics> debugger) {
35 ports::DenoiseResult res;
37 const auto path = std::filesystem::path(req.imagePath);
38 if (!std::filesystem::exists(path))
return res;
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);
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);
57 if (debugger && debugger->enabled()) debugger->writeText(
"opencv/error.txt",
"DenoiseAdapter::denoise failed");
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.