更新裁切纠偏算法,解决纸张尺寸比设置的尺寸小时,裁切异常的问题;BUG-815
This commit is contained in:
parent
4f78f416b5
commit
800fca0a0e
|
@ -193,22 +193,28 @@ void CImageApplyAutoCrop::autoCrop_desaskew_fillBlank(cv::Mat& src, cv::Mat& dst
|
||||||
cv::Mat dstROI;
|
cv::Mat dstROI;
|
||||||
if (isDesaskew && rect.angle != 0)
|
if (isDesaskew && rect.angle != 0)
|
||||||
{
|
{
|
||||||
|
cv::RotatedRect rect_temp = rect;
|
||||||
|
if (rect_temp.size.width > dst.cols)
|
||||||
|
rect_temp.size.width = dst.cols;
|
||||||
|
if (rect_temp.size.height > dst.rows)
|
||||||
|
rect_temp.size.height = dst.rows;
|
||||||
|
|
||||||
cv::Point2f srcTri[4], dstTri[3];
|
cv::Point2f srcTri[4], dstTri[3];
|
||||||
rect.points(srcTri);
|
rect_temp.points(srcTri);
|
||||||
srcTri[0].x -= 1;
|
srcTri[0].x -= 1;
|
||||||
srcTri[1].x -= 1;
|
srcTri[1].x -= 1;
|
||||||
srcTri[2].x -= 1;
|
srcTri[2].x -= 1;
|
||||||
|
|
||||||
int w = rect.size.width;
|
int w = rect_temp.size.width;
|
||||||
int h = rect.size.height;
|
int h = rect_temp.size.height;
|
||||||
int x = (dst.cols - w) / 2;
|
int x = (dst.cols - w) / 2;
|
||||||
int y = (dst.rows - h) / 2;
|
int y = (dst.rows - h) / 2;
|
||||||
dstTri[0] = cv::Point2f(x, y + h);
|
dstTri[0] = cv::Point2f(0, h);
|
||||||
dstTri[1] = cv::Point2f(x, y);
|
dstTri[1] = cv::Point2f(0, 0);
|
||||||
dstTri[2] = cv::Point2f(x + w, y);
|
dstTri[2] = cv::Point2f(w, 0);
|
||||||
|
|
||||||
dstROI = dst(cv::Rect(x, y, w, h) & cv::Rect(0, 0, dst.cols, dst.rows));
|
dstROI = dst(cv::Rect(x, y, w, h) & cv::Rect(0, 0, dst.cols, dst.rows));
|
||||||
myWarpAffine(src, dstROI, cv::getAffineTransform(srcTri, dstTri), dstROI.size(), cv::INTER_LINEAR, cv::BORDER_CONSTANT, cv::Scalar::all(0));
|
myWarpAffine(src, dstROI, cv::getAffineTransform(srcTri, dstTri), dstROI.size(), cv::INTER_LINEAR, cv::BORDER_CONSTANT, blankColor);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -35,7 +35,8 @@
|
||||||
2023/05/23 v1.5.6 修复纯黑图找不到外界轮廓会导致崩溃的BUG。
|
2023/05/23 v1.5.6 修复纯黑图找不到外界轮廓会导致崩溃的BUG。
|
||||||
2023/11/02 v1.6 优化抗噪能力。采用自适应背景色二值化。
|
2023/11/02 v1.6 优化抗噪能力。采用自适应背景色二值化。
|
||||||
2023/12/05 v1.6.1 替换获取背景色方案;修复固定幅面裁切纠偏的坐标计算BUG。
|
2023/12/05 v1.6.1 替换获取背景色方案;修复固定幅面裁切纠偏的坐标计算BUG。
|
||||||
* 版本号:v1.6.1
|
2023/12/06 v1.6.2 修复固定幅面裁切纠偏的坐标计算BUG。
|
||||||
|
* 版本号:v1.6.2
|
||||||
|
|
||||||
* ====================================================
|
* ====================================================
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -207,31 +207,45 @@ namespace hg
|
||||||
|
|
||||||
cv::Scalar getBackGroundColor(const cv::Mat& image, const cv::Mat& mask, int threshold)
|
cv::Scalar getBackGroundColor(const cv::Mat& image, const cv::Mat& mask, int threshold)
|
||||||
{
|
{
|
||||||
cv::Scalar bgc;
|
|
||||||
cv::Mat mv[3];
|
|
||||||
cv::split(image, mv);
|
|
||||||
float range[] = { 0, 256 };
|
float range[] = { 0, 256 };
|
||||||
const float* ranges = { range };
|
const float* ranges = { range };
|
||||||
|
|
||||||
int histSize = 256;
|
int histSize = 256;
|
||||||
|
cv::Scalar bgc;
|
||||||
cv::Mat hist[3];
|
if (image.channels() == 3)
|
||||||
double min, max;
|
|
||||||
cv::Point maxLoc;
|
|
||||||
for (int i = 0; i < 3; i++)
|
|
||||||
{
|
{
|
||||||
cv::calcHist(&mv[i], 1, 0, mask, hist[i], 1, &histSize, &ranges);
|
cv::Mat mv[3];
|
||||||
|
cv::split(image, mv);
|
||||||
|
|
||||||
|
cv::Mat hist[3];
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
cv::calcHist(&mv[i], 1, 0, mask, hist[i], 1, &histSize, &ranges);
|
||||||
|
|
||||||
|
int index_max = 0;
|
||||||
|
int max_value = 0;
|
||||||
|
for (size_t j = threshold; j < 256; j++)
|
||||||
|
if (hist[i].at<float>(j) > max_value)
|
||||||
|
{
|
||||||
|
index_max = j;
|
||||||
|
max_value = hist[i].at<float>(j);
|
||||||
|
}
|
||||||
|
|
||||||
|
bgc[i] = index_max;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cv::Mat hist;
|
||||||
|
cv::calcHist(&image, 1, 0, mask, hist, 1, &histSize, &ranges);
|
||||||
int index_max = 0;
|
int index_max = 0;
|
||||||
int max_value = 0;
|
int max_value = 0;
|
||||||
for (size_t j = threshold; j < 256; j++)
|
for (size_t j = threshold; j < 256; j++)
|
||||||
if (hist[i].at<float>(j) > max_value)
|
if (hist.at<float>(j) > max_value)
|
||||||
{
|
{
|
||||||
index_max = j;
|
index_max = j;
|
||||||
max_value = hist[i].at<float>(j);
|
max_value = hist.at<float>(j);
|
||||||
}
|
}
|
||||||
|
bgc = cv::Scalar::all(index_max);
|
||||||
bgc[i] = index_max;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return bgc;
|
return bgc;
|
||||||
|
|
|
@ -11,7 +11,8 @@
|
||||||
* 2023/12/01 v1.4 增加getBackGroundColor算法。
|
* 2023/12/01 v1.4 增加getBackGroundColor算法。
|
||||||
* 2023/12/02 v1.4.1 getBackGroundColor增加threshold阈值。
|
* 2023/12/02 v1.4.1 getBackGroundColor增加threshold阈值。
|
||||||
* 2023/12/04 v1.4.2 兼容opencv版本接口。
|
* 2023/12/04 v1.4.2 兼容opencv版本接口。
|
||||||
* 版本号:v1.4.2
|
* 2023/12/05 v1.4.3 getBackGroundColor支持单通道图像背景色识别。
|
||||||
|
* 版本号:v1.4.3
|
||||||
|
|
||||||
* ====================================================
|
* ====================================================
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue