更新自动颜色识别算法,解决自动颜色识别有误差的问题
This commit is contained in:
parent
c0bfb66ad1
commit
0599191708
|
@ -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)
|
||||||
|
|
|
@ -4,12 +4,13 @@
|
||||||
* 功能:色彩识别,将识别会“灰度”的24位图转化为256色8位图, 把识别为“黑白”图转化为二值化的8位图
|
* 功能:色彩识别,将识别会“灰度”的24位图转化为256色8位图, 把识别为“黑白”图转化为二值化的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
|
||||||
* ====================================================
|
* ====================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue