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); 代码:
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级联分类器实现人脸识别的更多相关文章
- OpenCV中基于Haar特征和级联分类器的人脸检测
使用机器学习的方法进行人脸检测的第一步需要训练人脸分类器,这是一个耗时耗力的过程,需要收集大量的正负样本,并且样本质量的好坏对结果影响巨大,如果样本没有处理好,再优秀的机器学习分类算法都是零. 今年3 ...
- OpenCV学习记录(二):自己训练haar特征的adaboost分类器进行人脸识别 标签: 脸部识别opencv 2017-07-03 21:38 26人阅读
上一篇文章中介绍了如何使用OpenCV自带的haar分类器进行人脸识别(点我打开). 这次我试着自己去训练一个haar分类器,前后花了两天,最后总算是训练完了.不过效果并不是特别理想,由于我是在自己的 ...
- OpenCV使用级联分类器实现人脸检测
一.概述 案例:使用opencv级联分类器CascadeClassifier+其提供的特征数据实现人脸检测,检测到人脸后使用红框画出来. API介绍:detectMultiScale( InputAr ...
- 利用opencv中的级联分类器进行人脸检測-opencv学习(1)
OpenCV支持的目标检測的方法是利用样本的Haar特征进行的分类器训练,得到的级联boosted分类器(Cascade Classification).注意,新版本号的C++接口除了Haar特征以外 ...
- Opencv——级联分类器(AdaBoost)
API说明: cv::CascadeClassifier::detectMultiScale(InputArray image,//输入灰度图像 CV_OUT std::vector<Rect& ...
- 使用OpenCV和Python进行人脸识别
介绍 人脸识别是什么?或识别是什么?当你看到一个苹果时,你的大脑会立刻告诉你这是一个苹果.在这个过程中,你的大脑告诉你这是一个苹果水果,用简单的语言来说就是识别.那么什么是人脸识别呢?我肯定你猜对了. ...
- OpenCV学习 物体检测 人脸识别 填充颜色
介绍 OpenCV是开源计算机视觉和机器学习库.包含成千上万优化过的算法.项目地址:http://opencv.org/about.html.官方文档:http://docs.opencv.org/m ...
- OpenCV——级联分类器(CascadeClassifier)
级联分类器的计算特征值的基础类FeatureEvaluator 功能:读操作read.复制clone.获得特征类型getFeatureType,分配图片分配窗口的操作setImage.setWindo ...
- python与opencv的结合之人脸识别值
首先还是要感谢http://www.jb51.net/article/67392.htm这位大神的无私奉献,开源的代码,让我省去了很多事,但是就光系统环境的配置就花去了我将近一个星期的时间,真是不容易 ...
随机推荐
- LVS与Keepalived
lvs与Nginx区别 LVS的负载能力强,因为其工作方式逻辑非常简单,仅进行请求分发,而且工作在网络的第4层,没有流量,所以其效率不需要有过多的忧虑. LVS基本能支持所有应用,因为工作在第4层,所 ...
- Spring Boot2.0之整合事物管理
首先Spring 事务分类 1.声明事务 原理:基于编程事务的 2.编程事务 指定范围 扫包去解决 3.事务原理:AOP技术 通过环绕通知进行了拦截 使用Spring 事务注意事项: 不要tr ...
- The Contiki build system
The Contiki build system http://contiki.sourceforge.net/docs/2.6/a01796.html 先看官方文档的说明,对contiki的构建系统 ...
- 强制浏览器下载PDF文件
if(empty($filename)) { return FALSE; } // http headers header('Content-Type: application-x/force-dow ...
- IntelliJ IDEA 的 project 和 module 区别与关系
在IDEA 创建一个project,目录结构是这样的:在project下创建一个module之后目录结构是这样的: 简单的概括如下: IntelliJ系中的 Project 相当于Eclipse系中 ...
- Linux下查找进程,kill进程
1. ps命令用来查找linux运行的进程,常用命令: ps aux | grep 进程名: eg:ps aux | grep admin 查找admin的进程 或者 ps -ef | grep j ...
- listen 74
Genes Link Touch and Hearing Sound and touch may seem completely separate, except possibly when play ...
- python程序打包工具 ── cx_Freeze
cx_Freeze是一个类似py2exe的工具,它们区别是py2exe是将python程序打包成windows下可以执行的exe文件的,而cx_Freeze则是将python程序打包为linux下可以 ...
- CodeForces - 311B:Cats Transport (DP+斜率优化)
Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight r ...
- POJ2657Comfort(扩展欧几里得基础)
A game-board consists of N fields placed around a circle. Fields are successively numbered from1 to ...