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

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

第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步:读取帧

您可以使用VideoCapture类的read()方法从相机中读取帧。此方法接受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 }

 






转载于:https://www.cnblogs.com/dgwblog/p/9440160.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值