Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

java is somewhat clumsy here, but it's just a few lines:

mask = Mat.zeros(400,400,CvType.CV_8U); // all black

// your RotatedRect will come from minAreaRect(), this is just demo data !
RotatedRect rc = new RotatedRect(Point(200,200), new Size(200,100), 23);

// retrieve corner points:
Point[] p = Point[4];
rc.points(p);

// draw white, filled poly:
MatOfPoint mp = new MatOfPoint(p);
Imgproc.fillConvexPoly(mask, mp, new Scalar(255));

image description

java is somewhat clumsy here, but it's just a few lines:

Mat mask = Mat.zeros(400,400,CvType.CV_8U); // all black

// your RotatedRect will come from minAreaRect(), this is just demo data !
RotatedRect rc = new RotatedRect(Point(200,200), new Size(200,100), 23);

// retrieve corner points:
Point[] p = Point[4];
rc.points(p);

// draw white, filled poly:
MatOfPoint mp = new MatOfPoint(p);
Imgproc.fillConvexPoly(mask, mp, new Scalar(255));

image description

java is somewhat clumsy here, but it's just a few lines:

Mat mask = Mat.zeros(400,400,CvType.CV_8U); // all black

// your RotatedRect will come from minAreaRect(), this is just demo data !
RotatedRect rc = new RotatedRect(Point(200,200), new Size(200,100), 23);

// retrieve corner points:
Point[] p = Point[4];
rc.points(p);

// draw white, filled poly:
MatOfPoint mp = new MatOfPoint(p);
Imgproc.fillConvexPoly(mask, mp, new Scalar(255));

image description

[edit]

  • usually you need a contour as input for minAreaRect(), see tutorial and some conversion code (sorry, untested):

    List<MatOfPoint> contours = new ArrayList<>();
    Imgproc.findContours(cannyOutput, contours, ...);
    Point [] pts = contours.get(i).toArray();
    RotatedRect rc = Imgproc.minAreaRect(new MatOfPoint2f(pts));
    
  • maybe you can use the points from findNonZero(), too, but to use that you need MatOfPoint (not 2f)

    MatOfPoint Points = new MatOfPoint();
    Core.findNonZero(paper_mat,Points);
    RotatedRect rc = Imgproc.minAreaRect(new MatOfPoint2f(Points.toArray()));
    
  • sorry, i was just trying out latest 4.0, where all drawing stuff was moved to Imgproc for consistency, but yes, it's likely in Core with your version

java is somewhat clumsy here, but it's just a few lines:

Mat mask = Mat.zeros(400,400,CvType.CV_8U); // all black

// your RotatedRect will come from minAreaRect(), this is just demo data !
RotatedRect rc = new RotatedRect(Point(200,200), new Size(200,100), 23);

// retrieve corner points:
Point[] p = Point[4];
rc.points(p);

// draw white, filled poly:
MatOfPoint mp = new MatOfPoint(p);
Imgproc.fillConvexPoly(mask, mp, new Scalar(255));

image description

[edit]

  • usually you need a contour as input for minAreaRect(), see tutorial and some conversion code (sorry, untested):

    List<MatOfPoint> contours = new ArrayList<>();
    Imgproc.findContours(cannyOutput, contours, ...);
    Point [] pts = contours.get(i).toArray();
    RotatedRect rc = Imgproc.minAreaRect(new MatOfPoint2f(pts));
    
  • maybe you can use the points from findNonZero(), too, but to use that you need MatOfPoint (not 2f)

    MatOfPoint Points = new MatOfPoint();
    Core.findNonZero(paper_mat,Points);
    RotatedRect rc = Imgproc.minAreaRect(new MatOfPoint2f(Points.toArray()));
    
  • sorry, i was just trying out latest 4.0, where all drawing stuff was moved to Imgproc for consistency, but yes, it's likely in Core with your version