156 lines
3.7 KiB
C++
156 lines
3.7 KiB
C++
#include "ImageApplyColorRecognition.h"
|
||
#include "ImageApplyHeaders.h"
|
||
|
||
static CImageApplyBWBinaray m_bw;
|
||
static CImageApplyAdjustColors m_ac(0, 50, 1.0f);
|
||
|
||
/// <summary>
|
||
/// 检测图像是否是彩色。当前逻辑仅针对红色像素进行判断,即存在红色像素则为彩色,否则为非彩色
|
||
/// </summary>
|
||
/// <param name="image">待测图像</param>
|
||
/// <returns>true为彩色,false为非彩色</returns>
|
||
bool isColor(const cv::Mat& image)
|
||
{
|
||
if (image.channels() != 3) return false;
|
||
|
||
cv::Mat pDib_resize;
|
||
cv::resize(image, pDib_resize, cv::Size(image.cols / 4, image.rows / 4), 0, 0, cv::INTER_NEAREST);
|
||
|
||
cv::Mat hsv;
|
||
cv::cvtColor(pDib_resize, hsv, cv::COLOR_BGR2HSV_FULL);
|
||
std::vector<cv::Mat> hsv_channels;
|
||
cv::split(hsv, hsv_channels);
|
||
|
||
cv::Mat range_s1, range_s2;
|
||
cv::inRange(hsv_channels[1], 220, 255, range_s1); //饱和度在[220, 255]的像素
|
||
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)
|
||
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)
|
||
{
|
||
//if (image.channels() == 3) return true;
|
||
|
||
//cv::Mat image_clone;
|
||
//cv::resize(image, image_clone, cv::Size(), 0.25, 0.25);
|
||
//int channels[] = { 0 };
|
||
//int histsize[] = { 256 };
|
||
//float range[] = { 0, 256 };
|
||
//const float* histRanges[] = { range };
|
||
//cv::Mat hist;
|
||
//cv::calcHist(&image_clone, 1, channels, cv::Mat(), hist, 1, histsize, histRanges, true, false);
|
||
//float pixels[256] = { 0 };
|
||
//for (size_t i = 0; i < 256; i++)
|
||
// pixels[i] = hist.at<float>(i, 0);
|
||
|
||
|
||
//float sum = 0;
|
||
//for (size_t i = 0; i < 40; i++)
|
||
//{
|
||
|
||
//}
|
||
//float pixel_count0 = hist.at<float>(0, 0);
|
||
//float pixel_count255 = hist.at<float>(255, 0);
|
||
//float total = image_clone.total();
|
||
|
||
//return ((pixel_count0 + pixel_count255) / total) > 0.95;
|
||
return false;
|
||
}
|
||
|
||
CImageApplyColorRecognition::CImageApplyColorRecognition(ColorRecognitionMode mode)
|
||
: m_mode(mode)
|
||
{
|
||
}
|
||
|
||
CImageApplyColorRecognition::~CImageApplyColorRecognition(void)
|
||
{
|
||
}
|
||
|
||
void CImageApplyColorRecognition::apply(cv::Mat& pDib, int side)
|
||
{
|
||
if (pDib.channels() != 3)
|
||
{
|
||
m_result = Gray;
|
||
return;
|
||
}
|
||
m_result = isColor(pDib) ? Color : Gray;
|
||
if (m_result == Gray && pDib.channels() == 3)
|
||
cv::cvtColor(pDib, pDib, cv::COLOR_BGR2GRAY);
|
||
|
||
//先判断是否需要判断是彩色
|
||
//if (m_mode == AllColor || m_mode == Color_Gray || m_mode == Color_Mono)
|
||
//{
|
||
// //如果是彩色,直接退出
|
||
// if (isColor(pDib))
|
||
// {
|
||
// m_result = Color;
|
||
// return;
|
||
// }
|
||
//}
|
||
|
||
//if (pDib.channels() == 3)
|
||
// cv::cvtColor(pDib, pDib, cv::COLOR_BGR2GRAY);
|
||
|
||
//if (m_mode == Color_Gray)
|
||
//{
|
||
// m_result = Gray;
|
||
// return;
|
||
//}
|
||
|
||
//if (m_mode == Color_Mono)
|
||
//{
|
||
// m_bw.apply(pDib, side);
|
||
// m_result = Mono;
|
||
// return;
|
||
//}
|
||
|
||
//if (isGray(pDib))
|
||
// m_result = Gray;
|
||
//else
|
||
//{
|
||
// m_bw.apply(pDib, side);
|
||
// m_result = Mono;
|
||
//}
|
||
}
|
||
|
||
void CImageApplyColorRecognition::apply(std::vector<cv::Mat>& mats, bool isTwoSide)
|
||
{
|
||
m_results.clear();
|
||
if (mats.empty()) return;
|
||
|
||
if (!mats[0].empty())
|
||
apply(mats[0], 0);
|
||
|
||
m_results.push_back(m_result);
|
||
|
||
if (isTwoSide && mats.size() > 1)
|
||
if (!mats[1].empty())
|
||
apply(mats[1], 1);
|
||
|
||
m_results.push_back(m_result);
|
||
}
|
||
|
||
CImageApplyColorRecognition::ColorType CImageApplyColorRecognition::getResult()
|
||
{
|
||
return m_result;
|
||
}
|
||
|
||
std::vector<CImageApplyColorRecognition::ColorType> CImageApplyColorRecognition::getResults()
|
||
{
|
||
return m_results;
|
||
}
|