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>
The main function is to crease a continuous smooth curve
There are four parameters
Inputarray curve: input curve. The data type can be vector & lt; point & gt;.
Outputarray approxcurve: output polyline. The data type can be vector & lt; point & gt;.
Double epsilon: the threshold value for judging the distance between the point and the corresponding line segment. (if the distance is greater than this threshold, it will be discarded. If the distance is less than this threshold, it will be retained. The smaller the epsilon is, the closer the shape of the broken line is to the curve.)
Bool closed: Flag of whether the curve is closed or not.
Program example:
#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);
}
Image “01. JPG”:
Epsilon was 15
Epsilon was 5
Read More:
- Opencv Python realizes the paint filling function in PS, one click filling color and the possible reasons for opencv’s frequent errors
- [Solved] cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\……
- [Solved] Pyinstaller package opencv error: ImportError: OpenCV loader: missing configuration file: [‘config.py’]. Check OpenCV installation.
- v2.error: OpenCV(4.5.5) error: (-215:Assertion failed) npoints > 0 in function ‘cv::drawContours‘
- [Solved] error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘copyMakeBorder‘
- [Solved] cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function ‘circle‘
- [Solved] cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function “pointPolygonTest“
- [Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed
- Opencv-python Install is Stuck Error: running setup.py bdist_wheel for opencv-python
- Full explanation of SYS module of Python
- Python uses try… Except… To output detailed errors
- [Solved] python opencv Error: findContours() Can only use the Grayscale
- Python+OpenCV: How to Use Background Subtraction Methods
- How to Solve Opencv Reads Video Error: cv2.error
- Opencv: How to Draw Palette
- [Solved] ERROR: Failed building wheel for opencv-python-headless
- Raspberry pie import opencv error: ImportError: numpy.core.multiarray failed to import
- [Solved] cv2.error: OpenCV(4.5.1) XXX\shapedescr.cpp:315: error: (-215:Assertion failed) npoints >= 0 &&……
- [Solved] OpenCV Import Error: ImportError: numpy.core.multiarray failed to import