【问题标题】:OpenCV C++ Conversion to Java for Shape Detection Issues用于形状检测问题的 OpenCV C++ 转换为 Java
【发布时间】:2014-07-26 23:08:19
【问题描述】:

我们对 openCV Java 开发有点陌生,遇到了一个问题。

我们正在尝试将this code 转换为适用于 Android 的 Java。

approxPolyDp 需要一个 MatOfPoint2f,其中我们有 'approx' 参数。但是,当我们需要在 if 语句中使用相同的变量时,就在 isContourConvex 之后,它需要一个 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;

【问题讨论】:

  • 你解决了这个问题吗?我需要这个。请分享

标签: java android c++ opencv


【解决方案1】:

对于以后看到这篇文章的人,这是我的代码的摘录

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
}

【讨论】:

    猜你喜欢
    • 2019-05-29
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    相关资源
    最近更新 更多