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这位大神的无私奉献,开源的代码,让我省去了很多事,但是就光系统环境的配置就花去了我将近一个星期的时间,真是不容易 ...
随机推荐
- codewar代码练习1——8级晋升7级
最近发现一个不错的代码练习网站codewar(http://www.codewars.com).注册了一个账号,花了几天的茶余饭后时间做题,把等级从8级升到了7级.本文的目的主要介绍使用感受及相应题目 ...
- ACM学习历程——UVA540 Team Queue(队列,map:Hash)
Description Team Queue Team Queue Queues and Priority Queues are data structures which are know ...
- MySQL整体架构与内存结构
一 mysql 整体框架: MySQL是由SQL接口,解析器,优化器,缓存,存储引擎等组成的. 1. Connectors指的是不同语言中与SQL的交互. 2. Management Serveic ...
- 单机 Oracle 11g(11.2.0.4)手动打补丁PSU(11.2.0.4.8)
环境说明:database : 11.2.0.4 x64os: centos6.7 x64 准备内容:OPatch : p6880880_112000_Linux-x86-64.zipDB PSU : ...
- Spring boot 学习 五:domain的定义
一 public class City implements Serializable 实现了Sericalizable接口,只是一种标志.表示可以被序列化. java的ObjectOutputStr ...
- JS---Math.Random()*10--[0,10)随机变颜色
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Python 查看本机WiFi密码
http://www.lijiejie.com/python-get-all-saved-wifi-passwords/ 很早以前我写过一个,丢了. 今天偶然看到这篇文章 , 也是很久以前写的,他用 ...
- 卸载 Ubuntu gnome 自带的 Videos, Browser, Document Viewer等
卸载命令 # uninstall Browser sudo apt-get remove --purge webbrowser-app # uninstall Videos sudo apt-get ...
- 怎么解决sublime的插件自动被禁用
前两天,我的sublime text安装的很多插件都被自动禁用了,每次都要我自己重新启用一下才可以,后来从网上找到了解决方法. 找到“设置”-“Package Settings”-“Package C ...
- 【机器学习】迭代决策树GBRT(渐进梯度回归树)
一.决策树模型组合 单决策树C4.5由于功能太简单,并且非常容易出现过拟合的现象,于是引申出了许多变种决策树,就是将单决策树进行模型组合,形成多决策树,比较典型的就是迭代决策树GBRT和随机森林RF. ...