OpenCV 多边形逼近

approxPolyDP(contours, Curve, epsilon, true);

 InputArray contours:输入的点集;
OutputArray Curve:输出的点集,当前点集是能最小包容指定点集的。画出来即是一个多边形;
double epsilon:指定的精度,也即是原始曲线与近似曲线之间的最大距离;
bool closed:若为true,则说明近似曲线是闭合的,反之,若为false,则断开。

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

void main()
{
    Mat img = imread("E:/1.jpg",0);
    imshow("img", img);
    Mat img_out(img.size(), CV_8UC3, Scalar::all(0));//纯黑图像
    threshold(img, img, 200, 255,THRESH_OTSU );
    vector<vector<Point>> contours;
    vector<Vec4i> hierarcy;
    findContours(img, contours, hierarcy, 0, CHAIN_APPROX_NONE);
    vector<vector<Point>> contours_poly(contours.size());//用于存放折线点集
    for (int i = 0; i < contours.size(); i++)
    {
        approxPolyDP(Mat(contours[i]), contours_poly[i],2, true);

        drawContours(img_out, contours_poly, i, Scalar(0, 255, 255), 2, 8);  //绘制
    }
    imshow("approx", img_out);
    waitKey(0);
}

原文地址:https://www.cnblogs.com/hsy1941/p/11282229.html