OpenCV approxPolyDP()函数详解

CV_EXPORTS_W void approxPolyDP( InputArray curve,OutputArray approxCurve,double epsilon, bool closed );

@param curve Input vector of a 2D point stored in std::vector or Mat
@param approxCurve Result of the approximation. The type should match the type of the input curve.
@param epsilon Parameter specifying the approximation accuracy. This is the maximum distance
between the original curve and its approximation.
@param closed If true, the approximated curve is closed (its first and last vertices are
connected). Otherwise, it is not closed.

The function cv::approxPolyDP approximates a curve or a polygon with another curve/polygon with less
vertices so that the distance between them is less or equal to the specified precision. It uses the
Douglas-Peucker algorithm <http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm>

主要功能是把一个连续光滑曲线折线化:

参数有4个:

InputArray curve:输入曲线,数据类型可以为vector<Point>。

OutputArray approxCurve:输出折线,数据类型可以为vector<Point>。

double epsilon:判断点到相对应的line segment 的距离的阈值。(距离大于此阈值则舍弃,小于此阈值则保留,epsilon越小,折线的形状越“接近”曲线。)

bool closed:曲线是否闭合的标志位。

程序示例:

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

using namespace std;
using namespace cv;

void main()
{
    Mat srcImg = imread("01.jpg");
    imshow("src", srcImg);
    Mat dstImg(srcImg.size(), CV_8UC3, Scalar::all(0));//纯黑图像

    cvtColor(srcImg, srcImg, CV_BGR2GRAY);
    threshold(srcImg, srcImg, 200, 255, CV_THRESH_BINARY_INV);
    vector<vector<Point>> contours;
    vector<Vec4i> hierarcy;
    findContours(srcImg, contours, hierarcy, 0, CV_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], 15, true);

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

  图像“01.jpg”:

epsilon为15:

 

 

 epsilon为5:

 

原文链接:https://www.cnblogs.com/k1412/p/6884484.html  感谢Banisher博主!

  • 7
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值