如何使用OpenCV和Java保存人脸和识别?

如何使用OpenCV和Java保存人脸和识别?,java,opencv,javacv,Java,Opencv,Javacv,我对OpenCV完全陌生。我能够从网络摄像头中检测到这张脸。我有点困惑如何保存检测到的人脸,如果那个人再次出现在摄像机前,如何保存和识别 检测码 private void detectAndDisplay(Mat frame) { MatOfRect faces = new MatOfRect(); Mat grayFrame = new Mat(); // convert the frame in gray scale Imgproc.cvtColor(fra

我对OpenCV完全陌生。我能够从网络摄像头中检测到这张脸。我有点困惑如何保存检测到的人脸,如果那个人再次出现在摄像机前,如何保存和识别

检测码

private void detectAndDisplay(Mat frame)
{
    MatOfRect faces = new MatOfRect();
    Mat grayFrame = new Mat();

    // convert the frame in gray scale
    Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
    // equalize the frame histogram to improve the result
    Imgproc.equalizeHist(grayFrame, grayFrame);

    // compute minimum face size (20% of the frame height, in our case)
    if (this.absoluteFaceSize == 0)
    {
        int height = grayFrame.rows();
        if (Math.round(height * 0.2f) > 0)
        {
            this.absoluteFaceSize = Math.round(height * 0.2f);
        }
    }

    // detect faces
    this.faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, 0 | Objdetect.CASCADE_SCALE_IMAGE,
            new Size(this.absoluteFaceSize, this.absoluteFaceSize), new Size());

    // each rectangle in faces is a face: draw them!
    Rect[] facesArray = faces.toArray();
    for (int i = 0; i < facesArray.length; i++)
        Imgproc.rectangle(frame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0), 3);

}
private void检测和显示(垫框)
{
MatOfRect面=新的MatOfRect();
Mat grayFrame=新Mat();
//将帧转换为灰度
Imgproc.cvt颜色(边框、灰色边框、Imgproc.COLOR\u bgr2灰色);
//均衡帧直方图以改善结果
Imgproc.equalizeHist(灰色帧,灰色帧);
//计算最小面大小(在我们的例子中,为帧高度的20%)
如果(this.absoluteFaceSize==0)
{
int height=grayFrame.rows();
如果(数学圆(高度*0.2f)>0)
{
此.absoluteFaceSize=数学圆(高度*0.2f);
}
}
//检测人脸
此.faceCascade.detectMultiScale(灰度框,面,1.1,2,0 | Objdetect.CASCADE_SCALE_图像,
新尺寸(this.absoluteFaceSize,this.absoluteFaceSize),新尺寸();
//面中的每个矩形都是一个面:绘制它们!
Rect[]facesArray=faces.toArray();
对于(int i=0;i
如果您想保存检测到的人脸图像,也许可以尝试以下方法

for (int i = 0; i < facesArray.length; i++)
{   
     Imgproc.rectangle(frame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0), 3);
     Rect rect(facesArray[i].tl().x, facesArray[i].tl().y, facesArray[i].br().x - facesArray[i].tl().x, facesArray[i].br().y - facesArray[i].tl().y);
     Mat cropFace = frame(rect);
     imwrite("./face"+i+".jpg", cropFace);
}
for(int i=0;i
我找到了解决方案

      for (Rect rect : facesArray) {
            Imgproc.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                    new Scalar(0, 255, 0)); // frame is Mat
            rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);

            Mat image_roi = new Mat(frame,rectCrop);
             Imgcodecs.imwrite("./face"+ i +".jpg",image_roi);
             i++;
        }

现在,我可以裁剪多张脸了

有两个不同的概念,称为人脸检测和人脸识别。这两个问题有完全不同的解决方案。要解决第一个问题,你需要在所有可用的面部图像上进行训练,但对于第二个问题,你至少需要10-20张特定人的样本图像来训练你的检测器,否则它的精确度会非常低。是的,我知道两者完全不同。但我需要一个如何将检测到的图像保存为本地系统中的.jpg或.pngwhat is cropFacecropFace是原始图像的感兴趣区域,它创建一个引用,而不是副本。对cropFace的任何更改都将更改原始图像。cropFace是一个Mat,您可以引用此链接什么是imwrite,我在那里遇到错误“类型FaceDetectionController的方法imwrite(字符串,Mat)未定义”可能您必须添加“import org.opencv.imgcodecs.imgcodecs”并修改为“imgcodecs.imwrite(./face.jpg,cropFace)”