10#ifndef COMMON_IMAGE_PREPROCESS_H
11#define COMMON_IMAGE_PREPROCESS_H
14#include <opencv2/core.hpp>
15#include <opencv2/opencv.hpp>
19#include <opencv2/imgproc/types_c.h>
72 std::vector<std::shared_ptr<ImagePreprocessor>> actions;
185 for (
auto&
action : actions)
203 actions.emplace_back(std::shared_ptr<ImagePreprocessor>(
self_pointer));
230 static const struct CommonThreshold {
233 static const struct AdaptiveThreshold {
249 explicit Binarization(
const CommonThreshold&
_,
double thresh = 0,
double maxval = 255,
int type = cv::THRESH_BINARY_INV | cv::THRESH_OTSU,
bool reverse =
false) : is_common(
true), thresh_or_c(
thresh), maxval(maxval), type(type), adaptive_method(), block_size(), reverse(reverse) {
268 explicit Binarization(
const AdaptiveThreshold&
_,
double maxval = 255,
int adaptive_method = cv::ADAPTIVE_THRESH_GAUSSIAN_C,
int block_size = 61,
double c = -7,
int threshold_type = cv::THRESH_BINARY,
bool reverse =
true) : is_common(
false), thresh_or_c(c), maxval(maxval), type(
threshold_type), adaptive_method(adaptive_method), block_size(block_size), reverse(reverse) {
293 if (
image.channels() == 3) {
300 cv::adaptiveThreshold(~
new_image,
new_image, maxval, adaptive_method, type, block_size, thresh_or_c);
305 cv::adaptiveThreshold(
new_image,
new_image, maxval, adaptive_method, type, block_size, thresh_or_c);
337 enum EdgeDetectionType {
346 } edge_detection_type;
364 static const struct SobelEdgeDection {
367 static const struct ScharrEdgeDection {
370 static const struct LaplacianEdgeDection {
373 static const struct CannyEdgeDection {
376 static const struct RobertsEdgeDection {
379 static const struct PrewittEdgeDection {
382 static const struct DoGEdgeDection {
401 explicit EdgeDetection(
const SobelEdgeDection&
_,
int ddepth = -1,
int dx = 1,
int dy = 1,
int ksize = 3,
double scale = 1,
double delta = 0,
int type = cv::BORDER_DEFAULT) : edge_detection_type(EDT_SOBEL), ddepth(ddepth), dx(dx), dy(dy), ksize(ksize), scale(scale), delta(delta), type(type), threshold1(), threshold2(), aperture_size(), l2_gradient(), kernel(), anchor() {
420 explicit EdgeDetection(
const ScharrEdgeDection&
_,
int ddepth = -1,
int dx = 0,
int dy = 1,
double scale = 1,
double delta = 0,
int type = cv::BORDER_DEFAULT) : edge_detection_type(EDT_SCHARR), ddepth(ddepth), dx(dx), dy(dy), ksize(), scale(scale), delta(delta), type(type), threshold1(), threshold2(), aperture_size(), l2_gradient(), kernel(), anchor() {
438 explicit EdgeDetection(
const LaplacianEdgeDection&
_,
int ddepth = -1,
int ksize = 1,
double scale = 1,
double delta = 0,
int type = cv::BORDER_DEFAULT) : edge_detection_type(EDT_LAPLACIAN), ddepth(ddepth), dx(), dy(), ksize(ksize), scale(scale), delta(delta), type(type), threshold1(), threshold2(), aperture_size(), l2_gradient(), kernel(), anchor() {
455 explicit EdgeDetection(
const CannyEdgeDection&
_,
double threshold1 = 100,
double threshold2 = 200,
int aperture_size = 3,
bool l2_gradient =
false) : edge_detection_type(EDT_CANNY), ddepth(), dx(), dy(), ksize(), scale(), delta(), type(), threshold1(threshold1), threshold2(threshold2), aperture_size(aperture_size), l2_gradient(l2_gradient), kernel(), anchor() {
475 explicit EdgeDetection(
const RobertsEdgeDection&
_,
int ddepth = -1, cv::Mat kernel = (cv::Mat_<float>(2, 2) << 1, 0, 0, -1), cv::Point anchor = cv::Point(-1, -1),
double delta = 0,
int type = cv::BORDER_DEFAULT) : edge_detection_type(EDT_ROBERTS), ddepth(ddepth), dx(), dy(), ksize(), scale(), delta(delta), type(type), threshold1(), threshold2(), aperture_size(), l2_gradient(), kernel(kernel), anchor(anchor) {
496 explicit EdgeDetection(
const PrewittEdgeDection&
_,
int ddepth = -1, cv::Mat kernel = (cv::Mat_<float>(3, 3) << -1, 0, 1, -1, 0, 1, -1, 0, 1), cv::Point anchor = cv::Point(-1, -1),
double delta = 0,
int type = cv::BORDER_DEFAULT) : edge_detection_type(EDT_PREWITT), ddepth(ddepth), dx(), dy(), ksize(), scale(), delta(delta), type(type), threshold1(), threshold2(), aperture_size(), l2_gradient(), kernel(kernel), anchor(anchor) {
519 explicit EdgeDetection(
const DoGEdgeDection&
_,
int ddepth = -1, cv::Mat kernel = (cv::Mat_<float>(5, 5) << 0, 0, 1, 0, 0, 0, 1, 2, 1, 0, 1, 2, -16, 2, 1, 0, 1, 2, 1, 0, 0, 0, 1, 0, 0), cv::Point anchor = cv::Point(-1, -1),
double delta = 0,
int type = cv::BORDER_DEFAULT) : edge_detection_type(EDT_DOG), ddepth(ddepth), dx(), dy(), ksize(), scale(), delta(delta), type(type), threshold1(), threshold2(), aperture_size(), l2_gradient(), kernel(kernel), anchor(anchor) {
544 switch (edge_detection_type) {
546 cv::Sobel(
image,
new_image, ddepth, dx, dy, ksize, scale, delta, type);
552 cv::Laplacian(
image,
new_image, ddepth, ksize, scale, delta, type);
555 cv::Canny(
image,
new_image, threshold1, threshold2, aperture_size, l2_gradient);
557 case EDT_ROBERTS | EDT_PREWITT | EDT_DOG:
558 cv::filter2D(
image,
new_image, ddepth, kernel, anchor, delta, type);
611 enum LinesDetectType {
622 static const struct SolidLinesDetection {
625 static const struct DashedLinesDetection {
628 static const struct DottedLinesDetection {
642 explicit LinesDetection(
const SolidLinesDetection&
_,
LineType line_type,
const Percent& ksize_or_scale = 1.0 / 75) : lines_detect_type(LDT_SOLID), is_hline((line_type ==
HLINE || line_type ==
VLINE) ? line_type ==
HLINE : (
throw Exception(
"The \"line type\" only supports \"cm::HLINE\" or \"cm::VLINE\" types!"))), ksize_or_scale(ksize_or_scale), ksize_or_scale2() {
657 explicit LinesDetection(
const DashedLinesDetection&
_,
LineType line_type,
const Percent& ksize_or_scale = 1.0 / 200) : lines_detect_type(LDT_DASHED), is_hline((line_type ==
HLINE || line_type ==
VLINE) ? line_type ==
HLINE : (
throw Exception(
"The \"line type\" only supports \"cm::HLINE\" or \"cm::VLINE\" types!"))), ksize_or_scale(ksize_or_scale), ksize_or_scale2() {
694 Cm_Assert(
image.channels() == 1,
"the image is a single channel image!!!");
704 switch (lines_detect_type) {
730 auto kernel = cv::getStructuringElement(cv::MORPH_RECT,
erode_ksize);
737 auto kernel = cv::getStructuringElement(cv::MORPH_RECT, ksize);
742 kernel = cv::getStructuringElement(cv::MORPH_RECT, ksize);
ImagePreprocessor operator|(const ImagePreprocessor &img_preprocessor) const
图像预处理器类重载 | 运算符
ImagePreprocessor(const ImagePreprocessor &img_preprocessor)=default
图像预处理器类的拷贝构造函数
ImagePreprocessor & AddAction(const ImagePreprocessor &img_preprocessor)
增加图像预处理动作
virtual ~ImagePreprocessor()=default
图像预处理器类的析构函数
cv::Mat operator()(const cv::Mat &image) const
图像预处理器类重载括号运算符
virtual cv::Mat Preprocess(const cv::Mat &image) const
预处理图像
ImagePreprocessor()=default
图像预处理器类的默认构造函数
ImagePreprocessor & operator|=(const ImagePreprocessor &img_preprocessor)
图像预处理器类重载 |= 运算符
Binarization(const CommonThreshold &_, double thresh=0, double maxval=255, int type=cv::THRESH_BINARY_INV|cv::THRESH_OTSU, bool reverse=false)
二值化图像预处理动作的构造函数
static const struct cm::imp::Binarization::AdaptiveThreshold ADAPTIVE
自适应二值化
Binarization(const Binarization &binarization)=default
二值化图像预处理动作类 拷贝构造函数
static const struct cm::imp::Binarization::CommonThreshold COMMON
通用二值化
~Binarization()=default
二值化图像预处理动作类 析构函数
Binarization(const AdaptiveThreshold &_, double maxval=255, int adaptive_method=cv::ADAPTIVE_THRESH_GAUSSIAN_C, int block_size=61, double c=-7, int threshold_type=cv::THRESH_BINARY, bool reverse=true)
二值化图像预处理动作的构造函数
cv::Mat Preprocess(const cv::Mat &image) const override
预处理图像
EdgeDetection(const DoGEdgeDection &_, int ddepth=-1, cv::Mat kernel=(cv::Mat_< float >(5, 5)<< 0, 0, 1, 0, 0, 0, 1, 2, 1, 0, 1, 2, -16, 2, 1, 0, 1, 2, 1, 0, 0, 0, 1, 0, 0), cv::Point anchor=cv::Point(-1, -1), double delta=0, int type=cv::BORDER_DEFAULT)
边缘检测动作的构造函数
static const struct cm::imp::EdgeDetection::DoGEdgeDection DOG
DoG 算子
static const struct cm::imp::EdgeDetection::SobelEdgeDection SOBEL
Sobel 算子
EdgeDetection(const ScharrEdgeDection &_, int ddepth=-1, int dx=0, int dy=1, double scale=1, double delta=0, int type=cv::BORDER_DEFAULT)
边缘检测动作的构造函数
EdgeDetection(const EdgeDetection &edge_detection)=default
边缘检测类默认构造函数
EdgeDetection(const SobelEdgeDection &_, int ddepth=-1, int dx=1, int dy=1, int ksize=3, double scale=1, double delta=0, int type=cv::BORDER_DEFAULT)
边缘检测动作的构造函数
static const struct cm::imp::EdgeDetection::RobertsEdgeDection ROBERTS
Roberts 算子
~EdgeDetection()=default
边缘检测类析构函数
static const struct cm::imp::EdgeDetection::CannyEdgeDection CANNY
Canny 算子
static const struct cm::imp::EdgeDetection::ScharrEdgeDection SCHARR
Scharr 算子
cv::Mat Preprocess(const cv::Mat &image) const override
预处理图像
EdgeDetection(const PrewittEdgeDection &_, int ddepth=-1, cv::Mat kernel=(cv::Mat_< float >(3, 3)<< -1, 0, 1, -1, 0, 1, -1, 0, 1), cv::Point anchor=cv::Point(-1, -1), double delta=0, int type=cv::BORDER_DEFAULT)
边缘检测动作的构造函数
static const struct cm::imp::EdgeDetection::PrewittEdgeDection PREWITT
Prewitt 算子
EdgeDetection(const LaplacianEdgeDection &_, int ddepth=-1, int ksize=1, double scale=1, double delta=0, int type=cv::BORDER_DEFAULT)
边缘检测动作的构造函数
static const struct cm::imp::EdgeDetection::LaplacianEdgeDection LAPLACIAN
Laplacian 算子
EdgeDetection(const CannyEdgeDection &_, double threshold1=100, double threshold2=200, int aperture_size=3, bool l2_gradient=false)
边缘检测动作的构造函数
EdgeDetection(const RobertsEdgeDection &_, int ddepth=-1, cv::Mat kernel=(cv::Mat_< float >(2, 2)<< 1, 0, 0, -1), cv::Point anchor=cv::Point(-1, -1), double delta=0, int type=cv::BORDER_DEFAULT)
边缘检测动作的构造函数
static const struct cm::imp::LinesDetection::DottedLinesDetection DOTTED
点线
LinesDetection(const LinesDetection &lines_detection)=default
线检测类默认构造函数
LinesDetection(const SolidLinesDetection &_, LineType line_type, const Percent &ksize_or_scale=1.0/75)
线检测动作的构造函数
static const struct cm::imp::LinesDetection::DashedLinesDetection DASHED
虚线
LinesDetection(const DashedLinesDetection &_, LineType line_type, const Percent &ksize_or_scale=1.0/200)
线检测动作的构造函数
~LinesDetection()=default
线检测析构函数
cv::Mat Preprocess(const cv::Mat &image) const override
预处理图像
LinesDetection(const DottedLinesDetection &_, LineType line_type, const Percent &dilate_ksize_or_scale=5, const Percent &open_ksize_or_scale=1.0/30)
线检测动作的构造函数
static const struct cm::imp::LinesDetection::SolidLinesDetection SOLID
实线
#define Cm_Assert(expr, message)
断言宏
#define Cm_WEAK_ATTRIBUTE
弱符号定义宏
const EdgeDetection ED_PREWITT(EdgeDetection::PREWITT)
Prewitt 算子默认参数
const LinesDetection LD_DASHED_V(LinesDetection::DASHED, VLINE)
默认参数虚竖线(表示模糊不清的实线)检测(仅支持长线的霍夫直线检测)
const EdgeDetection ED_SOBEL(EdgeDetection::SOBEL)
Sobel 算子默认参数
const Binarization THRESHOLD_ADAPTIVE(Binarization::ADAPTIVE)
自适应二值化默认参数
const EdgeDetection ED_LAPLACIAN(EdgeDetection::LAPLACIAN)
Laplacian 算子默认参数
const LinesDetection LD_DASHED_H(LinesDetection::DASHED, HLINE)
默认参数虚横线(表示模糊不清的实线)检测(仅支持长线的霍夫直线检测)
const LinesDetection LD_SOLID_H(LinesDetection::SOLID, HLINE)
默认参数实横线检测(支持任意长度线的霍夫直线检测)
const LinesDetection LD_SOLID_V(LinesDetection::SOLID, VLINE)
默认参数实竖线检测(支持任意长度线的霍夫直线检测)
const LinesDetection LD_DOTTED_V(LinesDetection::DOTTED, VLINE)
默认参数点竖线(由小线段或点构成的线)检测(仅支持长线的霍夫直线检测)
const Binarization THRESHOLD_COMMON(Binarization::COMMON)
通用二值化默认参数
const EdgeDetection ED_ROBERTS(EdgeDetection::ROBERTS)
Roberts 算子默认参数
const EdgeDetection ED_CANNY(EdgeDetection::CANNY)
Canny 算子默认参数
const EdgeDetection ED_SCHARR(EdgeDetection::SCHARR)
Scharr 算子默认参数
const EdgeDetection ED_DOG(EdgeDetection::DOG)
DoG 算子默认参数
const LinesDetection LD_DOTTED_H(LinesDetection::DOTTED, HLINE)
默认参数点横线(由小线段或点构成的线)检测(仅支持长线的霍夫直线检测)
@ HLINE
横线 (horizontal line)
@ VLINE
竖线 (vertical line)