如何复制由三次曲线包围的图像区域

时间:2016-06-01 22:54:13

标签: image-processing java-2d

我有一个与Inkscape中的Bezier工具非常相似的Java程序。我的程序的目的是允许用户使用曲线绘制对象周围的路径(如人的头部),然后提取(复制)封闭曲线内的像素。在附图中,您可以看到有3条蓝色曲线形成一个封闭区域。我想知道如何复制这3条曲线所包围的区域? enter image description here

我用来绘制曲线的代码(为了简单起见,我省略了红色切线和红色控制点):

        cubicCurve1 = new CubicCurve2D.Double(
            p1.x, p1.y, 
            p1Control1.x, p1Control1.y,
            p2Control1.x, p2Control1.y,
            p2.x, p2.y);

        cubicCurve2 = new CubicCurve2D.Double(
            p2.x, p2.y, 
            p2Control2.x, p2Control2.y,
            p3Control1.x, p3Control1.y,
            p3.x, p3.y);

        cubicCurve3 = new CubicCurve2D.Double(
                p3.x, p3.y, 
                p3Control2.x, p3Control2.y,
                p1Control1.x, p1Control1.y,
                p1.x, p1.y);

        g2D.setPaint(Color.BLUE);
    g2D.draw(cubicCurve1);    
    g2D.draw(cubicCurve2);
    g2D.draw(cubicCurve3);

1 个答案:

答案 0 :(得分:1)

以下是我解决问题的方法:

        GeneralPath shape = new GeneralPath();
        shape.moveTo(cubicCurve1.x1, cubicCurve1.y1);
        shape.curveTo(cubicCurve1.ctrlx1, cubicCurve1.ctrly1, cubicCurve1.ctrlx2, cubicCurve1.ctrly2, cubicCurve1.x2, cubicCurve1.y2);
        shape.curveTo(cubicCurve2.ctrlx1, cubicCurve2.ctrly1, cubicCurve2.ctrlx2, cubicCurve2.ctrly2, cubicCurve2.x2, cubicCurve2.y2);
        shape.curveTo(cubicCurve3.ctrlx1, cubicCurve3.ctrly1, cubicCurve3.ctrlx2, cubicCurve3.ctrly2, cubicCurve1.x1, cubicCurve1.y1);
        g2D.draw(shape);
        g2D.setClip(shape);

        // Draw an image

        g2D.drawImage(image, 0, 0, this);