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这位大神的无私奉献,开源的代码,让我省去了很多事,但是就光系统环境的配置就花去了我将近一个星期的时间,真是不容易 ...
随机推荐
- 版本名称SNAPSHOT、alpha、beta、release、GA含义
Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只有测试人员使用.Beta:也是测试版,这个阶段的版本会一直加入新的功能.在Alpha版之后推出.RC:(Release Candida ...
- Spring Boot2.0之多数据源分布式事务问题
分布式事务解决方案的问题, 分布式事务产生的原因: 多个不同的服务连接不同的数据源 ,做分布式事务的管理. 这种情况是连接两个数据源的情况,然后事务管理器是这样的 只管理了test02的这端业务代码. ...
- python 3 - 写一个自动生成密码文件的程序
1.你输入几,文件里面就给你产生多少条密码 2.密码必须包括,大写字母.小写字母.数字.特殊字符 3.密码不能重复 4.密码都是随机产生的 5.密码长度6-11 import string,rando ...
- c# 实现WebSocket
用C# ASP.NET MVC 实现WebSocket ,对于WebSocket想必都很了解了,不多说. 东西做的很粗糙 只能实现基本的聊天功能,不过基本的通信实现了,那么后序的扩展应该也不难(个人这 ...
- L90
On Motes and Beams 微尘与栋梁 It is curious that our own offenses should seem so much less heinous than t ...
- next enum in swift
enum Iter: Int{ case s1=0, s2, s3, s4 mutating func next(){ if self == .s4 { self = .s1 return } sel ...
- Nginx均衡负载配置
前言:Nginx也是一种服务器,反向代理服务器.单一tomcat能承受的并发访问量在150-200之间,还是在比较理想的情况下,当并发量超出这个范围,便需要Nginx实现多个tomcat的均衡负载,但 ...
- asteris录音设置
[macro-recording] exten =>s,1,Set(CALLFILENAME=${STRFTIME(${EPOCH},UTC-8,%Y-%m-%d-%H-%M-%S)}-${CA ...
- Stop logging "internal dummy connection" in Apache
Apache 2.x keeps child processes alive by creating internal connections which appear in the log file ...
- P2766 [网络流24题]最长不下降子序列问题
ha~ «问题描述: 给定正整数序列$x_1,...,x_n$ .$n<=500$ 求(1)计算其最长不下降子序列的长度$s$. (2)计算从给定的序列中最多可取出多少个长度为$s$的不下降子序 ...