• Opencv级联分类器实现人脸识别


    在本章中,我们将学习如何使用OpenCV使用系统相机捕获帧。org.opencv.videoioVideoCapture包含使用相机捕获视频的类和方法。让我们一步一步学习如何捕捉帧 -

    第1步:加载OpenCV本机库

    在使用OpenCV库编写Java代码时,您需要做的第一步是使用loadLibrary()加载OpenCV的本机库加载OpenCV本机库,如下所示。

    // Loading the core library 
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    

    第2步:实例化视频捕获类

    使用前面本教程中提到的任何函数来实例化Mat类。

    // Instantiating the VideoCapture class (camera:: 0) 
    VideoCapture capture = new VideoCapture(0);
    

    第3步:读取帧

    您可以使用VideoCaptureread()方法从相机中读取帧此方法接受Mat类的对象来存储读取的帧。

    // Reading the next video frame from the camera 
    Mat matrix = new Mat(); 
    capture.read(matrix);

    代码:

      1 package com.gitee.dgw.Camera;
      2 
      3 import javafx.application.Application;
      4 import javafx.embed.swing.SwingFXUtils;
      5 import javafx.scene.Group;
      6 import javafx.scene.Scene;
      7 import javafx.scene.image.ImageView;
      8 import javafx.scene.image.WritableImage;
      9 import javafx.stage.Stage;
     10 import org.opencv.core.Mat;
     11 import org.opencv.imgcodecs.Imgcodecs;
     12 import org.opencv.videoio.VideoCapture;
     13 
     14 import java.awt.image.BufferedImage;
     15 import java.awt.image.DataBufferByte;
     16 import java.awt.image.WritableRaster;
     17 
     18 public class CameraSnapshotJavaFX extends Application {
     19 
     20    static {
     21       platformUtils.loadLibraries();
     22    }
     23    Mat matrix = null;
     24 
     25    @Override
     26    public void start(Stage stage) {
     27       // Capturing the snapshot from the camera
     28       CameraSnapshotJavaFX obj = new CameraSnapshotJavaFX();
     29       WritableImage writableImage = obj.capureSnapShot();
     30 
     31       // Saving the image
     32       obj.saveImage();
     33 
     34       // Setting the image view
     35       ImageView imageView = new ImageView(writableImage);
     36 
     37       // setting the fit height and width of the image view
     38       imageView.setFitHeight(400);
     39       imageView.setFitWidth(600);
     40 
     41       // Setting the preserve ratio of the image view
     42       imageView.setPreserveRatio(true);
     43 
     44       // Creating a Group object
     45       Group root = new Group(imageView);
     46 
     47       // Creating a scene object
     48       Scene scene = new Scene(root, 600, 400);
     49 
     50       // Setting title to the Stage
     51       stage.setTitle("Capturing an image");
     52 
     53       // Adding scene to the stage
     54       stage.setScene(scene);
     55 
     56       // Displaying the contents of the stage
     57       stage.show();
     58    }
     59    public WritableImage capureSnapShot() {
     60       WritableImage WritableImage = null;
     61 
     62 
     63       // Instantiating the VideoCapture class (camera:: 0)
     64       VideoCapture capture = new VideoCapture(0);
     65 
     66       // Reading the next video frame from the camera
     67       Mat matrix = new Mat();
     68       capture.read(matrix);
     69 
     70       // If camera is opened
     71       if( capture.isOpened()) {
     72          // If there is next video frame
     73          if (capture.read(matrix)) {
     74             // Creating BuffredImage from the matrix
     75             BufferedImage image = new BufferedImage(matrix.width(),
     76                matrix.height(), BufferedImage.TYPE_3BYTE_BGR);
     77             
     78             WritableRaster raster = image.getRaster();
     79             DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
     80             byte[] data = dataBuffer.getData();
     81             matrix.get(0, 0, data);
     82             this.matrix = matrix;
     83             
     84             // Creating the Writable Image
     85             WritableImage = SwingFXUtils.toFXImage(image, null);
     86          }
     87       }
     88       return WritableImage;
     89    }
     90    public void saveImage() {
     91       // Saving the Image
     92       String file = "z://sanpshot.jpg";
     93 
     94       // Instantiating the imgcodecs class
     95       Imgcodecs imageCodecs = new Imgcodecs();
     96 
     97       // Saving it again 
     98       imageCodecs.imwrite(file, matrix);
     99    }
    100    public static void main(String args[]) {
    101       launch(args);
    102    }
    103 }





  • 相关阅读:
    [LeetCode] 394. Decode String 解码字符串
    [LeetCode] 393. UTF-8 Validation 编码验证
    [LeetCode] Is Subsequence 是子序列
    [LintCode] Maximum Gap 求最大间距
    [LintCode] Nuts & Bolts Problem 螺栓螺母问题
    [LintCode] Kth Smallest Number in Sorted Matrix 有序矩阵中第K小的数字
    [LeetCode] Perfect Rectangle 完美矩形
    LaTex Remove Left Margin 去除左边空间
    LaTex Font Size 字体大小命令
    [LintCode] Continuous Subarray Sum 连续子数组之和
  • 原文地址:https://www.cnblogs.com/dgwblog/p/9440160.html
Copyright © 2020-2023  润新知