更新裁切纠偏算法,解决固定幅面裁切纠偏位置异常的问题
This commit is contained in:
parent
cf1f7621e5
commit
35999b8f16
|
@ -96,59 +96,6 @@ void CImageApplyAutoCrop::myWarpAffine(cv::InputArray _src, cv::OutputArray _dst
|
|||
M, interpolation, borderType, borderValue.val);
|
||||
}
|
||||
|
||||
cv::Scalar CImageApplyAutoCrop::getBackGroudColor(const cv::Mat& image)
|
||||
{
|
||||
if (image.channels() == 3)
|
||||
{
|
||||
uchar table[768] = { 0 };
|
||||
int hist_bgr[3][256] = { 0 };
|
||||
int width = image.cols, height = image.rows, bytesPerLine = image.step;
|
||||
memset(table, 255, 768);
|
||||
|
||||
unsigned char* ptr_data = image.data;
|
||||
unsigned char b = 0;
|
||||
for (uint i = 0; i < height; i++, ptr_data += bytesPerLine)
|
||||
for (uint j = 0, x = 0; j < width; j++, x += 3)
|
||||
{
|
||||
b = table[ptr_data[x] + ptr_data[x + 1] + ptr_data[x + 2]];
|
||||
for (uint k = 0; k < 3; k++)
|
||||
hist_bgr[k][ptr_data[x + k] & b]++;
|
||||
}
|
||||
|
||||
int max_vals[3] = { 0 };
|
||||
cv::Scalar max_indexes(0, 0, 0);
|
||||
|
||||
for (uint i = 5; i < 256; i++)
|
||||
for (uint j = 0; j < 3; j++)
|
||||
if (hist_bgr[j][i] > max_vals[j])
|
||||
{
|
||||
max_vals[j] = hist_bgr[j][i];
|
||||
max_indexes[j] = i;
|
||||
}
|
||||
|
||||
return max_indexes;
|
||||
}
|
||||
else
|
||||
{
|
||||
uchar table[256] = { 0 };
|
||||
int hist_bgr[256] = { 0 };
|
||||
int width = image.cols, height = image.rows, bytesPerLine = image.step;
|
||||
memset(table, 255, 256);
|
||||
unsigned char* ptr_data = image.data;
|
||||
unsigned char b = 0;
|
||||
for (uint i = 0; i < height; i++, ptr_data += bytesPerLine)
|
||||
for (uint j = 0, x = 0; j < width; j++, x ++)
|
||||
hist_bgr[ptr_data[x] & table[ptr_data[x]]]++;
|
||||
|
||||
int max_vals = 5;
|
||||
for (uint i = 5; i < 256; i++)
|
||||
if (hist_bgr[i] > hist_bgr[max_vals])
|
||||
max_vals = i;
|
||||
|
||||
return cv::Scalar(max_vals);
|
||||
}
|
||||
}
|
||||
|
||||
void CImageApplyAutoCrop::autoCrop_desaskew_fillBlank(cv::Mat& src, cv::Mat& dst, bool isAutoCrop, bool isDesaskew, bool isFillBlank, int dWidth, int dHeight,
|
||||
bool isConvex, bool isColorBlank, double threshold, int noise, int indent, bool isNormalCrop, double fx, double fy)
|
||||
{
|
||||
|
@ -226,7 +173,9 @@ void CImageApplyAutoCrop::autoCrop_desaskew_fillBlank(cv::Mat& src, cv::Mat& dst
|
|||
boudingRect.width *= FX_FY;
|
||||
boudingRect.height *= FX_FY;
|
||||
|
||||
blankColor = getBackGroudColor(resizeMat(boudingRect));
|
||||
cv::Mat temp_bgc;
|
||||
cv::resize(resizeMat(boudingRect), temp_bgc, cv::Size(200, 200));
|
||||
blankColor = hg::getBackGroundColor(temp_bgc, cv::Mat(), threshold);
|
||||
}
|
||||
else
|
||||
blankColor = cv::Scalar::all(255);
|
||||
|
@ -254,9 +203,9 @@ void CImageApplyAutoCrop::autoCrop_desaskew_fillBlank(cv::Mat& src, cv::Mat& dst
|
|||
int h = rect.size.height;
|
||||
int x = (dst.cols - w) / 2;
|
||||
int y = (dst.rows - h) / 2;
|
||||
dstTri[0] = cv::Point2f(0, h);
|
||||
dstTri[1] = cv::Point2f(0, 0);
|
||||
dstTri[2] = cv::Point2f(w, 0);
|
||||
dstTri[0] = cv::Point2f(x, y + h);
|
||||
dstTri[1] = cv::Point2f(x, y);
|
||||
dstTri[2] = cv::Point2f(x + w, y);
|
||||
|
||||
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));
|
||||
|
|
|
@ -34,7 +34,8 @@
|
|||
2023/05/15 v1.5.5 优化文稿背景色识别算法。
|
||||
2023/05/23 v1.5.6 修复纯黑图找不到外界轮廓会导致崩溃的BUG。
|
||||
2023/11/02 v1.6 优化抗噪能力。采用自适应背景色二值化。
|
||||
* 版本号:v1.5.6
|
||||
2023/12/05 v1.6.1 替换获取背景色方案;修复固定幅面裁切纠偏的坐标计算BUG。
|
||||
* 版本号:v1.6.1
|
||||
|
||||
* ====================================================
|
||||
*/
|
||||
|
@ -115,8 +116,6 @@ public:
|
|||
|
||||
private:
|
||||
static void myWarpAffine(cv::InputArray _src, cv::OutputArray _dst, cv::InputArray _M0, cv::Size dsize, int flags, int borderType, const cv::Scalar& borderValue);
|
||||
|
||||
static cv::Scalar getBackGroudColor(const cv::Mat& image);
|
||||
private:
|
||||
bool m_isCrop;
|
||||
bool m_isDesaskew;
|
||||
|
|
Loading…
Reference in New Issue