4

I'm having problem about the relation between Imgproc.minEnclosingCircle method and Core.circle method. Below is how part of my code looks like:

/// Start of Code

//init
    List<MatOfPoint2f> contours2f   = new ArrayList<MatOfPoint2f>();
    List<MatOfPoint2f> polyMOP2f    = new ArrayList<MatOfPoint2f>();
    List<MatOfPoint> polyMOP        = new ArrayList<MatOfPoint>();
    Rect[] boundRect    = new Rect[contours.size()];
    Point[] mPusat      = new Point[contours.size()];
    float[] mJejari     = new float[contours.size()];
            
//initialize the Lists ?
    for (int i = 0; i < contours.size(); i++) {
        contours2f.add(new MatOfPoint2f());
        polyMOP2f.add(new MatOfPoint2f());
        polyMOP.add(new MatOfPoint());
    }
            
//Convert to MatOfPoint2f + approximate contours to polygon + get bounding rects and circles
    for (int i = 0; i < contours.size(); i++) {
        contours.get(i).convertTo(contours2f.get(i), CvType.CV_32FC2);
        Imgproc.approxPolyDP(contours2f.get(i), polyMOP2f.get(i), 3, true);
        polyMOP2f.get(i).convertTo(polyMOP.get(i), CvType.CV_32S);
        boundRect[i] = Imgproc.boundingRect(polyMOP.get(i));
        Imgproc.minEnclosingCircle(polyMOP2f.get(i), mPusat[i], mJejari);
    }
            
// Draw polygonal contours + boundingRects + circles
    for (int i = 0; i < contours.size(); i++) {
        Imgproc.drawContours(image3, polyMOP, i, green, 1);
        Core.rectangle(image3, boundRect[i].tl(), boundRect[i].br(), green, 2);
        Core.circle(image3, mPusat[i], (int)mJejari[i], green, 3);
    }

/// End of Code
            

I tried to run the program but it came up error with the exception java.lang.NullPointerException

Then I tried to modify the Imgproc.minEnclosingCircle() method a bit like this:

...
    Imgproc.minEnclosingCircle(polyMOP2f.get(i), mPusat[i], tempJejari);
    mJejari[i] = tempJejari[0];
...

but it turns out failed too.

My question is: it looks like the Imgproc.minEnclosingCircle() method requires the radius (in my code it's mJejari) to be inform of float[] array, but the Core.circle() method requires the radius to be in integer. So is there any way to manipulate the Imgproc.minEnclosingCircle() method to satisfy the Core.circle() method? Thanks before.

PS: some sources I used for the code: http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html

How to convert MatOfPoint to MatOfPoint2f in opencv java api

Community
  • 1
  • 1
Affan Widyan
  • 77
  • 1
  • 10

0 Answers0