approxPolyDP
方法:
approxPolyDP( MatOfPoint2f curve,
MatOfPoint2f approxCurve,
double epsilon,
boolean closed)
函数作用:
用指定的精度逼近多边形曲线
参数:
curve:输入的轮廓点集合
approxCurve:输出的轮廓点集合。保存的是多边形的顶点。
epsilon:逼近的精度,原始曲线和逼近曲线间的最大值
closed:是否为封闭曲线。如果为 true,表示逼近曲线为封闭曲线。
例子:
//查找轮廓
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(m, contours , new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
//多边形逼近
MatOfPoint2f curve = new MatOfPoint2f(contours.get(0).toArray());
MatOfPoint2f approxCurve = new MatOfPoint2f();
double epsilon = 15;
boolean closed = true;
ArrayList<MatOfPoint> list = new ArrayList<>();
Imgproc.approxPolyDP(curve, approxCurve, epsilon, closed );
list.add(new MatOfPoint(approxCurve.toArray()));
// 画出逼近的多边形图像
Mat image = Mat.zeros(m.size(), CvType.CV_8UC3);
Imgproc.drawContours(image , list, 0, new Scalar(255,255,0));