CV_EXPORTS_W void approxPolyDP( InputArray curve, OutputArray approxCurve, double epsilon, bool closed ); @param curve Input vector of a 2D point stor ...
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: