OpenCV - Ellipse完全没有显示

时间:2017-02-23 09:32:28

标签: java opencv

我想制作一个用于裁剪图像的椭圆蒙版,以便只显示椭圆内的内容。

你可以查看我的代码吗?

public static Mat cropImage(Mat imageOrig, MatOfPoint contour){
    Rect rect = Imgproc.boundingRect(contour);

    MatOfPoint2f contour2f = new MatOfPoint2f(contour.toArray());
    RotatedRect boundElps = Imgproc.fitEllipse(contour2f);

    Mat out = imageOrig.submat(rect);

    // the line function is working
    Imgproc.line(out, new Point(0,0), new Point(out.width(), out.height()), new Scalar(0,0,255), 5);

    // but not this one
    Imgproc.ellipse(out, boundElps, new Scalar(255, 0, 0), 99);

    return out;
}//cropImage

好像根本不起作用。虽然你可以看到我已经完成的线功能测试它是否正在处理正确的图像并且我可以看到一条线但是没有椭圆。

以下是cropImage函数的示例输出。

cropImage's Output

TIA

3 个答案:

答案 0 :(得分:1)

您正在检索imageOrig坐标系中的椭圆坐标,但您要在裁剪的out图像上显示椭圆坐标。

如果要在裁剪上显示椭圆,则需要将椭圆中心转换为裁剪引入的平移(rect的左上角坐标),例如:

boundElps.center().x -= rect.x; boundElps.center().y -= rect.y;

答案 1 :(得分:0)

你可以尝试一下:

RotatedRect rRect = Imgproc.minAreaRect(contour2f);
Imgproc.ellipse(out, rRect , new Scalar(255, 0, 0), 3);

答案 2 :(得分:0)

您应该检查使用fitEllipse的最低要求,如this post所示。 函数fitEllipse至少需要5分。 注意:虽然我提到的引用是针对Python的,但我希望你能为Java做同样的事情。

for cnt in contours:
    area = cv2.contourArea(cnt)
    # Probably this can help but not required 
    if area < 2000 or area > 4000:
         continue
    # This is the check I'm referring to
    if len(cnt) < 5:
         continue
    ellipse = cv2.fitEllipse(cnt)
    cv2.ellipse(roi, ellipse, (0, 255, 0), 2)

希望它有所帮助!