我们对 openCV Java 开发有点陌生,遇到了一个问题。
我们正在努力转型这段代码 https://github.com/bsdnoobz/opencv-code/blob/master/shape-detect.cpp到 Android 的 Java。
approxPolyDp 需要一个 MatOfPoint2f,其中我们有“approx”参数。不过,当我们需要在 isContourConvex 之后的 if 语句中使用相同的变量时,它需要一个 MatOfPoint。首先,原始代码使用 ArrayList 进行大约。我们对此感到非常困惑,需要在正确的方向上推动才能理解我们应该做什么。
// Find contours
java.util.ArrayList<java.util.ArrayList<Point>>();
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
Imgproc.findContours(bw.clone(), contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
// java.util.ArrayList<Point> approx = new java.util.ArrayList<Point>();
MatOfPoint2f approx = new MatOfPoint2f();
Mat dst = img.clone();
for (int i = 0; i < contours.size(); i++)
{
// Approximate contour with accuracy proportional
// to the contour perimeter
MatOfPoint2f contoursMat2 = new MatOfPoint2f( contours.get(i));
Imgproc.approxPolyDP(contoursMat2, approx, Imgproc.arcLength(contoursMat2, true) * 0.02, true);
// Skip small or non-convex objects
if (Math.abs(Imgproc.contourArea(contours.get(i))) < 100 || !Imgproc.isContourConvex(approx))
continue;
对于那些将来看到这篇文章的人,这是我的代码的摘录
MatOfPoint2f contour2f = new MatOfPoint2f(finalContour.get(i).toArray());
double approxDistance = Imgproc.arcLength(contour2f, true)*0.01;
Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
centers[i] = new Point();
MatOfPoint points = new MatOfPoint(approxCurve.toArray());
//if the contour has an circularness shape to it
if (approxCurve.toArray().length >= 8 && approxCurve.toArray().length <= 18) {
//insert codes here
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)