opencv怎么将离散的点集按照最外围的点变成一块完整的图像
vector<Point> pts1={pt1,pt2,pt3,....}; //一个轮廓
vector<vector<Point>> contoures={pts1,pts2,......}; //轮廓集合
drawContours(img,contours,contours_index,Scalar(0,0,255),-1,8);
可以使用OpenCV的轮廓检测功能来将离散的点集按照最外围的点变成一块完整的图像。下面是一种基本的方法:
1.将离散的点集按照顺序连接成多边形,可以使用OpenCV的cv::approxPolyDP函数进行多边形近似。
2.使用cv::findContours函数检测多边形的轮廓。
3.在轮廓中找到最外层的轮廓。
4.使用cv::drawContours函数将轮廓填充成完整的图像。
下面是一个简单的示例代码,其中假设输入的点集为points。
// 将点集转换成多边形
std::vector<cv::Point> approx;
cv::approxPolyDP(points, approx, 1.0, true);
// 检测多边形的轮廓
std::vector<std::vector<cv::Point>> contours;
cv::findContours(approx, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
// 找到最外层的轮廓
std::vector<cv::Point> largestContour = contours[0];
for (const auto& contour : contours) {
if (cv::contourArea(contour) > cv::contourArea(largestContour)) {
largestContour = contour;
}
}
// 填充轮廓
cv::Mat img = cv::Mat::zeros(cv::Size(800, 600), CV_8UC1);
cv::drawContours(img, std::vector<std::vector<cv::Point>>{largestContour}, 0, cv::Scalar(255), cv::FILLED);
在上面的代码中,我们使用cv::approxPolyDP函数将输入的点集points转换成多边形,并使用cv::findContours函数检测多边形的轮廓。然后我们找到最外层的轮廓,并使用cv::drawContours函数将其填充成完整的图像。最终得到的图像是一个单通道(灰度)图像,其中最外围的轮廓被填充为白色,其余部分为黑色。
遍历所有点,两两之间连成直线,然后求最大轮廓。