更新自动颜色识别算法,提高识别率
This commit is contained in:
parent
a423b0c8ba
commit
9590e61da2
|
@ -1,28 +1,5 @@
|
|||
#include "ImageApplyColorRecognition.h"
|
||||
|
||||
/// <summary>
|
||||
/// 检测图像是否是彩色。当前逻辑仅针对红色像素进行判断,即存在红色像素则为彩色,否则为非彩色
|
||||
/// </summary>
|
||||
/// <param name="image">待测图像</param>
|
||||
/// <returns>true为彩色,false为非彩色</returns>
|
||||
bool isColor(const cv::Mat& image, double threshold = 60)
|
||||
{
|
||||
if (image.channels() != 3) return false;
|
||||
|
||||
cv::Mat pDib_resize;
|
||||
cv::resize(image, pDib_resize, cv::Size(200, 200), 0, 0, cv::INTER_AREA);
|
||||
|
||||
cv::Mat hsv;
|
||||
cv::cvtColor(pDib_resize, hsv, cv::COLOR_BGR2HSV_FULL);
|
||||
std::vector<cv::Mat> hsv_channels;
|
||||
cv::split(hsv, hsv_channels);
|
||||
|
||||
double minVal, maxVal;
|
||||
cv::minMaxLoc(hsv_channels[1], &minVal, &maxVal);
|
||||
|
||||
return maxVal > threshold;
|
||||
}
|
||||
|
||||
bool isGray(const cv::Mat& image)
|
||||
{
|
||||
//if (image.channels() == 3) return true;
|
||||
|
@ -62,6 +39,7 @@ CImageApplyColorRecognition::~CImageApplyColorRecognition(void)
|
|||
{
|
||||
}
|
||||
|
||||
#define HSV_S_THRE 30
|
||||
void CImageApplyColorRecognition::apply(cv::Mat& pDib, int side)
|
||||
{
|
||||
if (pDib.channels() != 3)
|
||||
|
@ -69,7 +47,7 @@ void CImageApplyColorRecognition::apply(cv::Mat& pDib, int side)
|
|||
m_result = Gray;
|
||||
return;
|
||||
}
|
||||
m_result = isColor(pDib) ? Color : Gray;
|
||||
m_result = isColor(pDib, HSV_S_THRE) ? Color : Gray;
|
||||
if (m_result == Gray && pDib.channels() == 3)
|
||||
cv::cvtColor(pDib, pDib, cv::COLOR_BGR2GRAY);
|
||||
|
||||
|
@ -135,3 +113,26 @@ std::vector<CImageApplyColorRecognition::ColorType> CImageApplyColorRecognition:
|
|||
{
|
||||
return m_results;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检测图像是否是彩色。当前逻辑仅针对红色像素进行判断,即存在红色像素则为彩色,否则为非彩色
|
||||
/// </summary>
|
||||
/// <param name="image">待测图像</param>
|
||||
/// <returns>true为彩色,false为非彩色</returns>
|
||||
bool CImageApplyColorRecognition::isColor(const cv::Mat& image, double threshold)
|
||||
{
|
||||
if (image.channels() != 3) return false;
|
||||
|
||||
cv::Mat pDib_resize;
|
||||
cv::resize(image, pDib_resize, cv::Size(200, 200), 0, 0, cv::INTER_AREA);
|
||||
//cv::imwrite("pDib_resize.bmp", pDib_resize);
|
||||
cv::Mat hsv;
|
||||
cv::cvtColor(pDib_resize, hsv, cv::COLOR_BGR2HSV_FULL);
|
||||
std::vector<cv::Mat> hsv_channels;
|
||||
cv::split(hsv, hsv_channels);
|
||||
|
||||
double minVal, maxVal;
|
||||
cv::minMaxLoc(hsv_channels[1], &minVal, &maxVal);
|
||||
|
||||
return maxVal > threshold;
|
||||
}
|
||||
|
|
|
@ -10,7 +10,8 @@
|
|||
* v1.3 2021/04/19 修改识别策略,能够识别占比1‰的彩色图像。只区分彩色和灰度图。
|
||||
* v1.4 2021/06/18 调整二级色彩区间,从原来的[90, 200]调整为[50, 200]。
|
||||
* v1.5 2023/11/22 调整彩色判定策略。
|
||||
* 版本号:v1.5
|
||||
* v1.5.1 2023//11/28 调整饱和度判定阈值
|
||||
* 版本号:v1.5.1
|
||||
* ====================================================
|
||||
*/
|
||||
|
||||
|
@ -60,6 +61,9 @@ public:
|
|||
/// <returns>色彩类型数组</returns>
|
||||
std::vector<ColorType> getResults();
|
||||
|
||||
private:
|
||||
bool isColor(const cv::Mat& image, double threshold = 30);
|
||||
|
||||
private:
|
||||
ColorType m_result;
|
||||
std::vector<ColorType> m_results;
|
||||
|
|
Loading…
Reference in New Issue