145 lines
4.5 KiB
C++
145 lines
4.5 KiB
C++
/*
|
||
* ====================================================
|
||
|
||
* 功能:公共图像处理算法。部分功能可能会在ImageProcess里面多个类反复使用
|
||
* 作者:刘丁维
|
||
* 生成时间:2020/4/21
|
||
* 最近修改时间:2020/4/21
|
||
* 2021/07/12 v1.1 getBoundingRect中,增加考虑纠正初始 angle > 90 的情况。
|
||
* 2021/07/22 v1.2 convexHull中,修复点集为空可能导致崩溃的BUG。
|
||
* 2023/11/02 v1.3 threshold_Mat,当thre<0时,采用自适应背景二值化。
|
||
* 2023/12/01 v1.4 增加getBackGroundColor算法。
|
||
* 2023/12/02 v1.4.1 getBackGroundColor增加threshold阈值。
|
||
* 2023/12/04 v1.4.2 兼容opencv版本接口。
|
||
* 2023/12/05 v1.4.3 getBackGroundColor支持单通道图像背景色识别。
|
||
* 版本号:v1.4.3
|
||
|
||
* ====================================================
|
||
*/
|
||
|
||
#ifndef IMAGE_PROCESS_PUBLIC_H
|
||
#define IMAGE_PROCESS_PUBLIC_H
|
||
|
||
#include "opencv2/opencv.hpp"
|
||
#include <vector>
|
||
|
||
namespace hg
|
||
{
|
||
/*
|
||
* 功能:计算源点集的凸多边形轮廓,输出轮廓点集
|
||
* src: 源点集
|
||
* dst: 目标点集
|
||
* clockwise: true为顺时针排序,false为逆时针排序
|
||
*/
|
||
void convexHull(const std::vector<cv::Point>& src, std::vector<cv::Point>& dst, bool clockwise = false);
|
||
|
||
/*
|
||
* 功能:填充凸多边形,默认颜色为白色
|
||
* image: 填充图像
|
||
* points: 凸多边形轮廓点集(逆时针排序)
|
||
*/
|
||
void fillConvexHull(cv::Mat& image, const std::vector<cv::Point>& points);
|
||
|
||
/*
|
||
* 功能:填充凹多边形
|
||
* image: 填充图像
|
||
* contours: 凹多边形轮廓点集(逆时针排序)
|
||
* color: 填充颜色
|
||
*/
|
||
void fillPolys(cv::Mat& image, const std::vector<std::vector<cv::Point>>& contours, const cv::Scalar& color);
|
||
|
||
/*
|
||
* 功能:获取连通区域轮廓
|
||
* src: 源图像
|
||
* contours: 结果轮廓集
|
||
* hierarchy: 轮廓集的排序关系。与contours的数量对应,受retr选项不同,排序会有变化
|
||
* retr: 轮廓集排序方式,默认为链式排序
|
||
* method: 查找算法选择,默认为普通查找
|
||
* offset: 查找起始点,默认为(0,0)点
|
||
*/
|
||
void findContours(const cv::Mat& src, std::vector<std::vector<cv::Point>>& contours, std::vector<cv::Vec4i>& hierarchy,
|
||
int retr = cv::RETR_LIST, int method = cv::CHAIN_APPROX_SIMPLE, cv::Point offset = cv::Point(0, 0));
|
||
|
||
/// <summary>
|
||
/// 获取图片文稿底色
|
||
/// </summary>
|
||
/// <param name="image">图像,三通道</param>
|
||
/// <param name="mask">掩膜</param>
|
||
/// <param name="threshold">阈值,用于排除黑色背景</param>
|
||
/// <returns>文稿底色</returns>
|
||
cv::Scalar getBackGroundColor(const cv::Mat& image, const cv::Mat& mask = cv::Mat(), int threshold = 20);
|
||
|
||
/*
|
||
* 功能:获取覆盖点集的最小外接矩形
|
||
* contour: 点集
|
||
* 返回值: 旋转矩形
|
||
*/
|
||
cv::RotatedRect getBoundingRect(const std::vector<cv::Point>& contour);
|
||
|
||
/*
|
||
* 功能: 获取覆盖轮廓集的最小外接凸多边形轮廓
|
||
* contours: 轮廓集(每个轮廓由点集组成)
|
||
* hierarchy: 轮廓集中,轮廓之间的关系。数量与contours对应
|
||
* 返回值: 凸多边形轮廓点集
|
||
*/
|
||
std::vector<cv::Point> getMaxContour(const std::vector<std::vector<cv::Point>>& contours, const std::vector<cv::Vec4i>& hierarchy);
|
||
|
||
/*
|
||
* 功能: 获取覆盖轮廓集的最小外接凸多边形轮廓
|
||
* contours: 轮廓集(每个轮廓由点集组成)
|
||
* hierarchy: 轮廓集中,轮廓之间的关系。数量与contours对应
|
||
* 返回值: 凸多边形轮廓点集
|
||
*/
|
||
std::vector<cv::Point> getVertices(const cv::RotatedRect& rect);
|
||
|
||
/*
|
||
* 功能: 轮廓缩进
|
||
* points: 轮廓点集
|
||
* center: 围绕center点缩进
|
||
* indent: 缩进像素
|
||
*/
|
||
void polyIndent(std::vector<cv::Point>& points, const cv::Point& center, int indent);
|
||
|
||
/*
|
||
* 功能: 二值化,能够处理彩色和灰度图像。src为彩色图像时,灰度图取三个通道的最大值
|
||
* src: 源图
|
||
* dst: 目标图
|
||
* thre: 阈值。当thre < 0时,采用背景色作为每列自适应阈值。
|
||
*/
|
||
void threshold_Mat(const cv::Mat& src, cv::Mat& dst, double thre);
|
||
|
||
/*
|
||
* 功能: 彩色转灰度,灰度图取三个通道的最大值
|
||
* src: 源图
|
||
* 返回值: 灰度图
|
||
*/
|
||
cv::Mat transforColor(const cv::Mat& src);
|
||
|
||
/*
|
||
* 功能: 获取点的仿射变换
|
||
* p: 原点
|
||
* warp_mat: 仿射变换系数矩阵
|
||
* 返回值: 变换后的点
|
||
*/
|
||
cv::Point warpPoint(const cv::Point& p, const cv::Mat& warp_mat);
|
||
|
||
/*
|
||
* 功能: 点到点距离
|
||
* p1: 点1
|
||
* p2: 点2
|
||
* 返回值: 点到点距离
|
||
*/
|
||
int distanceP2P(const cv::Point& p1, const cv::Point& p2);
|
||
|
||
/*
|
||
* 功能: 点到直线距离
|
||
* p: 点
|
||
* l1: 直线端点1
|
||
* l2: 直线端点2
|
||
* 返回值: 点到直线距离
|
||
*/
|
||
float distanceP2L(const cv::Point& p, const cv::Point& l1, const cv::Point& l2);
|
||
}
|
||
|
||
#endif // !IMAGE_PROCESS_C_H
|