cv::contourArea()

函数作用

计算轮廓区域面积

函数原型

double contourArea(InputArray contour, bool oriented = false)
// oriented方向:默认为false,表示取面积的绝对值
// oriented = true:面积值会按照contour顺逆时针的不同出现正值或者负值
bool isPolygonInside(const std::vector<cv::Point>& polygon1, const std::vector<cv::Point>& polygon2, double& outsideArea) { // Check if all vertices of polygon1 are inside polygon2 for (const auto& vertex : polygon1) { double distance = cv::pointPolygonTest(polygon2, vertex, true); if (distance < 0) { // Vertex is outside polygon2 // Calculate area of polygon1 outside polygon2 cv::Mat polygon1Mat = cv::Mat(polygon1).reshape(1); cv::Mat polygon2Mat = cv::Mat(polygon2).reshape(1); std::vector<cv::Point2f> intersectionPolygon; if (cv::isContourConvex(polygon1) && cv::isContourConvex(polygon2)) { cv::Mat intersectionMat; cv::intersectConvexConvex(polygon1Mat, polygon2Mat, intersectionMat); if (cv::countNonZero(intersectionMat) > 0) { intersectionMat.reshape(2).copyTo(intersectionPolygon); } } else { cv::Rect rect1 = cv::boundingRect(polygon1Mat); cv::Rect rect2 = cv::boundingRect(polygon2Mat); cv::Rect intersectionRect = rect1 & rect2; if (!intersectionRect.empty()) { cv::Mat intersectionMat = cv::Mat::zeros(intersectionRect.size(), CV_8UC1); cv::fillConvexPoly(intersectionMat, polygon1 - rect1.tl(), cv::Scalar(255)); cv::fillConvexPoly(intersectionMat, polygon2 - rect2.tl(), cv::Scalar(0), cv::LINE_AA); std::vector<std::vector<cv::Point>> contours; cv::findContours(intersectionMat, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); if (!contours.empty()) { intersectionPolygon = contours[0]; } } } double intersectionArea = std::abs(cv::contourArea(intersectionPolygon)); double polygon1Area = std::abs(cv::contourArea(polygon1)); outsideArea = polygon1Area - intersectionArea; return false; } } // All vertices of polygon1 are inside polygon2 return true; }
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值