如何获取和打印.curveTo坐标?

时间:2015-06-23 15:14:22

标签: java coordinates bezier

我使用.curveTo方法绘制带Bezier曲线的曲线。现在,我想用Hausdorff距离法计算像素坐标之间的距离。我手动设置起点,控制点和终点。如何获取并打印曲线通过的所有(x,y)坐标?至少,对于右眉毛或左眉毛。

这是我的源代码:

package emodetection;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Path2D;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Bezier extends JPanel {

private static BufferedImage bi;
private static int w;
private static int h;

public Bezier() {
    ImageIcon ii = new ImageIcon("images/output.jpg");
    Image img = ii.getImage();
    int imageWidth = img.getWidth(this);
    int imageHeight = img.getHeight(this);
    w = imageWidth/2;
    h = imageHeight/2;

    bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics g = bi.createGraphics();
    g.drawImage(img, 0, 0, (int) w, (int) h, this);
    ImageIcon newii = new ImageIcon(bi);

    System.out.println("img height is " + img.getHeight(this));
    JLabel imageLabel = new JLabel("", newii, JLabel.RIGHT);
    super.add(imageLabel, JPanel.RIGHT_ALIGNMENT);
}

// Draw curve line in an image
public void paintComponent (Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = bi.createGraphics();

    ImageIcon ii = new ImageIcon("image/output.jpg");
    JLabel imageLabel = new JLabel("Face", ii, JLabel.CENTER);
    JPanel jp = new JPanel(new BorderLayout());
    jp.add(imageLabel, BorderLayout.CENTER);

    g2.setColor(Color.BLUE);

    // Right eyebrow
    Path2D rightEB = new Path2D.Double();
    rightEB.moveTo(80/2, 260/2);
    rightEB.curveTo(79/2, 169/2, 316/2, 171/2, 314/2, 196/2);
    g2.draw(rightEB);

    // Left eyebrow
    Path2D leftEB = new Path2D.Double();
    leftEB.moveTo(698/2, 227/2);
    leftEB.curveTo(702/2, 154/2, 465/2, 155/2, 467/2, 184/2);
    g2.draw(leftEB);

    // Right eye
    Path2D re1 = new Path2D.Double();
    re1.moveTo(156/2, 312/2);
    re1.curveTo(156/2, 278/2, 313/2, 277/2, 318/2, 321/2);
    g2.draw(re1);

    Path2D re2 = new Path2D.Double();
    re2.moveTo(156/2, 312/2);
    re2.curveTo(156/2, 342/2, 319/2, 338/2, 318/2, 321/2);
    g2.draw(re2);

    // Left eye
    Path2D le1 = new Path2D.Double();
    le1.moveTo(481/2, 310/2);
    le1.curveTo(480/2, 265/2, 642/2, 264/2, 642/2, 300/2);
    g2.draw(le1);

    Path2D le2 = new Path2D.Double();
    le2.moveTo(481/2, 310/2);
    le2.curveTo(481/2, 336/2, 642/2, 337/2, 642/2, 300/2);
    g2.draw(le2);

    // Lip
    // Upper lip
    Path2D m1 = new Path2D.Double();
    m1.moveTo(238/2, 657/2);
    m1.curveTo(238/2, 626/2, 577/2, 626/2, 577/2, 647/2);
    System.out.println(m1.getPathIterator(null));
    g2.draw(m1);
    // Lower lip
    Path2D m2 = new Path2D.Double();
    m2.moveTo(238/2, 657/2);
    m2.curveTo(238/2, 788/2, 579/2, 788/2, 577/2, 647/2);
    g2.draw(m2);

}

public static void main(String[] args) {
    JFrame frame = new JFrame("Testing Bezier Curve");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Bezier bezier = new Bezier();
    frame.add(bezier);
    frame.setSize(w*3, h);
    frame.setVisible(true);
}

}

我非常感谢所提供的任何帮助。

0 个答案:

没有答案