code_device/hgdriver/ImageProcess/ImageApplySizeDetection.cpp

47 lines
1.5 KiB
C++
Raw Permalink Normal View History

2022-07-29 08:41:34 +00:00
#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)
{
}
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)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õķ<C3B5><C4B7><EFBFBD>
{
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;
}