在本章中,我们将学习如何使用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); 代码:
 package com.gitee.dgw.Camera;

 import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture; import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster; public class CameraSnapshotJavaFX extends Application { static {
platformUtils.loadLibraries();
}
Mat matrix = null; @Override
public void start(Stage stage) {
// Capturing the snapshot from the camera
CameraSnapshotJavaFX obj = new CameraSnapshotJavaFX();
WritableImage writableImage = obj.capureSnapShot(); // Saving the image
obj.saveImage(); // Setting the image view
ImageView imageView = new ImageView(writableImage); // setting the fit height and width of the image view
imageView.setFitHeight(400);
imageView.setFitWidth(600); // Setting the preserve ratio of the image view
imageView.setPreserveRatio(true); // Creating a Group object
Group root = new Group(imageView); // Creating a scene object
Scene scene = new Scene(root, 600, 400); // Setting title to the Stage
stage.setTitle("Capturing an image"); // Adding scene to the stage
stage.setScene(scene); // Displaying the contents of the stage
stage.show();
}
public WritableImage capureSnapShot() {
WritableImage WritableImage = null; // Instantiating the VideoCapture class (camera:: 0)
VideoCapture capture = new VideoCapture(0); // Reading the next video frame from the camera
Mat matrix = new Mat();
capture.read(matrix); // If camera is opened
if( capture.isOpened()) {
// If there is next video frame
if (capture.read(matrix)) {
// Creating BuffredImage from the matrix
BufferedImage image = new BufferedImage(matrix.width(),
matrix.height(), BufferedImage.TYPE_3BYTE_BGR); WritableRaster raster = image.getRaster();
DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
byte[] data = dataBuffer.getData();
matrix.get(0, 0, data);
this.matrix = matrix; // Creating the Writable Image
WritableImage = SwingFXUtils.toFXImage(image, null);
}
}
return WritableImage;
}
public void saveImage() {
// Saving the Image
String file = "z://sanpshot.jpg"; // Instantiating the imgcodecs class
Imgcodecs imageCodecs = new Imgcodecs(); // Saving it again
imageCodecs.imwrite(file, matrix);
}
public static void main(String args[]) {
launch(args);
}
}

Opencv级联分类器实现人脸识别的更多相关文章

  1. OpenCV中基于Haar特征和级联分类器的人脸检测

    使用机器学习的方法进行人脸检测的第一步需要训练人脸分类器,这是一个耗时耗力的过程,需要收集大量的正负样本,并且样本质量的好坏对结果影响巨大,如果样本没有处理好,再优秀的机器学习分类算法都是零. 今年3 ...

  2. OpenCV学习记录(二):自己训练haar特征的adaboost分类器进行人脸识别 标签: 脸部识别opencv 2017-07-03 21:38 26人阅读

    上一篇文章中介绍了如何使用OpenCV自带的haar分类器进行人脸识别(点我打开). 这次我试着自己去训练一个haar分类器,前后花了两天,最后总算是训练完了.不过效果并不是特别理想,由于我是在自己的 ...

  3. OpenCV使用级联分类器实现人脸检测

    一.概述 案例:使用opencv级联分类器CascadeClassifier+其提供的特征数据实现人脸检测,检测到人脸后使用红框画出来. API介绍:detectMultiScale( InputAr ...

  4. 利用opencv中的级联分类器进行人脸检測-opencv学习(1)

    OpenCV支持的目标检測的方法是利用样本的Haar特征进行的分类器训练,得到的级联boosted分类器(Cascade Classification).注意,新版本号的C++接口除了Haar特征以外 ...

  5. Opencv——级联分类器(AdaBoost)

    API说明: cv::CascadeClassifier::detectMultiScale(InputArray image,//输入灰度图像 CV_OUT std::vector<Rect& ...

  6. 使用OpenCV和Python进行人脸识别

    介绍 人脸识别是什么?或识别是什么?当你看到一个苹果时,你的大脑会立刻告诉你这是一个苹果.在这个过程中,你的大脑告诉你这是一个苹果水果,用简单的语言来说就是识别.那么什么是人脸识别呢?我肯定你猜对了. ...

  7. OpenCV学习 物体检测 人脸识别 填充颜色

    介绍 OpenCV是开源计算机视觉和机器学习库.包含成千上万优化过的算法.项目地址:http://opencv.org/about.html.官方文档:http://docs.opencv.org/m ...

  8. OpenCV——级联分类器(CascadeClassifier)

    级联分类器的计算特征值的基础类FeatureEvaluator 功能:读操作read.复制clone.获得特征类型getFeatureType,分配图片分配窗口的操作setImage.setWindo ...

  9. python与opencv的结合之人脸识别值

    首先还是要感谢http://www.jb51.net/article/67392.htm这位大神的无私奉献,开源的代码,让我省去了很多事,但是就光系统环境的配置就花去了我将近一个星期的时间,真是不容易 ...

随机推荐

  1. C/C++连接查询MYSQL

    1. [代码][C/C++]代码 #include <mysql/mysql.h>#include <stdio.h>#include <string.h>int ...

  2. linux应用之Lamp(apache+mysql+php)的源码安装(centos)

    Linux+Apache+Mysql+Php源码安装 一.安装环境: 系统:Centos6.5x64 Apache: httpd-2.4.10.tar.gz Mysql: mysql-5.6.20-l ...

  3. dmidecode 命令

    dmidecode                                                 #  查看全面硬件信息dmidecode | grep "Product ...

  4. hdu3739 Anti LIS[最小割]

    长度为 n≤1000 的数列 ai,其中最长上升子序列的长度为 s.至少删去多少数使得最长上升子序列的长度小于 s. 其实这题和那个求有多少不重叠LIS是一样答案的. 先放个图. 图丑别说我. 原网络 ...

  5. 洛谷 1979 华容道——最短路+dp

    题目:https://www.luogu.org/problemnew/show/P1979 感到无从下手.但不妨用dp的角度来看.因为空格只有在指定棋子的旁边才有用,所以状态记成制定棋子的位置与空格 ...

  6. java计算两个时间相差(天、小时、分钟、秒)

    public static Long dateDiff(String startTime, String endTime, String format, String str) { // 按照传入的格 ...

  7. NSCLIENT++可以采集的指标

    Documentation   Information   Commands/Modules   CheckDisk   CheckFileSize   CheckDriveSize   CheckF ...

  8. 【转】Pro Android学习笔记(九):了解Content Provider(下下)

    Content provider作为信息的读出,比较常见的还有文件的读写,最基础的就是二进制文件的的读写,例如img文件,音频文件的读写.在数据库中存放了该文件的路径,我们可以通过ContentPro ...

  9. Linux命令总结_touch创建文件

    1.touch命令,用来创建文件或者修改文件时间戳 格式:touch [选项]... 文件... 选项 : -a   或--time=atime或--time=access或--time=use  只 ...

  10. CentOS快速搭建LAMP环境

    LAMP --  Linux Apache MySQL PHP 在CentOS安装的顺序,我一般是Apache -> MySQL -> PHP 第一步.安装并配置Apache 1.使用yu ...