OpenCV.图形绘制.折线

折线绘制

在OpenCV中折线的绘制依赖于polylines() 该函数。其解释如下:

  • mat
    表示要在其上绘制矩形的图像的Mat对象。
  • pts
    包含MatOfPoint类型的对象的List对象。
  • isClosed
    指定折线的布尔型类型的参数是否为关闭的。
  • color
    表示矩形颜色的标量对象(BGR),Scalar类型。
  • thickness
    表示矩形厚度的整数。

其中pts包含的类型解释如下:
pts接受一个List对象,该对象只可放入MatOfPoint类型的对象;而MatOfPoint是一组Point类型的数据。

Java代码(JavaFX Controller层)

public class Controller{

    @FXML private Text fxText;
    @FXML private ImageView imageView;

    @FXML public void handleButtonEvent(ActionEvent actionEvent) throws IOException {

        Node source = (Node) actionEvent.getSource();
        Window theStage = source.getScene().getWindow();
        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.png");
        fileChooser.getExtensionFilters().add(extFilter);
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JPG Files(*.jpg)", "*.jpg"));
        File file = fileChooser.showOpenDialog(theStage);

        runInSubThread(file.getPath());

    }

    private void runInSubThread(String filePath){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    WritableImage writableImage = drawPolylines(filePath);

                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setImage(writableImage);
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private WritableImage drawPolylines(String filePath) throws IOException {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread(filePath);

		// pts object.
        List matList = new ArrayList();
        matList.add(new MatOfPoint(
                new Point(150, 170), new Point(170,70),
                new Point(180,180), new Point(200,110),
                new Point(235,170), new Point(240,90)
        ));

        Imgproc.polylines(src, matList, false, new Scalar(0,255,255), 2);

        MatOfByte matOfByte = new MatOfByte();
        Imgcodecs.imencode(".jpg", src, matOfByte);

        byte[] bytes = matOfByte.toArray();
        InputStream in = new ByteArrayInputStream(bytes);
        BufferedImage bufImage = ImageIO.read(in);

        WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);

        return writableImage;
    }

}

运行图
OpenCV.图形绘制.折线_第1张图片

你可能感兴趣的:(OpenCV,JavaFX,1024程序员节,opencv,计算机视觉,javafx)