更新自动颜色识别算法,解决自动颜色识别有误差的问题

This commit is contained in:
yangjiaxuan 2023-11-22 14:18:34 +08:00
parent c0bfb66ad1
commit 0599191708
3 changed files with 14 additions and 31 deletions

View File

@ -1,44 +1,26 @@
#include "ImageApplyColorRecognition.h" #include "ImageApplyColorRecognition.h"
#include "ImageApplyHeaders.h"
//static CImageApplyBWBinaray m_bw;
//static CImageApplyAdjustColors m_ac(0, 50, 1.0f);
/// <summary> /// <summary>
/// 检测图像是否是彩色。当前逻辑仅针对红色像素进行判断,即存在红色像素则为彩色,否则为非彩色 /// 检测图像是否是彩色。当前逻辑仅针对红色像素进行判断,即存在红色像素则为彩色,否则为非彩色
/// </summary> /// </summary>
/// <param name="image">待测图像</param> /// <param name="image">待测图像</param>
/// <returns>true为彩色false为非彩色</returns> /// <returns>true为彩色false为非彩色</returns>
bool isColor(const cv::Mat& image) bool isColor(const cv::Mat& image, double threshold = 60)
{ {
if (image.channels() != 3) return false; if (image.channels() != 3) return false;
cv::Mat pDib_resize; cv::Mat pDib_resize;
cv::resize(image, pDib_resize, cv::Size(image.cols / 4, image.rows / 4), 0, 0, cv::INTER_NEAREST); cv::resize(image, pDib_resize, cv::Size(200, 200), 0, 0, cv::INTER_AREA);
cv::Mat hsv; cv::Mat hsv;
cv::cvtColor(pDib_resize, hsv, cv::COLOR_BGR2HSV_FULL); cv::cvtColor(pDib_resize, hsv, cv::COLOR_BGR2HSV_FULL);
std::vector<cv::Mat> hsv_channels; std::vector<cv::Mat> hsv_channels;
cv::split(hsv, hsv_channels); cv::split(hsv, hsv_channels);
cv::Mat range_s1, range_s2; double minVal, maxVal;
cv::inRange(hsv_channels[1], 220, 255, range_s1); //饱和度在[220, 255]的像素 cv::minMaxLoc(hsv_channels[1], &minVal, &maxVal);
cv::inRange(hsv_channels[1], 50, 220, range_s2); //饱和度在[50, 220]的像素
#if 0
cv::imwrite("range_s1.bmp", range_s1);
cv::imwrite("range_s2.bmp", range_s2);
#endif
double sum = cv::sum(range_s1)[0] / 255;
double total = range_s1.total();
// if (sum / total > 0.0001) return maxVal > threshold;
if (sum / total > 0.001)
return true;
sum += cv::sum(range_s2)[0] / 255;
// if (sum / total > 0.001)
if (sum / total > 0.03)
return true;
return false;
} }
bool isGray(const cv::Mat& image) bool isGray(const cv::Mat& image)

View File

@ -4,12 +4,13 @@
* 242568 8 * 242568 8
* *
* 2020/7/17 * 2020/7/17
* 2021/04/19 * v1.0 2020/7/17
* v1.0 2020/7/17
* v1.1 2020/12/15 * v1.1 2020/12/15
* v1.2 2020/12/16 访 * v1.2 2020/12/16 访
* v1.3 2021/04/19 1 * v1.3 2021/04/19 1
* v1.4 2021/06/18 [90, 200][50, 200] * v1.4 2021/06/18 [90, 200][50, 200]
* v1.5 2023/11/22
* v1.5
* ==================================================== * ====================================================
*/ */

View File

@ -5262,7 +5262,7 @@ void hg_scanner::image_process(std::shared_ptr<tiny_buffer>& buffer, uint32_t id
if (img_conf_.is_colorcast && pid_ != 0x239 && pid_ != 0x439) if (img_conf_.is_colorcast && pid_ != 0x239 && pid_ != 0x439)
{ {
ret = hg_imgproc::color_cast_correction(ImagePrc_pHandle_); ret = hg_imgproc::color_cast_correction(ImagePrc_pHandle_);
(this->*dump_img_)(ImagePrc_pHandle_, "discardBlank"); (this->*dump_img_)(ImagePrc_pHandle_, "color_cast_correction");
} }
if ((img_conf_.is_autodiscradblank_normal || img_conf_.is_autodiscradblank_vince || img_conf_.detect_size_diascard_blank) && (pid_ != 0x239 && pid_ != 0x439)) if ((img_conf_.is_autodiscradblank_normal || img_conf_.is_autodiscradblank_vince || img_conf_.detect_size_diascard_blank) && (pid_ != 0x239 && pid_ != 0x439))
{ {
@ -5292,7 +5292,7 @@ void hg_scanner::image_process(std::shared_ptr<tiny_buffer>& buffer, uint32_t id
if ((img_conf_.cropRect.enable && !img_conf_.is_autocrop) && (pid_ != 0x239 && pid_ != 0x439)) if ((img_conf_.cropRect.enable && !img_conf_.is_autocrop) && (pid_ != 0x239 && pid_ != 0x439))
{ {
hg_imgproc::croprect(ImagePrc_pHandle_); hg_imgproc::croprect(ImagePrc_pHandle_);
(this->*dump_img_)(ImagePrc_pHandle_, "resolution_change"); (this->*dump_img_)(ImagePrc_pHandle_, "croprect");
} }
if ((img_conf_.filter != ColorFilter::FILTER_NONE if ((img_conf_.filter != ColorFilter::FILTER_NONE