107 lines
3.5 KiB
C++
107 lines
3.5 KiB
C++
#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<cv::Vec4i> hierarchy;
|
||
std::vector<std::vector<cv::Point>> contours;
|
||
hg::findContours(thre, contours, hierarchy, cv::RETR_EXTERNAL);
|
||
|
||
std::vector<cv::Point> 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<cv::Mat>& mats, bool isTwoSide)
|
||
{
|
||
(void)mats;
|
||
(void)isTwoSide;
|
||
}
|