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 bool allInside = true; for (const auto& vertex : polygon1) { double distance = cv::pointPolygonTest(polygon2, vertex, true); if (distance < 0) { allInside = false; break; } } if (allInside) { return true; } // Polygon1 is partially or completely outside polygon2 std::vector<std::vector<cv::Point>> intersectionPolygon; // 修改此处 if (cv::isContourConvex(polygon1) && cv::isContourConvex(polygon2)) { cv::Mat intersectionMat; cv::intersectConvexConvex(cv::Mat(polygon1), cv::Mat(polygon2), intersectionMat); if (cv::countNonZero(intersectionMat) > 0) { cv::findContours(intersectionMat, intersectionPolygon, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); } } else { std::vector<cv::Point> hull1, hull2; cv::convexHull(polygon1, hull1); cv::convexHull(polygon2, hull2); std::vector<cv::Point> hullIntersection; cv::convexHull(hull1, hullIntersection, false, false); cv::fillConvexPoly(cv::Mat(hullIntersection), hull2, cv::Scalar(0), false); cv::findContours(cv::Mat(hullIntersection), intersectionPolygon, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); } if (intersectionPolygon.empty()) { outsideArea = 0; return false; } double intersectionArea = std::abs(cv::contourArea(intersectionPolygon[0])); // 修改此处 double polygon1Area = std::abs(cv::contourArea(polygon1)); outsideArea = polygon1Area - intersectionArea; return true; // 修改此处 } 这个是你给的代码,运行后提示报错:terminate called after throwing an instance of 'cv::Exception' what(): OpenCV(4.5.0) /home/sniper/Downloads/opencv-4.5.0/modules/imgproc/src/contours.cpp:195: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl'
时间: 2024-04-21 10:25:27 浏览: 7
根据报错信息,问题可能出现在 `cv::findContours()` 函数中。这个函数要求输入的图像是 CV_8UC1 格式的,但是你传递给函数的参数类型可能不正确。请检查你传递给 `cv::findContours()` 函数的参数类型是否正确。如果你的图像不是 CV_8UC1 格式,可以尝试将它转换为正确的格式。如果你仍然无法解决问题,请提供更多的代码和上下文信息,以便我可以更好地帮助你解决这个问题。