#include "ImageApplyDetachNoise.h" #include "ImageProcess_Public.h" CImageApplyDetachNoise::CImageApplyDetachNoise(int noise) : m_noise(noise) { } CImageApplyDetachNoise::~CImageApplyDetachNoise(void) { } void CImageApplyDetachNoise::apply(cv::Mat& pDib, int side) { (void)side; if (pDib.empty()||pDib.channels() != 1) return; cv::Mat mask; cv::threshold(pDib, mask, 127, 255, cv::THRESH_BINARY_INV); std::vector> contours; std::vector h; hg::findContours(mask, contours, h); for (const std::vector& contour : contours) { cv::Rect rect = cv::boundingRect(contour); if (rect.width <= m_noise && rect.height <= m_noise) fillConvexPoly(pDib, contour, cv::Scalar(255)); } } void CImageApplyDetachNoise::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++; } }