更新裁切纠偏算法,解决固定幅面裁切纠偏位置异常的问题

This commit is contained in:
yangjiaxuan 2023-12-05 15:51:43 +08:00
parent cf1f7621e5
commit 35999b8f16
2 changed files with 8 additions and 60 deletions

View File

@ -96,59 +96,6 @@ void CImageApplyAutoCrop::myWarpAffine(cv::InputArray _src, cv::OutputArray _dst
M, interpolation, borderType, borderValue.val); 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, 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) 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.width *= FX_FY;
boudingRect.height *= 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 else
blankColor = cv::Scalar::all(255); 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 h = rect.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(0, h); dstTri[0] = cv::Point2f(x, y + h);
dstTri[1] = cv::Point2f(0, 0); dstTri[1] = cv::Point2f(x, y);
dstTri[2] = cv::Point2f(w, 0); dstTri[2] = cv::Point2f(x + w, y);
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, cv::Scalar::all(0));

View File

@ -34,7 +34,8 @@
2023/05/15 v1.5.5 稿 2023/05/15 v1.5.5 稿
2023/05/23 v1.5.6 BUG 2023/05/23 v1.5.6 BUG
2023/11/02 v1.6 2023/11/02 v1.6
* v1.5.6 2023/12/05 v1.6.1 BUG
* v1.6.1
* ==================================================== * ====================================================
*/ */
@ -115,8 +116,6 @@ public:
private: 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 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: private:
bool m_isCrop; bool m_isCrop;
bool m_isDesaskew; bool m_isDesaskew;