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这位大神的无私奉献,开源的代码,让我省去了很多事,但是就光系统环境的配置就花去了我将近一个星期的时间,真是不容易 ...
随机推荐
- PYTHON 爬虫笔记七:Selenium库基础用法
知识点一:Selenium库详解及其基本使用 什么是Selenium selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium ...
- JAVA- JDBC之DBHelper
package com.myit.util; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.D ...
- Object.is() Pollyfill
if (!Object.is) { Object.is = function(x, y) { // SameValue algorithm if (x === y) { // Steps 1-5, 7 ...
- Mybatis 批量删除 单引号
MySQL效果: ' AND NAME IN ('policycustom1.xmlx','policycustom.xmlx','policycustom1.xmlx','policycustom. ...
- 迁移学习——使用Tensorflow和VGG16预训模型进行预测
使用Tensorflow和VGG16预训模型进行预测 from:https://zhuanlan.zhihu.com/p/28997549 fast.ai的入门教程中使用了kaggle: dogs ...
- css书写规则
无规矩不成方圆,不管有多少人共同参与同一项目,一定要确保每一行代码都像是同一个人编写的 不要在自闭合(self-closing)元素的尾部添加斜线 不要省略可选的结束标签(closing tag)(例 ...
- bjwc Day3 & 4 妈妈我这是来了个什么地方呀
真·bjwc开始了 Day3 T1啥啥啥 第k大斜率?想都没想码了个暴力,然后爆零...暴力都能错,退役 T2看着像网络流就扔了个网络流大暴力上去,六七十分的样子然后蜜汁wa T3题面说“想都没想就弄 ...
- 如何理解 Spring 注入
先看一段代码 假设你编写了两个类,一个是人(Person),一个是手机(Mobile). 人有时候需要用手机打电话,需要用到手机的dialUp方法. 传统的写法是这样: Java code publi ...
- Digging-贪心
When it comes to the Maya Civilization, we can quickly remind of a term called the end of the world. ...
- Autofac依赖注入框架使用
简介: Autofac是一款IOC框架,比较于其他的IOC框架,如Spring.NET,Unity,Castle等等所包含的,它很轻量级性能上非常高 控制反转和依赖注入: 控制反转 IOC(Inver ...