#include "ImageApplyRefuseInflow.h" #include "ImageProcess_Public.h" CImageApplyRefuseInflow::CImageApplyRefuseInflow(int threshold, int range) : m_threshold(threshold) , m_range(range) { } CImageApplyRefuseInflow::~CImageApplyRefuseInflow() { } void CImageApplyRefuseInflow::apply(cv::Mat& pDib, int side) { if (pDib.channels() == 3) { cv::Mat resizeMat; cv::resize(pDib, resizeMat, cv::Size(200, 200)); cv::Mat mask; cv::cvtColor(resizeMat, mask, cv::COLOR_BGR2GRAY); cv::threshold(mask, mask, m_threshold, 255, cv::THRESH_BINARY); cv::Scalar bgc = hg::getBackGroundColor(resizeMat, mask, m_threshold); std::vector low, up; for (size_t i = 0; i < 3; i++) { low.push_back(cv::max((int)bgc[i] - m_range, 0)); up.push_back(cv::min((int)bgc[i] + m_range, 255)); } cv::inRange(pDib, low, up, mask); pDib = pDib.setTo(bgc, mask); } else { cv::Mat resizeMat; cv::resize(pDib, resizeMat, cv::Size(200, 200)); cv::Mat mask; cv::threshold(resizeMat, mask, m_threshold, 255, cv::THRESH_BINARY); cv::Mat hist; double min, max; cv::Point maxLoc; float range[] = { 0, 256 }; const float* ranges = { range }; int histSize = 256; cv::calcHist(&resizeMat, 1, 0, mask, hist, 1, &histSize, &ranges); int index_max = 0; int max_value = 0; for (size_t j = m_threshold; j < 256; j++) if (hist.at(j) > max_value) { index_max = j; max_value = hist.at(j); } cv::inRange(pDib, cv::max(index_max - m_range, 0), cv::min(index_max + m_range, 255), mask); pDib = pDib.setTo(cv::Scalar::all(index_max), mask); } } void CImageApplyRefuseInflow::apply(std::vector& mats, bool isTwoSide) { (void)isTwoSide; int i = 0; for (cv::Mat& var : mats) { if (i != 0 && isTwoSide == false) break; if (!var.empty()) apply(var, 0); i++; } }