OpenCV.图形绘制.箭头

箭头绘制

在OpenCV中箭头的绘制依赖于arrowedLine() 该函数。该函数的一个重载函数如下:
Imgproc.arrowedLine(mat, pt1, pt2, color, thickness);
各参数解释如下:

  • mat
    表示要在其上绘制箭头的图像的Mat对象。

  • pt1pt2
    两个Point对象,表示要绘制的箭头的点。其中pt1系起点,pt2系箭头点。

  • color
    表示矩形颜色的标量对象(BGR)。

  • thickness
    表示厚度。

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 = drawArrow(filePath);

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

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

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

        Mat src = Imgcodecs.imread(filePath);

        Imgproc.arrowedLine(src, new Point(187,90), new Point(300,170), new Scalar(0,0,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.图形绘制.箭头

上一篇:Java设计模式之(二)——工厂模式


下一篇:下载远程图片并返回给前端