更新跳过空白页算法v1.10,优化算法效能
This commit is contained in:
parent
b0274c4b31
commit
f3a06285ff
|
@ -33,28 +33,38 @@ void CImageApplyDiscardBlank::apply(std::vector<cv::Mat>& mats, bool isTwoSide)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool maxMinCompare(const cv::Mat& img, const cv::Mat& mask, double devTh, double meanTh)
|
bool maxMinCompare(const cv::Mat& img, const cv::Mat& mask, double devTh, double bgc)
|
||||||
{
|
{
|
||||||
double min, max;
|
double min, max;
|
||||||
cv::minMaxLoc(img, &min, &max, 0, 0, mask);
|
cv::minMaxLoc(img, &min, &max, 0, 0, mask);
|
||||||
if (cv::mean(img, mask)[0] < meanTh)
|
|
||||||
return false;
|
return abs(max - bgc) < devTh && abs(min - bgc) < devTh;
|
||||||
return (max - min) < devTh;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int a = 0;
|
||||||
bool CImageApplyDiscardBlank::apply(const cv::Mat& pDib, double threshold, int edge, double devTh, double meanTh, int dilate)
|
bool CImageApplyDiscardBlank::apply(const cv::Mat& pDib, double threshold, int edge, double devTh, double meanTh, int dilate)
|
||||||
{
|
{
|
||||||
if (pDib.empty())
|
if (pDib.empty())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
double resizeScale = 1.0;
|
double resizeScale = 1.0;
|
||||||
while (pDib.cols * resizeScale > 400)
|
while ((pDib.cols * resizeScale > 500) && (pDib.rows * resizeScale > 500))
|
||||||
resizeScale /= 2;
|
resizeScale /= 2;
|
||||||
|
|
||||||
cv::Mat img_resize;
|
cv::Mat img_resize;
|
||||||
cv::resize(pDib, img_resize, cv::Size(), resizeScale, resizeScale, cv::INTER_LINEAR);
|
cv::resize(pDib, img_resize, cv::Size(), resizeScale, resizeScale, cv::INTER_LINEAR);
|
||||||
cv::blur(img_resize, img_resize, cv::Size(3, 3));
|
//cv::dilate(img_resize, img_resize, cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(dilate, dilate)));
|
||||||
//cv::imwrite("img_resize.jpg", img_resize);
|
cv::blur(img_resize, img_resize, cv::Size(dilate, dilate));
|
||||||
|
|
||||||
|
cv::Scalar bgc = hg::getBackGroundColor(img_resize, cv::Mat(), 20);
|
||||||
|
if (pDib.channels() == 3)
|
||||||
|
if ((bgc[0] + bgc[1] + bgc[2]) < meanTh * 3)
|
||||||
|
return false;
|
||||||
|
if (pDib.channels() == 1)
|
||||||
|
if (bgc[0] < meanTh )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
//cv::imwrite(std::to_string(a) + "r.jpg", img_resize);
|
||||||
cv::Mat threshold_img;
|
cv::Mat threshold_img;
|
||||||
if (img_resize.channels() == 3)
|
if (img_resize.channels() == 3)
|
||||||
{
|
{
|
||||||
|
@ -74,19 +84,20 @@ bool CImageApplyDiscardBlank::apply(const cv::Mat& pDib, double threshold, int e
|
||||||
contour.push_back(p);
|
contour.push_back(p);
|
||||||
|
|
||||||
cv::RotatedRect rect = hg::getBoundingRect(contour);
|
cv::RotatedRect rect = hg::getBoundingRect(contour);
|
||||||
rect.size = cv::Size2f(rect.size.width - edge * resizeScale, rect.size.height - edge * resizeScale);
|
rect.size = cv::Size2f(rect.size.width - edge * resizeScale * 2, rect.size.height - edge * resizeScale * 2);
|
||||||
cv::Point2f box[4];
|
cv::Point2f box[4];
|
||||||
rect.points(box);
|
rect.points(box);
|
||||||
|
|
||||||
contour.clear();
|
contour.clear();
|
||||||
contours.clear();
|
contours.clear();
|
||||||
|
|
||||||
for (size_t i = 0; i < 4; i++)
|
for (size_t i = 0; i < 4; i++)
|
||||||
contour.push_back(box[i]);
|
contour.push_back(box[i]);
|
||||||
contours.push_back(contour);
|
contours.push_back(contour);
|
||||||
|
|
||||||
cv::Mat mask = cv::Mat::zeros(img_resize.size(), CV_8UC1);
|
cv::Mat mask = cv::Mat::zeros(img_resize.size(), CV_8UC1);
|
||||||
hg::fillPolys(mask, contours, cv::Scalar::all(255));
|
hg::fillPolys(mask, contours, cv::Scalar::all(255));
|
||||||
//cv::imwrite("mask.jpg", mask);
|
//cv::imwrite(std::to_string(a) + "m.jpg", mask);
|
||||||
|
a++;
|
||||||
bool b = true;
|
bool b = true;
|
||||||
if (img_resize.channels() == 3)
|
if (img_resize.channels() == 3)
|
||||||
{
|
{
|
||||||
|
@ -94,11 +105,11 @@ bool CImageApplyDiscardBlank::apply(const cv::Mat& pDib, double threshold, int e
|
||||||
cv::split(img_resize, bgr);
|
cv::split(img_resize, bgr);
|
||||||
for (size_t i = 0; i < 3; i++)
|
for (size_t i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
b &= maxMinCompare(bgr[i], mask, devTh, meanTh);
|
b &= maxMinCompare(bgr[i], mask, devTh, bgc[i]);
|
||||||
if (!b) break;
|
if (!b) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
b &= maxMinCompare(img_resize, mask, devTh, meanTh);
|
b &= maxMinCompare(img_resize, mask, devTh, bgc[0]);
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,8 @@
|
||||||
2023/12/01 v1.8 取消JPEG/PNG文件大小判断空白页方案
|
2023/12/01 v1.8 取消JPEG/PNG文件大小判断空白页方案
|
||||||
2023/12/04 v1.9 提高算法效率。
|
2023/12/04 v1.9 提高算法效率。
|
||||||
2023/12/05 v1.9.1 修改错误代码;修改参数注释。
|
2023/12/05 v1.9.1 修改错误代码;修改参数注释。
|
||||||
* 版本号:v1.9.1
|
2023/12/08 v1.10 调整空白页判定条件。修复mask精度bug。
|
||||||
|
* 版本号:v1.10
|
||||||
|
|
||||||
* ====================================================
|
* ====================================================
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue