code_device/hgdriver/ImageProcess/ImageApplyFadeBackGroundCol...

86 lines
2.3 KiB
C++
Raw Permalink Normal View History

2022-07-29 08:41:34 +00:00
#include "ImageApplyFadeBackGroundColor.h"
#include "ImageProcess_Public.h"
2022-07-29 08:41:34 +00:00
CImageApplyFadeBackGroudColor::CImageApplyFadeBackGroudColor(int threshold, int offset, int range)
: m_threshold(threshold)
, m_offset(offset)
, m_range(range)
{
}
CImageApplyFadeBackGroudColor::~CImageApplyFadeBackGroudColor()
{
}
void CImageApplyFadeBackGroudColor::apply(cv::Mat& pDib, int side)
{
if (pDib.channels() != 3)
return;
#if 0
cv::Mat mask;
cv::cvtColor(pDib, mask, cv::COLOR_BGR2GRAY);
cv::threshold(mask, mask, m_threshold, 255, cv::THRESH_BINARY);
//cv::imwrite("mask.jpg", mask);
cv::Mat bgr[3];
cv::split(pDib, bgr);
int histSize = 255;
float range[] = { 0, 255 };
const float* histRange = { range };
cv::Mat hist_bgr[3];
cv::Scalar mean_bgr;
for (size_t i = 0; i < 3; i++)
{
cv::calcHist(&bgr[i], 1, 0, mask, hist_bgr[i], 1, &histSize, &histRange);
double maxVal = 0;
cv::Point maxLoc;
cv::minMaxLoc(hist_bgr[i], NULL, &maxVal, NULL, &maxLoc);
mean_bgr[i] = maxLoc.y;
}
cv::add(pDib, cv::Scalar::all(255 + m_offset) - mean_bgr, pDib, mask);
#else
#if 0
2023-02-22 03:47:55 +00:00
fadeBackground(pDib.data, pDib.cols, pDib.rows, pDib.step, m_threshold, m_offset, m_range);
#else
fadeBackground(pDib, m_threshold, m_offset, m_range);
#endif
2022-07-29 08:41:34 +00:00
#endif
}
void CImageApplyFadeBackGroudColor::fadeBackground(cv::Mat& image, int threshold, int offset, int range)
2022-07-29 08:41:34 +00:00
{
cv::Mat resizeMat;
cv::resize(image, resizeMat, cv::Size(200, 200));
cv::Mat mask;
cv::cvtColor(resizeMat, mask, cv::COLOR_BGR2GRAY);
cv::threshold(mask, mask, threshold, 255, cv::THRESH_BINARY);
2022-07-29 08:41:34 +00:00
cv::Scalar bgc = hg::getBackGroundColor(resizeMat, mask, threshold);
2022-07-29 08:41:34 +00:00
std::vector<int> low, up;
for (size_t i = 0; i < 3; i++)
2022-07-29 08:41:34 +00:00
{
low.push_back(cv::max((int)bgc[i] - range, 0));
up.push_back(cv::min((int)bgc[i] + range, 255));
2022-07-29 08:41:34 +00:00
}
cv::inRange(image, low, up, mask);
cv::add(image, cv::Scalar::all(offset), image, mask);
2022-07-29 08:41:34 +00:00
}
void CImageApplyFadeBackGroudColor::apply(std::vector<cv::Mat>& mats, bool isTwoSide)
{
(void)isTwoSide;
int i = 0;
for (cv::Mat& var : mats)
if (!var.empty())
{
apply(var, i);
i++;
}
}