diff --git a/hgdriver/ImageProcess/ImageApplyAutoCrop.cpp b/hgdriver/ImageProcess/ImageApplyAutoCrop.cpp index 20976a7..a453a90 100644 --- a/hgdriver/ImageProcess/ImageApplyAutoCrop.cpp +++ b/hgdriver/ImageProcess/ImageApplyAutoCrop.cpp @@ -193,22 +193,28 @@ void CImageApplyAutoCrop::autoCrop_desaskew_fillBlank(cv::Mat& src, cv::Mat& dst cv::Mat dstROI; 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]; - rect.points(srcTri); + rect_temp.points(srcTri); srcTri[0].x -= 1; srcTri[1].x -= 1; srcTri[2].x -= 1; - int w = rect.size.width; - int h = rect.size.height; + int w = rect_temp.size.width; + int h = rect_temp.size.height; int x = (dst.cols - w) / 2; int y = (dst.rows - h) / 2; - dstTri[0] = cv::Point2f(x, y + h); - dstTri[1] = cv::Point2f(x, y); - dstTri[2] = cv::Point2f(x + w, y); + dstTri[0] = cv::Point2f(0, h); + dstTri[1] = cv::Point2f(0, 0); + dstTri[2] = cv::Point2f(w, 0); 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 { diff --git a/hgdriver/ImageProcess/ImageApplyAutoCrop.h b/hgdriver/ImageProcess/ImageApplyAutoCrop.h index 1f68256..8f940b0 100644 --- a/hgdriver/ImageProcess/ImageApplyAutoCrop.h +++ b/hgdriver/ImageProcess/ImageApplyAutoCrop.h @@ -35,7 +35,8 @@ 2023/05/23 v1.5.6 修复纯黑图找不到外界轮廓会导致崩溃的BUG。 2023/11/02 v1.6 优化抗噪能力。采用自适应背景色二值化。 2023/12/05 v1.6.1 替换获取背景色方案;修复固定幅面裁切纠偏的坐标计算BUG。 - * 版本号:v1.6.1 + 2023/12/06 v1.6.2 修复固定幅面裁切纠偏的坐标计算BUG。 + * 版本号:v1.6.2 * ==================================================== */ diff --git a/hgdriver/ImageProcess/ImageProcess_Public.cpp b/hgdriver/ImageProcess/ImageProcess_Public.cpp index d8a7001..f70024c 100644 --- a/hgdriver/ImageProcess/ImageProcess_Public.cpp +++ b/hgdriver/ImageProcess/ImageProcess_Public.cpp @@ -207,31 +207,45 @@ namespace hg 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 }; const float* ranges = { range }; - int histSize = 256; - - cv::Mat hist[3]; - double min, max; - cv::Point maxLoc; - for (int i = 0; i < 3; i++) + cv::Scalar bgc; + if (image.channels() == 3) { - 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(j) > max_value) + { + index_max = j; + max_value = hist[i].at(j); + } + + bgc[i] = index_max; + } + } + else + { + cv::Mat hist; + cv::calcHist(&image, 1, 0, mask, hist, 1, &histSize, &ranges); int index_max = 0; - int max_value = 0; + int max_value = 0; for (size_t j = threshold; j < 256; j++) - if (hist[i].at(j) > max_value) + if (hist.at(j) > max_value) { index_max = j; - max_value = hist[i].at(j); + max_value = hist.at(j); } - - bgc[i] = index_max; + bgc = cv::Scalar::all(index_max); } return bgc; diff --git a/hgdriver/ImageProcess/ImageProcess_Public.h b/hgdriver/ImageProcess/ImageProcess_Public.h index 632a8b8..a0627f4 100644 --- a/hgdriver/ImageProcess/ImageProcess_Public.h +++ b/hgdriver/ImageProcess/ImageProcess_Public.h @@ -11,7 +11,8 @@ * 2023/12/01 v1.4 getBackGroundColor㷨 * 2023/12/02 v1.4.1 getBackGroundColorthresholdֵ * 2023/12/04 v1.4.2 opencv汾ӿڡ - * 汾ţv1.4.2 + * 2023/12/05 v1.4.3 getBackGroundColorֵ֧ͨͼ񱳾ɫʶ + * 汾ţv1.4.3 * ==================================================== */