mirror of http://192.168.1.51:8099/lmh188/twain3.0
127 lines
2.9 KiB
C++
127 lines
2.9 KiB
C++
|
#include "ImageApplyColorRecognition.h"
|
|||
|
#include "ImageApplyHeaders.h"
|
|||
|
|
|||
|
static CImageApplyBWBinaray m_bw;
|
|||
|
static CImageApplyAdjustColors m_ac(0, 50, 1.0f);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>Ƿ<EFBFBD><C7B7>Dz<EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><DFBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD>жϣ<D0B6><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>Dz<EFBFBD>ɫ
|
|||
|
/// </summary>
|
|||
|
/// <param name="image"><3E><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC></param>
|
|||
|
/// <returns>trueΪ<65><CEAA>ɫ<EFBFBD><C9AB>falseΪ<65>Dz<EFBFBD>ɫ</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 / 9, image.rows / 9), 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);
|
|||
|
|
|||
|
cv::Mat range_h1, range_h2, range_s, range_v;
|
|||
|
cv::inRange(hsv_channels[0], 0, 85, range_h1);
|
|||
|
cv::inRange(hsv_channels[0], 170, 255, range_h2);
|
|||
|
cv::inRange(hsv_channels[1], 60, 255, range_s);
|
|||
|
cv::inRange(hsv_channels[2], 100, 255, range_v);
|
|||
|
|
|||
|
cv::Mat thre = (range_h1 | range_h2) & range_s & range_v;
|
|||
|
return cv::sum(thre)[0] > 4;
|
|||
|
}
|
|||
|
|
|||
|
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 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;
|
|||
|
}
|
|||
|
|
|||
|
CImageApplyColorRecognition::CImageApplyColorRecognition(ColorRecognitionMode mode)
|
|||
|
: m_mode(mode)
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
CImageApplyColorRecognition::~CImageApplyColorRecognition(void)
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
void CImageApplyColorRecognition::apply(cv::Mat& pDib, int side)
|
|||
|
{
|
|||
|
//<2F><><EFBFBD>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD>ж<EFBFBD><D0B6>Dz<EFBFBD>ɫ
|
|||
|
if (m_mode == AllColor || m_mode == Color_Gray || m_mode == Color_Mono)
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD>ɫ<EFBFBD><C9AB>ֱ<EFBFBD><D6B1><EFBFBD>˳<EFBFBD>
|
|||
|
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;
|
|||
|
}
|