Skip to content

include/cvw/algorithms/threshold.hpp

Global and adaptive thresholding algorithms. More...

Namespaces

Name
cvw

Detailed Description

Global and adaptive thresholding algorithms.

Author: Charliechen114514

Version: 1.0.0

Since: 1.0.0

Date: 2026-05-15

Provides fixed-level and adaptive threshold operations on grayscale images with expected-based error handling.

Source code

cpp

#pragma once
#include "detail.hpp"
#include <opencv2/imgproc.hpp>

namespace cvw {

[[nodiscard]] inline expected<Image<Gray>, AlgorithmError>
threshold(Image<Gray> img, double thresh, double max_val,
          int type = cv::THRESH_BINARY) {
    auto ec = detail::check_non_empty(img);
    if (!ec) {
        return unexpected(ec.error());
    }

    cv::threshold(img.mat(), img.mat(), thresh, max_val, type);
    return img;
}

[[nodiscard]] inline expected<Image<Gray>, AlgorithmError>
adaptive_threshold(Image<Gray> img, int block_size, double C,
                   int method = cv::ADAPTIVE_THRESH_GAUSSIAN_C) {
    auto ec = detail::check_non_empty(img);
    if (!ec) {
        return unexpected(ec.error());
    }
    ec = detail::require_odd(block_size);
    if (!ec) {
        return unexpected(ec.error());
    }
    if (block_size < 3) {
        return unexpected(AlgorithmError::InvalidParameter);
    }

    cv::adaptiveThreshold(img.mat(), img.mat(), 255.0, method,
                          cv::THRESH_BINARY, block_size, C);
    return img;
}

} // namespace cvw

Updated on 2026-05-17 at 13:22:38 +0000

Built with VitePress