48 lines
1.6 KiB
C++
48 lines
1.6 KiB
C++
|
#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;
|
|||
|
}
|