OpenCV.图形绘制.直线

直线绘制

在OpenCV环境中,直线的绘制依赖于Imgproc.line() 这个方法。该方法的一个构造方法如下:
line(img, pt1, pt2, color);

其参数解释如下:

  • mat
    表示要在其上绘制线条的图像的Mat对象。
  • pt1,pt2
    表示要绘制线条的点的两个点对象。
  • scalar
    表示圆的颜色的标量对象(BGR)。

当然了,还可以控制直线的粗细,即上述构造方法最后追加一个参数,thickness来表示粗细,厚度值为1.

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);

		imageView.setImage(drawLine(file.getPath()));

    }


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

        Mat src = Imgcodecs.imread(filePath);

        // Draw red, green blue and white line in src.
        Imgproc.line(src, new Point(117,120), new Point(300,120), new Scalar(0,0,255));
        Imgproc.line(src, new Point(117,130), new Point(300,130), new Scalar(0,255,0));
        Imgproc.line(src, new Point(117,140), new Point(300,140), new Scalar(255,0,0));
        Imgproc.line(src, new Point(117,150), new Point(300,150), new Scalar(255,255,255));

        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进行的操作大部分都是比较耗时的,在主线程里进行处理比较棘手,因此最佳方法是在子线程处理数据而后在回到主线程更新UI。下面是JavaFX的操作示例示例。

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());

    }

	// Consumer time task and  return a WriteableImage for JavaFX platform.
    private WritableImage drawLine(String filePath) throws IOException {
    
    
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread(filePath);

        // Draw red, green blue and white line in src.
        Imgproc.line(src, new Point(117,120), new Point(300,120), new Scalar(0,0,255));
        Imgproc.line(src, new Point(117,130), new Point(300,130), new Scalar(0,255,0));
        Imgproc.line(src, new Point(117,140), new Point(300,140), new Scalar(255,0,0));
        Imgproc.line(src, new Point(117,150), new Point(300,150), new Scalar(255,255,255));

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

	// Process CV task in sub thread and update View in main thread.
    private void runInSubThread(String filePath){
    
    
        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                try {
    
    
                    WritableImage writableImage = drawLine(filePath);

					// Switch to main thread, JavaFX's features.
                    Platform.runLater(new Runnable() {
    
    
                        @Override
                        public void run() {
    
    
                            imageView.setImage(writableImage);
                        }
                    });

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

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

猜你喜欢

转载自blog.csdn.net/kicinio/article/details/120894435