更新自动颜色识别算法,提高识别率

This commit is contained in:
yangjiaxuan 2023-11-28 18:04:08 +08:00
parent a423b0c8ba
commit 9590e61da2
2 changed files with 36 additions and 31 deletions

View File

@ -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;
}

View File

@ -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;