形状检测问题的 OpenCV C++ 到 Java 的转换 [英] OpenCV C++ Conversion to Java for Shape Detection Issues
问题描述
我们对 openCV Java 开发有点陌生,遇到了一个问题.
We are a bit new to the openCV Java development and ran across an issue.
我们正在尝试将此代码转换为适用于 Android 的 Java.
We are trying to convert this code to Java for Android.
approxPolyDp 需要一个 MatOfPoint2f,其中我们有 'approx' 参数.但是,当我们需要在 isContourConvex 之后的 if 语句中使用相同的变量时,它需要一个 MatOfPoint.最初的原始代码是使用 ArrayList 大约.我们对此感到非常困惑,需要朝着正确的方向推动以了解我们应该做什么.
The approxPolyDp requires a MatOfPoint2f where we have 'approx' parameter. Though, when we need to use this same variable in the if statement just after for the isContourConvex, it requires a MatOfPoint. The original code in the first place was using a ArrayList for approx. We're very confused by this and need a nudge in the right direction to understand what we're supposed to do.
// 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;
推荐答案
对于以后看到这篇文章的人,这里是我的代码摘录
For those who see this post in the future, here's an excerpt from my codes
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
}
这篇关于形状检测问题的 OpenCV C++ 到 Java 的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!