OpenCV.图形绘制.折线

这篇博客介绍了如何在JavaFX环境中利用OpenCV的polylines()函数来绘制折线。通过创建MatOfPoint对象,定义点的坐标,并设置颜色和线宽,最终将绘制好的图像显示在ImageView上。代码示例展示了从文件读取图像,然后在其上绘制折线并转换为JavaFX可显示的图像。
摘要由CSDN通过智能技术生成

折线绘制

在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;
    }

}

运行图
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值