新增尺寸检测

This commit is contained in:
mo1027728827@qq.com 2022-05-19 14:36:28 +08:00
parent 59691f8704
commit f7c7fc6cf3
2 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,47 @@
#include "ImageApplySizeDetection.h"
#include "ImageProcess_Public.h"
CImageApplySizeDetection::CImageApplySizeDetection(int paperType, int thre_x, int thre_y)
: m_paperType(paperType)
, m_thre_x(thre_x)
, m_thre_y(thre_y)
{
printf("\n paperType =%d \r\n", paperType);
}
CImageApplySizeDetection::~CImageApplySizeDetection()
{
}
#define THRESHOLD 40
#define ELEMNT_K 8
int CImageApplySizeDetection::apply(const cv::Mat& pDib)
{
if (pDib.empty()) return 0;
float width, height;
cv::Mat thre;
hg::threshold_Mat(pDib, thre, THRESHOLD);
cv::Mat element = getStructuringElement(cv::MORPH_RECT, cv::Size(ELEMNT_K, 1));
cv::morphologyEx(thre, thre, cv::MORPH_OPEN, element, cv::Point(-1, -1), 1, cv::BORDER_CONSTANT, cv::Scalar::all(0));
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
hg::findContours(thre, contours, hierarchy, cv::RETR_EXTERNAL);
std::vector<cv::Point> maxContour = hg::getMaxContour(contours, hierarchy);
cv::RotatedRect rect = hg::getBoundingRect(maxContour);
width = rect.size.width;
height = rect.size.height;
printf("\n width =%f ,height = %f ", width, height);
HGSize dstSize;
if (m_supportPaper.count((PaperSize)m_paperType) > 0)//包含设置的幅面
{
dstSize = m_supportPaper[(PaperSize)m_paperType];
if ((width > (dstSize.width + m_thre_x)) ||
(width < (dstSize.width - m_thre_x)) ||
(height > (dstSize.height + m_thre_y)) ||
(height < (dstSize.height - m_thre_y)))
return 1;
}
return 0;
}

View File

@ -0,0 +1,84 @@
/*
* ====================================================
*
*
* 2022/05/11
* 2022/05/11
* v1.0
* ====================================================
*/
#ifndef IMAGE_APPLY_SIZE_DETECTION_H
#define IMAGE_APPLY_SIZE_DETECTION_H
#include "ImageApply.h"
#include <map>
class CImageApplySizeDetection
{
public:
enum PaperSize
{
G400_A3,
G400_A4,
G400_A4R,
G400_A5,
G400_A5R,
G400_A6,
G400_A6R,
G400_B4,
G400_B5,
G400_B5R,
G400_B6R,
G400_B6,
G400_DOUBLELETTER,
G400_LEGAL,
G400_LETTER,
G400_LONGLETTER,
G400_MAXSIZE
};
struct HGSize
{
int width;
int height;
};
public:
CImageApplySizeDetection(int paperType, int thre_x = 70, int thre_y = 80);
~CImageApplySizeDetection();
virtual int apply(const cv::Mat& pDib);
inline void setPaperType(int paperType) { m_paperType = paperType; }
private:
int m_paperType;
int m_thre_x;
int m_thre_y;
std::map<PaperSize, HGSize> m_supportPaper = {
{PaperSize::G400_A3,HGSize{2338,3307}},
{PaperSize::G400_A4,HGSize{1653,2338}},
{PaperSize::G400_A4R,HGSize{2338,1653}},
{PaperSize::G400_A5,HGSize{1165,1653}},
{PaperSize::G400_A5R,HGSize{1653,1165}},
{PaperSize::G400_A6,HGSize{826,1165}},
{PaperSize::G400_A6R,HGSize{1165,826}},
{PaperSize::G400_B4,HGSize{1969,2780}},
{PaperSize::G400_B5,HGSize{1385,1968}},
{PaperSize::G400_B5R,HGSize{1968,1385}},
{PaperSize::G400_B6R,HGSize{1433,1007}},
{PaperSize::G400_B6,HGSize{1007,1433}},
{PaperSize::G400_DOUBLELETTER,HGSize{2200,3400}},
{PaperSize::G400_LEGAL,HGSize{1700,2800}},
{PaperSize::G400_LETTER,HGSize{1700,2198}},
{PaperSize::G400_LONGLETTER,HGSize{2040,2640}},
{PaperSize::G400_MAXSIZE,HGSize{2338,6614}}
};
};
#endif // !IMAGE_APPLY_SIZE_DETECTION_H