赞
踩
1.将轮廓分别画在两个图层中,ImSrc1,ImSrc2;
2.合成两个图层到Imsrc = ImSrc1 + ImSrc2;
3.二值化(threshold )Imsrc图层;
4.findContours得到相交轮廓;
5.contourArea计算相交区域面积。
具体上代码:
cv::Mat Imsrc1,Imsrc2,Imsrc,Imdest; Imsrc1 = cv::Mat(600, 800, CV_8U, cv::Scalar(255)); Imsrc2 = cv::Mat(600, 800, CV_8U, cv::Scalar(255)); //画实心椭圆 cv::ellipse(Imsrc1, cv::Point(0, 0), cv::Size(100,50), 0, 0, 360, cv::Scalar(200), -1); //画实心多边形 std::vector<cv::Point > contour; std::vector<std::vector<cv::Point >> contours; contour.reserve(m_pntNum); //填充多边形的顶点坐标 for (int i = 0; i < m_pntNum; ++i) { cv::Point pnt(imgPnt[i].x(), imgPnt[i].y()); contour.push_back(pnt); } contours.push_back(contour); cv::fillPoly(Imsrc2, contours, cv::Scalar(200));//fillPoly函数的第二个参数是二维数组!! Imsrc = Imsrc1 + Imsrc2; cv::threshold(Imsrc, Imdest, 200, 255, cv::/*THRESH_BINARY*/THRESH_BINARY_INV);//THRESH_BINARY_INV不检测图片画布轮廓 cv::findContours(Imdest, contoursDest, hierarchy, cv::RETR_EXTERNAL/* RETR_TREE*/, cv::CHAIN_APPROX_SIMPLE);//RETR_EXTERNAL,内轮廓不重复计算 double dbArea = 0; for (int c = 0; c < contoursDest.size(); ++c) { dbArea += contourArea(contoursDest[c]); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。