zoukankan      html  css  js  c++  java
  • 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 }





  • 相关阅读:
    PAT1037:Magic Coupon
    PAT1081:Rational Sum
    PAT1039: Course List for Student
    PAT1069:The Black Hole of Numbers
    VC++中字符串编码处理的一些相关问题
    PAT1110:Complete Binary Tree
    Java编译器003---javac -d/-sourcepath/-classpath选项
    Java编译器002---javac -source/-target选项
    Java编译器001---javac -g选项
    力扣练习010---把字符串转换成整数
  • 原文地址:https://www.cnblogs.com/dgwblog/p/9440160.html
Copyright © 2011-2022 走看看