code_device/hgdriver/ImageProcess/ImageApplyAutoCrop.h

113 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* ====================================================
* 功能:自动裁剪、纠偏、除黑底
* 作者:刘丁维
* 生成时间2020/4/21
* 最近修改时间2020/4/21 v1.0
2020/7/22 v1.1 增加获取图像有效区域轮廓的接口maxContour用于配合一体机的“跳过空白页”算法PC端暂时无需使用
2020/10/16 v1.2 修复自动裁剪尺寸精度丢失的BUG提高除黑底缩进精度。
2020/10/28 v1.2.1 修复凹凸多边形填充背景的逻辑BUG。
2020/10/28 v1.2.2 修复图像处理必定会缩小尺寸的BUG。
2020/10/29 v1.2.3 避免无谓的纠偏0°纠偏
2020/11/30 v1.3.0 增加功能,可识别文稿颜色进行填充黑底。
* 版本号v1.3.0
* ====================================================
*/
#ifndef IMAGE_APPLY_AUTO_CROP_H
#define IMAGE_APPLY_AUTO_CROP_H
#include "ImageApply.h"
class CImageApplyAutoCrop : public CImageApply
{
public:
CImageApplyAutoCrop();
/*
* isCrop [in]:自动幅面裁剪使能true自动裁剪false为固定裁剪
* isDesaskew [in]:自动纠偏使能true自动纠偏false为不纠偏
* isFillBlank [in]:黑底填充使能true为填充false为不填充
* fixedSize [in]:固定幅面尺寸当isCrop为false时生效结果尺寸按fixedSize大小输出单位像素
* isConvex [in]:黑底填充时的填充方式,true为凸多边形填充false为凹多边形填充默认true
* isFillColor [in]:黑底填充时采用自适应色彩填充false为白色填充true为自适应文稿底色填充默认false
* threshold [in]:二值化阈值,取值范围(0, 255)默认40
* noise [in]:除噪像素能够消除noise宽度的背景竖条纹干扰默认2
* indent [in]:轮廓缩进裁剪、纠偏或者黑底填充时对探索到的纸张轮廓进行缩进indent像素默认5
* normalCrop [in]:为true且m_isCrop m_isDesaskew m_isFillBlank均为false时生效固定裁切采用最传统的裁切方式默认false
*/
CImageApplyAutoCrop(bool isCrop, bool isDesaskew, bool isFillBlank, const cv::Size& fixedSize, bool isConvex = true,
bool isFillColor = false, double threshold = 40, int noise = 8, int indent = 5, bool normalCrop = false);
virtual ~CImageApplyAutoCrop();
virtual void apply(cv::Mat& pDib, int side);
virtual void apply(std::vector<cv::Mat>& mats, bool isTwoSide);
bool isAutoCrop() { return m_isCrop; }
bool isFillBlank() { return m_isFillBlank; }
bool isDesaskew() { return m_isDesaskew; }
bool isConvexHull() { return m_isConvexHull; }
double threshold() { return m_threshold; }
const cv::RotatedRect& rotatedROI() { return m_rect; }
const std::vector<cv::RotatedRect>& rotatedROIs() { return m_rects; }
int noise() { return m_noise; }
int indent() { return m_indent; }
cv::Size fixedSize() { return m_fixedSize; }
const std::vector<cv::Point>& maxContour() { return m_maxContour; }
void setAutoCrop(bool enabled) { m_isCrop = enabled; }
void setFillBlank(bool enabled) { m_isFillBlank = enabled; }
void setDesaskew(bool enabled) { m_isDesaskew = enabled; }
void setConvexHull(bool convex) { m_isConvexHull = convex; }
void setThreshold(double value) { m_threshold = value; }
void setNoise(int value) { m_noise = value; }
void setIndent(int value) { m_indent = value; }
void setFixedSize(cv::Size size) { m_fixedSize = size; }
private:
cv::Scalar getBackGroudColor(const cv::Mat& image, int total);
uchar getBackGroudChannelMean(const cv::Mat& gray, int total);
private:
bool m_isCrop;
bool m_isDesaskew;
bool m_isFillBlank;
bool m_isConvexHull;
bool m_isFillColor;
double m_threshold;
int m_noise;
int m_indent;
bool m_normalCrop; //为true且m_isCrop m_isDesaskew m_isFillBlank均为false时生效固定裁切采用最传统的裁切方式
cv::Size m_fixedSize;
cv::RotatedRect m_rect;
std::vector<cv::Point> m_maxContour;
std::vector<cv::RotatedRect> m_rects;
};
#endif // !IMAGE_APPLY_AUTO_CROP_H