 
- OpenCV 教程
- OpenCV - 主页
- OpenCV - 概述
- OpenCV - 环境
- OpenCV - 存储图像
- OpenCV - 读取图像
- OpenCV - 写入图像
- OpenCV-图形用户界面
- 绘图功能
- OpenCV - 画一个圆
- OpenCV - 画一条线
- OpenCV - 绘制矩形
- OpenCV - 绘制椭圆
- OpenCV - 绘制折线
- OpenCV - 绘制凸折线
- OpenCV - 绘制箭头线
- OpenCV - 添加文本
- 过滤
- OpenCV - 双边滤波器
- OpenCV - 盒式过滤器
- OpenCV - SQRBox 滤波器
- OpenCV - Filter2D
- OpenCV - 膨胀
- OpenCV - 侵蚀
- OpenCV - 形态运算
- OpenCV - 图像Pyramid
- 摄像头和人脸检测
- OpenCV - 使用相机
- OpenCV - 图片中的人脸检测
- 使用相机进行人脸检测
- OpenCV 有用资源
- OpenCV - 快速指南
- OpenCV - 有用的资源
- OpenCV - 讨论
OpenCV - 图片中的人脸检测
org.opencv.videoio包的 VideoCapture 类包含使用系统摄像头捕获视频的类和方法。让我们一步一步来学习如何做。
第1步:加载OpenCV原生库
使用 OpenCV 库编写 Java 代码时,您需要做的第一步是使用 loadLibrary ()加载 OpenCV 的本机库。加载 OpenCV 本机库,如下所示。
// Loading the core library System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
步骤 2:实例化 CascadeClassifier 类
org.opencv.objdetect包中的CascadeClassifier类用于加载分类器文件。通过传递xml文件lbpcascade_frontalface.xml来实例化此类,如下所示。
// Instantiating the CascadeClassifier String xmlFile = "E:/OpenCV/facedetect/lbpcascade_frontalface.xml"; CascadeClassifier classifier = new CascadeClassifier(xmlFile);
第三步:检测人脸
您可以使用名为CascadeClassifier的类的detectorMultiScale()方法来检测图像中的面部。此方法接受保存输入图像的Mat类的对象和存储检测到的人脸的MatOfRect类的对象。
// Detecting the face in the snap MatOfRect faceDetections = new MatOfRect(); classifier.detectMultiScale(src, faceDetections);
例子
以下程序演示了如何检测图像中的人脸。
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
  
public class FaceDetectionImage {
   public static void main (String[] args) {
      // Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      // Reading the Image from the file and storing it in to a Matrix object
      String file ="E:/OpenCV/chap23/facedetection_input.jpg";
      Mat src = Imgcodecs.imread(file);
      // Instantiating the CascadeClassifier
      String xmlFile = "E:/OpenCV/facedetect/lbpcascade_frontalface.xml";
      CascadeClassifier classifier = new CascadeClassifier(xmlFile);
      // Detecting the face in the snap
      MatOfRect faceDetections = new MatOfRect();
      classifier.detectMultiScale(src, faceDetections);
      System.out.println(String.format("Detected %s faces", 
         faceDetections.toArray().length));
      // Drawing boxes
      for (Rect rect : faceDetections.toArray()) {
         Imgproc.rectangle(
            src,                                               // where to draw the box
            new Point(rect.x, rect.y),                            // bottom left
            new Point(rect.x + rect.width, rect.y + rect.height), // top right
            new Scalar(0, 0, 255),
            3                                                     // RGB colour
         );
      }
      // Writing the image
      Imgcodecs.imwrite("E:/OpenCV/chap23/facedetect_output1.jpg", src);
      System.out.println("Image Processed");
   }
}
假设以下是上述程序中指定的输入图像facedetection_input.jpg 。
 
输出
执行程序时,您将得到以下输出 -
Detected 3 faces Image Processed
如果打开指定的路径,您可以观察输出图像,如下所示 -
