opencv如何在java中绘制minAreaRect

时间:2014-04-27 18:48:11

标签: java android opencv

您好我正在尝试从minAreaRect中绘制旋转的rect,但我只在python中找到代码。

rect = cv2.minAreaRect(cnt)
box = cv2.cv.BoxPoints(rect)
box = np.int0(box)
cv2.drawContours(im,[box],0,(0,0,255),2)

如何在java中绘制它?

4 个答案:

答案 0 :(得分:3)

我知道这个问题已经过时了,但最近我遇到了同样的问题所以我决定在这里发布答案。

不幸的是,我在Android上使用的OpenCV版本没有支持rectangle作为参数的方法RotatedRect。所以我不得不即兴发挥。

    Point points[] = new Point[4];
    rect.points(points);
    for(int i=0; i<4; ++i){
        Core.line(init, points[i], points[(i+1)%4], new Scalar(255,255,255));
    }

答案 1 :(得分:2)

我使用如下代码从minAreaRect中绘制旋转的rect:

rRect = Imgproc.minAreaRect(mop2f);

        Point[] vertices = new Point[4];  
        rRect.points(vertices);  
        for (int j = 0; j < 4; j++){  
            Imgproc.line(mat, vertices[j], vertices[(j+1)%4], new Scalar(0,255,0));
        }

答案 2 :(得分:2)

您可以像这样使用drawContours

Point[] vertices = new Point[4];
rotatedRect.points(vertices);
List<MatOfPoint> boxContours = new ArrayList<>();
boxContours.add(new MatOfPoint(vertices));
Imgproc.drawContours(out, boxContours, 0, new Scalar(128, 128, 128), -1);

使用此方法,您可以绘制轮廓并用纯色填充(如果使用Imgproc.line则无法执行此操作。)

答案 3 :(得分:0)

这样的事情:

MatOfPoint2f points = new MatOfPoint2f(new Point(1, 1), new Point(5, 1), new Point(4, 3), new Point(6, 2));
RotatedRect rrect = Imgproc.minAreaRect(points);