更新黑白阈值算法,解决黑白阈值不生效的问题

This commit is contained in:
yangjiaxuan 2023-11-22 15:37:50 +08:00
parent 0599191708
commit 3ff93b1dca
5 changed files with 15 additions and 14 deletions

View File

@ -1,5 +1,7 @@
#include "ImageApplyBWBinaray.h" #include "ImageApplyBWBinaray.h"
#define THRESHOLD_LOW 30
#define THRESHOLD_UP 245
CImageApplyBWBinaray::CImageApplyBWBinaray(ThresholdType type, int threshold, int blockSize, int constant) CImageApplyBWBinaray::CImageApplyBWBinaray(ThresholdType type, int threshold, int blockSize, int constant)
: m_threshold(threshold) : m_threshold(threshold)
, m_type(type) , m_type(type)
@ -27,8 +29,6 @@ CImageApplyBWBinaray::~CImageApplyBWBinaray(void)
delete[] m_table; delete[] m_table;
} }
#define THRESHOLD_LOW 30
#define THRESHOLD_UP 245
void CImageApplyBWBinaray::apply(cv::Mat& pDib, int side) void CImageApplyBWBinaray::apply(cv::Mat& pDib, int side)
{ {
(void)side; (void)side;
@ -40,7 +40,7 @@ void CImageApplyBWBinaray::apply(cv::Mat& pDib, int side)
cv::Mat integ; cv::Mat integ;
int blockSize = m_blockSize;//邻域尺寸 int blockSize = m_blockSize;//邻域尺寸
int threshold = m_constant; int threshold = m_constant;
int low = THRESHOLD_LOW; int low = m_threshold;
int up = THRESHOLD_UP; int up = THRESHOLD_UP;
int halfSize = blockSize / 2; int halfSize = blockSize / 2;
int square_blockSize = blockSize * blockSize; int square_blockSize = blockSize * blockSize;
@ -71,13 +71,13 @@ void CImageApplyBWBinaray::apply(cv::Mat& pDib, int side)
cv::threshold(pDib(cv::Rect(0, pDib.rows - halfSize, pDib.cols, halfSize)), pDib(cv::Rect(0, pDib.rows - halfSize, pDib.cols, halfSize)), m_threshold, 255, cv::THRESH_BINARY); cv::threshold(pDib(cv::Rect(0, pDib.rows - halfSize, pDib.cols, halfSize)), pDib(cv::Rect(0, pDib.rows - halfSize, pDib.cols, halfSize)), m_threshold, 255, cv::THRESH_BINARY);
break; break;
case ThresholdType::THRESH_OTSU: case ThresholdType::THRESH_OTSU:
cv::threshold(pDib, pDib, m_threshold, 255, CV_THRESH_OTSU); cv::threshold(pDib, pDib, m_threshold, 255, cv::THRESH_OTSU);
break; break;
case ThresholdType::ADAPTIVE_GAUSSIAN: case ThresholdType::ADAPTIVE_GAUSSIAN:
cv::adaptiveThreshold(pDib, pDib, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY, m_blockSize, m_constant); cv::adaptiveThreshold(pDib, pDib, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, m_blockSize, m_constant);
break; break;
case ThresholdType::ADAPTIVE_MEAN: case ThresholdType::ADAPTIVE_MEAN:
cv::adaptiveThreshold(pDib, pDib, 255, cv::ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, m_blockSize, m_constant); cv::adaptiveThreshold(pDib, pDib, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, m_blockSize, m_constant);
break; break;
case ThresholdType::ERROR_DIFFUSION: case ThresholdType::ERROR_DIFFUSION:
errorDiffuse(pDib); errorDiffuse(pDib);
@ -108,7 +108,7 @@ void CImageApplyBWBinaray::errorDiffuse(cv::Mat& image)
{ {
if (image.rows < 3 || image.cols < 3) if (image.rows < 3 || image.cols < 3)
{ {
cv::threshold(image, image, m_threshold, 255, CV_THRESH_BINARY); cv::threshold(image, image, m_threshold, 255, cv::THRESH_BINARY);
return; return;
} }

View File

@ -10,7 +10,8 @@
2020/12/21 v1.3.1 2020/12/21 v1.3.1
2020/12/21 v1.3.2 blockSize,5125 2020/12/21 v1.3.2 blockSize,5125
2022/05/25 v1.3.3 blockSize和constantTHRESH_BINARY同样有效 2022/05/25 v1.3.3 blockSize和constantTHRESH_BINARY同样有效
* v1.3.3 2023/11/22 v1.4 THRESH_BINARY模式时m_threshold作为THRESHOLD_LOW使用
* v1.4
* ==================================================== * ====================================================
*/ */

View File

@ -5372,7 +5372,7 @@ void hg_scanner::image_process(std::shared_ptr<tiny_buffer>& buffer, uint32_t id
if ((img_conf_.pixtype == COLOR_MODE_BLACK_WHITE) && (pid_ != 0x239 && pid_ != 0x439)) if ((img_conf_.pixtype == COLOR_MODE_BLACK_WHITE) && (pid_ != 0x239 && pid_ != 0x439))
{ {
hg_imgproc::errorextention(ImagePrc_pHandle_); hg_imgproc::errorextention(ImagePrc_pHandle_, bw_threshold_);
(this->*dump_img_)(ImagePrc_pHandle_, "errorextention"); (this->*dump_img_)(ImagePrc_pHandle_, "errorextention");
} }

View File

@ -1124,7 +1124,7 @@ namespace hg_imgproc
return SCANNER_ERR_OK; return SCANNER_ERR_OK;
} }
int errorextention() int errorextention(int bw_threshold)
{ {
int ret = SCANNER_ERR_OK; int ret = SCANNER_ERR_OK;
std::vector<cv::Mat> mats(mats_); std::vector<cv::Mat> mats(mats_);
@ -1136,7 +1136,7 @@ namespace hg_imgproc
else else
thrtype = CImageApplyBWBinaray::ThresholdType::THRESH_BINARY; thrtype = CImageApplyBWBinaray::ThresholdType::THRESH_BINARY;
CImageApplyBWBinaray BWBinaray(thrtype); CImageApplyBWBinaray BWBinaray(thrtype, bw_threshold);
for (size_t i = 0; i < mats.size(); ++i) for (size_t i = 0; i < mats.size(); ++i)
{ {
BWBinaray.apply(mats[i],img_conf_.is_duplex); BWBinaray.apply(mats[i],img_conf_.is_duplex);
@ -2025,9 +2025,9 @@ namespace hg_imgproc
{ {
return ((imgproc*)himg)->nosieDetach(); return ((imgproc*)himg)->nosieDetach();
} }
int errorextention(HIMGPRC himg) int errorextention(HIMGPRC himg, int bw_threshold)
{ {
return ((imgproc*)himg)->errorextention(); return ((imgproc*)himg)->errorextention(bw_threshold);
} }
int discardBlank(HIMGPRC himg) int discardBlank(HIMGPRC himg)
{ {

View File

@ -196,7 +196,7 @@ namespace hg_imgproc
int remove_morr(HIMGPRC himg); int remove_morr(HIMGPRC himg);
int sharpenType(HIMGPRC himg); int sharpenType(HIMGPRC himg);
int nosieDetach(HIMGPRC himg); int nosieDetach(HIMGPRC himg);
int errorextention(HIMGPRC himg); int errorextention(HIMGPRC himg, int bw_threshold);
int discardBlank(HIMGPRC himg); int discardBlank(HIMGPRC himg);
int answerSheetFilterRed(HIMGPRC himg); int answerSheetFilterRed(HIMGPRC himg);
int imgtypechange(HIMGPRC himg,std::string img_type_,void *buf,std::vector<unsigned char> &bmpdata); int imgtypechange(HIMGPRC himg,std::string img_type_,void *buf,std::vector<unsigned char> &bmpdata);