#include "ImageApplyDogEarDetection.h" #include "ImageProcess_Public.h" CImageApplyDogEarDetection::CImageApplyDogEarDetection() : m_threshold(40) , m_zoom_x(1.0) , m_zoom_y(1.0) , m_distance1(50) , m_distance2(50) , m_result(0) { } CImageApplyDogEarDetection::CImageApplyDogEarDetection(double threshlod, double zoom_x, double zoom_y, double distance1, double distance2) : m_threshold(threshlod) , m_zoom_x(zoom_x) , m_zoom_y(zoom_y) , m_distance1(distance1) , m_distance2(distance2) , m_result(0) { } CImageApplyDogEarDetection::~CImageApplyDogEarDetection() { } #define EDGE_ABS 4 void CImageApplyDogEarDetection::apply(cv::Mat& pDib, int side) { m_result = 0; (void)side; if (pDib.empty()) return; cv::Mat src; if (m_zoom_x != 1.0 && m_zoom_y != 1.0) cv::resize(pDib, src, cv::Size(), m_zoom_x, m_zoom_y, cv::INTER_NEAREST); else src = pDib; cv::Mat thre; hg::threshold_Mat(src, thre, m_threshold); cv::Mat element = getStructuringElement(cv::MORPH_RECT, cv::Size(5, 1)); cv::morphologyEx(thre, thre, cv::MORPH_OPEN, element, cv::Point(-1, -1), 1, cv::BORDER_CONSTANT, cv::Scalar::all(0)); std::vector hierarchy; std::vector> contours; hg::findContours(thre, contours, hierarchy, cv::RETR_EXTERNAL); std::vector maxContour = hg::getMaxContour(contours, hierarchy); if (maxContour.size() == 0) { m_result = true; return; } hg::convexHull(maxContour, maxContour); cv::RotatedRect rect = hg::getBoundingRect(maxContour); cv::Point2f vertexes[4]; rect.points(vertexes); double distance; for (int i = 0; i < 4; i++) { distance = -cv::pointPolygonTest(maxContour, vertexes[i], true); //判断是否为普通折角 if (distance > (m_distance1 / m_zoom_x)) { if (cv::Rect(0, 0, src.cols, src.rows).contains(vertexes[i])) m_result = 1; else if (vertexes[i].x < 0 && cv::abs(distance + vertexes[i].x) > EDGE_ABS) //如果是越界折角,修改m_result值,但是需要继续判断,防止漏掉普通折角 m_result = 1; else if (vertexes[i].y < 0 && cv::abs(distance + vertexes[i].y) > EDGE_ABS) m_result = 1; else if (vertexes[i].x > src.cols && cv::abs(distance + src.cols - vertexes[i].x) > EDGE_ABS) m_result = 1; else if (vertexes[i].y > src.rows && cv::abs(distance + src.rows - vertexes[i].y) > EDGE_ABS) m_result = 1; } if (m_result == 1) return; //判断是否为扫描越界导致的折角 if (distance > (m_distance2 / m_zoom_x)) { if (vertexes[i].x < 0 && cv::abs(distance + vertexes[i].x) < EDGE_ABS) //如果是越界折角,修改m_result值,但是需要继续判断,防止漏掉普通折角 m_result = 2; else if (vertexes[i].y < 0 && cv::abs(distance + vertexes[i].y) < EDGE_ABS) m_result = 2; else if (vertexes[i].x > src.cols && cv::abs(distance + src.cols - vertexes[i].x) < EDGE_ABS) m_result = 2; else if (vertexes[i].y > src.rows && cv::abs(distance + src.rows - vertexes[i].y) < EDGE_ABS) m_result = 2; } } } void CImageApplyDogEarDetection::apply(std::vector& mats, bool isTwoSide) { (void)mats; (void)isTwoSide; }