简体   繁体   中英

OpenCV C++ Conversion to Java for Shape Detection Issues

We are a bit new to the openCV Java development and ran across an issue.

We are trying to convert this code to Java for Android.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM