#include "ImageApplyAutoCrop.h" #include "ImageProcess_Public.h" CImageApplyAutoCrop::CImageApplyAutoCrop() : m_isCrop(false) , m_isDesaskew(false) , m_isFillBlank(false) , m_isConvexHull(true) , m_isFillColor(false) , m_threshold(40) , m_noise(2) , m_indent(5) { } CImageApplyAutoCrop::CImageApplyAutoCrop(bool isCrop, bool isDesaskew, bool isFillBlank, const cv::Size& fixedSize, bool isConvex, bool isFillColor, double threshold, int noise, int indent) : m_isCrop(isCrop) , m_isDesaskew(isDesaskew) , m_isFillBlank(isFillBlank) , m_isConvexHull(isConvex) , m_isFillColor(isFillColor) , m_threshold(threshold) , m_noise(noise) , m_indent(indent) , m_fixedSize(fixedSize) { } CImageApplyAutoCrop::~CImageApplyAutoCrop() { } void CImageApplyAutoCrop::apply(cv::Mat& pDib, int side) { (void)side; if (pDib.empty()) return; if (!m_isCrop && !m_isDesaskew && !m_isFillBlank && m_fixedSize.empty()) return; cv::Mat src = pDib; cv::Mat thre; cv::Mat dst; hg::threshold_Mat(src, thre, m_threshold); if (m_noise > 0) { cv::Mat element = getStructuringElement(cv::MORPH_RECT, cv::Size(m_noise, m_noise)); cv::morphologyEx(thre, thre, cv::MORPH_OPEN, element); } std::vector hierarchy; std::vector> contours; hg::findContours(thre, contours, hierarchy, cv::RETR_EXTERNAL); m_maxContour = hg::getMaxContour(contours, hierarchy); if (m_maxContour.size() == 0) { thre.release(); return; } thre.release(); dst.release(); cv::RotatedRect rect = hg::getBoundingRect(m_maxContour); cv::Rect boudingRect = cv::boundingRect(m_maxContour); boudingRect.x -= 1; boudingRect.y -= 1; boudingRect.width += 2; boudingRect.height += 2; if (m_isDesaskew && rect.angle != 0) { cv::Point2f srcTri[4]; cv::Point2f dstTri[3]; rect.points(srcTri); dstTri[0] = cv::Point2f(0, rect.size.height - 1); dstTri[1] = cv::Point2f(0, 0); dstTri[2] = cv::Point2f(rect.size.width - 1, 0); cv::Mat warp_mat; warp_mat = cv::getAffineTransform(srcTri, dstTri); cv::warpAffine(src, dst, warp_mat, rect.size); } else dst = src(boudingRect & cv::Rect(0, 0, src.cols, src.rows)); m_maxContour.clear(); m_maxContour.push_back(cv::Point(-1, dst.rows)); m_maxContour.push_back(cv::Point(-1, -1)); m_maxContour.push_back(cv::Point(dst.cols, -1)); m_maxContour.push_back(cv::Point(dst.cols, dst.rows)); if (m_isFillBlank) { cv::Mat thre_dst; hg::threshold_Mat(dst, thre_dst, m_threshold); if (m_indent > 0) { std::vector rectEdge{ cv::Point(0, 0) ,cv::Point(thre_dst.cols - 1, 0), cv::Point(thre_dst.cols - 1, thre_dst.rows - 1), cv::Point(0, thre_dst.rows - 1) }; std::vector> rectEdges{ rectEdge }; cv::drawContours(thre_dst, rectEdges, 0, cv::Scalar::all(0)); cv::Mat element = cv::getStructuringElement(cv::MorphShapes::MORPH_RECT, cv::Size(m_indent*2, m_indent*2)); cv::erode(thre_dst, thre_dst, element, cv::Point(-1, -1), 1); } hierarchy.clear(); contours.clear(); m_maxContour.clear(); hg::findContours(thre_dst, contours, hierarchy, cv::RETR_EXTERNAL); if (m_isConvexHull) { m_maxContour = hg::getMaxContour(contours, hierarchy); hg::convexHull(m_maxContour, m_maxContour); contours.clear(); contours.push_back(m_maxContour); } contours.push_back(std::vector()); contours[contours.size() - 1].push_back(cv::Point(-1, dst.rows - 1)); contours[contours.size() - 1].push_back(cv::Point(-1, -1)); contours[contours.size() - 1].push_back(cv::Point(dst.cols, -1)); contours[contours.size() - 1].push_back(cv::Point(dst.cols, dst.rows)); hg::fillPolys(dst, contours, m_isFillColor ? getBackGroudColor(pDib, rect.size.area()) : cv::Scalar(255, 255, 255)); } pDib.release(); if (/*(m_isCrop && side == 0) || (side == 1 && m_fixedSize.width * m_fixedSize.height == 0)*/ m_isCrop) pDib = dst.clone(); else { pDib = cv::Mat(m_fixedSize, dst.type(), m_isFillBlank ? cv::Scalar(255, 255, 255) : cv::Scalar(0, 0, 0)); cv::Rect roi; roi.x = dst.cols > pDib.cols ? (dst.cols - pDib.cols) / 2 : 0; roi.width = cv::min(pDib.cols, dst.cols); roi.y = dst.rows > pDib.rows ? (dst.rows - pDib.rows) / 2 : 0; roi.height = cv::min(pDib.rows, dst.rows); cv::Rect rect((pDib.cols - roi.width) / 2, (pDib.rows - roi.height) / 2, roi.width, roi.height); for (cv::Point& p : m_maxContour) p += roi.tl(); dst(roi).copyTo(pDib(rect)); } } void CImageApplyAutoCrop::apply(std::vector& mats, bool isTwoSide) { if (mats.empty()) return; if (!mats[0].empty()) { apply(mats[0], 0); } if (isTwoSide && mats.size() > 1) { cv::Size dSize = m_fixedSize; if (!mats[0].empty()) m_fixedSize = mats[0].size(); if (!mats[1].empty()) { apply(mats[1], 1); } if (!mats[0].empty()) m_fixedSize = dSize; } } cv::Scalar CImageApplyAutoCrop::getBackGroudColor(const cv::Mat& image, int total) { if (image.channels() == 3) { cv::Mat image_bgr[3]; cv::split(image, image_bgr); uchar bgr[3]; for (size_t i = 0; i < 3; i++) bgr[i] = getBackGroudChannelMean(image_bgr[i], total); return cv::Scalar(bgr[0], bgr[1], bgr[2]); } else return cv::Scalar::all(getBackGroudChannelMean(image, total)); } uchar CImageApplyAutoCrop::getBackGroudChannelMean(const cv::Mat& gray, int total) { cv::Mat image_clone; cv::resize(gray, image_clone, cv::Size(), 0.25, 0.25); int threnshold = total / 32; int channels[] = { 0 }; int nHistSize[] = { 256 }; float range[] = { 0, 256 }; const float* fHistRanges[] = { range }; cv::Mat hist; cv::calcHist(&image_clone, 1, channels, cv::Mat(), hist, 1, nHistSize, fHistRanges, true, false); int hist_array[256]; for (int i = 0; i < 256; i++) hist_array[i] = hist.at(i, 0); int length = 1; const int length_max = 255 - m_threshold; while (length < length_max) { for (size_t i = m_threshold + 1; i < 256 - length; i++) { int count = 0; uint pixSum = 0; for (size_t j = 0; j < length; j++) { count += hist_array[j + i]; pixSum += hist_array[j + i] * (i + j); } if (count >= threnshold) return pixSum / count; } length++; } return 255; }